.env
file with the name WRITER_API_KEY
.tools
array when calling the chat endpoint:
Parameter | Type | Description |
---|---|---|
type | string | The type of tool. Must be graph for Knowledge Graph chat. |
function | object | An object containing the graph_ids , description , and subqueries parameters |
function.graph_ids | array | An array of strings containing the graph IDs you wish to reference |
function.description | string | A description of the graphs you are referencing. This helps the model understand when to use the Knowledge Graph tool in the chat. If there are multiple graphs, include a description for each, referencing the graph by name. |
function.subqueries | Boolean | A Boolean indicating whether to include the subqueries used by Palmyra in the response. |
"tools": [
{
"type": "graph",
"function": {
"description": "Description of the graph(s)",
"graph_ids": [
"your-graph-id"
],
"subqueries": true
}
}
]
tools
array at a time. However, you can pass multiple custom tools in the same request.Prebuilt tools are:graph_data
object. That object contains the following fields:
Parameter | Type | Description |
---|---|---|
sources | array | An array of objects containing the source file IDs and snippets that helped the model answer the question. |
sources.file_id | string | The ID of the source file. |
sources.snippet | string | A snippet from the source file that helped the model answer the question. |
status | string | The status of the query. |
subqueries | array | An array of objects containing the subqueries used by Palmyra in the response. |
subqueries.query | string | The query used by Palmyra to answer the question. |
subqueries.answer | string | The answer to the question. |
subqueries.sources | array | An array of objects containing the source file IDs and snippets that helped the model answer the question. |
subqueries
parameter is false
, or if the model doesn’t need subqueries to answer the question, this array is be empty.
{
"id": "1234",
"object": "chat.completion.chunk",
"choices": [
{
"index": 0,
"finish_reason": "stop",
"delta": {
"content": "None of our products contain both chocolate and food coloring. The products containing chocolate are different from those containing food coloring.",
"role": "assistant",
"tool_calls": null,
"graph_data": {
"sources": [
{
"file_id": "1234",
"snippet": "with cocoa for an extra touch of chocolate…"
},
{
"file_id": "5678",
"snippet": "Sugar, corn syrup, artificial flavors, food coloring…"
}
],
"status": "finished",
"subqueries": [
{
"query": "Which of our products contain food coloring?",
"answer": "The products that contain food coloring are...",
"sources": [
{
"file_id": "1234",
"snippet": "Sugar, citric acid, artificial flavors…"
},
{
"file_id": "5678",
"snippet": "Coffee, coconut milk, ice"
}
]
},
{
"query": "Which of our products contain chocolate?",
"answer": "Several products contain chocolate. These include…",
"sources": [
{
"file_id": "1234",
"snippet": "with cocoa for an extra touch of chocolate…"
}
]
}
]
}
},
}
]
// Other fields omitted for brevity
}
tools
array with the type
set to graph
. The function
object contains the graph_ids
, description
, and subqueries
parameters.
In this example, the subqueries are included in the response. Subqueries can be useful for debugging or for providing additional context to the user about how the model arrived at the answer.
"tools": [
{
"type": "graph",
"function": {
"description": "Knowledge Graph containing information about Acme Inc. food products",
"graph_ids": [
"6029b226-1ee0-4239-a1b0-cdeebfa3ad5a"
],
"subqueries": true
}
}
]
tool_choice
to auto
allows the model to choose when to use the Knowledge Graph tool, based on the user’s question and the description of the tool.
This example streams the response in real-time as the model generates it.
If you are unfamiliar with the chat completions endpoint or streaming vs. non-streaming responses, learn more in the chat completion guide.
curl --location 'https://api.writer.com/v1/chat' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $WRITER_API_KEY" \
--data '{
"model": "palmyra-x5",
"messages": [
{
"role": "user",
"content": "Which of our products contain both food coloring and chocolate?"
}
],
"tool_choice": "auto",
"tools": [
{
"type": "graph",
"function": {
"description": "Knowledge Graph containing information about Acme Inc. food products",
"graph_ids": [
"6029b226-1ee0-4239-a1b0-cdeebfa3ad5a"
],
"subqueries": true
}
}
],
"stream": true
}'
sources
or subqueries
in your UI to assist your user in understanding how the model derived the answer to the question. The following example shows how to display the subqueries as well as the status of the query from the Knowledge Graph.
from writerai import Writer
# Initialize the client.
client = Writer()
messages = [{"role": "user", "content": "Which of our products contain both food coloring and chocolate?"}]
response = client.chat.chat(
model="palmyra-x5",
messages=messages,
tools=tools,
tool_choice="auto",
stream=True
)
for chunk in response:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="", flush=True)
if chunk.choices[0].delta.graph_data is not None:
if chunk.choices[0].delta.graph_data.status is not None:
print(f"Query status: {chunk.choices[0].delta.graph_data.status}")
if chunk.choices[0].delta.graph_data.subqueries:
print(f"Subquery: {chunk.choices[0].delta.graph_data.subqueries[0].query}")
Was this page helpful?