openai_dive/lib.rs
1//! # OpenAI Dive
2//!
3//! OpenAI Dive is an unofficial async Rust library that allows you to interact with the OpenAI API.
4//!
5//! ```ini
6//! [dependencies]
7//! openai_dive = "1.2"
8//! ```
9//!
10//! ## Get started
11//!
12//! ```rust
13//! use openai_dive::v1::api::Client;
14//!
15//! let api_key = std::env::var("OPENAI_API_KEY").expect("$OPENAI_API_KEY is not set");
16//!
17//! let client = Client::new_from_env(); // or Client::new(api_key);
18//!
19//! let result = client
20//! .models()
21//! .list()
22//! .await?;
23//! ```
24//!
25//! - [Set API key](#set-api-key)
26//! - [Using OpenAI-compatible APIs](#using-openai-compatible-apis)
27//! - [Set organization/project id](#set-organizationproject-id)
28//! - [Add proxy](#add-proxy)
29//! - [Available models](#available-models)
30//!
31//! ## Endpoints
32//!
33//! - [Chat](#chat)
34//! - [Completion](#completion)
35//! - [Vision](#vision)
36//! - [Voice](#voice)
37//! - [Function calling](#function-calling)
38//! - [Structured outputs](#structured-outputs)
39//! - [Web search](#web-search)
40//! - [Responses](#responses)
41//! - [Images](#images)
42//! - [Audio](#audio)
43//! - [Models](#models)
44//! - [Files](#files)
45//! - [Embeddings](#embeddings)
46//! - [Moderation](#moderation)
47//! - [Uploads](#uploads)
48//! - [Fine-tuning](#fine-tuning)
49//! - [Batches](#batches)
50//! - [Administration](#administration)
51//! - [Usage](#usage)
52//! - [Realtime](#realtime)
53//!
54//! ## Completion
55//!
56//! Given a list of messages comprising a conversation, the model will return a response.
57//!
58//! ### Create chat completion
59//!
60//! Creates a model response for the given chat conversation.
61//!
62//! ```rust
63//! let parameters = ChatCompletionParametersBuilder::default()
64//! .model(FlagshipModel::Gpt4O.to_string())
65//! .messages(vec![
66//! ChatMessage::User {
67//! content: ChatMessageContent::Text("Hello!".to_string()),
68//! name: None,
69//! },
70//! ChatMessage::User {
71//! content: ChatMessageContent::Text("What is the capital of Vietnam?".to_string()),
72//! name: None,
73//! },
74//! ])
75//! .response_format(ChatCompletionResponseFormat::Text)
76//! .build()?;
77//!
78//! let result = client
79//! .chat()
80//! .create(parameters)
81//! .await?;
82//! ```
83//!
84//! More information: [Create chat completion](https://platform.openai.com/docs/api-reference/chat/create)
85//!
86//! ### Vision
87//!
88//! Learn how to use vision capabilities to understand images.
89//!
90//! ```rust
91//! let parameters = ChatCompletionParametersBuilder::default()
92//! .model(FlagshipModel::Gpt4O.to_string())
93//! .messages(vec![
94//! ChatMessage::User {
95//! content: ChatMessageContent::Text("What is in this image?".to_string()),
96//! name: None,
97//! },
98//! ChatMessage::User {
99//! content: ChatMessageContent::ContentPart(vec![ChatMessageContentPart::Image(
100//! ChatMessageImageContentPart {
101//! r#type: "image_url".to_string(),
102//! image_url: ImageUrlType {
103//! url:
104//! "https://images.unsplash.com/photo-1526682847805-721837c3f83b?w=640"
105//! .to_string(),
106//! detail: None,
107//! },
108//! },
109//! )]),
110//! name: None,
111//! },
112//! ])
113//! .build()?;
114//!
115//! let result = client
116//! .chat()
117//! .create(parameters)
118//! .await?;
119//! ```
120//!
121//! More information: [Vision](https://platform.openai.com/docs/guides/vision)
122//!
123//! ### Voice
124//!
125//! Learn how to use audio capabilities to understand audio files.
126//!
127//! ```rust
128//! let recording = std::fs::read("example-audio.txt").unwrap();
129//!
130//! let parameters = ChatCompletionParametersBuilder::default()
131//! .model(FlagshipModel::Gpt4OAudioPreview.to_string())
132//! .messages(vec![
133//! ChatMessage::User {
134//! content: ChatMessageContent::Text(
135//! "What do you hear in this recording?".to_string(),
136//! ),
137//! name: None,
138//! },
139//! ChatMessage::User {
140//! content: ChatMessageContent::AudioContentPart(vec![ChatMessageAudioContentPart {
141//! r#type: "input_audio".to_string(),
142//! input_audio: InputAudioData {
143//! data: String::from_utf8(recording).unwrap(),
144//! format: "mp3".to_string(),
145//! },
146//! }]),
147//! name: None,
148//! },
149//! ])
150//! .build()?;
151//!
152//! let result = client
153//! .chat()
154//! .create(parameters)
155//! .await?;
156//! ```
157//!
158//! More information: [Vision](https://platform.openai.com/docs/guides/audio)
159//!
160//! ### Function calling
161//!
162//! In an API call, you can describe functions and have the model intelligently choose to output a JSON object containing arguments to call one or many functions. The Chat Completions API does not call the function; instead, the model generates JSON that you can use to call the function in your code.
163//!
164//! ```rust
165//! let messages = vec![ChatMessage::User {
166//! content: ChatMessageContent::Text(
167//! "Give me a random number higher than 100 but less than 2*150?".to_string(),
168//! ),
169//! name: None,
170//! }];
171//!
172//! let parameters = ChatCompletionParametersBuilder::default()
173//! .model(FlagshipModel::Gpt4O.to_string())
174//! .messages(messages)
175//! .tools(vec![ChatCompletionTool {
176//! r#type: ChatCompletionToolType::Function,
177//! function: ChatCompletionFunction {
178//! name: "get_random_number".to_string(),
179//! description: Some("Get a random number between two values".to_string()),
180//! parameters: json!({
181//! "type": "object",
182//! "properties": {
183//! "min": {"type": "integer", "description": "Minimum value of the random number."},
184//! "max": {"type": "integer", "description": "Maximum value of the random number."},
185//! },
186//! "required": ["min", "max"],
187//! }),
188//! },
189//! }])
190//! .build()?;
191//!
192//! let result = client
193//! .chat()
194//! .create(parameters)
195//! .await?;
196//!
197//! let message = result.choices[0].message.clone();
198//!
199//! if let ChatMessage::Assistant {
200//! tool_calls: Some(tool_calls),
201//! ..
202//! } = message
203//! {
204//! for tool_call in tool_calls {
205//! let name = tool_call.function.name;
206//! let arguments = tool_call.function.arguments;
207//!
208//! if name == "get_random_number" {
209//! let random_numbers: RandomNumber = serde_json::from_str(&arguments).unwrap();
210//!
211//! println!("Min: {:?}", &random_numbers.min);
212//! println!("Max: {:?}", &random_numbers.max);
213//!
214//! let random_number_result = get_random_number(random_numbers);
215//!
216//! println!(
217//! "Random number between those numbers: {:?}",
218//! random_number_result.clone()
219//! );
220//! }
221//! }
222//! }
223//!
224//! #[derive(Serialize, Deserialize)]
225//! pub struct RandomNumber {
226//! min: u32,
227//! max: u32,
228//! }
229//!
230//! fn get_random_number(params: RandomNumber) -> Value {
231//! let random_number = rand::thread_rng().gen_range(params.min..params.max);
232//!
233//! random_number.into()
234//! }
235//! ```
236//!
237//! More information: [Function calling](https://platform.openai.com/docs/guides/function-calling)
238//!
239//! ### Structured outputs
240//!
241//! Structured Outputs is a feature that guarantees the model will always generate responses that adhere to your supplied JSON Schema, so you don't need to worry about the model omitting a required key, or hallucinating an invalid enum value.
242//!
243//! ```rust
244//! let parameters = ChatCompletionParametersBuilder::default()
245//! .model("gpt-4o-2024-08-06")
246//! .messages(vec![
247//! ChatMessage::System {
248//! content: ChatMessageContent::Text(
249//! "You are a helpful math tutor. Guide the user through the solution step by step."
250//! .to_string(),
251//! ),
252//! name: None,
253//! },
254//! ChatMessage::User {
255//! content: ChatMessageContent::Text(
256//! "How can I solve 8x + 7 = -23"
257//! .to_string(),
258//! ),
259//! name: None,
260//! },
261//! ])
262//! .response_format(ChatCompletionResponseFormat::JsonSchema {
263//! json_schema: JsonSchemaBuilder::default()
264//! .name("math_reasoning")
265//! .schema(serde_json::json!({
266//! "type": "object",
267//! "properties": {
268//! "steps": {
269//! "type": "array",
270//! "items": {
271//! "type": "object",
272//! "properties": {
273//! "explanation": { "type": "string" },
274//! "output": { "type": "string" }
275//! },
276//! "required": ["explanation", "output"],
277//! "additionalProperties": false
278//! }
279//! },
280//! "final_answer": { "type": "string" }
281//! },
282//! "required": ["steps", "final_answer"],
283//! "additionalProperties": false
284//! }))
285//! .strict(true)
286//! .build()?
287//! }
288//! )
289//! .build()?;
290//!
291//! let result = client.chat().create(parameters).await?;
292//! ```
293//!
294//! More information: [Structured outputs](https://platform.openai.com/docs/guides/structured-outputs)
295//!
296//! ### Web search
297//!
298//! Allow models to search the web for the latest information before generating a response.
299//!
300//! ```rust
301//! let parameters = ChatCompletionParametersBuilder::default()
302//! .model(ToolModel::Gpt4OMiniSearchPreview.to_string())
303//! .messages(vec![ChatMessage::User {
304//! content: ChatMessageContent::Text(
305//! "What was a positive news story from today?!".to_string(),
306//! ),
307//! name: None,
308//! }])
309//! .web_search_options(WebSearchOptions {
310//! search_context_size: Some(WebSearchContextSize::Low),
311//! user_location: Some(ApproximateUserLocation {
312//! r#type: UserLocationType::Approximate,
313//! approximate: WebSearchUserLocation {
314//! city: Some("Amsterdam".to_string()),
315//! country: Some("NL".to_string()),
316//! region: None,
317//! timezone: None,
318//! },
319//! }),
320//! })
321//! .response_format(ChatCompletionResponseFormat::Text)
322//! .build()?;
323//!
324//! let result = client.chat().create(parameters).await?;
325//! ```
326//!
327//! More information: [Web search](https://platform.openai.com/docs/guides/web-search)
328//!
329//! ## Responses
330//!
331//! OpenAI's most advanced interface for generating model responses. Supports text and image inputs, and text outputs. Create stateful interactions with the model, using the output of previous responses as input. Extend the model's capabilities with built-in tools for file search, web search, computer use, and more. Allow the model access to external systems and data using function calling.
332//!
333//! For more information see the examples in the [examples/responses](https://github.com/tjardoo/openai-client/tree/master/examples/responses) directory.
334//!
335//! - Text & image inputs
336//! - Text outputs
337//! - Stateful interactions
338//! - File search
339//! - Web search
340//! - Computer use
341//! - Function calling
342//!
343//! ## Images
344//!
345//! Given a prompt and/or an input image, the model will generate a new image.
346//!
347//! - Create image
348//! - Create image edit
349//! - Create image variation
350//!
351//! For more information see the examples in the [examples/images](https://github.com/tjardoo/openai-client/tree/master/examples/images) directory.
352//!
353//! More information [Images](https://platform.openai.com/docs/api-reference/images)
354//!
355//! ## Audio
356//!
357//! Learn how to turn audio into text or text into audio.
358//!
359//! - Create speech
360//! - Create transcription
361//! - Create translation
362//!
363//! For more information see the examples in the [examples/audio](https://github.com/tjardoo/openai-client/tree/master/examples/audio) directory.
364//!
365//! More information [Audio](https://platform.openai.com/docs/api-reference/audio)
366//!
367//! ## Models
368//!
369//! List and describe the various models available in the API.
370//!
371//! For more information see the examples in the [examples/models](https://github.com/tjardoo/openai-client/tree/master/examples/models) directory.
372//!
373//! - List models
374//! - Retrieve model
375//! - Delete fine-tune model
376//!
377//! More information [Models](https://platform.openai.com/docs/api-reference/models)
378//!
379//! ## Files
380//!
381//! Files are used to upload documents that can be used with features like Assistants, Fine-tuning, and Batch API.
382//!
383//! For more information see the examples in the [examples/files](https://github.com/tjardoo/openai-client/tree/master/examples/files) directory.
384//!
385//! - List files
386//! - Upload file
387//! - Delete file
388//! - Retrieve file
389//! - Retrieve file content
390//!
391//! More information [Files](https://platform.openai.com/docs/api-reference/files)
392//!
393//! ## Embeddings
394//!
395//! Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
396//!
397//! For more information see the examples in the [examples/embeddings](https://github.com/tjardoo/openai-client/tree/master/examples/embeddings) directory.
398//!
399//! - Create embeddings
400//!
401//! More information: [Embeddings](https://platform.openai.com/docs/api-reference/embeddings)
402//!
403//! ## Moderation
404//!
405//! Given some input text, outputs if the model classifies it as potentially harmful across several categories.
406//!
407//! For more information see the examples in the [examples/moderations](https://github.com/tjardoo/openai-client/tree/master/examples/moderations) directory.
408//!
409//! - Create moderation
410//!
411//! More information [Moderation](https://platform.openai.com/docs/api-reference/moderations)
412//!
413//! ## Uploads
414//!
415//! Creates an intermediate Upload object that you can add Parts to. Currently, an Upload can accept at most 8 GB in total and expires after an hour after you create it.
416//!
417//! Once you complete the Upload, we will create a File object that contains all the parts you uploaded. This File is usable in the rest of our platform as a regular File object.
418//!
419//! For more information see the examples in the [examples/uploads](https://github.com/tjardoo/openai-client/tree/master/examples/uploads) directory.
420//!
421//! - Create upload
422//! - Add upload part
423//! - Complete upload
424//! - Cancel upload
425//!
426//! More information [Uploads](https://platform.openai.com/docs/api-reference/uploads)
427//!
428//! ## Fine-tuning
429//!
430//! Manage fine-tuning jobs to tailor a model to your specific training data.
431//!
432//! For more information see the examples in the [examples/fine_tuning](https://github.com/tjardoo/openai-client/tree/master/examples/fine_tuning) directory.
433//!
434//! - Create fine-tuning job
435//! - List fine-tuning jobs
436//! - Retrieve fine-tuning job
437//! - Cancel fine-tuning job
438//! - List fine-tuning events
439//! - List fine-tuning checkpoints
440//!
441//! More information [Fine-tuning](https://platform.openai.com/docs/api-reference/fine-tuning)
442//!
443//! ## Batches
444//!
445//! Create large batches of API requests for asynchronous processing. The Batch API returns completions within 24 hours for a 50% discount.
446//!
447//! For more information see the examples in the [examples/batches](https://github.com/tjardoo/openai-client/tree/master/examples/batches) directory.
448//!
449//! - Create batch
450//! - List batches
451//! - Retrieve batch
452//! - Cancel batch
453//!
454//! More information [Batch](https://platform.openai.com/docs/api-reference/batch)
455//!
456//! ## Administration
457//!
458//! Programmatically manage your organization.
459//!
460//! For more information see the examples in the [examples/administration](https://github.com/tjardoo/openai-client/tree/master/examples/administration) directory.
461//!
462//! - Users
463//! - Invites
464//! - Projects
465//! - Project Users
466//! - Project Service Accounts
467//! - Project API Keys
468//! - Rate Limits
469//! - Audit Logs
470//!
471//! More information [Administration](https://platform.openai.com/docs/api-reference/administration)
472//!
473//! ## Usage
474//!
475//! The Usage API provides detailed insights into your activity across the OpenAI API.
476//!
477//! It also includes a separate Costs endpoint, which offers visibility into your spend, breaking down consumption by invoice line items and project IDs.
478//!
479//! For more information see the examples in the [examples/usage](https://github.com/tjardoo/openai-client/tree/master/examples/usage) directory.
480//!
481//! - Completions
482//! - Embeddings
483//! - Moderations
484//! - Images
485//! - Audio speeches
486//! - Audio transcriptions
487//! - Vector stores
488//! - Code interpreter sessions
489//! - Costs
490//!
491//! More information [Usage](https://platform.openai.com/docs/api-reference/usage)
492//!
493//! ## Realtime
494//!
495//! Communicate with a GPT-4o class model live, in real time, over WebSocket. Produces both audio and text transcriptions.
496//!
497//! Enable the feature flag `realtime` to use this feature.
498//!
499//! For more information see the examples in the [examples/realtime](https://github.com/tjardoo/openai-client/tree/master/examples/realtime) directory.
500//!
501//! - All client events
502//! - All server events
503//!
504//! More information [Realtime](https://platform.openai.com/docs/api-reference/realtime)
505//!
506//! ## Configuration
507//!
508//! ### Set API key
509//!
510//! Add the OpenAI API key to your environment variables.
511//!
512//! ```sh
513//! # Windows PowerShell
514//! $Env:OPENAI_API_KEY='sk-...'
515//!
516//! # Windows cmd
517//! set OPENAI_API_KEY=sk-...
518//!
519//! # Linux/macOS
520//! export OPENAI_API_KEY='sk-...'
521//! ```
522//!
523//! ### Using OpenAI-compatible APIs
524//!
525//! By simply changing the base URL, you can use this crate with other OpenAI-compatible APIs.
526//!
527//! ```rust
528//! let deepseek_api_key = std::env::var("DEEPSEEK_API_KEY").expect("DEEPSEEK_API_KEY is not set");
529//!
530//! let mut client = Client::new(deepseek_api_key);
531//! client.set_base_url("https://api.deepseek.com");
532//! ```
533//!
534//! Use `extra_body` in `ChatCompletionParameters` to pass non-standard parameters supported by OpenAI-compatible APIs.
535//!
536//! Use `query_params` in `ChatCompletionParameters` to pass non-standard `query` parameters supported by OpenAI-compatible APIs.
537//!
538//! ### Set organization/project ID
539//!
540//! You can create multiple organizations and projects in the OpenAI platform. This allows you to group files, fine-tuned models and other resources.
541//!
542//! You can set the organization ID and/or project ID on the client via the `set_organization` and `set_project` methods. If you don't set the organization and/or project ID, the client will use the default organization and default project.
543//!
544//! ```rust
545//! let mut client = Client::new_from_env();
546//!
547//! client
548//! .set_organization("org-XXX")
549//! .set_project("proj_XXX");
550//! ```
551//!
552//! ### Add proxy
553//!
554//! This crate uses `reqwest` as HTTP Client. Reqwest has proxies enabled by default. You can set the proxy via the system environment variable or by overriding the default client.
555//!
556//! #### Example: set system environment variable
557//!
558//! You can set the proxy in the system environment variables ([https://docs.rs/reqwest/latest/reqwest/#proxies](https://docs.rs/reqwest/latest/reqwest/#proxies)).
559//!
560//! ```sh
561//! export HTTPS_PROXY=socks5://127.0.0.1:1086
562//! ```
563//!
564//! #### Example: overriding the default client
565//!
566//! ```rust
567//! use openai_dive::v1::api::Client;
568//!
569//! let http_client = reqwest::Client::builder()
570//! .proxy(reqwest::Proxy::https("socks5://127.0.0.1:1086")?)
571//! .build()?;
572//!
573//! let api_key = std::env::var("OPENAI_API_KEY").expect("$OPENAI_API_KEY is not set");
574//!
575//! let client = Client {
576//! http_client,
577//! base_url: "https://api.openai.com/v1".to_string(),
578//! api_key,
579//! headers: None,
580//! organization: None,
581//! project: None,
582//! };
583//! ```
584//!
585//! ### Available Models
586//!
587//! You can use these predefined constants to set the model in the parameters or use any string representation (ie. for your custom models).
588//!
589//! #### Flagship Models
590//!
591//! - Gpt41 (`gpt-4.1`)
592//! - Gpt4O (`gpt-4o`)
593//! - Gpt4OAudioPreview (`gpt-4o-audio-preview`)
594//!
595//! #### Cost-Optimized Models
596//!
597//! - O4Mini (`o4-mini`)
598//! - Gpt41Nano (`gpt-4.1-nano`)
599//! - Gpt4OMini (`gpt-4o-mini`)
600//!
601//! #### Reasoning Models
602//!
603//! - O4Mini (`o4-mini`)
604//! - O3Mini (`o3-mini`)
605//!
606//! #### Tool Models
607//!
608//! - Gpt4OSearchPreview (`gpt-4o-search-preview`)
609//! - Gpt4OMiniSearchPreview (`gpt-4o-mini-search-preview`)
610//! - ComputerUsePreview (`computer-use-preview`)
611//!
612//! #### Moderation Models
613//!
614//! - OmniModerationLatest (`omni-moderation-latest`)
615//!
616//! #### Embedding Models
617//!
618//! - TextEmbedding3Small (`text-embedding-3-small`)
619//! - TextEmbedding3Large (`text-embedding-3-large`)
620//!
621//! #### Transcription Models
622//!
623//! - Gpt4OTranscribe (`gpt-4o-transcribe`)
624//! - Whisper1 (`whisper-1`)
625//!
626//! #### TTS Models
627//!
628//! - Gpt4OMiniTts (`gpt-4o-mini-tts`)
629//! - Tts1 (`tts-1`)
630//! - Tts1HD (`tts-1-hd`)
631//!
632//! #### Image Models
633//!
634//! - GptImage1 (`gpt-image-1`)
635//! - DallE3 (`dall-e-3`)
636//! - DallE2 (`dall-e-2`)
637//!
638//! More information: [Models](https://platform.openai.com/docs/models)
639
640pub mod v1;