#[cfg(feature = "postgres")]
use oris_runtime::{
add_documents,
embedding::openai::openai_embedder::OpenAiEmbedder,
schemas::Document,
similarity_search,
vectorstore::{pgvector::StoreBuilder, VectorStore},
};
#[cfg(feature = "postgres")]
use std::io::Write;
#[cfg(feature = "postgres")]
use tokio::io::{self, AsyncBufReadExt, BufReader};
#[cfg(feature = "postgres")]
#[tokio::main]
async fn main() {
let embedder = OpenAiEmbedder::default();
let store = StoreBuilder::new()
.embedder(embedder)
.pre_delete_collection(true)
.connection_url("postgresql://username:password@localhost:5432/oris")
.vector_dimensions(1536)
.build()
.await
.unwrap();
let mut input = String::new();
print!("Please enter a list separated by commas: ");
std::io::stdout().flush().unwrap();
let mut reader = BufReader::new(io::stdin());
reader.read_line(&mut input).await.unwrap();
let input = input.trim_end();
let list: Vec<&str> = input.split(',').collect();
let documents: Vec<Document> = list
.iter()
.map(|text| Document::new(text.trim().to_string()))
.collect();
let _ = add_documents!(store, &documents).await.map_err(|e| {
println!("Error adding documents: {:?}", e);
});
let mut search_input = String::new();
print!("Please enter the text you want to search: ");
std::io::stdout().flush().unwrap();
reader.read_line(&mut search_input).await.unwrap();
let search_input = search_input.trim_end();
let data = similarity_search!(store, search_input, 10)
.await
.map_err(|e| {
println!("Error searching documents: {:?}", e);
})
.unwrap();
data.iter().for_each(|d| println!("{:?}", d.page_content));
}
#[cfg(not(feature = "postgres"))]
fn main() {
println!("This example requires the 'postgres' feature to be enabled.");
println!("Please run the command as follows:");
println!("cargo run --example vector_store_postgres --features postgres");
}