Module completions

Source
Expand description

This module provides functionality for creating text completions using the OpenAI Completions API.

Note: This struct (CreateCompletionRequest) has been expanded to capture additional fields from the OpenAI specification, including best_of, seed, suffix, etc. Some fields support multiple data types (e.g., prompt, stop) using #[serde(untagged)] enums for flexible deserialization and serialization.

§Overview

The Completions API can generate or manipulate text based on a given prompt. You specify a model (e.g., "gpt-3.5-turbo-instruct"), a prompt, and various parameters like max_tokens and temperature.
Important: This request object allows for advanced configurations such as best_of, seed, and logit_bias. Use them carefully, especially if they can consume many tokens or produce unexpected outputs.

Typical usage involves calling create_completion with a CreateCompletionRequest:

use chat_gpt_lib_rs::api_resources::completions::{create_completion, CreateCompletionRequest, PromptInput};
use chat_gpt_lib_rs::OpenAIClient;
use chat_gpt_lib_rs::error::OpenAIError;

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

    let request = CreateCompletionRequest {
        model: "gpt-3.5-turbo-instruct".into(),
        // `PromptInput::String` variant if we just have a single prompt text
        prompt: Some(PromptInput::String("Tell me a joke about cats".to_string())),
        max_tokens: Some(50),
        temperature: Some(1.0),
        ..Default::default()
    };

    let response = create_completion(&client, &request).await?;
    if let Some(choice) = response.choices.get(0) {
        println!("Completion: {}", choice.text);
    }
    Ok(())
}

Structs§

ChatCompletionStreamOptions
Placeholder for potential streaming options, per the spec reference: #/components/schemas/ChatCompletionStreamOptions.
CompletionChoice
A single generated completion choice within a CreateCompletionResponse.
CompletionUsage
Token usage data, if requested or included by default.
CreateCompletionRequest
A request struct for creating text completions with the OpenAI API.
CreateCompletionResponse
The response returned by the OpenAI Completions API.

Enums§

PromptInput
Represents the diverse ways a prompt can be supplied:
StopSequence
Represents the different ways stop can be supplied:

Functions§

create_completion
Creates a text completion using the OpenAI Completions API.
create_completion_stream
Creates a streaming text completion using the OpenAI Completions API.