diff --git a/Cargo.toml b/Cargo.toml index 5e57eb3..f7b7db4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "openai-api-rs" -version = "0.1.5" +version = "0.1.6" edition = "2021" authors = ["Dongri Jin "] license = "MIT" diff --git a/examples/embedding.rs b/examples/embedding.rs new file mode 100644 index 0000000..d5c53e4 --- /dev/null +++ b/examples/embedding.rs @@ -0,0 +1,19 @@ +use openai_api_rs::v1::api::Client; +use openai_api_rs::v1::embedding::EmbeddingRequest; +use std::env; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string()); + let req = EmbeddingRequest { + model: "text-embedding-ada-002".to_string(), + input: "story time".to_string(), + user: Option::None, + }; + let result = client.embedding(req).await?; + println!("{:?}", result.data); + + Ok(()) +} + +// OPENAI_API_KEY=xxxx cargo run --package openai-api-rs --example embedding diff --git a/src/v1/embedding.rs b/src/v1/embedding.rs index 27d65f2..0baf189 100644 --- a/src/v1/embedding.rs +++ b/src/v1/embedding.rs @@ -1,14 +1,11 @@ use serde::{Deserialize, Serialize}; use std::option::Option; -use crate::v1::common; - #[derive(Debug, Deserialize)] pub struct EmbeddingData { pub object: String, pub embedding: Vec, pub index: i32, - pub usage: common::Usage, } #[derive(Debug, Serialize)] @@ -23,4 +20,12 @@ pub struct EmbeddingRequest { pub struct EmbeddingResponse { pub object: String, pub data: Vec, + pub model: String, + pub usage: Usage, +} + +#[derive(Debug, Deserialize)] +pub struct Usage { + pub prompt_tokens: i32, + pub total_tokens: i32, }