Module chat

Source
Expand description

This module provides functionality for creating chat-based completions using the OpenAI Chat Completions API.

The Chat API is designed for conversational interactions, where each request includes a list of messages with a role (system, user, or assistant). The model responds based on the context established by these messages, allowing for more interactive and context-aware responses compared to plain completions.

§Overview

The core usage involves calling create_chat_completion with a CreateChatCompletionRequest, which includes a sequence of ChatMessage items. Each ChatMessage has a role and content. The API then returns a CreateChatCompletionResponse containing one or more ChatCompletionChoice objects (depending on the n parameter).

use chat_gpt_lib_rs::api_resources::chat::{create_chat_completion, CreateChatCompletionRequest, ChatMessage, ChatRole};
use chat_gpt_lib_rs::api_resources::models::Model;
use chat_gpt_lib_rs::error::OpenAIError;
use chat_gpt_lib_rs::OpenAIClient;

#[tokio::main]
async fn main() -> Result<(), OpenAIError> {
    let client = OpenAIClient::new(None)?; // Reads API key from OPENAI_API_KEY

    let request = CreateChatCompletionRequest {
        model: Model::O1Mini,
        messages: vec![
            ChatMessage {
                role: ChatRole::System,
                content: "You are a helpful assistant.".to_string(),
                name: None,
            },
            ChatMessage {
                role: ChatRole::User,
                content: "Write a tagline for an ice cream shop.".to_string(),
                name: None,
            },
        ],
        max_tokens: Some(50),
        temperature: Some(0.7),
        ..Default::default()
    };

    let response = create_chat_completion(&client, &request).await?;

    for choice in &response.choices {
        println!("Assistant: {}", choice.message.content);
    }

    Ok(())
}

Structs§

ChatCompletionChoice
A single chat completion choice within a CreateChatCompletionResponse.
ChatCompletionChunkChoice
A single choice within a streaming chat completion chunk.
ChatCompletionDelta
— Streaming Types —
ChatCompletionUsage
Token usage data, if requested or included by default.
ChatMessage
A single message in a chat conversation.
CreateChatCompletionChunk
A streaming chat completion chunk returned by the API.
CreateChatCompletionRequest
A request struct for creating chat completions with the OpenAI Chat Completions API.
CreateChatCompletionResponse
The response returned by the OpenAI Chat Completions API.

Enums§

ChatRole
The role of a message in the chat sequence.

Functions§

create_chat_completion
Creates a chat-based completion using the OpenAI Chat Completions API.
create_chat_completion_stream
Creates a streaming chat-based completion using the OpenAI Chat Completions API. When stream is set to Some(true), partial updates (chunks) are returned. Each item in the stream is a partial update represented by CreateChatCompletionChunk.