Expand description
§tysm
- Thank You So Much
Typed OpenAI Chat Completions.
A strongly-typed Rust client for OpenAI’s ChatGPT API that enforces type-safe responses using Structured Outputs.
This library uses the schemars crate to generate a schema for the desired response type. It also uses serde to deserialize the response into the desired type. Install these crates like so:
cargo add tysm serde schemars@1.0.0-alpha.17
.
§Usage
ⓘ
use tysm::chat_completions::ChatClient;
// We want names separated into `first` and `last`.
#[derive(serde::Deserialize, schemars::JsonSchema)]
struct Name {
first: String,
last: String,
}
async fn get_president_name() {
// Create a client.
// `from_env` will look for an API key under the environment
// variable "OPENAI_API_KEY"
// It will also look inside `.env` if such a file exists.
let client = ChatClient::from_env("gpt-4o").unwrap();
// Request a chat completion from OpenAI and
// parse the response into our `Name` struct.
let name: Name = client
.chat("Who was the first US president?")
.await
.unwrap();
assert_eq!(name.first, "George");
assert_eq!(name.last, "Washington");
}
Alternative name: Typed Schema Magic.
Modules§
- batch
- Batch API for processing large numbers of requests asynchronously.
- chat_
completions - Chat completions are the most common way to interact with the OpenAI API. This module provides a client for interacting with the ChatGPT API.
- embeddings
- Embeddings are a way to represent text in a vector space. This module provides a client for interacting with the OpenAI Embeddings API.
- files
- Files API for interacting with OpenAI’s file management endpoints. This module provides a client for uploading, listing, retrieving, and deleting files.
Structs§
- Open
AiApi KeyError - An error that occurs when the OpenAI API key is not found in the environment.
- Open
AiError - Emitted by the OpenAI API when an error occurs.