Edgee Rust SDK
A modern, idiomatic Rust SDK for the Edgee AI Gateway.
Features
- 🦀 Idiomatic Rust - Leverages Rust's type system, ownership, and error handling
- ⚡ Async/Await - Built on tokio for efficient async operations
- 🔒 Type-Safe - Strong typing with enums, structs, and comprehensive error types
- 📡 Streaming - First-class support for streaming responses with
Streamtrait - 🛠️ Tool Calling - Full support for function/tool calling
- 🎯 Flexible Input - Accept strings, message arrays, or structured objects
- 🚀 Zero-Cost Abstractions - Efficient implementation with minimal overhead
- 📦 Minimal Dependencies - Only essential, well-maintained dependencies
Installation
Add this to your Cargo.toml:
[]
= "2.0"
= { = "1", = ["full"] }
Quick Start
use Edgee;
async
Configuration
The SDK supports multiple ways to configure the client:
From Environment Variables
Set EDGEE_API_KEY (required) and optionally EDGEE_BASE_URL:
let client = from_env?;
With API Key
let client = with_api_key;
With Custom Configuration
use EdgeeConfig;
let config = new
.with_base_url;
let client = new;
Usage Examples
Simple Text Completion
use Edgee;
let client = from_env?;
let response = client.send.await?;
println!;
Multi-turn Conversation
use ;
let client = from_env?;
let messages = vec!;
let response = client.send.await?;
println!;
Streaming Responses
use Edgee;
use StreamExt;
let client = from_env?;
let mut stream = client.stream.await?;
while let Some = stream.next.await
Tool/Function Calling
use ;
use HashMap;
let client = from_env?;
// Define a function
let function = FunctionDefinition ;
// Send request with tools
let input = new
.with_tools;
let response = client.send.await?;
// Handle tool calls
if let Some = response.tool_calls
API Reference
Client
Edgee::new(config: EdgeeConfig) -> Self
Create a new client with the given configuration.
Edgee::from_env() -> Result<Self>
Create a client from environment variables (EDGEE_API_KEY, EDGEE_BASE_URL).
Edgee::with_api_key(api_key: impl Into<String>) -> Self
Create a client with just an API key (uses default base URL).
Edgee::send(model: impl Into<String>, input: impl Into<Input>) -> Result<SendResponse>
Send a non-streaming chat completion request.
- model: Model identifier (e.g., "gpt-4o", "mistral-large-latest")
- input: Can be a
&str,String,Vec<Message>, orInputObject
Edgee::stream(model: impl Into<String>, input: impl Into<Input>) -> Result<impl Stream<Item = Result<StreamChunk>>>
Send a streaming chat completion request.
Returns a Stream of StreamChunk items that can be processed as they arrive.
Data Models
Message
Represents a message in the conversation.
Constructors:
Message::system(content)- System messageMessage::user(content)- User messageMessage::assistant(content)- Assistant messageMessage::tool(tool_call_id, content)- Tool response message
InputObject
Structured input for chat completions.
let input = new
.with_tools
.with_tool_choice;
SendResponse
Response from a non-streaming request.
Convenience methods:
text()- Get text from the first choicemessage()- Get the message from the first choicefinish_reason()- Get the finish reasontool_calls()- Get tool calls from the first choice
StreamChunk
Chunk from a streaming response.
Convenience methods:
text()- Get text delta from the first choicerole()- Get the role from the first choicefinish_reason()- Get the finish reason
Error Handling
The SDK uses a custom Error enum with thiserror:
use ;
match client.send.await
Supported Models
The SDK works with any model supported by the Edgee AI Gateway, including:
- OpenAI:
gpt-4o,gpt-4-turbo,gpt-3.5-turbo - Anthropic:
claude-3-5-sonnet-20241022,claude-3-opus-20240229 - Mistral:
mistral-large-latest,mistral-medium-latest - And more...
Examples
Run the examples to see the SDK in action:
# Set your API key
# Simple example
# Streaming example
# Tool calling example
Comparison with Python SDK
This Rust SDK provides similar functionality to the Python SDK with Rust-specific improvements:
| Feature | Python SDK | Rust SDK |
|---|---|---|
| API | Synchronous | Async/await |
| Type Safety | Runtime (dataclasses) | Compile-time (strong types) |
| Error Handling | Exceptions | Result<T, E> |
| Streaming | Generator | Stream trait |
| Input Flexibility | ✅ | ✅ (with Into traits) |
| Tool Calling | ✅ | ✅ |
| Dependencies | Zero (stdlib only) | Minimal (tokio, reqwest, serde) |
| Performance | Good | Excellent (zero-cost abstractions) |
Rust Idioms Used
This SDK follows Rust best practices:
- Strong Typing: Uses enums for roles, structs for messages
- Builder Pattern:
EdgeeConfig::new().with_base_url() - Into Traits: Flexible input with
impl Into<Input> - Error Handling:
Result<T, E>withthiserror - Async/Await: Non-blocking I/O with tokio
- Stream Trait: Idiomatic streaming with
futures::Stream - Option Types:
Option<T>for optional fields - Zero-Copy: Efficient string handling with references
Testing
Run the test suite:
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.