This guide explains the Chat completion endpoint which can be thought of as having a conversation with our LLM.

This endpoint facilitates engaging and dynamic conversations between a user and an AI-assisted chat model. It’s designed to handle both multi-turn and single-turn interactions effectively. This guide will walk you through making requests to the endpoint and understanding its responses.

Your API key can be generated using these steps.

Endpoint overview

This endpoint is designed for generating chat responses based on a conversation history provided in the request. It supports complex, contextual interactions that can simulate a conversational partner. The responses will contain the following variables:

  • id: A unique identifier for the conversation.
  • choices: Objects describing the completion, including the role (user or assistant) and content.
    • index: The index of the choice.
    • message when stream is false or delta when stream is true:
      • content: The text generated by the model. Note that the content of the first chunk in a streaming response will be null.
      • role: The role of the message (e.g. user or assistant).
      • tool_calls: The tool calls made during the completion.
    • finish_reason: Indicates why the response was concluded (stop, length, tool_calls, content_filter, null).
  • created: The timestamp of the completion.
  • model: The model used for the completion.

Usage example

Here’s how you can use the endpoint in a practical scenario:

1

Create a request

Use your preferred HTTP client or one of our SDKs to send a POST request to api.writer.com/v1/chat with the JSON payload.

2

Handle the response

For the response parse the JSON to access the generated text. When stream is set to true, the response is delivered in chunks. The structure remains the same except that you will use delta instead of message. Note that the content of the first chunk in a streaming response will be null.

Here are examples of how to handle the response in the SDKs:

3

Error handling

Ensure to handle potential errors such as timeouts or model errors gracefully.

4

Rate limiting

Be mindful of any rate limits that might apply to avoid service disruptions.

Best practices

This guide should equip you with the knowledge to integrate and utilize the Chat completion endpoint effectively, enhancing your applications with robust, conversational AI capabilities.