.env
file with the name WRITER_API_KEY
.POST https://api.writer.com/v1/chat
/chat
endpoint results in charges for model usage. See the pricing page for more information.Parameter | Type | Description |
---|---|---|
model | string | Required. The ID of the model to use for the chat completion. Can be palmyra-x4 , palmyra-x5 , palmyra-fin , palmyra-med , palmyra-creative , or palmyra-x-003-instruct . |
messages | array | Required. The conversation history. |
messages[].role | string | Required. The role of the message sender. Can be user , assistant , system , or tool . system messages are system prompts, used to provide instructions to the model. tool messages are the result of a tool call, and contain the output of the tool call. |
messages[].content | string | Required. The content of the message. |
temperature | float | Temperature influences the randomness in generated text. Defaults to 1 . Increase the value for more creative responses, and decrease the value for more predictable responses. |
stream | Boolean | A Boolean value that indicates whether to stream the response. Defaults to false . |
stream
parameter to false
, the response is delivered as a single JSON object. It contains several parameters describing the response, including the choices
array, which contains the generated text.
Parameter | Type | Description |
---|---|---|
model | string | The ID of the model used to generate the response. |
choices | array | An array containing one object with the generated text and additional information. |
choices[0].message.content | string | The generated text. |
stream
parameter to true
, the response is delivered as server-sent events. The event contains several parameters. The content of the chunk is in the choices[0].delta.content
parameter.
Parameter | Type | Description |
---|---|---|
choices[0].delta.content | string | The content of the chunk. |
exit
.
system
to the messages
array.
For example, you can set a system prompt to have the assistant respond in a certain tone or style, or to provide additional context for the conversation. Here’s a system prompt that directs the assistant to be casual and use emojis in its responses:
stream
helper method of the chat
endpoint to print the chat responses to the console in real time.
The stream
method also collects the final response and returns it so it can be added to the conversation history.
exit
.
messages
array to maintain context, as the model doesn’t retain memory of past interactions.