my-chatgpt 0.2.2

A simple API wrapper for the ChatGPT API
Documentation

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 mode for API responses
  • Conversation memory to maintain chat history
  • Comprehensive error handling
  • Detailed token usage tracking with reasoning tokens
  • Flexible output handling via callback functions
  • Type-safe API interactions
  • Async/await support
  • Support for multiple GPT-4 models with appropriate tool configurations

Version

Current version: 0.2.1

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

Installation

Add this to your Cargo.toml:

[dependencies]

my-chatgpt = { git = "https://github.com/bongkow/chatgpt-api", version = "0.2.1" }

Usage

use my_chatgpt::response::{send_chat, ResponseError, UsageInfo, Message, SendChatResult};

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let api_key = "your-api-key";
    let model = "gpt-4o";  // or any other supported model
    let instructions = "You are a helpful assistant.";
    
    // Define a handler function for the API responses
    let handler = |usage: Option<&UsageInfo>, error: Option<&ResponseError>, raw_chunk: Option<&serde_json::Value>| {
        if let Some(e) = error {
            eprintln!("Error: {:?}", e);
        }
        
        if let Some(u) = usage {
            println!("Input tokens: {}", u.input_tokens.unwrap_or(0));
            println!("Output tokens: {}", u.output_tokens.unwrap_or(0));
            println!("Total tokens: {}", u.total_tokens.unwrap_or(0));
            if let Some(details) = &u.input_tokens_details {
                println!("Cached tokens: {}", details.cached_tokens.unwrap_or(0));
            }
            if let Some(details) = &u.output_tokens_details {
                println!("Reasoning tokens: {}", details.reasoning_tokens.unwrap_or(0));
            }
        }
    };
    
    // First message
    let input1 = "Tell me about Rust programming language.";
    let response1 = match send_chat(instructions, input1, api_key, model, handler, None).await {
        SendChatResult::Ok(response) => {
            println!("First response: {}", response.message);
            println!("Model used: {}", response.model);
            println!("Response ID: {}", response.id);
            response
        },
        SendChatResult::Err(e) => panic!("Error: {:?}", e),
    };
    
    // Follow-up question using previous response ID
    let input2 = "What are its main advantages over C++?";
    match send_chat(instructions, input2, api_key, model, handler, Some(&response1.id)).await {
        SendChatResult::Ok(response) => {
            println!("Second response: {}", response.message);
            println!("Model used: {}", response.model);
            println!("Response ID: {}", response.id);
        },
        SendChatResult::Err(e) => panic!("Error: {:?}", e),
    };
    
    Ok(())
}

Error Handling

The library provides a ResponseError enum for different error cases:

pub enum ResponseError {
    RequestError(String),    // Errors related to API requests
    ParseError(String),      // Errors in parsing responses
    NetworkError(String),    // Network-related errors
    Unknown(String),         // Other unexpected errors
}

Token Usage

Enhanced token usage information is provided via the UsageInfo struct:

pub struct UsageInfo {
    pub input_tokens: Option<u32>,
    pub input_tokens_details: Option<InputTokensDetails>,
    pub output_tokens: Option<u32>,
    pub output_tokens_details: Option<OutputTokensDetails>,
    pub total_tokens: Option<u32>,
}

pub struct InputTokensDetails {
    pub cached_tokens: Option<u32>,
}

pub struct OutputTokensDetails {
    pub reasoning_tokens: Option<u32>,
}

Response Structure

The enhanced SendChatOK structure provides more detailed response information:

pub struct SendChatOK {
    pub message: String,     // The response message
    pub id: String,         // Unique response identifier
    pub model: String,      // Model used for the response
    pub usage: UsageInfo,   // Detailed token usage information
    pub tools: Vec<Tool>,   // List of tools used in the response
}

Supported Models

The library supports various GPT-4 models with appropriate tool configurations:

  • gpt-4.1-nano
  • gpt-4.1-mini
  • gpt-4.1
  • gpt-4o-mini
  • gpt-4o-mini-search-preview
  • gpt-4o
  • gpt-4o-search-preview

Each model is automatically configured with appropriate tools (e.g., web search preview) based on its capabilities.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT