-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvector_db_example.rs
More file actions
156 lines (128 loc) · 5.24 KB
/
Copy pathvector_db_example.rs
File metadata and controls
156 lines (128 loc) · 5.24 KB
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
// Copyright {{.Year}} Conductor OSS
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
use std::collections::HashMap;
use std::time::Duration;
use conductor::{
ChatMessage, Configuration, OrkesClients, StartWorkflowRequest, WorkflowDef, WorkflowTask,
};
fn get_username() -> String {
std::env::var("USER")
.or_else(|_| std::env::var("USERNAME"))
.unwrap_or_else(|_| "user".to_string())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize logging
tracing_subscriber::fmt::init();
// Configuration
let llm_provider = format!("open_ai_{}", get_username());
let vector_db = format!("pinecone_{}", get_username());
let embedding_model = "text-embedding-ada-002";
let chat_model = "gpt-4";
// Create clients
let config = Configuration::default();
let clients = OrkesClients::new(config)?;
let workflow_client = clients.get_workflow_client();
let metadata_client = clients.get_metadata_client();
let prompt_client = clients.get_prompt_client();
let integration_client = clients.get_integration_client();
// Define QA prompt
let prompt_name = "us_constitution_qna";
let prompt_text = r#"
Here is the fragment of the us constitution ${text}.
I have a question ${question}.
Given the text fragment from the constitution - please answer the question.
If you cannot answer from within this context of text then say I don't know.
"#;
prompt_client
.save_prompt(prompt_name, "US Constitution QnA", prompt_text)
.await?;
integration_client
.associate_prompt_with_integration(&llm_provider, chat_model, prompt_name)
.await?;
// Build the RAG workflow:
// 1. Search the vector index for relevant text
// 2. Use LLM to answer the question with context
let question = "what is the first amendment to the constitution?";
// Search the vector DB for relevant content
let search_index =
WorkflowTask::llm_search_index("search_vectordb", &vector_db, "test", question)
.with_namespace("us_constitution")
.with_max_results(2)
.with_embedding_model(&llm_provider, embedding_model);
// Use chat complete to answer with context
let chat_complete =
WorkflowTask::llm_chat_complete("chat_complete_ref", &llm_provider, chat_model)
.with_instructions_template(prompt_name)
.with_messages(vec![ChatMessage::user(question)])
.with_prompt_variable(
"text",
serde_json::json!("${search_vectordb.output.result..text}"),
)
.with_prompt_variable("question", serde_json::json!(question));
// Create workflow
let workflow_def = WorkflowDef::new("vector_db_rag_example")
.with_description("RAG workflow: Search vector DB and answer question")
.with_version(1)
.with_task(search_index)
.with_task(chat_complete)
.with_output_param("answer", "${chat_complete_ref.output.result}")
.with_output_param("sources", "${search_vectordb.output.result}");
// Register workflow
metadata_client
.register_or_update_workflow_def(&workflow_def, true)
.await?;
println!("Workflow registered: {}", workflow_def.name);
// Execute the workflow
let request = StartWorkflowRequest::new(&workflow_def.name).with_version(1);
println!("\nAsking: {}", question);
println!("Searching vector database and generating answer...\n");
let result = workflow_client
.execute_workflow(&request, Duration::from_secs(30))
.await?;
println!("Workflow status: {:?}", result.status);
println!("\nAnswer:");
if let Some(answer) = result.output.get("answer") {
println!("{}", answer);
}
Ok(())
}
/// Example: Indexing documents to the vector database
/// This would typically be done separately before running RAG queries
#[allow(dead_code)]
async fn index_document_example(
clients: &OrkesClients,
llm_provider: &str,
vector_db: &str,
embedding_model: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let metadata_client = clients.get_metadata_client();
let workflow_client = clients.get_workflow_client();
// Index a document from URL
let index_doc = WorkflowTask::llm_index_document(
"index_doc_ref",
vector_db,
"test",
"https://constitutioncenter.org/media/files/constitution.pdf",
"us_constitution",
)
.with_namespace("us_constitution")
.with_media_type("application/pdf")
.with_embedding_model(llm_provider, embedding_model)
.with_metadata({
let mut m = HashMap::new();
m.insert("source".to_string(), "constitution center".to_string());
m
});
let index_workflow = WorkflowDef::new("index_document_workflow").with_task(index_doc);
metadata_client
.register_or_update_workflow_def(&index_workflow, true)
.await?;
// Execute indexing
let request = StartWorkflowRequest::new("index_document_workflow");
let result = workflow_client
.execute_workflow(&request, Duration::from_secs(120))
.await?;
println!("Indexing completed: {:?}", result.status);
Ok(())
}