|
| 1 | +use helix_rs::{HelixDB, HelixDBClient}; |
| 2 | +use rig::{ |
| 3 | + Embed, |
| 4 | + client::{EmbeddingsClient, ProviderClient}, |
| 5 | + embeddings::EmbeddingsBuilder, |
| 6 | + vector_store::{InsertDocuments, VectorSearchRequest, VectorStoreIndex}, |
| 7 | +}; |
| 8 | +use rig_helixdb::HelixDBVectorStore; |
| 9 | +use serde::{Deserialize, Serialize}; |
| 10 | + |
| 11 | +// A vector search needs to be performed on the `definitions` field, so we derive the `Embed` trait for `WordDefinition` |
| 12 | +// and tag that field with `#[embed]`. |
| 13 | +// We are not going to store the definitions on our database so we skip the `Serialize` trait |
| 14 | +#[derive(Embed, Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Default)] |
| 15 | +struct WordDefinition { |
| 16 | + word: String, |
| 17 | + #[serde(skip)] // we don't want to serialize this field, we use only to create embeddings |
| 18 | + #[embed] |
| 19 | + definition: String, |
| 20 | +} |
| 21 | + |
| 22 | +impl std::fmt::Display for WordDefinition { |
| 23 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 24 | + write!(f, "{}", self.word) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +#[tokio::main] |
| 29 | +async fn main() { |
| 30 | + let openai_model = |
| 31 | + rig::providers::openai::Client::from_env().embedding_model("text-embedding-ada-002"); |
| 32 | + |
| 33 | + let helixdb_client = HelixDB::new(None, Some(6969), None); // Uses default port 6969 |
| 34 | + let vector_store = HelixDBVectorStore::new(helixdb_client, openai_model.clone()); |
| 35 | + |
| 36 | + let words = vec![ |
| 37 | + WordDefinition { |
| 38 | + word: "flurbo".to_string(), |
| 39 | + definition: "1. *flurbo* (name): A fictional digital currency that originated in the animated series Rick and Morty.".to_string() |
| 40 | + }, |
| 41 | + WordDefinition { |
| 42 | + word: "glarb-glarb".to_string(), |
| 43 | + definition: "1. *glarb-glarb* (noun): A fictional creature found in the distant, swampy marshlands of the planet Glibbo in the Andromeda galaxy.".to_string() |
| 44 | + }, |
| 45 | + WordDefinition { |
| 46 | + word: "linglingdong".to_string(), |
| 47 | + definition: "1. *linglingdong* (noun): A term used by inhabitants of the far side of the moon to describe humans.".to_string(), |
| 48 | + }]; |
| 49 | + |
| 50 | + let documents = EmbeddingsBuilder::new(openai_model) |
| 51 | + .documents(words) |
| 52 | + .unwrap() |
| 53 | + .build() |
| 54 | + .await |
| 55 | + .expect("Failed to create embeddings"); |
| 56 | + |
| 57 | + vector_store.insert_documents(documents).await.unwrap(); |
| 58 | + |
| 59 | + let query = "What is a flurbo?"; |
| 60 | + let vector_req = VectorSearchRequest::builder() |
| 61 | + .query(query) |
| 62 | + .samples(5) |
| 63 | + .build() |
| 64 | + .unwrap(); |
| 65 | + |
| 66 | + let docs = vector_store |
| 67 | + .top_n::<WordDefinition>(vector_req) |
| 68 | + .await |
| 69 | + .unwrap(); |
| 70 | + |
| 71 | + for doc in docs { |
| 72 | + println!( |
| 73 | + "Vector found with id: {id} and score: {score} and word def: {doc}", |
| 74 | + id = doc.1, |
| 75 | + score = doc.0, |
| 76 | + doc = doc.2 |
| 77 | + ) |
| 78 | + } |
| 79 | +} |
0 commit comments