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:

  1. id: A unique identifier for the conversation.
  2. choices: Contains objects describing the completion, including the role (user or assistant) and content.
  3. finish_reason: Indicates why the response was concluded (stop, length, function_call, content_filter, null).

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, response is delivered in chunks but the structure remains the same.

Response
{
  "id": "1d199764-4a26-4318-8daa-3fafebca83ce",
  "choices": [
      {
          "finish_reason": "length",
          "message": {
              "content": "Here's a product description for a cozy, stylish sweater: \"Stay effortlessly stylish with this versatile sweater, perfect for both casual days and formal events, featuring a luxurious feel and elegant design that will keep you cozy and looking your best.\"",
              "role": "assistant"
          }
      }
  ],
  "created": 1715606709,
  "model": "palmyra-chat-002-32k"
}

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.