KrosAI Docs
DashboardGet Started
  • Getting Started
    • Introduction
    • Quickstart Guide
    • Authentication
  • OpenAI SDK Compatability
  • Endpoints
    • Completions API
      • Create Chat Completion
    • Translation API
    • Text Summarization API
    • Audio Transcription API
    • Sentiment Analysis API
  • AGENT
    • Agents API
      • Create Agent
      • List Agents
      • Get Agent
      • Update Agent
      • Delete Agent
      • Start Voice Session
    • Conversations API
      • Create Conversation
      • List Conversations
      • Get Conversation
      • Get Conversation Messages
    • Knowledge Base API
      • Create Knowledge Base
      • List Knowledge Base
      • Upload Document
      • List Document
  • Resources
    • AI Voice Agents
    • In-App Voice Assistants
    • Virtual Health Assistants
    • EdTech Language Tutors
  • SDKS
    • Node.JS SDK
    • React Native SDK
    • Flutter
Powered by GitBook
On this page
  • Getting Started
  • Sentiment Analysis
  • Batch Sentiment Analysis
  • Translation
  • Chat Completions with Streaming
  • Compatibility Notes
  • Error Handling
  • API Key Management

OpenAI SDK Compatability

KrosAI Platform offers OpenAI SDK compatibility, allowing you to use familiar tools and libraries while benefiting from KrosAI's specialized features for African languages.

Getting Started

To use KrosAI with existing OpenAI integrations, simply change the base URL and API key in your configuration:

import openai

# Configure the client
client = openai.OpenAI(
    api_key="your-krosai-api-key",
    base_url="https://api.krosai.com/v1"
)

Sentiment Analysis

import openai

# Configure the client
client = openai.OpenAI(
    api_key="your-krosai-api-key",
    base_url="https://api.krosai.com/v1"
)

# Analyze sentiment
response = client.post(
    url="/sentiment",
    json={
        "text": "I am very happy with the service provided. Thank you!",
        "language": "english",
        "detailed": True
    }
)

result = response.json()
print(f"Sentiment: {result['results']['sentiment']}")
print(f"Confidence: {result['results']['confidence']}")
print(f"Detected language: {result['language']['detected']}")

Batch Sentiment Analysis

For analyzing multiple texts efficiently:

import openai
import json

# Configure the client
client = openai.OpenAI(
    api_key="your-krosai-api-key",
    base_url="https://api.krosai.com/v1"
)

# Batch sentiment analysis
texts = [
    {"text": "This product is amazing!", "language": "english"},
    {"text": "Mo ni ayọ pupọ loni.", "language": "yoruba"},
    {"text": "أنا لا أحب هذه الخدمة.", "language": "arabic"}
]

response = client.post(
    url="/sentiment/batch",
    json={
        "texts": texts,
        "detailed": True
    }
)

results = response.json()
for idx, result in enumerate(results):
    print(f"\nText {idx+1}:")
    print(f"Sentiment: {result['results']['sentiment']}")
    print(f"Language: {result['language']['detected']}")

Translation

import openai

# Configure the client
client = openai.OpenAI(
    api_key="your-krosai-api-key",
    base_url="https://api.krosai.com/v1"
)

# Translate audio to English
with open("recording.mp3", "rb") as audio_file:
    translation = client.audio.translations.create(
        file=audio_file,
        model="KrosMLingualSTT1.0.0"
    )

print(f"Translation: {translation.text}")

Chat Completions with Streaming

KrosAI also supports streaming responses for chat completions, just like the OpenAI API:

import openai

# Configure the client
client = openai.OpenAI(
    api_key="your-krosai-api-key",
    base_url="https://api.krosai.com/v1"
)

# Stream chat completions
stream = client.chat.completions.create(
    model="KrosMlingual2.0.1",
    messages=[
        {"role": "system", "content": "You are a helpful assistant that specializes in African languages."},
        {"role": "user", "content": "Translate 'Hello, how are you?' to Yoruba."}
    ],
    stream=True
)

# Process the streaming response
for chunk in stream:
    if chunk.choices[0].delta.content is not None:
        print(chunk.choices[0].delta.content, end="")

Compatibility Notes

While KrosAI maintains compatibility with the OpenAI SDK, there are some important differences to be aware of:

  1. Models: KrosAI uses its own models optimized for African languages. Replace OpenAI model names with KrosAI equivalents:

  • KrosMlingual2.0.1 for chat completions

  • KrosMLingualSTT1.0.0 for transcription and translation

  1. Additional Parameters: KrosAI supports extra parameters for working with African languages, such as specifying source language.

  2. Sentiment Analysis: This is a KrosAI-specific endpoint not available in the standard OpenAI API.

  3. Response Format: While we maintain compatibility with OpenAI's response structure, some fields may contain additional information specific to KrosAI services.

Error Handling

KrosAI follows the same error handling patterns as OpenAI:

import openai

client = openai.OpenAI(
    api_key="your-krosai-api-key",
    base_url="https://api.krosai.com/v1"
)

try:
    response = client.post(
        url="/sentiment",
        json={"text": "Sample text"}
    )
    result = response.json()
    print(f"Sentiment: {result['results']['sentiment']}")
except openai.BadRequestError as e:
    print(f"Bad request: {e}")
except openai.AuthenticationError as e:
    print(f"Authentication error: {e}")
except openai.RateLimitError as e:
    print(f"Rate limit exceeded: {e}")
except openai.APIError as e:
    print(f"API error: {e}")

API Key Management

For security best practices, we recommend storing your API key as an environment variable:

import os
import openai

# Load API key from environment variable
api_key = os.getenv("KROSAI_API_KEY")

client = openai.OpenAI(
    api_key=api_key,
    base_url="https://api.krosai.com/v1"
)

This compatibility layer makes it easy to transition existing applications to KrosAI while taking advantage of our specialized capabilities for African languages.

PreviousAuthenticationNextCompletions API

Last updated 1 month ago