This document outlines the API for interacting with our AI chat system. The system uses a combination of HTTP endpoints and WebSocket connections to provide real-time chat functionality with AI models.
Currently, the API does not require authentication. However, this may change in future versions.
- URL:
/models - Method: GET
- Description: Retrieves the list of available AI models for chat and embedding.
- Response: JSON object containing
chatModelProvidersandembeddingModelProviders.
Example Response:
{
"chatModelProviders": {
"openai": {
"gpt-4": {},
"gpt-3.5-turbo": {}
},
"anthropic": {
"Claude 3.5 Sonnet": {}
}
},
"embeddingModelProviders": {
"openai": {
"text-embedding-ada-002": {}
}
}
}- URL:
/suggestions - Method: POST
- Description: Generates chat suggestions based on the provided chat history.
- Request Body:
{ "chat_history": Array, "chat_model": String, "chat_model_provider": String } - Response: JSON array of suggestion strings.
Example Request:
{
"chat_history": [
{
"role": "user",
"content": "Hello, how can you help me today?"
},
{
"role": "assistant",
"content": "Hello! I'm here to assist you with any questions or tasks you might have. How can I help you today?"
},
{
"role": "user",
"content": "I'm working on a Cairo project and need to understand Starknet better."
}
],
"chat_model": "Claude 3.5 Sonnet",
"chat_model_provider": "anthropic"
}Example Response:
{
"suggestions": [
"Explain the key features and benefits of Starknet for Cairo projects",
"What are the main differences between Starknet and other Layer 2 scaling solutions?",
"Can you provide an overview of Cairo programming language and its use in Starknet?",
"What are some popular tools and resources for developing on Starknet?",
"How does Starknet integrate with Ethereum and other blockchain networks?"
]
}ws://<server-url>/ws
chatModel: The selected chat modelchatModelProvider: The provider of the chat modelembeddingModel: The selected embedding modelembeddingModelProvider: The provider of the embedding modelopenAIApiKey: (Optional) API key for custom OpenAI setupopenAIBaseURL: (Optional) Base URL for custom OpenAI setup
Example Connection URL:
ws://api.example/ws?chatModel=Claude+3.5+Sonnet&chatModelProvider=anthropic&embeddingModel=Text+embedding+3+large&embeddingModelProvider=openai
Messages sent to the WebSocket should be JSON strings with the following structure:
{
"type": "message",
"message": {
"chatId": String,
"content": String
},
"focusMode": String,
"history": Array
}Example Message:
{
"type": "message",
"message": {
"messageId": "unique-message-id-1",
"chatId": "unique-chat-id-1",
"content": "Hello! Can you help me understand how Cairo smart contracts work?"
},
"copilot": false,
"focusMode": "cairoBookSearch",
"history": [
["human", "Hello! I'm interested in learning about Cairo."],
[
"ai",
"Hello! I'd be happy to help you learn about Cairo. Cairo is a programming language designed for writing provable programs, particularly smart contracts for the StarkNet platform. What specific aspect of Cairo would you like to know more about?"
],
["human", "Can you help me understand how Cairo smart contracts work?"]
]
}The server will send JSON messages with the following types:
start: Indicates the start of a responsecontent: Contains a chunk of the AI's responseend: Indicates the end of a responseerror: Contains error information
Example Server Responses:
{"type":"sources","data":[{"pageContent":"...","metadata":{"url":"https://book.cairo-lang.org/ch13-01-general-introduction-to-smart-contracts.html#general-introduction-to-smart-contracts"}}]}
{"type":"message","data":"Certainly! I","messageId":"e52729dfd0921e"}
{"type":"messageEnd","messageId":"e52729dfd0921e"}Error messages will be sent as JSON with the following structure:
{
"type": "error",
"data": String,
"key": String
}Example Error Message:
{
"type": "error",
"data": "Failed to connect to the specified chat model",
"key": "MODEL_CONNECTION_ERROR"
}- Fetch available models using the
/modelsHTTP endpoint. - Establish a WebSocket connection with the appropriate query parameters.
- Send a message to the WebSocket with the user's input and chat history.
- Listen for responses from the server and handle them accordingly.
- The system uses a focus mode to determine the context of the conversation. Ensure the correct focus mode is specified in each message.
- The chat history should be maintained on the client-side and sent with each message for context.
- Handle WebSocket disconnections gracefully and implement reconnection logic if necessary.
This formatted version of the document includes improved examples for each section, making it easier for external teams to understand and integrate with the AI chat system.