Skip to content

Commit 96acd50

Browse files
committed
feat: add examples/containers
1 parent 3bb7eb7 commit 96acd50

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

examples/containers/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "containers"
3+
version = "0.1.0"
4+
edition = "2021"
5+
publish = false
6+
7+
[dependencies]
8+
async-openai = {path = "../../async-openai"}
9+
tokio = { version = "1.43.0", features = ["full"] }
10+

examples/containers/src/main.rs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
use async_openai::{
2+
types::{
3+
ContainerExpiresAfter, ContainerExpiresAfterAnchor, CreateContainerFileRequest,
4+
CreateContainerRequestArgs, InputSource,
5+
},
6+
Client,
7+
};
8+
9+
#[tokio::main]
10+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
11+
// Initialize the OpenAI client
12+
let client = Client::new();
13+
14+
// Create a new container
15+
println!("Creating a new container...");
16+
let create_request = CreateContainerRequestArgs::default()
17+
.name("My Test Container")
18+
.expires_after(ContainerExpiresAfter {
19+
anchor: ContainerExpiresAfterAnchor::LastActiveAt,
20+
minutes: 20,
21+
})
22+
.build()?;
23+
24+
let container = client.containers().create(create_request).await?;
25+
println!("Created container with ID: {}", container.id);
26+
println!("Container name: {}", container.name);
27+
println!("Container status: {}", container.status);
28+
29+
// List all containers
30+
println!("\nListing all containers...");
31+
let query = [("limit", "10")];
32+
let list_response = client.containers().list(&query).await?;
33+
println!("Found {} containers", list_response.data.len());
34+
for c in &list_response.data {
35+
println!(" - {} ({})", c.name, c.id);
36+
}
37+
38+
// Retrieve the container
39+
println!("\nRetrieving container...");
40+
let retrieved = client.containers().retrieve(&container.id).await?;
41+
println!("Retrieved container: {}", retrieved.name);
42+
43+
// Create a file in the container using in-memory content
44+
println!("\nCreating a file in the container...");
45+
let file_content = b"Hello from the container!";
46+
let create_file_request = CreateContainerFileRequest {
47+
file: Some(InputSource::VecU8 {
48+
filename: "hello.txt".to_string(),
49+
vec: file_content.to_vec(),
50+
}),
51+
file_id: None,
52+
};
53+
54+
let container_file = client
55+
.containers()
56+
.files(&container.id)
57+
.create(create_file_request)
58+
.await?;
59+
println!("Created file with ID: {}", container_file.id);
60+
println!("File path: {}", container_file.path);
61+
println!("File size: {} bytes", container_file.bytes);
62+
63+
// List files in the container
64+
println!("\nListing files in the container...");
65+
let files_query = [("limit", "10")];
66+
let files_list = client
67+
.containers()
68+
.files(&container.id)
69+
.list(&files_query)
70+
.await?;
71+
println!("Found {} files", files_list.data.len());
72+
for f in &files_list.data {
73+
println!(" - {} ({} bytes)", f.path, f.bytes);
74+
}
75+
76+
// Retrieve the file
77+
println!("\nRetrieving the file...");
78+
let retrieved_file = client
79+
.containers()
80+
.files(&container.id)
81+
.retrieve(&container_file.id)
82+
.await?;
83+
println!("Retrieved file: {}", retrieved_file.path);
84+
85+
// Get file content
86+
println!("\nRetrieving file content...");
87+
let content = client
88+
.containers()
89+
.files(&container.id)
90+
.content(&container_file.id)
91+
.await?;
92+
println!(
93+
"File content: {}",
94+
String::from_utf8_lossy(content.as_ref())
95+
);
96+
97+
// Delete the file
98+
println!("\nDeleting the file...");
99+
let delete_file_response = client
100+
.containers()
101+
.files(&container.id)
102+
.delete(&container_file.id)
103+
.await?;
104+
println!("File deleted: {}", delete_file_response.deleted);
105+
106+
// Delete the container
107+
println!("\nDeleting the container...");
108+
let delete_response = client.containers().delete(&container.id).await?;
109+
println!("Container deleted: {}", delete_response.deleted);
110+
111+
println!("\nAll operations completed successfully!");
112+
Ok(())
113+
}

0 commit comments

Comments
 (0)