Implementation Examples

See real-world implementation examples for the Kodesage Chat API including sample code in multiple languages and integration patterns.

Example 1: Simple question

Request:

curl -X POST "https://your-kodesage-instance.com/api/projects/01K5BQSXND0N595C18DJMVN4VA/v1/chat/completions" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
    "messages": [
      {
        "role": "user",
        "content": "How is user authentication implemented?"
      }
  ]
}'

Response:

{
    "choices": [
      {
        "index": 0,
        "message": {
          "role": "assistant",
          "content": "According to WebSecurityConfig.java, the project uses Spring Security with a custom CosmossAuthenticationProvider..."
         }
      }
   ]
}

Example 2: Multi-turn conversation

Request:

curl -X POST "https://your-kodesage-instance.com/api/projects/01K5BQSXND0N595C18DJMVN4VA/v1/chat/completions" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
    "messages": [
     {
       "role": "user",
       "content": "What database is used?"
     },
     {
       "role": "assistant",
       "content": "The project uses PostgreSQL as described in application.properties..."
     },
     {
       "role": "user",
       "content": "What is the connection pool configuration?"
     }
  ]
}'

The API is compatible with official OpenAI client libraries. This means you can use standard OpenAI SDKs with just a custom base URL.

Quick Start (Python and C#)

Below you will find minimal working examples for both Python and C# using the official OpenAI SDKs.

Python example

from openai import OpenAI

client = OpenAI(
    api_key=KODESAGE_API_TOKEN",
    base_url="https://your-kodesage-instance.com/api/projects/YOUR_PROJECT_ID/v1"
)

completion = client.chat.completions.create(
    model="not-used", # Model parameter is ignored by Kodesage
    messages=[
        {"role": "user","content": "What programming languages are used in this project?"}
    ]
)

print(completion.choices[0].message.content)

C# (.NET) Example

using OpenAI;
using OpenAI.Chat;

ChatClient client = new(
    model: "not-used", // Model parameter is ignored by Kodesage
    credential: new ApiKeyCredential(Environment.GetEnvironmentVariable("KODESAGE_API_TOKEN")),
    options: new OpenAIClientOptions()
    {
        Endpoint = new Uri("https://your-kodesage-instance.com/api/projects/YOUR_PROJECT_ID/v1")
    }
);

ChatCompletion completion = client.CompleteChat("What programming languages are used in this project?");
Console.WriteLine($"[ASSISTANT]: {completion.Content[0].Text}");

Error handling

Error Response Format

All errors return a JSON object with an error field:

{
    "detail": {
        "error": {
            "message": "Error description",
            "type": "error_type",
            "code": "error_code"
        }
    }
}

Last updated