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 to send a POST request to api.writer.com/v1/chat with the JSON payload.

curl --location 'https://api.writer.com/v1/chat' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <token>' \
--data '{
    "model": "palmyra-chat-v2-32k",
    "temperature": 0.7,
    "messages": [
        {
            "role": "user",
            "content": "You are an expert at writing concise product descriptions for an E-Commerce Retailer"
        },
        {
            "role": "assistant",
            "content": "Okay, great I can help write these descriptions. Do you have a specific product in mind?"
        },
        {
            "role": "user",
            "content": "Please write a one sentence product description for a cozy, stylish sweater suitable for both casual and formal occasions"
        }
    ]
}'
2

Handle the response

For the response parse the JSON to access the generated text.

   {
    "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-v2-32k"
}
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.