openai_rust/
embeddings.rs

1//! See <https://platform.openai.com/docs/api-reference/embeddings>.
2//! Use with [Client::create_embeddings](crate::Client::create_embeddings).
3
4use serde::{Deserialize, Serialize};
5/// Request arguments for embeddings.
6///
7/// See <https://platform.openai.com/docs/api-reference/embeddings/create>.
8///
9/// ```
10/// openai_rust::embeddings::EmbeddingsArguments::new(
11///     "text-embedding-ada-002",
12///     "The food was delicious and the waiter...".to_owned(),
13/// );
14/// ```
15#[derive(Serialize, Debug, Clone)]
16pub struct EmbeddingsArguments {
17    /// ID of the model to use. You can use the [List models](crate::Client::list_models) API to see all of your available models, or see our [Model overview](https://platform.openai.com/docs/models/overview) for descriptions of them.
18    pub model: String,
19    /// Input text to embed, encoded as a string or array of tokens. To embed multiple inputs in a single request, pass an array of strings or array of token arrays. Each input must not exceed the max input tokens for the model (8191 tokens for `text-embedding-ada-002`). [Example Python code](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb) for counting tokens.
20    pub input: String,
21    /// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids).
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub user: Option<String>,
24}
25
26impl EmbeddingsArguments {
27    pub fn new(model: impl AsRef<str>, input: String) -> EmbeddingsArguments {
28        EmbeddingsArguments {
29            model: model.as_ref().to_owned(),
30            input,
31            user: None,
32        }
33    }
34}
35
36/// The response of an embeddings request.
37#[derive(Deserialize, Debug, Clone)]
38pub struct EmbeddingsResponse {
39    pub data: Vec<EmbeddingsData>,
40    pub model: String,
41    pub usage: Usage,
42}
43
44/// The data from an embeddings request.
45#[derive(Deserialize, Debug, Clone)]
46pub struct EmbeddingsData {
47    pub embedding: Vec<f32>,
48    pub index: usize,
49}
50
51/// Token usage information for an [EmbeddingsResponse].
52#[derive(Deserialize, Debug, Clone)]
53pub struct Usage {
54    pub prompt_tokens: u32,
55    pub total_tokens: u32,
56}