Chat with LLM

Chat with state-of-the-art LLM using Cosmos.

About LLM Chat

The LLM Chat Service allows for conversational interactions with the AI, providing a user-friendly interface for message exchanges and responses. It offers several state-of-the-art models, such as GPT-4o, Llama-3, and more.

Large Language ModelDeveloper
gpt-3.5 (legacy)OpenAI
gpt-4-turboOpenAI
gpt-4oOpenAI
gpt-4o-mini (default)OpenAI
command-rCohere
command-r-plusCohere
llama-3-70b-instructMeta
mistral-largeMistral AI
mistral-smallMistral AI

Key features of the CosmosPlatform Chat Service:

  • Natural language interaction
  • Context-aware responses
  • Low-latency AI communication
  • Transparent cost tracking

Step-by-step Tutorial

1. Prerequisites

Before you begin, ensure you have:

2. Setup Cosmos Python Client

Using Python Cosmos client you can perform the API requests in a convenient way.

2.1. Install Cosmos Python Client:

In order to install the Python client, you can add it to your project by doing:

poetry add delos-cosmos

Or install it in your virtual environment with:

pip install delos-cosmos

2.2. Authenticate Requests:

  • Initialize the client with your API key:
from cosmos import CosmosClient

cosmos_client = CosmosClient(apikey=your-cosmos-api-key)

2.3. Call API:

  • You can start invoking any Cosmos endpoints. For example, let's try the /health endpoint to check the validity of your API key and the availability of the client services:
response = cosmos_client.status_health_request()
print(response)

3. Chat with LLM

Here is an example of a LLM chat request using Python client.

from cosmos import CosmosClient

cosmos_client = CosmosClient(apikey="your-cosmos-api-key")
response = cosmos_client.llm_chat_request(text="Hello, how are you today?")
print(response)

The requested LLModel is an optional parameter, that will default to gpt-4o-mini if not specified, but there are several

See an example of how to specify a different LLM model:

from cosmos import CosmosClient

cosmos_client = CosmosClient(apikey="")
response = cosmos_client.llm_chat_request(text="Hello, how are you today?", model="mistral-small")
print(response)

Other optional parameters that may be interesting are the temperature and other kwargs to provide the LLM.

from cosmos import CosmosClient

cosmos_client = CosmosClient(apikey="")
response = cosmos_client.llm_chat_request(
                            text="Hello, how are you today?",
                            model="gpt-4o",
                            response_format={"type":"json_object"},
                            temperature=0.7
                          )
print(response)

A successful response will return the AI's reply:

{
  "request_id": "4fa2fb9d-d8ac-4995-8dd9-836323f11148",
  "response_id": "48b4a03a-e406-45e7-bf06-0d44b68f48af",
  "status_code": 200,
  "status": "success",
  "message": "Chat response received.",
  "data": {
    "answer": "I'm doing well, thank you for asking! How can I help you today?"
  },
  "timestamp": "2024-11-20T15:21:40.127776Z",
  "cost": "0.0023"
}

4. Specify output format

The response_format allows to require json-parsed responses. For example:

from cosmos import CosmosClient

cosmos_client = CosmosClient(apikey="")
response = cosmos_client.llm_chat_request(
                            text="What is the capital city and GDP of Germany? Reply in a JSON",
                            model="gpt-4o",
                            response_format={"type":"json_object"}
                          )
print(response)

The AI's response will follow the JSON format. Notice that the more precise you are in your instructions and requirements, the better the response will fit. The response may be similar to:

{
  "request_id": "4fa2fb9d-d8ac-4995-8dd9-836323f11148",
  "response_id": "48b4a03a-e406-45e7-bf06-0d44b68f48af",
  "status_code": 200,
  "status": "success",
  "message": "Chat response received.",
  "data": {
    "answer": {
      "capital": "Berlin",
      "GDP": "approximately 4.2 trillion USD"
    }
  },
  "timestamp": "2024-11-20T15:21:40.127776Z",
  "cost": "0.0023"
}

5. Handle Errors

Common errors include:

  • Missing API key
  • No text provided

Example error response:

{
  "status_code": 422,
  "status": "error",
  "message": "Validation error",
  "error": {
    "error_code": "422",
    "error_message": "Validation failed for the input fields.",
    "details": "[{'loc': ('header', 'apikey'), 'msg': 'Field required', 'type': 'missing'}]"
  }
}