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"
)
# 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:
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
Additional Parameters: KrosAI supports extra parameters for working with African languages, such as specifying source language.
Sentiment Analysis: This is a KrosAI-specific endpoint not available in the standard OpenAI API.
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:
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.