|
| 1 | +use std::{collections::HashMap, time::Instant}; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + llm::llm::{BaseLLM, LLMParams, MessageType}, |
| 5 | + search::base::{ContextData, ContextText, ResponseType}, |
| 6 | +}; |
| 7 | + |
| 8 | +use super::{ |
| 9 | + mixed_context::{LocalSearchContextBuilderParams, LocalSearchMixedContext}, |
| 10 | + prompts::LOCAL_SEARCH_SYSTEM_PROMPT, |
| 11 | +}; |
| 12 | + |
| 13 | +pub struct LocalSearchResult { |
| 14 | + pub response: ResponseType, |
| 15 | + pub context_data: ContextData, |
| 16 | + pub context_text: ContextText, |
| 17 | + pub completion_time: f64, |
| 18 | + pub llm_calls: usize, |
| 19 | + pub prompt_tokens: usize, |
| 20 | +} |
| 21 | + |
| 22 | +pub struct LocalSearch { |
| 23 | + llm: Box<dyn BaseLLM>, |
| 24 | + context_builder: LocalSearchMixedContext, |
| 25 | + num_tokens_fn: fn(&str) -> usize, |
| 26 | + system_prompt: String, |
| 27 | + response_type: String, |
| 28 | + llm_params: LLMParams, |
| 29 | + context_builder_params: LocalSearchContextBuilderParams, |
| 30 | +} |
| 31 | + |
| 32 | +impl LocalSearch { |
| 33 | + pub fn new( |
| 34 | + llm: Box<dyn BaseLLM>, |
| 35 | + context_builder: LocalSearchMixedContext, |
| 36 | + num_tokens_fn: fn(&str) -> usize, |
| 37 | + llm_params: LLMParams, |
| 38 | + context_builder_params: LocalSearchContextBuilderParams, |
| 39 | + response_type: String, |
| 40 | + system_prompt: Option<String>, |
| 41 | + ) -> Self { |
| 42 | + let system_prompt = system_prompt.unwrap_or(LOCAL_SEARCH_SYSTEM_PROMPT.to_string()); |
| 43 | + |
| 44 | + LocalSearch { |
| 45 | + llm, |
| 46 | + context_builder, |
| 47 | + num_tokens_fn, |
| 48 | + system_prompt, |
| 49 | + response_type, |
| 50 | + llm_params, |
| 51 | + context_builder_params, |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + pub async fn asearch(&self, query: String) -> anyhow::Result<LocalSearchResult> { |
| 56 | + let start_time = Instant::now(); |
| 57 | + let (context_text, context_records) = self |
| 58 | + .context_builder |
| 59 | + .build_context(self.context_builder_params.clone()) |
| 60 | + .await?; |
| 61 | + |
| 62 | + let search_prompt = self |
| 63 | + .system_prompt |
| 64 | + .replace("{context_data}", &context_text) |
| 65 | + .replace("{response_type}", &self.response_type); |
| 66 | + |
| 67 | + let mut search_messages = Vec::new(); |
| 68 | + search_messages.push(HashMap::from([ |
| 69 | + ("role".to_string(), "system".to_string()), |
| 70 | + ("content".to_string(), search_prompt.clone()), |
| 71 | + ])); |
| 72 | + search_messages.push(HashMap::from([ |
| 73 | + ("role".to_string(), "user".to_string()), |
| 74 | + ("content".to_string(), query.to_string()), |
| 75 | + ])); |
| 76 | + |
| 77 | + let search_response = self |
| 78 | + .llm |
| 79 | + .agenerate( |
| 80 | + MessageType::Dictionary(search_messages), |
| 81 | + false, |
| 82 | + None, |
| 83 | + self.llm_params.clone(), |
| 84 | + ) |
| 85 | + .await?; |
| 86 | + |
| 87 | + Ok(LocalSearchResult { |
| 88 | + response: ResponseType::String(search_response), |
| 89 | + context_data: ContextData::Dictionary(context_records), |
| 90 | + context_text: ContextText::String(context_text), |
| 91 | + completion_time: start_time.elapsed().as_secs_f64(), |
| 92 | + llm_calls: 1, |
| 93 | + prompt_tokens: (self.num_tokens_fn)(&search_prompt), |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
0 commit comments