-
Notifications
You must be signed in to change notification settings - Fork 3
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 #15 from oramasearch/feat/adds-llm-module
feat: adds llm module
- Loading branch information
Showing
15 changed files
with
468 additions
and
58 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,30 @@ | ||
use futures::executor::block_on; | ||
use llm::questions_generation::generator::generate_questions; | ||
use textwrap::dedent; | ||
|
||
fn main() { | ||
let context = dedent( | ||
r" | ||
Introduction | ||
When we say that Orama Cloud is batteries-included, we mean that it gives you everything you need to start searching and generating answers (conversations) without any complex configuration. Out of the box, Orama Cloud also includes: | ||
🧩 Native and Custom integrations to easily import your data. | ||
🚀 Web Components to easily integrate a full-featured Searchbox on your website in no time. | ||
📊 Quality checks, analytics and quality control tools to fine-tune your users experience. | ||
🔐 Secure proxy configuration and advanced security options. | ||
and much more… | ||
Basic concepts | ||
At the core of Orama Cloud, there are three simple concepts: | ||
📖 Index: a collection of documents that you can search through. | ||
📄 Schema: a set of rules that define how the documents are structured. | ||
🗿 Immutability: once you’ve created an index and populated it with documents, it will remain immutable. To change the content of an index, you have to perform a re-deployment. | ||
With your index, you can perform full-text, vector, and hybrid search queries, as well as generative conversations. Add your data, define the schema, and you’re ready to go! | ||
", | ||
); | ||
|
||
let questions = block_on(generate_questions(context)).unwrap(); | ||
|
||
dbg!(questions); | ||
} |
2 changes: 1 addition & 1 deletion
2
content_expander/src/bin/test_code.rs → llm/src/bin/test_code.rs
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
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use anyhow::{Context, Result}; | ||
use async_once_cell::OnceCell; | ||
use async_std::sync::RwLock; | ||
use mistralrs::{IsqType, Model, TextModelBuilder}; | ||
use serde::Serialize; | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
use strum::{Display, EnumIter}; | ||
|
||
pub mod content_expander; | ||
pub mod questions_generation; | ||
|
||
static MODELS: OnceCell<RwLock<HashMap<LocalLLM, Arc<Model>>>> = OnceCell::new(); | ||
|
||
#[derive(Serialize, EnumIter, Eq, PartialEq, Hash, Clone, Display)] | ||
pub enum LocalLLM { | ||
#[serde(rename = "microsoft/Phi-3.5-mini-instruct")] | ||
#[strum(serialize = "microsoft/Phi-3.5-mini-instruct")] | ||
Phi3_5MiniInstruct, | ||
|
||
#[serde(rename = "microsoft/Phi-3.5-vision-instruct")] | ||
#[strum(serialize = "microsoft/Phi-3.5-vision-instruct")] | ||
Phi3_5VisionInstruct, | ||
} | ||
|
||
impl LocalLLM { | ||
async fn try_new(&self) -> Result<Arc<Model>> { | ||
MODELS | ||
.get_or_init(async { | ||
let mut models_map = HashMap::new(); | ||
let model = TextModelBuilder::new(self) | ||
.with_isq(IsqType::Q8_0) | ||
.with_logging() | ||
.build() | ||
.await | ||
.with_context(|| "Failed to build the text model") | ||
.unwrap(); | ||
|
||
models_map.insert(self.clone(), Arc::new(model)); | ||
RwLock::new(models_map) | ||
}) | ||
.await; | ||
|
||
let models = MODELS.get().unwrap().read().await; | ||
models | ||
.get(self) | ||
.cloned() | ||
.ok_or_else(|| anyhow::anyhow!("Model not found")) | ||
} | ||
} |
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,48 @@ | ||
use crate::questions_generation::prompts::{ | ||
get_questions_generation_prompt, QUESTIONS_GENERATION_SYSTEM_PROMPT, | ||
}; | ||
use crate::LocalLLM; | ||
use anyhow::{Context, Result}; | ||
use mistralrs::{IsqType, TextMessageRole, TextMessages, TextModelBuilder}; | ||
use serde_json::Value; | ||
use textwrap::dedent; | ||
use utils::parse_json_safely; | ||
|
||
pub async fn generate_questions(context: String) -> Result<Vec<String>> { | ||
let model = LocalLLM::Phi3_5MiniInstruct.try_new().await?; | ||
|
||
let messages = TextMessages::new() | ||
.add_message( | ||
TextMessageRole::System, | ||
dedent(QUESTIONS_GENERATION_SYSTEM_PROMPT), | ||
) | ||
.add_message( | ||
TextMessageRole::User, | ||
get_questions_generation_prompt(context), | ||
); | ||
|
||
let response = model | ||
.send_chat_request(messages) | ||
.await | ||
.context("Failed to send chat request")?; | ||
|
||
if let Some(content) = response | ||
.choices | ||
.first() | ||
.and_then(|choice| choice.message.content.clone()) | ||
{ | ||
match parse_json_safely(content) { | ||
Ok(Value::Array(json_array)) => { | ||
let questions: Vec<String> = json_array | ||
.iter() | ||
.filter_map(|val| val.as_str().map(|s| s.to_string())) | ||
.collect(); | ||
Ok(questions) | ||
} | ||
Ok(_) => anyhow::bail!("Parsed content is not an array of strings"), | ||
Err(e) => Err(e).context("Failed to parse response content as JSON"), | ||
} | ||
} else { | ||
anyhow::bail!("No content in the 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,2 @@ | ||
pub mod generator; | ||
mod prompts; |
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,23 @@ | ||
use textwrap::dedent; | ||
|
||
pub const QUESTIONS_GENERATION_SYSTEM_PROMPT: &str = r#" | ||
Pretend you're a user searching on Google, a forum, or a blog. Your task is to generate a list of questions that relates to the the context (### Context). | ||
For example, if the context was the following: | ||
``` | ||
At Orama, we specialize in edge-application development. This allows us to build high-performance, low-latency applications distributed via global CDNs. In other words, we prioritize performance and security when developing software. | ||
``` | ||
Valid questions would look like the following: | ||
```json | ||
["What does Orama specialize on?", "Is Orama a low-latency edge application?", "Do Orama prioritize security when developing software?"] | ||
``` | ||
Reply with a valid array of strings in a JSON format and nothing more. | ||
"#; | ||
|
||
pub fn get_questions_generation_prompt(context: String) -> String { | ||
format!("### Context\n\n{}", context) | ||
} |
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