cognis 0.2.0

LLM application framework built on cognis-core
Documentation
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Voyage AI embedding model implementation (Anthropic ecosystem).
//!
//! Provides [`VoyageEmbeddings`], an implementation of the [`Embeddings`] trait
//! for the Voyage AI Embeddings API. Anthropic recommends Voyage AI for embeddings.

use async_trait::async_trait;
use reqwest::Client;
use secrecy::{ExposeSecret, SecretString};
use serde_json::{json, Value};

use cognis_core::embeddings::Embeddings;
use cognis_core::error::{CognisError, Result};

/// Builder for constructing a [`VoyageEmbeddings`] instance.
#[derive(Debug)]
pub struct VoyageEmbeddingsBuilder {
    api_key: Option<SecretString>,
    model: Option<String>,
    input_type: Option<String>,
}

impl VoyageEmbeddingsBuilder {
    /// Create a new builder.
    pub fn new() -> Self {
        Self {
            api_key: None,
            model: None,
            input_type: None,
        }
    }

    /// Set the API key. Falls back to `VOYAGE_API_KEY` or `ANTHROPIC_API_KEY` env var.
    pub fn api_key(mut self, key: impl Into<String>) -> Self {
        self.api_key = Some(SecretString::from(key.into()));
        self
    }

    /// Set the model name (default: `"voyage-3"`).
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.model = Some(model.into());
        self
    }

    /// Set the input type (optional, e.g. `"query"`, `"document"`).
    pub fn input_type(mut self, input_type: impl Into<String>) -> Self {
        self.input_type = Some(input_type.into());
        self
    }

    /// Build the [`VoyageEmbeddings`] instance.
    ///
    /// Returns an error if the API key cannot be resolved from the builder
    /// or environment.
    pub fn build(self) -> Result<VoyageEmbeddings> {
        let api_key = match self.api_key {
            Some(key) => key,
            None => {
                let key = std::env::var("VOYAGE_API_KEY")
                    .or_else(|_| std::env::var("ANTHROPIC_API_KEY"))
                    .map_err(|_| {
                        CognisError::Other(
                            "api_key not provided and neither VOYAGE_API_KEY nor ANTHROPIC_API_KEY env var is set".into(),
                        )
                    })?;
                SecretString::from(key)
            }
        };

        Ok(VoyageEmbeddings {
            api_key,
            model: self.model.unwrap_or_else(|| "voyage-3".into()),
            input_type: self.input_type,
            client: Client::new(),
        })
    }
}

impl Default for VoyageEmbeddingsBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Voyage AI embeddings model (Anthropic ecosystem).
///
/// Implements the Voyage AI Embeddings API for generating text embeddings.
/// Anthropic recommends Voyage AI as the embeddings provider for the Anthropic
/// ecosystem.
///
/// # Example
///
/// ```no_run
/// use cognis::embeddings::anthropic::VoyageEmbeddings;
///
/// let embeddings = VoyageEmbeddings::builder()
///     .api_key("pa-...")
///     .model("voyage-3")
///     .build()
///     .unwrap();
/// ```
pub struct VoyageEmbeddings {
    /// Secret API key.
    api_key: SecretString,
    /// The model identifier (e.g. "voyage-3").
    pub model: String,
    /// Optional input type for the embedding request ("query" or "document").
    pub input_type: Option<String>,
    /// HTTP client.
    client: Client,
}

impl std::fmt::Debug for VoyageEmbeddings {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("VoyageEmbeddings")
            .field("model", &self.model)
            .field("input_type", &self.input_type)
            .finish()
    }
}

impl VoyageEmbeddings {
    /// Returns a new builder for `VoyageEmbeddings`.
    pub fn builder() -> VoyageEmbeddingsBuilder {
        VoyageEmbeddingsBuilder::new()
    }

    /// Build the JSON request payload for the Voyage AI Embeddings API.
    fn build_payload(&self, texts: &[String], input_type: Option<&str>) -> Value {
        let mut payload = json!({
            "model": self.model,
            "input": texts,
        });

        // Use the provided input_type override, falling back to the instance default.
        let effective_input_type = input_type.or(self.input_type.as_deref());
        if let Some(it) = effective_input_type {
            payload["input_type"] = json!(it);
        }

        payload
    }

    /// Call the Voyage AI Embeddings API and return raw embedding vectors.
    async fn call_api(
        &self,
        texts: Vec<String>,
        input_type: Option<&str>,
    ) -> Result<Vec<Vec<f32>>> {
        let url = "https://api.voyageai.com/v1/embeddings";
        let payload = self.build_payload(&texts, input_type);

        let response = self
            .client
            .post(url)
            .header(
                "Authorization",
                format!("Bearer {}", self.api_key.expose_secret()),
            )
            .header("Content-Type", "application/json")
            .json(&payload)
            .send()
            .await
            .map_err(|e| CognisError::Other(format!("HTTP request failed: {}", e)))?;

        let status = response.status().as_u16();
        if !(200..300).contains(&status) {
            let body = response.text().await.unwrap_or_default();
            return Err(CognisError::HttpError { status, body });
        }

        let body: Value = response
            .json()
            .await
            .map_err(|e| CognisError::Other(format!("Failed to parse response JSON: {}", e)))?;

        Self::parse_response(&body)
    }

    /// Parse the Voyage AI embeddings response.
    ///
    /// Expected format: `{"data": [{"embedding": [f32, ...], "index": 0}, ...], ...}`
    fn parse_response(body: &Value) -> Result<Vec<Vec<f32>>> {
        let data = body.get("data").and_then(|v| v.as_array()).ok_or_else(|| {
            CognisError::Other("Missing 'data' array in Voyage AI embeddings response".into())
        })?;

        let mut embeddings: Vec<Vec<f32>> = Vec::with_capacity(data.len());
        for item in data {
            let embedding = item
                .get("embedding")
                .and_then(|v| v.as_array())
                .ok_or_else(|| {
                    CognisError::Other("Missing 'embedding' array in response data item".into())
                })?;

            let vec: Vec<f32> = embedding
                .iter()
                .map(|v| {
                    v.as_f64().map(|f| f as f32).ok_or_else(|| {
                        CognisError::Other("Non-numeric value in embedding array".into())
                    })
                })
                .collect::<Result<Vec<f32>>>()?;

            embeddings.push(vec);
        }

        Ok(embeddings)
    }
}

#[async_trait]
impl Embeddings for VoyageEmbeddings {
    /// Embed a list of documents using the Voyage AI Embeddings API.
    ///
    /// Automatically sets `input_type` to `"document"` for optimal retrieval performance.
    async fn embed_documents(&self, texts: Vec<String>) -> Result<Vec<Vec<f32>>> {
        if texts.is_empty() {
            return Ok(Vec::new());
        }
        self.call_api(texts, Some("document")).await
    }

    /// Embed a single query text using the Voyage AI Embeddings API.
    ///
    /// Automatically sets `input_type` to `"query"` for optimal retrieval performance.
    async fn embed_query(&self, text: &str) -> Result<Vec<f32>> {
        let results = self.call_api(vec![text.to_string()], Some("query")).await?;
        results
            .into_iter()
            .next()
            .ok_or_else(|| CognisError::Other("Empty embedding response for query".into()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_builder_defaults() {
        let embeddings = VoyageEmbeddings::builder()
            .api_key("test-key")
            .build()
            .unwrap();

        assert_eq!(embeddings.model, "voyage-3");
        assert!(embeddings.input_type.is_none());
    }

    #[test]
    fn test_builder_custom_values() {
        let embeddings = VoyageEmbeddings::builder()
            .api_key("test-key")
            .model("voyage-3-lite")
            .input_type("query")
            .build()
            .unwrap();

        assert_eq!(embeddings.model, "voyage-3-lite");
        assert_eq!(embeddings.input_type, Some("query".to_string()));
    }

    #[test]
    fn test_builder_requires_api_key() {
        // Clear env vars to ensure they are not set
        std::env::remove_var("VOYAGE_API_KEY");
        std::env::remove_var("ANTHROPIC_API_KEY");
        let result = VoyageEmbeddings::builder().build();
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("VOYAGE_API_KEY"));
        assert!(err.contains("ANTHROPIC_API_KEY"));
    }

    #[test]
    fn test_build_payload_for_query() {
        let embeddings = VoyageEmbeddings::builder()
            .api_key("test-key")
            .build()
            .unwrap();

        let texts = vec!["what is machine learning?".to_string()];
        let payload = embeddings.build_payload(&texts, Some("query"));

        assert_eq!(payload["model"], "voyage-3");
        assert_eq!(payload["input"], json!(["what is machine learning?"]));
        assert_eq!(payload["input_type"], "query");
    }

    #[test]
    fn test_build_payload_for_documents() {
        let embeddings = VoyageEmbeddings::builder()
            .api_key("test-key")
            .build()
            .unwrap();

        let texts = vec!["hello".to_string(), "world".to_string()];
        let payload = embeddings.build_payload(&texts, Some("document"));

        assert_eq!(payload["model"], "voyage-3");
        assert_eq!(payload["input"], json!(["hello", "world"]));
        assert_eq!(payload["input_type"], "document");
    }

    #[test]
    fn test_parse_response() {
        let body = json!({
            "data": [
                {"embedding": [0.1, 0.2, 0.3], "index": 0},
                {"embedding": [0.4, 0.5, 0.6], "index": 1}
            ],
            "model": "voyage-3",
            "usage": {"total_tokens": 10}
        });

        let result = VoyageEmbeddings::parse_response(&body).unwrap();
        assert_eq!(result.len(), 2);
        assert_eq!(result[0].len(), 3);
        assert!((result[0][0] - 0.1).abs() < 1e-6);
        assert!((result[1][2] - 0.6).abs() < 1e-6);
    }

    #[tokio::test]
    async fn test_embed_documents_empty() {
        let embeddings = VoyageEmbeddings::builder()
            .api_key("test-key")
            .build()
            .unwrap();

        let result = embeddings.embed_documents(vec![]).await.unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn test_api_key_from_env_voyage() {
        // Set VOYAGE_API_KEY and verify it's used
        std::env::set_var("VOYAGE_API_KEY", "env-voyage-key");
        std::env::remove_var("ANTHROPIC_API_KEY");

        let embeddings = VoyageEmbeddings::builder().build().unwrap();
        // Just verify it built successfully (key was found from env)
        assert_eq!(embeddings.model, "voyage-3");

        std::env::remove_var("VOYAGE_API_KEY");
    }

    #[test]
    fn test_api_key_from_env_anthropic_fallback() {
        // Only set ANTHROPIC_API_KEY, not VOYAGE_API_KEY
        std::env::remove_var("VOYAGE_API_KEY");
        std::env::set_var("ANTHROPIC_API_KEY", "env-anthropic-key");

        let embeddings = VoyageEmbeddings::builder().build().unwrap();
        assert_eq!(embeddings.model, "voyage-3");

        std::env::remove_var("ANTHROPIC_API_KEY");
    }

    #[test]
    fn test_custom_model_name() {
        let embeddings = VoyageEmbeddings::builder()
            .api_key("test-key")
            .model("voyage-code-3")
            .build()
            .unwrap();

        assert_eq!(embeddings.model, "voyage-code-3");

        let payload = embeddings.build_payload(&["code snippet".to_string()], Some("document"));
        assert_eq!(payload["model"], "voyage-code-3");
    }

    #[test]
    fn test_debug_does_not_leak_api_key() {
        let embeddings = VoyageEmbeddings::builder()
            .api_key("super-secret-key")
            .build()
            .unwrap();

        let debug_str = format!("{:?}", embeddings);
        assert!(!debug_str.contains("super-secret-key"));
        assert!(debug_str.contains("VoyageEmbeddings"));
        assert!(debug_str.contains("voyage-3"));
    }

    #[test]
    fn test_build_payload_without_input_type() {
        let embeddings = VoyageEmbeddings::builder()
            .api_key("test-key")
            .build()
            .unwrap();

        let texts = vec!["hello".to_string()];
        let payload = embeddings.build_payload(&texts, None);

        assert_eq!(payload["model"], "voyage-3");
        assert_eq!(payload["input"], json!(["hello"]));
        assert!(payload.get("input_type").is_none());
    }

    #[test]
    fn test_build_payload_with_builder_input_type_default() {
        let embeddings = VoyageEmbeddings::builder()
            .api_key("test-key")
            .input_type("document")
            .build()
            .unwrap();

        let texts = vec!["hello".to_string()];
        // When no override is passed, the builder default is used
        let payload = embeddings.build_payload(&texts, None);
        assert_eq!(payload["input_type"], "document");

        // Override takes precedence
        let payload = embeddings.build_payload(&texts, Some("query"));
        assert_eq!(payload["input_type"], "query");
    }

    #[test]
    fn test_parse_response_missing_data() {
        let body = json!({"error": "something"});
        let result = VoyageEmbeddings::parse_response(&body);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("data"));
    }
}