Skip to content

Commit 26bdedd

Browse files
committed
add multipart to create_file
1 parent d05165f commit 26bdedd

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

openai_client/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ categories = ["api-bindings", "web-programming::http-client"]
1111
description = "A native Rust client for the complete OpenAI REST API."
1212

1313
[dependencies]
14-
reqwest = "0.12.22"
14+
reqwest = { version = "0.12.22", features = ["multipart"] }
1515
serde = { version = "1.0.219", features = ["derive"] }
1616
serde_json = "1.0.140"
1717
tokio = { version = "1.46.0", features = ["rt", "macros"] }

openai_client/src/client.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ with the `store` parameter set to `true` will be returned. */
451451
Ok(serde_json::from_slice(&response_bytes)?)
452452
}
453453

454-
/** **Starting a new project?** We recommend trying [Responses](/docs/api-reference/responses)
454+
/** **Starting a new project?** We recommend trying [Responses](/docs/api-reference/responses)
455455
to take advantage of the latest OpenAI platform features. Compare
456456
[Chat Completions with Responses](/docs/guides/responses-vs-chat-completions?api-mode=responses).
457457
@@ -463,8 +463,8 @@ and [audio](/docs/guides/audio) guides.
463463
464464
Parameter support can differ depending on the model used to generate the
465465
response, particularly for newer reasoning models. Parameters that are only
466-
supported for reasoning models are noted below. For the current state of
467-
unsupported parameters in reasoning models,
466+
supported for reasoning models are noted below. For the current state of
467+
unsupported parameters in reasoning models,
468468
[refer to the reasoning guide](/docs/guides/reasoning). */
469469
pub async fn create_chat_completion(&self, request_body: crate::types::CreateChatCompletionRequest, ) -> ConversaResult<CreateChatCompletionResponse> {
470470
let address = format!("{}/chat/completions", self.base_address);
@@ -1183,12 +1183,17 @@ The Fine-tuning API only supports `.jsonl` files. The input also has certain req
11831183
The Batch API only supports `.jsonl` files up to 200 MB in size. The input also has a specific required [format](/docs/api-reference/batch/request-input).
11841184
11851185
Please [contact us](https://help.openai.com/) if you need to increase these storage limits. */
1186-
pub async fn create_file(&self, request_body: crate::types::CreateFileRequest, ) -> ConversaResult<crate::types::OpenAIFile> {
1186+
pub async fn create_file(&self, request_body: crate::types::CreateFileRequest, file_name: String) -> ConversaResult<crate::types::OpenAIFile> {
11871187
let address = format!("{}/files", self.base_address);
1188-
let mut request = self.client.post(&address);
1189-
request = request.bearer_auth(&self.api_key);
1190-
request = request.body(serde_json::to_string(&request_body)?);
1191-
let result = request.send().await?;
1188+
let file_part = reqwest::multipart::Part::text(request_body.file)
1189+
.mime_str("application/octet-stream").unwrap()
1190+
.file_name(file_name);
1191+
let form = reqwest::multipart::Form::new()
1192+
.text("purpose", serde_json::to_string(&request_body.purpose)?.replace(r#"""#,""))
1193+
.part("file", file_part);
1194+
1195+
let result = self.client.post(&address).bearer_auth(&self.api_key).multipart(form).send().await?;
1196+
11921197
let status_code = result.status().as_u16();
11931198
let _content_type = result.headers()[reqwest::header::CONTENT_TYPE].to_str()?.to_string();
11941199
let response_bytes = result.bytes().await?;
@@ -2426,7 +2431,7 @@ You can atomically and idempotently activate up to 10 certificates at a time. */
24262431
Ok(serde_json::from_slice(&response_bytes)?)
24272432
}
24282433

2429-
/** Deactivate certificates at the project level. You can atomically and
2434+
/** Deactivate certificates at the project level. You can atomically and
24302435
idempotently deactivate up to 10 certificates at a time. */
24312436
pub async fn deactivate_project_certificates(&self, project_id: &str, request_body: crate::types::ToggleCertificatesRequest, ) -> ConversaResult<crate::types::ListCertificatesResponse> {
24322437
let address = format!("{}/organization/projects/{project_id}/certificates/deactivate", self.base_address);
@@ -3211,7 +3216,7 @@ for the Realtime API. */
32113216
}
32123217

32133218
/** Create an ephemeral API token for use in client-side applications with the
3214-
Realtime API specifically for realtime transcriptions.
3219+
Realtime API specifically for realtime transcriptions.
32153220
Can be configured with the same session parameters as the `transcription_session.update` client event.
32163221
32173222
It responds with a session object, plus a `client_secret` key which contains
@@ -3322,7 +3327,7 @@ as input for the model's response. */
33223327
}
33233328

33243329
/** Cancels a model response with the given ID. Only responses created with
3325-
the `background` parameter set to `true` can be cancelled.
3330+
the `background` parameter set to `true` can be cancelled.
33263331
[Learn more](/docs/guides/background). */
33273332
pub async fn cancel_response(&self, response_id: &str, ) -> ConversaResult<crate::types::Response> {
33283333
let address = format!("{}/responses/{response_id}/cancel", self.base_address);
@@ -3826,8 +3831,8 @@ Once you complete the Upload, we will create a
38263831
you uploaded. This File is usable in the rest of our platform as a regular
38273832
File object.
38283833
3829-
For certain `purpose` values, the correct `mime_type` must be specified.
3830-
Please refer to documentation for the
3834+
For certain `purpose` values, the correct `mime_type` must be specified.
3835+
Please refer to documentation for the
38313836
[supported MIME types for your use case](/docs/assistants/tools/file-search#supported-files).
38323837
38333838
For guidance on the proper filename extensions for each purpose, please
@@ -3875,7 +3880,7 @@ File](/docs/api-reference/files/create). */
38753880
Ok(serde_json::from_slice(&response_bytes)?)
38763881
}
38773882

3878-
/** Completes the [Upload](/docs/api-reference/uploads/object).
3883+
/** Completes the [Upload](/docs/api-reference/uploads/object).
38793884
38803885
Within the returned Upload object, there is a nested [File](/docs/api-reference/files/object) object that is ready to use in the rest of the platform.
38813886
@@ -3903,7 +3908,7 @@ The number of bytes uploaded upon completion must match the number of bytes init
39033908
Ok(serde_json::from_slice(&response_bytes)?)
39043909
}
39053910

3906-
/** Adds a [Part](/docs/api-reference/uploads/part-object) to an [Upload](/docs/api-reference/uploads/object) object. A Part represents a chunk of bytes from the file you are trying to upload.
3911+
/** Adds a [Part](/docs/api-reference/uploads/part-object) to an [Upload](/docs/api-reference/uploads/object) object. A Part represents a chunk of bytes from the file you are trying to upload.
39073912
39083913
Each Part can be at most 64 MB, and you can add Parts until you hit the Upload maximum of 8 GB.
39093914

0 commit comments

Comments
 (0)