Skip to main content

rig_core/providers/gemini/
embedding.rs

1//! Batch text embeddings through the [Gemini API](https://ai.google.dev/api/embeddings).
2//!
3//! ```no_run
4//! use rig_core::providers::gemini::{Gemini, embedding::EMBEDDING_001};
5//!
6//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
7//! let model = Gemini::from_env()?.embedding(EMBEDDING_001, None);
8//! # Ok(())
9//! # }
10//! ```
11
12use crate::error::ProviderError;
13use crate::wire::Flow;
14use serde_json::json;
15
16use crate::embeddings;
17use crate::error::EncodeError;
18use crate::providers::internal::wire::classify_marker_keyed_frame;
19use crate::wire::{
20    Body, Capabilities, Decoder, Descriptor, Encoded, Framing, Mode, Out, Wire, WireEvent,
21    WireFrame,
22};
23
24/// `gemini-embedding-001` embedding model (3072 dimensions by default)
25pub const EMBEDDING_001: &str = "gemini-embedding-001";
26/// `text-embedding-004` embedding model (768 dimensions by default)
27pub const EMBEDDING_004: &str = "text-embedding-004";
28
29/// Returns the default output dimensionality for known Gemini embedding models.
30///
31/// See <https://ai.google.dev/gemini-api/docs/models#gemini-embedding>
32fn model_default_ndims(model: &str) -> Option<usize> {
33    match model {
34        EMBEDDING_001 => Some(3072),
35        EMBEDDING_004 => Some(768),
36        _ => None,
37    }
38}
39
40/// Gemini's batch embedding endpoint.
41///
42/// `POST /v1beta/models/{model}:batchEmbedContents`, authenticated by the
43/// `key` query parameter the GenerateContent family uses.
44#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
45pub struct Embeddings {
46    /// The provider this wire speaks to.
47    pub provider: super::GeminiConfig,
48    /// The embedding model, as the path names it.
49    pub model: String,
50    /// The `output_dimensionality` every document in the batch asks for.
51    pub ndims: usize,
52}
53
54impl Embeddings {
55    /// Build a wire for `model` with the requested output dimensions.
56    /// If `ndims` is absent, use the model default or 768 for unknown models.
57    pub fn new(
58        provider: super::GeminiConfig,
59        model: impl Into<String>,
60        ndims: Option<usize>,
61    ) -> Self {
62        let model = model.into();
63        let ndims = ndims.or_else(|| model_default_ndims(&model)).unwrap_or(768);
64        Self {
65            provider,
66            model,
67            ndims,
68        }
69    }
70}
71
72impl Wire for Embeddings {
73    type Op = crate::operation::Embedding;
74    type Payload = crate::wire::Encoded;
75    type Frame = crate::wire::WireFrame;
76    type Decoder<'id> = EmbeddingsDecoder;
77
78    fn describe(&self) -> Descriptor<'_> {
79        Descriptor::new(super::PROVIDER_NAME)
80            .model(self.model.as_str())
81            .capabilities(Capabilities::embedding(1024, self.ndims))
82    }
83
84    fn encode(&self, request: Vec<String>, _mode: Mode) -> Result<Encoded, EncodeError> {
85        let requests: Vec<_> = request
86            .iter()
87            .map(|doc| {
88                json!({
89                    "model": format!("models/{}", self.model),
90                    "content": json!({
91                        "parts": [json!({
92                            "text": doc
93                        })]
94                    }),
95                    "output_dimensionality": self.ndims,
96                })
97            })
98            .collect();
99
100        let body = json!({ "requests": requests  });
101
102        // Avoid allocating formatted batch JSON unless tracing consumes it.
103        if tracing::enabled!(target: "rig::embedding", tracing::Level::TRACE)
104            && let Ok(pretty_body) = serde_json::to_string_pretty(&body)
105        {
106            tracing::trace!(
107                target: "rig::embedding",
108                "Sending embedding request to Gemini API {pretty_body}"
109            );
110        }
111
112        let request = http::Request::post(format!(
113            "{}/v1beta/models/{}:batchEmbedContents?key={}",
114            self.provider.base_url,
115            self.model,
116            self.provider.api_key.expose()
117        ))
118        .header(http::header::CONTENT_TYPE, "application/json")
119        .body(Body::Bytes(serde_json::to_vec(&body)?))?;
120        // `batchEmbedContents` has no streaming variant, so a streamed call
121        // sends the same bytes and reads the same whole reply.
122        Ok(Encoded::new(request, Framing::Whole))
123    }
124
125    fn decoder<'id>(&self) -> Self::Decoder<'id> {
126        EmbeddingsDecoder
127    }
128}
129
130/// Decode a `batchEmbedContents` reply containing vectors without usage or identity.
131#[derive(Default)]
132pub struct EmbeddingsDecoder;
133
134impl<'id> Decoder<'id, crate::operation::Embedding> for EmbeddingsDecoder {
135    type Event = gemini_api_types::EmbeddingResponse;
136
137    fn classify(&self, frame: WireFrame) -> WireEvent<Self::Event> {
138        classify_marker_keyed_frame(&frame.as_str(), &["embeddings"])
139    }
140
141    fn decode(
142        &mut self,
143        event: Self::Event,
144        out: Out<'id, crate::operation::Embedding>,
145    ) -> Result<Flow, ProviderError> {
146        let vectors = event
147            .embeddings
148            .into_iter()
149            .map(|embedding| embeddings::Embedding {
150                // The document each vector belongs to is not on this wire;
151                // the operation's fold pairs the batch back on by position.
152                document: String::new(),
153                vec: embedding
154                    .values
155                    .into_iter()
156                    .filter_map(|value| value.as_f64())
157                    .collect(),
158            })
159            .collect();
160        // Gemini supplies no usage or response id; the driver attaches the raw body.
161        Ok(out.end(embeddings::EmbeddingResponse::new(vectors)))
162    }
163}
164
165/// Request and response types for [Gemini embeddings](https://ai.google.dev/api/embeddings).
166///
167/// ```
168/// use rig_core::providers::gemini::embedding::gemini_api_types::EmbeddingValues;
169///
170/// let vector = EmbeddingValues { values: vec![1.into(), 2.into()] };
171/// ```
172pub mod gemini_api_types {
173    use serde::{Deserialize, Serialize};
174
175    #[derive(Debug, Clone, Serialize, Deserialize)]
176    pub struct EmbeddingResponse {
177        pub embeddings: Vec<EmbeddingValues>,
178    }
179
180    #[derive(Debug, Clone, Serialize, Deserialize)]
181    pub struct EmbeddingValues {
182        #[serde(default)]
183        pub values: Vec<serde_json::Number>,
184    }
185}
186
187#[cfg(test)]
188mod tests;