openai_dive 1.0.0

OpenAI Dive is an unofficial async Rust library that allows you to interact with the OpenAI API.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
# OpenAI Dive


[![crates.io](https://img.shields.io/crates/v/openai_dive.svg?style=flat-square)](https://crates.io/crates/openai_dive)
![cargo build](https://img.shields.io/github/actions/workflow/status/tjardoo/openai-client/cargo-build.yml?style=flat-square)
[![docs.rs](https://img.shields.io/docsrs/openai_dive?style=flat-square)](https://docs.rs/openai_dive)
[![crates.io](https://img.shields.io/crates/d/openai_dive.svg?style=flat-square)](https://crates.io/crates/openai_dive)

OpenAI Dive is an unofficial async Rust library that allows you to interact with the OpenAI API.

```ini
[dependencies]
openai_dive = "0.7"
```

## Get started


```rust
use openai_dive::v1::api::Client;

let api_key = std::env::var("OPENAI_API_KEY").expect("$OPENAI_API_KEY is not set");

let client = Client::new_from_env(); // or Client::new(api_key);

let result = client
    .models()
    .list()
    .await?;
```

- [Set API key]#set-api-key
- [Using OpenAI-compatible APIs]#using-openai-compatible-apis
- [Set organization/project id]#set-organizationproject-id
- [Add proxy]#add-proxy
- [Available models]#available-models

## Endpoints


- [Chat]#chat
  - [Create chat completion]#create-chat-completion
  - [Chat vision]#chat-vision
  - [Chat audio]#chat-audio
  - [Function calling]#function-calling
  - [Structured outputs]#structured-outputs
  - [Web search]#web-search
- [Responses]#responses
- [Images]#images
- [Audio]#audio
- [Models]#models
- [Files]#files
- [Embeddings]#embeddings
- [Moderation]#moderation
- [Uploads]#uploads
- [Fine-tuning]#fine-tuning
- [Batches]#batches
- [Administration]#administration
- [Usage]#usage
- [Realtime]#realtime

## Chat


Given a list of messages comprising a conversation, the model will return a response.

### Create chat completion


Creates a model response for the given chat conversation.

```rust
let parameters = ChatCompletionParametersBuilder::default()
    .model(FlagshipModel::Gpt4O.to_string())
    .messages(vec![
        ChatMessage::User {
            content: ChatMessageContent::Text("Hello!".to_string()),
            name: None,
        },
        ChatMessage::User {
            content: ChatMessageContent::Text("What is the capital of Vietnam?".to_string()),
            name: None,
        },
    ])
    .response_format(ChatCompletionResponseFormat::Text)
    .build()?;

let result = client
    .chat()
    .create(parameters)
    .await?;
```

More information: [Create chat completion](https://platform.openai.com/docs/api-reference/chat/create)

### Chat vision


Learn how to use vision capabilities to understand images.

```rust
let parameters = ChatCompletionParametersBuilder::default()
    .model(FlagshipModel::Gpt4O.to_string())
    .messages(vec![
        ChatMessage::User {
            content: ChatMessageContent::Text("What is in this image?".to_string()),
            name: None,
        },
        ChatMessage::User {
            content: ChatMessageContent::ContentPart(vec![ChatMessageContentPart::Image(
                ChatMessageImageContentPart {
                    r#type: "image_url".to_string(),
                    image_url: ImageUrlType {
                        url:
                            "https://images.unsplash.com/photo-1526682847805-721837c3f83b?w=640"
                                .to_string(),
                        detail: None,
                    },
                },
            )]),
            name: None,
        },
    ])
    .build()?;

let result = client
    .chat()
    .create(parameters)
    .await?;
```

More information: [Vision](https://platform.openai.com/docs/guides/vision)

### Chat audio


Learn how to use audio capabilities to understand audio files.

```rust
let recording = std::fs::read("example-audio.txt").unwrap();

let parameters = ChatCompletionParametersBuilder::default()
    .model(FlagshipModel::Gpt4OAudioPreview.to_string())
    .messages(vec![
        ChatMessage::User {
            content: ChatMessageContent::Text(
                "What do you hear in this recording?".to_string(),
            ),
            name: None,
        },
        ChatMessage::User {
            content: ChatMessageContent::AudioContentPart(vec![ChatMessageAudioContentPart {
                r#type: "input_audio".to_string(),
                input_audio: InputAudioData {
                    data: String::from_utf8(recording).unwrap(),
                    format: "mp3".to_string(),
                },
            }]),
            name: None,
        },
    ])
    .build()?;

let result = client
    .chat()
    .create(parameters)
    .await?;
```

More information: [Vision](https://platform.openai.com/docs/guides/audio)

### Function calling


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.

```rust
let messages = vec![ChatMessage::User {
    content: ChatMessageContent::Text(
        "Give me a random number higher than 100 but less than 2*150?".to_string(),
    ),
    name: None,
}];

let parameters = ChatCompletionParametersBuilder::default()
    .model(FlagshipModel::Gpt4O.to_string())
    .messages(messages)
    .tools(vec![ChatCompletionTool {
        r#type: ChatCompletionToolType::Function,
        function: ChatCompletionFunction {
            name: "get_random_number".to_string(),
            description: Some("Get a random number between two values".to_string()),
            parameters: json!({
                "type": "object",
                "properties": {
                    "min": {"type": "integer", "description": "Minimum value of the random number."},
                    "max": {"type": "integer", "description": "Maximum value of the random number."},
                },
                "required": ["min", "max"],
            }),
        },
    }])
    .build()?;

let result = client
    .chat()
    .create(parameters)
    .await?;

let message = result.choices[0].message.clone();

if let ChatMessage::Assistant {
    tool_calls: Some(tool_calls),
    ..
} = message
{
    for tool_call in tool_calls {
        let name = tool_call.function.name;
        let arguments = tool_call.function.arguments;

        if name == "get_random_number" {
            let random_numbers: RandomNumber = serde_json::from_str(&arguments).unwrap();

            println!("Min: {:?}", &random_numbers.min);
            println!("Max: {:?}", &random_numbers.max);

            let random_number_result = get_random_number(random_numbers);

            println!(
                "Random number between those numbers: {:?}",
                random_number_result.clone()
            );
        }
    }
}

#[derive(Serialize, Deserialize)]

pub struct RandomNumber {
    min: u32,
    max: u32,
}

fn get_random_number(params: RandomNumber) -> Value {
    let random_number = rand::thread_rng().gen_range(params.min..params.max);

    random_number.into()
}
```

More information: [Function calling](https://platform.openai.com/docs/guides/function-calling)

### Structured outputs


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.

```rust
let parameters = ChatCompletionParametersBuilder::default()
    .model("gpt-4o-2024-08-06")
    .messages(vec![
        ChatMessage::System {
            content: ChatMessageContent::Text(
                "You are a helpful math tutor. Guide the user through the solution step by step."
                    .to_string(),
            ),
            name: None,
        },
        ChatMessage::User {
            content: ChatMessageContent::Text(
                "How can I solve 8x + 7 = -23"
                    .to_string(),
            ),
            name: None,
        },
    ])
    .response_format(ChatCompletionResponseFormat::JsonSchema(JsonSchemaBuilder::default()
        .name("math_reasoning")
        .schema(serde_json::json!({
            "type": "object",
            "properties": {
                "steps": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "properties": {
                            "explanation": { "type": "string" },
                            "output": { "type": "string" }
                        },
                        "required": ["explanation", "output"],
                        "additionalProperties": false
                    }
                },
                "final_answer": { "type": "string" }
            },
            "required": ["steps", "final_answer"],
            "additionalProperties": false
        }))
        .strict(true)
        .build()?
    ))
    .build()?;

let result = client.chat().create(parameters).await?;
```

More information: [Structured outputs](https://platform.openai.com/docs/guides/structured-outputs)

### Web search


Allow models to search the web for the latest information before generating a response.

```rust
let parameters = ChatCompletionParametersBuilder::default()
    .model(ToolModel::Gpt4OMiniSearchPreview.to_string())
    .messages(vec![ChatMessage::User {
        content: ChatMessageContent::Text(
            "What was a positive news story from today?!".to_string(),
        ),
        name: None,
    }])
    .web_search_options(WebSearchOptions {
        search_context_size: Some(WebSearchContextSize::Low),
        user_location: Some(ApproximateUserLocation {
            r#type: UserLocationType::Approximate,
            approximate: WebSearchUserLocation {
                city: Some("Amsterdam".to_string()),
                country: Some("NL".to_string()),
                region: None,
                timezone: None,
            },
        }),
    })
    .response_format(ChatCompletionResponseFormat::Text)
    .build()?;

let result = client.chat().create(parameters).await?;
```

More information: [Web search](https://platform.openai.com/docs/guides/web-search)

## Responses


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.

For more information see the examples in the [examples/responses](https://github.com/tjardoo/openai-client/tree/master/examples/responses) directory.

- Text & image inputs
- Text outputs
- Stateful interactions
- File search
- Web search
- Computer use
- Function calling

## Images


Given a prompt and/or an input image, the model will generate a new image.

- Create image
- Create image edit
- Create image variation

For more information see the examples in the [examples/images](https://github.com/tjardoo/openai-client/tree/master/examples/images) directory.

More information [Images](https://platform.openai.com/docs/api-reference/images)

## Audio


Learn how to turn audio into text or text into audio.

- Create speech
- Create transcription
- Create translation

For more information see the examples in the [examples/audio](https://github.com/tjardoo/openai-client/tree/master/examples/audio) directory.

More information [Audio](https://platform.openai.com/docs/api-reference/audio)

## Models


List and describe the various models available in the API.

For more information see the examples in the [examples/models](https://github.com/tjardoo/openai-client/tree/master/examples/models) directory.

- List models
- Retrieve model
- Delete fine-tune model

More information [Models](https://platform.openai.com/docs/api-reference/models)

## Files


Files are used to upload documents that can be used with features like Assistants, Fine-tuning, and Batch API.

For more information see the examples in the [examples/files](https://github.com/tjardoo/openai-client/tree/master/examples/files) directory.

- List files
- Upload file
- Delete file
- Retrieve file
- Retrieve file content

More information [Files](https://platform.openai.com/docs/api-reference/files)

## Embeddings


Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.

For more information see the examples in the [examples/embeddings](https://github.com/tjardoo/openai-client/tree/master/examples/embeddings) directory.

- Create embeddings

More information: [Embeddings](https://platform.openai.com/docs/api-reference/embeddings)

## Moderation


Given some input text, outputs if the model classifies it as potentially harmful across several categories.

For more information see the examples in the [examples/moderations](https://github.com/tjardoo/openai-client/tree/master/examples/moderations) directory.

- Create moderation

More information [Moderation](https://platform.openai.com/docs/api-reference/moderations)

## Uploads


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.

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.

For more information see the examples in the [examples/uploads](https://github.com/tjardoo/openai-client/tree/master/examples/uploads) directory.

- Create upload
- Add upload part
- Complete upload
- Cancel upload

More information [Uploads](https://platform.openai.com/docs/api-reference/uploads)

## Fine-tuning


Manage fine-tuning jobs to tailor a model to your specific training data.

For more information see the examples in the [examples/fine_tuning](https://github.com/tjardoo/openai-client/tree/master/examples/fine_tuning) directory.

- Create fine-tuning job
- List fine-tuning jobs
- Retrieve fine-tuning job
- Cancel fine-tuning job
- List fine-tuning events
- List fine-tuning checkpoints

More information [Fine-tuning](https://platform.openai.com/docs/api-reference/fine-tuning)

## Batches


Create large batches of API requests for asynchronous processing. The Batch API returns completions within 24 hours for a 50% discount.

For more information see the examples in the [examples/batches](https://github.com/tjardoo/openai-client/tree/master/examples/batches) directory.

- Create batch
- List batches
- Retrieve batch
- Cancel batch

More information [Batch](https://platform.openai.com/docs/api-reference/batch)

## Administration


Programmatically manage your organization.

For more information see the examples in the [examples/administration](https://github.com/tjardoo/openai-client/tree/master/examples/administration) directory.

- Users
- Invites
- Projects
- Project Users
- Project Service Accounts
- Project API Keys
- Rate Limits
- Audit Logs

More information [Administration](https://platform.openai.com/docs/api-reference/administration)

## Usage


The Usage API provides detailed insights into your activity across the OpenAI API.

It also includes a separate Costs endpoint, which offers visibility into your spend, breaking down consumption by invoice line items and project IDs.

For more information see the examples in the [examples/usage](https://github.com/tjardoo/openai-client/tree/master/examples/usage) directory.

- Completions
- Embeddings
- Moderations
- Images
- Audio speeches
- Audio transcriptions
- Vector stores
- Code interpreter sessions
- Costs

More information [Usage](https://platform.openai.com/docs/api-reference/usage)

## Realtime


Communicate with a GPT-4o class model live, in real time, over WebSocket. Produces both audio and text transcriptions.

Enable the feature flag `realtime` to use this feature.

For more information see the examples in the [examples/realtime](https://github.com/tjardoo/openai-client/tree/master/examples/realtime) directory.

- All client events
- All server events

More information [Realtime](https://platform.openai.com/docs/api-reference/realtime)

## Configuration


### Set API key


Add the OpenAI API key to your environment variables.

```sh
# Windows PowerShell

$Env:OPENAI_API_KEY='sk-...'

# Windows cmd

set OPENAI_API_KEY=sk-...

# Linux/macOS

export OPENAI_API_KEY='sk-...'
```

### Using OpenAI-compatible APIs


By simply changing the base URL, you can use this crate with other OpenAI-compatible APIs.

```rust
let deepseek_api_key = std::env::var("DEEPSEEK_API_KEY").expect("DEEPSEEK_API_KEY is not set");

let mut client = Client::new(deepseek_api_key);
client.set_base_url("https://api.deepseek.com");
```

### Set organization/project ID


You can create multiple organizations and projects in the OpenAI platform. This allows you to group files, fine-tuned models and other resources.

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.

```rust
let mut client = Client::new_from_env();

client
    .set_organization("org-XXX")
    .set_project("proj_XXX");
```

### Add proxy


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.

#### Example: set system environment variable


You can set the proxy in the system environment variables ([https://docs.rs/reqwest/latest/reqwest/#proxies](https://docs.rs/reqwest/latest/reqwest/#proxies)).

```sh
export HTTPS_PROXY=socks5://127.0.0.1:1086
```

#### Example: overriding the default client


```rust
use openai_dive::v1::api::Client;

let http_client = reqwest::Client::builder()
    .proxy(reqwest::Proxy::https("socks5://127.0.0.1:1086")?)
    .build()?;

let api_key = std::env::var("OPENAI_API_KEY").expect("$OPENAI_API_KEY is not set");

let client = Client {
    http_client,
    base_url: "https://api.openai.com/v1".to_string(),
    api_key,
    headers: None,
    organization: None,
    project: None,
};
```

### Available Models


You can use these predefined constants to set the model in the parameters or use any string representation (ie. for your custom models).

#### Flagship Models


- Gpt45Preview (`gpt-4.5-preview`)
- Gpt4O (`gpt-4o`)
- Gpt4OAudioPreview (`gpt-4o-audio-preview`)

#### Cost-Optimized Models


- Gpt4OMini (`gpt-4o-mini`)
- Gpt4OMiniAudioPreview (`gpt-4o-mini-audio-preview`)

#### Reasoning Models


- O3Mini (`o3-mini`)
- O1 (`o1`)
- O1Mini (`o1-mini`)

#### Tool Models


- Gpt4OSearchPreview (`gpt-4o-search-preview`)
- Gpt4OMiniSearchPreview (`gpt-4o-mini-search-preview`)
- ComputerUsePreview (`computer-use-preview`)

#### Moderation Models


- OmniModerationLatest (`omni-moderation-latest`)
- TextModerationLatest (`text-moderation-latest`)

#### Embedding Models


- TextEmbedding3Small (`text-embedding-3-small`)
- TextEmbedding3Large (`text-embedding-3-large`)

#### Whisper Models


- Whisper1 (`whisper-1`)

#### TTS Models


- Tts1 (`tts-1`)
- Tts1HD (`tts-1-hd`)

#### DALLĀ·E Models


- DallE3 (`dall-e-3`)
- DallE2 (`dall-e-2`)

More information: [Models](https://platform.openai.com/docs/models)