web search done right
Qa model
Mikkel Denker8c6a6a1a48e0
diff --git a/configs/frontend.toml b/configs/frontend.tomlindex e17cda94..9363b767 100644--- a/configs/frontend.toml+++ b/configs/frontend.toml@@ -2,4 +2,5 @@ queries_csv_path = "data/queries_us.csv"host = "0.0.0.0:3000"search_servers = [["0.0.0.0:3001"]]crossencoder_model_path = "data/cross_encoder"+qa_model_path = "data/qa_model"bangs_path = "data/bangs.json"\ No newline at end of filediff --git a/core/src/api/mod.rs b/core/src/api/mod.rsindex 4cb5f44d..47227fa6 100644--- a/core/src/api/mod.rs+++ b/core/src/api/mod.rs@@ -20,6 +20,7 @@ use tower_http::{compression::CompressionLayer, services::ServeDir};use crate::{autosuggest::Autosuggest,bangs::Bangs,+ qa_model::QaModel,ranking::models::cross_encoder::CrossEncoderModel,searcher::{DistributedSearcher, Shard},};@@ -79,6 +80,7 @@ pub async fn favicon() -> impl IntoResponse {pub fn router(queries_csv_path: &str,crossencoder_model_path: &str,+ qa_model_path: &str,bangs_path: &str,shards: Vec<Vec<String>>,) -> Result<Router> {@@ -90,8 +92,9 @@ pub fn router(let autosuggest = Autosuggest::load_csv(queries_csv_path)?;let crossencoder = CrossEncoderModel::open(crossencoder_model_path)?;+ let qa_model = QaModel::open(qa_model_path)?;let bangs = Bangs::from_path(bangs_path);- let searcher = DistributedSearcher::new(shards, crossencoder, bangs);+ let searcher = DistributedSearcher::new(shards, crossencoder, qa_model, bangs);let state = Arc::new(State {searcher,diff --git a/core/src/api/search.rs b/core/src/api/search.rsindex 7d65eddf..be8bc769 100644--- a/core/src/api/search.rs+++ b/core/src/api/search.rs@@ -23,8 +23,8 @@ use axum_macros::debug_handler;use crate::{search_prettifier::{- thousand_sep_number, CodeOrText, DisplayedWebpage, HighlightedSpellCorrection, Sidebar,- Snippet,+ thousand_sep_number, CodeOrText, DisplayedAnswer, DisplayedWebpage,+ HighlightedSpellCorrection, Sidebar, Snippet,},searcher::{self, SearchQuery, SearchResult, NUM_RESULTS_PER_PAGE},webpage::region::{Region, ALL_REGIONS},@@ -49,6 +49,7 @@ struct SearchTemplate {query: String,sidebar: Option<Sidebar>,widget: Option<Widget>,+ direct_answer: Option<DisplayedAnswer>,spell_correction: Option<HighlightedSpellCorrection>,num_matches: String,search_duration_sec: String,@@ -158,6 +159,7 @@ pub async fn route(query,sidebar: result.sidebar,widget: result.widget,+ direct_answer: result.direct_answer,spell_correction: result.spell_corrected_query,num_matches,search_duration_sec,diff --git a/core/src/entrypoint/frontend.rs b/core/src/entrypoint/frontend.rsindex 0b827ea6..ddd08c2b 100644--- a/core/src/entrypoint/frontend.rs+++ b/core/src/entrypoint/frontend.rs@@ -22,6 +22,7 @@ pub async fn run(config: FrontendConfig) -> Result<()> {let app = router(&config.queries_csv_path,&config.crossencoder_model_path,+ &config.qa_model_path,&config.bangs_path,config.search_servers,)?;diff --git a/core/src/lib.rs b/core/src/lib.rsindex aa196245..95bf77d7 100644--- a/core/src/lib.rs+++ b/core/src/lib.rs@@ -48,6 +48,7 @@ mod intmap;mod kahan_sum;mod kv;pub mod prehashed;+mod qa_model;mod query;pub mod ranking;mod schema;@@ -153,6 +154,7 @@ pub struct FrontendConfig {pub queries_csv_path: String,pub host: String,pub crossencoder_model_path: String,+ pub qa_model_path: String,pub bangs_path: String,pub search_servers: Vec<Vec<String>>,}diff --git a/core/src/qa_model.rs b/core/src/qa_model.rsnew file mode 100644index 00000000..95652b5c--- /dev/null+++ b/core/src/qa_model.rs@@ -0,0 +1,398 @@+// Cuely is an open source web search engine.+// Copyright (C) 2022 Cuely ApS+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as+// published by the Free Software Foundation, either version 3 of the+// License, or (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++use std::{cmp::Ordering, ops::Range, path::Path, sync::Mutex};++use crate::Result;+use itertools::Itertools;+use onnxruntime::{+ environment::Environment,+ ndarray::{Array2, Axis},+ tensor::OrtOwnedTensor,+ GraphOptimizationLevel, LoggingLevel,+};+use tokenizers::{PaddingParams, TruncationParams};++pub static ONNX_ENVIRONMENT: once_cell::sync::Lazy<Environment> =+ once_cell::sync::Lazy::new(|| {+ Environment::builder()+ .with_name("qa")+ .with_log_level(LoggingLevel::Info)+ .build()+ .unwrap()+ });++const TRUNCATE_INPUT: usize = 128;+const SCORE_THRESHOLD: f64 = 0.0;++pub struct QaModel {+ tokenizer: tokenizers::Tokenizer,+ session: Mutex<onnxruntime::session::Session<'static>>,+}++#[derive(Debug)]+pub struct Answer {+ pub offset: Range<usize>,+ pub context_idx: usize,+ pub score: f64,+}++impl QaModel {+ pub fn open<P: AsRef<Path>>(folder: P) -> Result<Self> {+ let truncation = TruncationParams {+ max_length: TRUNCATE_INPUT,+ ..Default::default()+ };+ let padding = PaddingParams {+ ..Default::default()+ };++ let mut tokenizer =+ tokenizers::Tokenizer::from_file(folder.as_ref().join("tokenizer.json"))?;+ tokenizer.with_truncation(Some(truncation));+ tokenizer.with_padding(Some(padding));++ let session = Mutex::new(+ ONNX_ENVIRONMENT+ .new_session_builder()?+ .with_optimization_level(GraphOptimizationLevel::All)?+ .with_number_threads(1)?+ .with_model_from_file(folder.as_ref().join("model_quantized.onnx"))?,+ );++ Ok(Self { tokenizer, session })+ }++ pub fn run(&self, question: &str, contexts: &[&str]) -> Option<Answer> {+ if contexts.is_empty() {+ return None;+ }++ let mut batches = Vec::with_capacity(contexts.len());++ for context in contexts {+ batches.push((question.to_string(), context.to_string()));+ }+ let encoded = self.tokenizer.encode_batch(batches, true).unwrap();++ let encoded_ids: Vec<_> = encoded+ .iter()+ .map(|encoding| encoding.get_ids().to_vec())+ .collect();++ let mut ids = Array2::zeros((encoded_ids.len(), encoded_ids.first().unwrap().len()));+ for i in 0..encoded_ids.len() {+ for j in 0..encoded_ids[i].len() {+ ids[[i, j]] = encoded_ids[i][j] as i64;+ }+ }++ let encoded_masks: Vec<_> = encoded+ .iter()+ .map(|encoding| encoding.get_attention_mask().to_vec())+ .collect();++ let mut masks = Array2::zeros((encoded_masks.len(), encoded_masks.first().unwrap().len()));+ for i in 0..encoded_masks.len() {+ for j in 0..encoded_masks[i].len() {+ masks[[i, j]] = encoded_masks[i][j] as i64;+ }+ }++ let mut sess = self.session.lock().unwrap();++ let mut res: Vec<OrtOwnedTensor<f32, _>> = sess.run(vec![ids, masks]).unwrap();++ if res.len() != 2 {+ return None;+ }++ let start_logits = res.remove(0);+ let end_logits = res.remove(0);++ if start_logits.shape()[0] != start_logits.shape()[0] {+ return None;+ }++ let mut best_answer: Option<Answer> = None;++ #[allow(clippy::needless_range_loop)]+ for idx in 0..start_logits.shape()[0] {+ let mut start: Vec<f32> = start_logits+ .index_axis(Axis(0), idx)+ .into_iter()+ .copied()+ .collect();+ softmax(&mut start);++ let mut end: Vec<f32> = end_logits+ .index_axis(Axis(0), idx)+ .into_iter()+ .copied()+ .collect();+ softmax(&mut end);++ let best_start = start+ .iter()+ .position_max_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal))+ .unwrap();+ let best_end = end+ .iter()+ .position_max_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal))+ .unwrap();++ if (best_start == 0 && best_end == 0) || (best_end <= best_start) {+ continue;+ }++ let score = (start[best_start] * end[best_end]) as f64;++ if score < SCORE_THRESHOLD {+ continue;+ }++ let offsets = encoded[idx].get_offsets();+ if offsets[best_end].1 <= offsets[best_start].0 {+ continue;+ }++ let offset = offsets[best_start].0..offsets[best_end].1;++ let candidate_answer = Answer {+ offset,+ context_idx: idx,+ score,+ };++ best_answer = match best_answer {+ Some(curr_best) => {+ if candidate_answer.score > curr_best.score {+ Some(candidate_answer)+ } else {+ Some(curr_best)+ }+ }+ None => Some(candidate_answer),+ }+ }++ best_answer+ }+}++fn softmax(vec: &mut [f32]) {+ let s: f32 = vec.iter().map(|z| z.exp()).sum();++ for z in vec {+ *z = z.exp() / s+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn sanity() {+ let question = "Where is amazon rain forest located";+ let contexts = [+ r#"The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of+which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations.+The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with+minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. States or departments in four nations contain+"Amazonas" in their names. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract+of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species."#,+ ];++ match QaModel::open("../data/qa_model") {+ Ok(model) => {+ let answer = model.run(question, &contexts).unwrap();++ assert!(answer.score > 0.6);+ assert_eq!(+ &contexts[answer.context_idx][answer.offset],+ "South America"+ );+ }+ Err(err) => {+ panic!("{err:?}");+ }+ }+ }++ #[test]+ fn truncated() {+ let question = "Where is amazon rain forest located";+ let contexts = [+ r#"The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of+which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations.+The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with+minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. States or departments in four nations contain+"Amazonas" in their names. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract+of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species.+The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of+which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations.+The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with+minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. States or departments in four nations contain+"Amazonas" in their names. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract+of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species.+The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of+which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations.+The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with+minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. States or departments in four nations contain+"Amazonas" in their names. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract+of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species.+"#,+ ];++ match QaModel::open("../data/qa_model") {+ Ok(model) => {+ let answer = model.run(question, &contexts).unwrap();++ assert!(answer.score > 0.6);+ assert_eq!(+ &contexts[answer.context_idx][answer.offset],+ "South America"+ );+ }+ Err(err) => {+ panic!("{err:?}");+ }+ }+ }++ #[test]+ fn padding() {+ let question = "Where is amazon rain forest located";+ let contexts = [+ r#"The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of"#,+ r#"The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of+which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations.+The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with+minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. States or departments in four nations contain+"Amazonas" in their names. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract+of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species.+The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of+which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations.+The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with+minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. States or departments in four nations contain+"Amazonas" in their names. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract+of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species.+The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of+which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations.+The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with+minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. States or departments in four nations contain+"Amazonas" in their names. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract+of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species."#,+ ];++ match QaModel::open("../data/qa_model") {+ Ok(model) => {+ let answer = model.run(question, &contexts).unwrap();++ assert!(answer.score > 0.6);+ assert_eq!(+ &contexts[answer.context_idx][answer.offset],+ "South America"+ );+ }+ Err(err) => {+ panic!("{err:?}");+ }+ }+ }++ #[test]+ fn no_answer() {+ let question = "Who wrote smells like teen spirit";+ let contexts = [+ r#"The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of+which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations.+The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with+minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. States or departments in four nations contain+"Amazonas" in their names. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract+of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species.+The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of+which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations.+The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with+minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. States or departments in four nations contain+"Amazonas" in their names. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract+of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species.+The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of+which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations.+The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with+minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. States or departments in four nations contain+"Amazonas" in their names. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract+of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species."#,+ ];++ match QaModel::open("../data/qa_model") {+ Ok(model) => {+ let answer = model.run(question, &contexts);++ assert!(answer.is_none());+ }+ Err(err) => {+ panic!("{err:?}");+ }+ }+ }++ #[test]+ fn empty() {+ match QaModel::open("../data/qa_model") {+ Ok(model) => {+ let question = "";+ let contexts = [+ r#"The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of+ which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations.+ The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with+ minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. States or departments in four nations contain+ "Amazonas" in their names. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract+ of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species.+ The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of+ which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations.+ The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with+ minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. States or departments in four nations contain+ "Amazonas" in their names. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract+ of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species.+ The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of+ which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations.+ The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with+ minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. States or departments in four nations contain+ "Amazonas" in their names. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract+ of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species."#,+ ];+ let answer = model.run(question, &contexts);++ assert!(answer.is_none());++ let question = "test";+ let contexts = [r#""#];+ let answer = model.run(question, &contexts);+ assert!(answer.is_none());++ let question = "";+ let contexts = [r#""#];+ let answer = model.run(question, &contexts);+ assert!(answer.is_none());+ }+ Err(err) => {+ panic!("{err:?}");+ }+ }+ }+}diff --git a/core/src/ranking/models/cross_encoder.rs b/core/src/ranking/models/cross_encoder.rsindex 8ba3d688..8546b324 100644--- a/core/src/ranking/models/cross_encoder.rs+++ b/core/src/ranking/models/cross_encoder.rs@@ -142,8 +142,7 @@ mod tests {);}Err(err) => {- dbg!(err);- panic!();+ panic!("{err:?}");}}}diff --git a/core/src/search_prettifier/mod.rs b/core/src/search_prettifier/mod.rsindex 2c4de6c7..83f5aabd 100644--- a/core/src/search_prettifier/mod.rs+++ b/core/src/search_prettifier/mod.rs@@ -161,6 +161,16 @@ pub struct DisplayedWebpage {pub primary_image_uuid: Option<String>,}+#[derive(Debug, Serialize, Deserialize)]+pub struct DisplayedAnswer {+ pub title: String,+ pub url: String,+ pub pretty_url: String,+ pub snippet: String,+ pub answer: String,+ pub body: String,+}+impl From<RetrievedWebpage> for DisplayedWebpage {fn from(webpage: RetrievedWebpage) -> Self {let snippet = generate_snippet(&webpage);diff --git a/core/src/searcher/distributed.rs b/core/src/searcher/distributed.rsindex c1a0c801..f6279db1 100644--- a/core/src/searcher/distributed.rs+++ b/core/src/searcher/distributed.rs@@ -19,13 +19,15 @@ use crate::{collector::{self, BucketCollector},exponential_backoff::ExponentialBackoff,inverted_index::{self, RetrievedWebpage},+ qa_model::QaModel,query,ranking::{models::cross_encoder::CrossEncoderModel,pipeline::{AsRankingWebsite, RankingPipeline, RankingWebsite},},search_prettifier::{- create_stackoverflow_sidebar, DisplayedWebpage, HighlightedSpellCorrection, Sidebar,+ create_stackoverflow_sidebar, DisplayedAnswer, DisplayedWebpage,+ HighlightedSpellCorrection, Sidebar,},searcher::WebsitesResult,widgets::Widget,@@ -34,6 +36,7 @@ use crate::{use std::{cmp::Ordering,net::SocketAddr,+ ops::Range,sync::Arc,time::{Duration, Instant},};@@ -193,6 +196,7 @@ pub enum Request {pub struct DistributedSearcher {shards: Vec<Shard>,cross_encoder: Arc<CrossEncoderModel>,+ qa_model: Arc<QaModel>,bangs: Bangs,}@@ -227,10 +231,16 @@ impl collector::Doc for ScoredWebsitePointer {}impl DistributedSearcher {- pub fn new(shards: Vec<Shard>, model: CrossEncoderModel, bangs: Bangs) -> Self {+ pub fn new(+ shards: Vec<Shard>,+ model: CrossEncoderModel,+ qa_model: QaModel,+ bangs: Bangs,+ ) -> Self {Self {shards,cross_encoder: Arc::new(model),+ qa_model: Arc::new(qa_model),bangs,}}@@ -495,7 +505,7 @@ impl DistributedSearcher {let top_websites = self.combine_results(initial_results, pipeline);// retrieve webpages- let retrieved_webpages: Vec<_> = self+ let mut retrieved_webpages: Vec<_> = self.retrieve_webpages(&top_websites, &query.query).await.into_iter()@@ -506,12 +516,15 @@ impl DistributedSearcher {return Err(Error::SearchFailed);}+ let direct_answer = self.answer(&query.query, &mut retrieved_webpages);+let search_duration_ms = start.elapsed().as_millis();Ok(WebsitesResult {spell_corrected_query,num_hits: num_docs,webpages: retrieved_webpages,+ direct_answer,sidebar,widget,discussions,@@ -534,4 +547,160 @@ impl DistributedSearcher {Widget::try_new(&query.query)}++ fn answer(&self, query: &str, webpages: &mut Vec<DisplayedWebpage>) -> Option<DisplayedAnswer> {+ let contexts: Vec<_> = webpages+ .iter()+ .take(3)+ .map(|webpage| webpage.body.as_str())+ .collect();++ match self.qa_model.run(query, &contexts) {+ Some(answer) => {+ let answer_webpage = webpages.remove(answer.context_idx);+ Some(DisplayedAnswer {+ title: answer_webpage.title,+ url: answer_webpage.url,+ pretty_url: answer_webpage.pretty_url,+ snippet: generate_answer_snippet(&answer_webpage.body, answer.offset.clone()),+ answer: answer_webpage.body[answer.offset].to_string(),+ body: answer_webpage.body,+ })+ }+ None => None,+ }+ }+}++fn generate_answer_snippet(body: &str, answer_offset: Range<usize>) -> String {+ let mut best_start = 0;+ let mut best_end = 0;+ const SNIPPET_LENGTH: usize = 200;++ if body.is_empty() || answer_offset.start > body.len() - 1 {+ return body.to_string();+ }++ for (idx, _) in body.char_indices().filter(|(_, c)| *c == '.') {+ if idx > answer_offset.end + SNIPPET_LENGTH {+ break;+ }++ if idx < answer_offset.start {+ best_start = idx;+ }++ if idx > answer_offset.end {+ best_end = idx;+ }+ }++ if (answer_offset.end - best_start > SNIPPET_LENGTH) || (best_start >= best_end) {+ if answer_offset.end - answer_offset.start >= SNIPPET_LENGTH {+ let end = floor_char_boundary(body, answer_offset.start + SNIPPET_LENGTH);++ return "<b>".to_string() + &body[answer_offset.start..end] + "</b>";+ }++ let chars_either_side = (SNIPPET_LENGTH - (answer_offset.end - answer_offset.start)) / 2;++ let start = ceil_char_boundary(+ body,+ answer_offset+ .start+ .checked_sub(chars_either_side)+ .unwrap_or_default(),+ );+ let mut end = ceil_char_boundary(body, answer_offset.end + chars_either_side);++ if end >= body.len() {+ end = floor_char_boundary(body, body.len());+ }++ body[start..answer_offset.start].to_string()+ + "<b>"+ + &body[answer_offset.clone()]+ + "</b>"+ + &body[answer_offset.end..end]+ } else {+ let mut res = body[best_start..answer_offset.start].to_string()+ + "<b>"+ + &body[answer_offset.clone()]+ + "</b>";++ let remaining_chars = SNIPPET_LENGTH - (res.len() - 7);+ let end = ceil_char_boundary(body, (remaining_chars + answer_offset.end).min(best_end));++ res += &body[answer_offset.end..end];++ res+ }+}++fn ceil_char_boundary(str: &str, index: usize) -> usize {+ let mut res = index;++ while !str.is_char_boundary(res) && res < str.len() {+ res += 1;+ }++ res+}++fn floor_char_boundary(str: &str, index: usize) -> usize {+ let mut res = index;++ while !str.is_char_boundary(res) && res > 0 {+ res -= 1;+ }++ res+}++#[cfg(test)]+mod tests {+ use super::*;+ #[test]+ fn test_generate_answer_snippet() {+ assert_eq!(+ generate_answer_snippet("this is a test", 0..4),+ "<b>this</b> is a test".to_string()+ );++ assert_eq!(+ generate_answer_snippet("this is a test", 0..1000),+ "<b>this is a test</b>".to_string()+ );+ assert_eq!(+ generate_answer_snippet("this is a test", 1000..2000),+ "this is a test".to_string()+ );+ let input = r#"+this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test+this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test+this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test+this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test+this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test+this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test+this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test+this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test+this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test+this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test+this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test+this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test+this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test+this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test+this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test+this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test this is a long test+ "#;++ let res = generate_answer_snippet(input, 0..500);+ assert!(!res.is_empty());+ assert!(res.len() > 100);+ assert!(res.len() < input.len());+ assert!(res.starts_with("<b>"));+ assert!(res.ends_with("</b>"));++ assert_eq!(generate_answer_snippet("", 0..2000), "".to_string());+ }}diff --git a/core/src/searcher/local.rs b/core/src/searcher/local.rsindex 0c1a5e2f..2e935d71 100644--- a/core/src/searcher/local.rs+++ b/core/src/searcher/local.rs@@ -329,6 +329,7 @@ impl LocalSearcher {.collect(),discussions: None,widget: None,+ direct_answer: None,sidebar: search_result.entity_sidebar.map(Sidebar::Entity),search_duration_ms: start.elapsed().as_millis(),})diff --git a/core/src/searcher/mod.rs b/core/src/searcher/mod.rsindex 8de8f49a..fe1bbf6a 100644--- a/core/src/searcher/mod.rs+++ b/core/src/searcher/mod.rs@@ -25,7 +25,9 @@ use serde::{Deserialize, Serialize};use crate::{bangs::BangHit,ranking::pipeline::RankingWebsite,- search_prettifier::{DisplayedEntity, DisplayedWebpage, HighlightedSpellCorrection, Sidebar},+ search_prettifier::{+ DisplayedAnswer, DisplayedEntity, DisplayedWebpage, HighlightedSpellCorrection, Sidebar,+ },spell::Correction,webpage::region::Region,widgets::Widget,@@ -46,6 +48,7 @@ pub struct WebsitesResult {pub num_hits: usize,pub sidebar: Option<Sidebar>,pub widget: Option<Widget>,+ pub direct_answer: Option<DisplayedAnswer>,pub discussions: Option<Vec<DisplayedWebpage>>,pub search_duration_ms: u128,}diff --git a/core/src/snippet.rs b/core/src/snippet.rsindex 2b2d2587..7ecb1bb1 100644--- a/core/src/snippet.rs+++ b/core/src/snippet.rs@@ -79,7 +79,7 @@ struct SnippetString {highlighted: Vec<Range<usize>>,}-const HIGHLIGHTEN_PREFIX: &str = "<b style=\"font-weight: 700;\">";+const HIGHLIGHTEN_PREFIX: &str = "<b>";const HIGHLIGHTEN_POSTFIX: &str = "</b>";impl SnippetString {diff --git a/frontend/src/components/DirectAnswer.astro b/frontend/src/components/DirectAnswer.astronew file mode 100644index 00000000..ed8a22db--- /dev/null+++ b/frontend/src/components/DirectAnswer.astro@@ -0,0 +1,53 @@+---+import { askama } from "../askama";++export interface Props {+ direct_answer: string;+}+---+{askama.if_("let Some(direct_answer) = direct_answer", (+ <div class="answer-container border-b">+ <div class="answer">+ {askama`direct_answer.answer`}+ </div>+ <div class="answer-snippet">+ {askama`direct_answer.snippet|safe`}+ </div>+ <div class="flex flex-col">+ <a class="answer-url-link p-0 m-0" href={askama`direct_answer.url`}>+ {askama`direct_answer.pretty_url`}+ </a>+ <div class="answer-title">+ <a class="answer-title-link" href={askama`direct_answer.url`}>+ {askama`direct_answer.title`}+ </a>+ </div>+ </div>+ </div>+))}++<style>+ .asnwer-container {+ @apply flex flex-col;+ }+ .answer {+ @apply text-xl font-medium mb-3;+ }++ .answer-snippet {+ @apply text-sm mb-5;+ }++ .answer-snippet > b {+ font-weight: 700;+ }++ .answer-title-link {+ @apply text-blue-800 visited:text-purple-800 inline-block truncate text-xl font-medium;+ max-width: calc(100% - 30px);+ }+ .answer-url-link {+ @apply inline-block truncate text-sm text-gray-800 hover:no-underline;+ max-width: calc(100% - 30px);+ }+</style>diff --git a/frontend/src/pages/search.astro b/frontend/src/pages/search.astroindex 0009bd8d..a28bcf8f 100644--- a/frontend/src/pages/search.astro+++ b/frontend/src/pages/search.astro@@ -9,6 +9,7 @@ import Header from "../components/Header.astro";import RankingModal from "../components/RankingModal.astro";import Discussions from "../components/Discussions.astro";import Widget from "../components/Widget.astro";+import DirectAnswer from "../components/DirectAnswer.astro";import { askama } from "../askama";---@@ -80,6 +81,7 @@ import { askama } from "../askama";}<Widget widget="widget" />+ <DirectAnswer direct_answer="direct_answer" />{askama.for_("item in search_result", () => (@@ -197,6 +199,9 @@ import { askama } from "../askama";.text-snippet {font-weight: 400;}+ .text-snippet>b {+ font-weight: 700;+ }</style><noscript>diff --git a/scripts/export_qa_model b/scripts/export_qa_modelnew file mode 100755index 00000000..42be940d--- /dev/null+++ b/scripts/export_qa_model@@ -0,0 +1,22 @@+#!/usr/bin/env python3+import os+from optimum.onnxruntime import ORTModelForQuestionAnswering, ORTQuantizer+from optimum.onnxruntime.configuration import AutoQuantizationConfig+import transformers+from functools import partial++MODEL = "deepset/tinyroberta-squad2"++os.system("mkdir data/qa_model")++model = ORTModelForQuestionAnswering.from_pretrained(+ MODEL, from_transformers=True)++tokenizer = transformers.AutoTokenizer.from_pretrained(MODEL)+tokenizer.save_pretrained("data/qa_model")++qconfig = AutoQuantizationConfig.avx512(is_static=False)+quantizer = ORTQuantizer.from_pretrained(model)++quantizer.quantize(+ quantization_config=qconfig, save_dir="data/qa_model")