My ChatGPT API Rust Client
A Rust library for interacting with OpenAI's ChatGPT API with streaming support. This library provides a simple and efficient way to communicate with OpenAI's API while handling streaming responses and token usage tracking.
Features
- Stream or non-stream mode for API responses
- Conversation memory to maintain chat history
- Comprehensive error handling
- Token usage tracking
- Flexible output handling via callback functions
- Type-safe API interactions
- Async/await support
Version
Current version: 0.1.3
Requirements
- Rust edition 2024
- Dependencies:
- reqwest
0.12.15(with json, stream, rustls-tls features) - serde
1.0(with derive feature) - serde_json
1.0 - tokio
1.44.2(with full features) - futures-util
0.3 - dotenv
0.15
- reqwest
Installation
Add this to your Cargo.toml:
[]
= { = "https://github.com/bongkow/chatgpt-api", = "0.1.3" }
Usage
use ;
async
Error Handling
The library provides a ChatError enum for different error cases:
Token Usage
Token usage information is provided via the UsageInfo struct:
Chat History
The library maintains conversation context through the ChatMessage struct:
When you pass a chat history to send_chat, the function automatically:
- Includes previous messages in the API request
- Updates the history with new messages
- Maintains context for more coherent multi-turn conversations
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT