Expand description
§otari
A unified Rust SDK for interacting with LLMs via the Otari gateway.
This library provides a single, consistent interface to interact with the Otari gateway, a FastAPI-based proxy that exposes an OpenAI-compatible API and routes requests to multiple upstream LLM providers.
§Features
- Unified API: Single interface for all models through the gateway
- Streaming support: Real-time token streaming with async streams
- Tool calling: Function/tool calling with automatic format conversion
- Image support: Send images to vision-capable models
- Extended thinking: Support for reasoning features
- Reranking: Document reranking support
- Batch operations: Create, retrieve, cancel, and list batch jobs
- Moderation: Content moderation via the gateway
- Type-safe: Strong Rust types with serde serialization
§Quick Start
use otari::{completion, Message, CompletionOptions};
#[tokio::main]
async fn main() -> otari::Result<()> {
let messages = vec![Message::user("Hello, how are you?")];
let response = completion(
"openai:gpt-4o-mini",
messages,
CompletionOptions::with_api_key("your-api-key")
.api_base("http://localhost:8000"),
).await?;
println!("{}", response.content().unwrap_or_default());
Ok(())
}§Streaming
use otari::{completion_stream, Message, CompletionOptions, ChunkAccumulator};
use futures::StreamExt;
let messages = vec![Message::user("Tell me a story")];
let mut stream = completion_stream(
"openai:gpt-4o-mini",
messages,
CompletionOptions::with_api_key("your-api-key")
.api_base("http://localhost:8000"),
).await?;
let mut accumulator = ChunkAccumulator::new();
while let Some(chunk) = stream.next().await {
let chunk = chunk?;
if let Some(content) = chunk.content() {
print!("{}", content);
}
accumulator.add(&chunk);
}
// Access accumulated data
println!("\nFull response: {}", accumulator.content);§Tool Calling
use otari::{completion, Message, CompletionOptions, Tool, ToolChoice};
use serde_json::json;
let weather_tool = Tool::function("get_weather", "Get the current weather")
.parameters(json!({
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name"
}
},
"required": ["location"]
}))
.build();
let messages = vec![Message::user("What's the weather in Paris?")];
let options = CompletionOptions::with_api_key("your-api-key")
.api_base("http://localhost:8000")
.tools(vec![weather_tool])
.tool_choice(ToolChoice::auto());
let response = completion("openai:gpt-4o-mini", messages, options).await?;
if let Some(tool_calls) = &response.choices[0].message.tool_calls {
for call in tool_calls {
println!("Function: {}, Args: {}", call.function.name, call.function.arguments);
}
}§Environment Variables
OTARI_API_KEY: API key for the Otari gatewayOTARI_API_BASE: Base URL of the Otari gateway
Re-exports§
pub use api::completion;pub use api::completion_stream;pub use api::rerank;pub use api::CompletionOptions;pub use api::RerankOptions;pub use client::Otari;pub use config::Config;pub use error::OtariError;pub use error::Result;pub use types::Batch;pub use types::BatchRequestCounts;pub use types::BatchRequestItem;pub use types::BatchResult;pub use types::BatchResultError;pub use types::BatchResultItem;pub use types::BatchStatus;pub use types::ChatCompletion;pub use types::ChatCompletionChunk;pub use types::ChatCompletionMessage;pub use types::Choice;pub use types::ChoiceDelta;pub use types::ChunkAccumulator;pub use types::ChunkChoice;pub use types::CompletionParams;pub use types::CompletionStream;pub use types::CompletionUsage;pub use types::Content;pub use types::ContentPart;pub use types::CreateBatchParams;pub use types::Function;pub use types::ImageUrl;pub use types::ListBatchesOptions;pub use types::Message;pub use types::ModerationContentPart;pub use types::ModerationImageUrl;pub use types::ModerationInput;pub use types::ModerationParams;pub use types::ModerationResponse;pub use types::ModerationResult;pub use types::Reasoning;pub use types::ReasoningEffort;pub use types::RerankMeta;pub use types::RerankParams;pub use types::RerankResponse;pub use types::RerankResult;pub use types::RerankUsage;pub use types::Role;pub use types::StopSequence;pub use types::Tool;pub use types::ToolCall;pub use types::ToolCallDelta;pub use types::ToolChoice;pub use types::ToolFunction;