Knowledge Graph, our graph-based retrieval-augmented generation (RAG), achieves higher accuracy than traditional RAG approaches that use vector retrieval.

This guide will help you understand and use the Knowledge Graph API to integrate RAG capabilities into your no-code applications.

You need an API key to access the Writer API. Get an API key by following the steps in the API quickstart.

We recommend setting the API key as an environment variable in a .env file with the name WRITER_API_KEY.

Create a Knowledge Graph

A Knowledge Graph is a collection of files that are used to answer questions. To start working with a Knowledge Graph, create an empty Knowledge Graph that you can add files to.

Endpoint: POST https://api.writer.com/v1/graphs

curl --location --request POST https://api.writer.com/v1/graphs \
  --header "Authorization: Bearer $WRITER_API_KEY" \
  --header "Content-Type: application/json" \
  --data-raw '{
    "name": "Financial Reports",
    "description": "Knowledge Graph of 2024 financial reports"
    }'

Request body

The request body is a JSON object that contains the following fields:

ParameterTypeDescription
namestringThe name of the Knowledge Graph.
descriptionstringThe description of the Knowledge Graph. The description should help the model understand the purpose of the Knowledge Graph and when it should be used.

Response format

The response has the following structure:

ParameterTypeDescription
idstringThe ID of the Knowledge Graph.
created_atstringThe date and time the Knowledge Graph was created.
namestringThe name of the Knowledge Graph.
descriptionstringThe description of the Knowledge Graph.
{
  "id": "6029b226-1ee0-4239-a1b0-cdeebfa3ad5a",
  "created_at": "2024-06-24T12:34:56Z",
  "name": "Financial Reports",
  "description": "Knowledge Graph of 2024 financial reports"
}

Add a file to a Knowledge Graph

Once you’ve created a Knowledge Graph, you can add files to it. The files you add to a Knowledge Graph are used to answer questions and enhance the accuracy of the responses from the LLM

You must upload the file to the Writer API before adding it to a Knowledge Graph. See Manage files for more information about how to upload files.

Endpoint: POST /v1/graphs/{graph_id}/file

curl --location --request POST https://api.writer.com/v1/graphs/{graph_id}/file \
--header "Authorization: Bearer $WRITER_API_KEY" \
--header "Content-Type: application/json" \
--data-raw '{"file_id":"1862f090-a281-48f3-8838-26c1e78b605e"}'

The response will have this structure:

{
  "id": "1862f090-a281-48f3-8838-26c1e78b605e",
  "created_at": "2024-07-01T20:41:44.159505Z",
  "name": "test.txt",
  "graph_ids": [
    "6029b226-1ee0-4239-a1b0-cdeebfa3ad5a"
  ]
}

Path parameters

ParameterDescription
graph_idThe ID of the Knowledge Graph to update.

Request body

The request body is a JSON object that contains the following fields:

ParameterDescription
file_idThe ID of the file to add to the Knowledge Graph.

You must upload the file to the Writer API before adding it to a Knowledge Graph. See Manage files for more information about how to upload files.

Remove a file from a Knowledge Graph

Endpoint: DELETE /v1/graphs/{graph_id}/file/{file_id}

curl --location --request DELETE "https://api.writer.com/v1/graphs/{graph_id}/file/{file_id}" \
--header "Authorization: Bearer $WRITER_API_KEY"

Path parameters

ParameterDescription
graph_idThe ID of the Knowledge Graph to update.
file_idThe ID of the file to remove from the Knowledge Graph.

Response format

The response has this structure:

{
  "id": "1862f090-a281-48f3-8838-26c1e78b605e",
  "deleted": true
}

Add a Knowledge Graph to a no-code application

After you’ve created a Knowledge Graph, you can add it to a no-code application to add RAG capabilities.

You can use this endpoint to add or remove Knowledge Graphs from a no-code application.

Endpoint: PUT /v1/applications/{application_id}/graphs

curl --location --request PUT "https://api.writer.com/v1/applications/{application_id}/graphs" \
--header "Authorization: Bearer $WRITER_API_KEY" \
--header "Content-Type: application/json" \
--data-raw '{"graph_id":"6029b226-1ee0-4239-a1b0-cdeebfa3ad5a"}'

Path parameters

ParameterDescription
application_idThe ID of the no-code application to update.

Request body

The request body is a JSON object that contains the following fields:

ParameterTypeDescription
graph_idsarray[string]The IDs of the Knowledge Graphs to associate with the no-code application.

This method updates the associated Knowledge Graphs to the exact list of IDs provided in the request.

To remove a single Knowledge Graph from the no-code application, set the graph_ids parameter to an array containing the IDs of all the other Knowledge Graphs associated with the no-code application, excluding the ID of the Knowledge Graph to remove. To remove all Knowledge Graphs from the no-code application, set this parameter to an empty array.

Response format

The response contains the new list of IDs of the Knowledge Graphs associated with the no-code application.

{
  "graph_ids": [
    "6029b226-1ee0-4239-a1b0-cdeebfa3ad5a"
  ]
}

Next steps

Now that you’ve created a Knowledge Graph, learn how to ask it questions in a chat completion via tool calling.