-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathmain.rs
95 lines (82 loc) · 2.73 KB
/
main.rs
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
// To run this example execute: `cargo run` in the folder.
use langchain_rust::{
embedding::openai::openai_embedder::OpenAiEmbedder,
schemas::Document,
vectorstore::{surrealdb::StoreBuilder, VecStoreOptions, VectorStore},
};
use std::io::Write;
#[tokio::main]
async fn main() {
// Initialize Embedder
let embedder = OpenAiEmbedder::default();
let database_url = std::env::var("DATABASE_URL").unwrap_or("memory".to_string());
let surrealdb_config = surrealdb::opt::Config::new()
.set_strict(true)
.capabilities(surrealdb::opt::capabilities::Capabilities::all());
// Uncomment the following lines to enable authentication
// .user(surrealdb::opt::auth::Root {
// username: "root".into(),
// password: "root".into(),
// });
let db = surrealdb::engine::any::connect((database_url, surrealdb_config))
.await
.unwrap();
db.query("DEFINE NAMESPACE test;")
.await
.unwrap()
.check()
.unwrap();
db.query("USE NAMESPACE test; DEFINE DATABASE test;")
.await
.unwrap()
.check()
.unwrap();
db.use_ns("test").await.unwrap();
db.use_db("test").await.unwrap();
// Initialize the Sqlite Vector Store
let store = StoreBuilder::new()
.embedder(embedder)
.db(db)
.vector_dimensions(1536)
.build()
.await
.unwrap();
// Intialize the tables in the database. This is required to be done only once.
store.initialize().await.unwrap();
// Add documents to the database
let doc1 = Document::new(
"langchain-rust is a port of the langchain python library to rust and was written in 2024.",
);
let doc2 = Document::new(
"langchaingo is a port of the langchain python library to go language and was written in 2023."
);
let doc3 = Document::new(
"Capital of United States of America (USA) is Washington D.C. and the capital of France is Paris."
);
let doc4 = Document::new("Capital of France is Paris.");
store
.add_documents(&vec![doc1, doc2, doc3, doc4], &VecStoreOptions::default())
.await
.unwrap();
// Ask for user input
print!("Query> ");
std::io::stdout().flush().unwrap();
let mut query = String::new();
std::io::stdin().read_line(&mut query).unwrap();
let results = store
.similarity_search(
&query,
2,
&VecStoreOptions::default().with_score_threshold(0.6),
)
.await
.unwrap();
if results.is_empty() {
println!("No results found.");
return;
} else {
results.iter().for_each(|r| {
println!("Document: {}", r.page_content);
});
}
}