mirror of
https://github.com/mii443/openai-api-rs.git
synced 2025-08-23 15:48:07 +00:00
Fix embedding response
This commit is contained in:
19
examples/embedding.rs
Normal file
19
examples/embedding.rs
Normal file
@ -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<dyn std::error::Error>> {
|
||||||
|
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
|
@ -1,14 +1,11 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::option::Option;
|
use std::option::Option;
|
||||||
|
|
||||||
use crate::v1::common;
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct EmbeddingData {
|
pub struct EmbeddingData {
|
||||||
pub object: String,
|
pub object: String,
|
||||||
pub embedding: Vec<f32>,
|
pub embedding: Vec<f32>,
|
||||||
pub index: i32,
|
pub index: i32,
|
||||||
pub usage: common::Usage,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
@ -23,4 +20,12 @@ pub struct EmbeddingRequest {
|
|||||||
pub struct EmbeddingResponse {
|
pub struct EmbeddingResponse {
|
||||||
pub object: String,
|
pub object: String,
|
||||||
pub data: Vec<EmbeddingData>,
|
pub data: Vec<EmbeddingData>,
|
||||||
|
pub model: String,
|
||||||
|
pub usage: Usage,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct Usage {
|
||||||
|
pub prompt_tokens: i32,
|
||||||
|
pub total_tokens: i32,
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user