docs.rs failed to build grok-rust-sdk-0.1.0
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Grok Rust SDK 🦀⚡
The most comprehensive Rust SDK for xAI's Grok API - Built for developers who want to harness the power of Grok in their Rust applications.
🚀 Early Access: One of the first complete Rust implementations for Grok's advanced features including tool calling, stateful sessions, and collections.
✨ Features
- 🔥 Full Chat API - Support for all Grok models (grok-4-fast-reasoning, grok-4, grok-3, grok-2, grok-1)
- 🛠️ Tool Calling - Function calling with automatic execution and result handling
- 💬 Stateful Sessions - Persistent conversations with context preservation
- 📁 Collections - Organize and manage conversation groups
- ⚡ Streaming - Real-time streaming responses
- 🔒 Type Safe - Comprehensive error handling and async/await support
- 📚 Well Documented - Extensive examples and documentation
🚀 Quick Start
use ;
async
🛠️ Tool Calling Example
use ;
use async_trait;
;
// Register and use tools
let mut registry = new;
registry.register;
let tools = registry.api_tools;
let response = client.chat.await?;
💬 Stateful Sessions
// Create a session manager
let session_mgr = client.session_manager;
// Start a conversation session
let session = session_mgr.create_session.await?;
// Chat with persistent context
let response1 = session.chat.await?;
let response2 = session.chat.await?; // Context preserved!
println!;
📁 Collections
// Organize conversations into collections
let collection_mgr = client.collection_manager;
let tech_collection = collection_mgr.create_collection.await?;
// Add sessions to collections
collection.add_session.await?;
// Search and manage collections
let tech_collections = collection_mgr.search_collections.await;
📦 Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
= { = "1.0", = ["full"] }
= "1.0"
Or install from source:
🔧 Requirements
- Rust 1.70+
- Valid xAI API key (get one at x.ai)
📚 Examples
Run the included examples:
# Basic chat
# Tool calling
# Sessions
# Collections
🏗️ Architecture
grok-rust-sdk/
├── src/
│ ├── lib.rs # Main exports
│ ├── client.rs # HTTP client & API calls
│ ├── chat.rs # Chat completions
│ ├── tools.rs # Tool calling system
│ ├── session.rs # Stateful sessions
│ ├── collections.rs # Collection management
│ └── error.rs # Error handling
├── examples/ # Usage examples
└── tests/ # Integration tests
🤝 Contributing
Contributions welcome! This SDK is built for the community:
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Submit a pull request
📄 License
Licensed under MIT OR Apache-2.0 - see LICENSE for details.
🙏 Acknowledgments
- Built for the xAI developer community
- Inspired by the power of Grok and the elegance of Rust
- Thanks to xAI for their amazing API
Made with ❤️ by @oogalieboogalie
Early access to cutting-edge AI in Rust 🚀
use ;
use tool_params;
use async_trait;
;
// Register and use tools
let mut registry = new;
registry.register;
let tools = registry.api_tools;
let response = client.chat.await?;
// Execute any tool calls
if let Some = &response.message.tool_calls
Stateful Sessions
use SessionManager;
// Create a session manager
let session_mgr = client.session_manager;
// Create a new session
let session = session_mgr.create_session.await;
// Chat with context maintained
let response1 = session.chat.await?;
let response2 = session.chat.await?; // Context preserved
println!;
Collections
use CollectionManager;
// Create a collection manager
let collection_mgr = client.collection_manager;
// Create a collection
let collection = collection_mgr.create_collection.await;
// Add sessions to collection
collection.add_session.await?;
// Search collections
let weather_collections = collection_mgr.search_collections.await;
Streaming
use StreamExt;
let mut stream = client.chat_stream.await?;
while let Some = stream.next.await
Models
Model::Grok4FastReasoning- Fast reasoning model (recommended for most use cases)Model::Grok4- Standard Grok-4 modelModel::Grok3- Legacy Grok-3 modelModel::Grok2- Legacy Grok-2 modelModel::Grok1- Legacy Grok-1 model
Error Handling
The SDK uses a custom GrokError type with specific variants:
GrokError::Http- HTTP request failuresGrokError::Json- Serialization errorsGrokError::Api- API error responsesGrokError::InvalidConfig- Configuration issuesGrokError::Authentication- Auth failuresGrokError::RateLimit- Rate limitingGrokError::ToolExecution- Tool execution failuresGrokError::Session- Session management errorsGrokError::Collection- Collection management errors
Advanced Usage
Custom Tool Parameters
use tool_params;
use param;
// Complex parameter schema
let params = tool_params!;
Session Management
// List all sessions
let sessions = session_mgr.list_sessions.await;
// Get session by ID
if let Some = session_mgr.get_session.await
// Delete old sessions
session_mgr.delete_session.await?;
Collection Organization
// Get collections by tag
let ai_collections = collection_mgr.collections_by_tag.await;
// Get collection statistics
let stats = collection_mgr.stats.await;
println!;
Contributing
This SDK is one of the first comprehensive Rust implementations for the Grok API. Contributions are welcome!
Building from Source
Running Tests
# Set your API key
# Run tests
License
MIT OR Apache-2.0