-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathembeddings.rs
40 lines (34 loc) · 1.2 KB
/
embeddings.rs
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
//! An example showcasing how to use the embeddings api
//!
//! To run this example:
//! ```bash
//! cargo run --example embeddings
//! ```
use chat_gpt_lib_rs::api_resources::embeddings::{
create_embeddings, CreateEmbeddingsRequest, EmbeddingsInput,
};
use chat_gpt_lib_rs::error::OpenAIError;
use chat_gpt_lib_rs::OpenAIClient;
#[tokio::main]
async fn main() -> Result<(), OpenAIError> {
// Load environment variables from a .env file, if present (optional).
dotenvy::dotenv().ok();
let client = OpenAIClient::new(None)?; // Reads API key from OPENAI_API_KEY
let request = CreateEmbeddingsRequest {
model: "text-embedding-ada-002".to_string(),
input: EmbeddingsInput::String("Hello world".to_string()),
user: None,
};
let response = create_embeddings(&client, &request).await?;
for (i, emb) in response.data.iter().enumerate() {
println!("Embedding #{}: vector size = {}", i, emb.embedding.len());
}
println!("Model used: {}", response.model);
if let Some(usage) = &response.usage {
println!(
"Usage => prompt_tokens: {}, total_tokens: {}",
usage.prompt_tokens, usage.total_tokens
);
}
Ok(())
}