Expand description
§Grok API Client
A Rust client library for interacting with the Grok AI API (xAI).
This library provides a simple and robust interface to the Grok AI models, with built-in retry logic, error handling, and support for challenging network conditions (like Starlink satellite connections).
§Features
- Simple API: Easy-to-use async interface
- Robust Error Handling: Comprehensive error types with detailed messages
- Automatic Retries: Built-in retry logic for transient network failures
- Starlink Optimized: Special handling for satellite network drops
- Rate Limiting: Client-side rate limit tracking (optional)
- Tool/Function Calling: Support for function calling and tools
§Quick Start
use grok_api::{GrokClient, Result};
#[tokio::main]
async fn main() -> Result<()> {
// Create a client with your API key
let client = GrokClient::new("your-api-key")?;
// Send a simple chat message
let response = client
.chat("What is Rust?", None)
.await?;
println!("Response: {}", response);
Ok(())
}§Advanced Usage
use grok_api::{GrokClient, ChatMessage, Result};
use serde_json::json;
#[tokio::main]
async fn main() -> Result<()> {
let client = GrokClient::builder()
.api_key("your-api-key")
.timeout_secs(60)
.max_retries(5)
.build()?;
// Build a conversation
let messages = vec![
ChatMessage::system("You are a helpful Rust programming assistant."),
ChatMessage::user("How do I create a Vec in Rust?"),
];
let response = client
.chat_with_history(&messages)
.temperature(0.7)
.max_tokens(1000)
.model("grok-3")
.send()
.await?;
println!("Response: {}", response.content().unwrap_or(""));
Ok(())
}Modules§
- prelude
- Re-export commonly used types
Structs§
- Chat
Message - Represents a chat message in a conversation
- Chat
Request - A chat completion request
- Chat
Response - Response from a chat completion request
- Choice
- A single completion choice
- Function
Call - Details of a function call
- Grok
Client - Main client for interacting with the Grok API
- Grok
Client Builder - Builder for configuring a GrokClient
- Image
Url - Represents an image URL
- Message
- A message in a chat completion
- Retry
Config - Configuration for retry behavior
- Tool
Call - A tool/function call made by the assistant
- Usage
- Token usage information
- Video
Url - Represents a video URL
Enums§
- Content
Part - A part of a message content (text, image, or video)
- Error
- Main error type for the Grok API client
- Message
Content - The content of a message, which can be a simple string or a list of parts
- Model
- Available Grok models
- Role
- Role of a message in a conversation
- Stop
Reason - Reason why a completion finished
Constants§
- DEFAULT_
API_ BASE_ URL - The default API base URL for Grok (xAI)
- DEFAULT_
MAX_ RETRIES - Default maximum number of retries
- DEFAULT_
MODEL - The default model to use if none is specified
- DEFAULT_
TIMEOUT_ SECS - Default timeout in seconds
- VERSION
- The version of this crate
Type Aliases§
- Result
- Result type alias for Grok API operations