-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from curieo-org/eng-204
Context Search Integration
- Loading branch information
Showing
16 changed files
with
303 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
pub use summarizer::*; | ||
pub use llm_lingua::*; | ||
pub use models::*; | ||
pub use toxicity_llm::*; | ||
pub use prompt_compression::*; | ||
pub use query_rephraser::*; | ||
pub use summarizer::*; | ||
pub use toxicity::*; | ||
|
||
pub mod summarizer; | ||
pub mod llm_lingua; | ||
pub mod models; | ||
pub mod toxicity_llm; | ||
pub mod prompt_compression; | ||
pub mod query_rephraser; | ||
pub mod summarizer; | ||
pub mod toxicity; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use crate::llms::LLMSettings; | ||
use color_eyre::eyre::eyre; | ||
use reqwest::Client; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct PromptCompressionInput { | ||
pub query: String, | ||
pub target_token: u16, | ||
pub context_texts_list: Vec<String>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct PromptCompressionOutput { | ||
pub compressed_prompt: String, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
struct PromptCompressionAPIResponse { | ||
pub response: PromptCompressionOutput, | ||
} | ||
|
||
pub async fn compress( | ||
llm_settings: &LLMSettings, | ||
prompt_compression_input: PromptCompressionInput, | ||
) -> crate::Result<PromptCompressionOutput> { | ||
let client = Client::new(); | ||
let response = client | ||
.post(llm_settings.prompt_compression_url.as_str()) | ||
.json(&prompt_compression_input) | ||
.send() | ||
.await | ||
.map_err(|e| eyre!("Request to prompt compression failed: {e}"))?; | ||
|
||
let prompt_compression_response = response | ||
.json::<PromptCompressionAPIResponse>() | ||
.await | ||
.map_err(|e| eyre!("Failed to parse prompt compression response: {e}"))?; | ||
|
||
Ok(prompt_compression_response.response) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use crate::secrets::Secret; | ||
use color_eyre::eyre::eyre; | ||
use reqwest::header::{HeaderMap, HeaderName, HeaderValue}; | ||
use reqwest::Client; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct QueryRephraserSettings { | ||
pub api_key: Secret<String>, | ||
pub api_url: String, | ||
pub max_tokens: u16, | ||
pub model: String, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct QueryResult { | ||
pub query: String, | ||
pub result: String, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct QueryRephraserInput { | ||
pub query: String, | ||
pub previous_context: Vec<QueryResult>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
struct Choice { | ||
pub text: String, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
struct Output { | ||
pub choices: Vec<Choice>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
struct QueryRephraserAPIResponse { | ||
pub output: Output, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct QueryRephraserOutput { | ||
pub rephrased_query: String, | ||
} | ||
|
||
fn prepare_prompt(query_rephraser_input: &QueryRephraserInput) -> String { | ||
"Rephrase the input text based on the context and the final sentence. So that it can be understood without the context.\n\n---\n\nFollow the following format.\n\nContext: contains the chat history\n\nQuestion: ${question}\n\nReasoning: Let's think step by step in order to ${produce the answer}. We ...\n\nAnswer: Given a chat history and the latest user question, which might reference the context from the chat history, formulate a standalone question that can be understood from the history without needing the chat history. DO NOT ANSWER THE QUESTION - just reformulate it\n\n---\n\nContext: ".to_string() | ||
+ query_rephraser_input.previous_context.iter().map(|x| format!("{}: {}", x.query, x.result)).collect::<Vec<String>>().join("\n").as_str() | ||
+ "\n\nQuestion: " | ||
+ query_rephraser_input.query.as_str() | ||
+ "\n\nReasoning: Let's think step by step in order to...\n\nAnswer: " | ||
} | ||
|
||
#[tracing::instrument(level = "debug", ret, err)] | ||
pub async fn rephrase_query( | ||
settings: &QueryRephraserSettings, | ||
query_rephraser_input: &QueryRephraserInput, | ||
) -> crate::Result<QueryRephraserOutput> { | ||
let client = Client::new(); | ||
let mut headers = HeaderMap::new(); | ||
headers.insert( | ||
HeaderName::from_bytes(b"Authorization") | ||
.map_err(|e| eyre!("Failed to create header: {e}"))?, | ||
HeaderValue::from_str(&settings.api_key.expose()) | ||
.map_err(|e| eyre!("Failed to create header: {e}"))?, | ||
); | ||
|
||
let prompt = prepare_prompt(query_rephraser_input); | ||
|
||
let response = client | ||
.post(&settings.api_url) | ||
.json(&serde_json::json!({ | ||
"model": settings.model, | ||
"prompt": prompt, | ||
"max_tokens": settings.max_tokens, | ||
// "temperature": 1.0, | ||
// "top_p": 1.0, | ||
// "top_k": 50, | ||
// "frequency_penalty": 0.0, | ||
// "presence_penalty": 0.0, | ||
// "repetition_penalty": 1.0, | ||
})) | ||
.headers(headers) | ||
.send() | ||
.await | ||
.map_err(|e| eyre!("Request to query rephraser failed: {e}"))?; | ||
|
||
let response_body = serde_json::from_slice::<QueryRephraserAPIResponse>( | ||
&response | ||
.bytes() | ||
.await | ||
.map_err(|e| eyre!("Failed to read response: {e}"))?, | ||
) | ||
.map_err(|e| eyre!("Failed to parse response: {e}"))?; | ||
|
||
Ok(QueryRephraserOutput { | ||
rephrased_query: response_body.output.choices[0].text.trim().to_string(), | ||
}) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.