web search done right
remove webpage body from api and use snippets in alice and qa model to generate answers
Mikkel Denker61b7a3deb28b
diff --git a/configs/alice.toml b/configs/alice.tomlindex c6b0bf00..6419a619 100644--- a/configs/alice.toml+++ b/configs/alice.toml@@ -3,7 +3,6 @@ gossip_addr = "0.0.0.0:3008"host = "0.0.0.0:3004"gossip_seed_nodes = ["0.0.0.0:3005", "0.0.0.0:3006"]alice_path = "data/alice"-summarizer_path = "data/summarizer/dual_encoder/"encryption_key = "URyJQTjwUjTq6FSRoZGdbUdTIvqs/QxkPacQio8Lhxc=" # don't use this key for anything important[accelerator]@@ -14,4 +13,4 @@ quantize_fraction = 0.0 # percentage of layers on accelerator to quantizetype = "mps"[accelerator.dtype]-type = "float"\ No newline at end of file+type = "float"diff --git a/core/examples/alice.rs b/core/examples/alice.rsindex d6ad78cc..adb08df7 100644--- a/core/examples/alice.rs+++ b/core/examples/alice.rs@@ -16,7 +16,6 @@ async fn main() {let model = Alice::open("data/alice",- "data/summarizer/dual_encoder",Some(AcceleratorConfig {layer_fraction: 1.0,quantize_fraction: 0.0,diff --git a/core/src/alice/mod.rs b/core/src/alice/mod.rsindex 73e13214..567fae1e 100644--- a/core/src/alice/mod.rs+++ b/core/src/alice/mod.rs@@ -50,7 +50,6 @@ use crate::{llm_utils::ClonableTensor,search_prettifier::DisplayedWebpage,searcher::{SearchResult, WebsitesResult},- summarizer::ExtractiveSummarizer,};use self::{@@ -100,8 +99,18 @@ pub struct SimplifiedWebsite {}impl SimplifiedWebsite {- fn new(webpage: DisplayedWebpage, query: &str, summarizer: &ExtractiveSummarizer) -> Self {- let text = summarizer.summarize(query, &webpage.body);+ fn new(webpage: DisplayedWebpage) -> Self {+ let text = webpage+ .snippet+ .text()+ .map(|t| {+ t.fragments+ .iter()+ .map(|f| f.text.clone())+ .collect::<String>()+ })+ .unwrap_or_default();+let url = Url::parse(&webpage.url).unwrap();Self {@@ -152,7 +161,6 @@ impl From<SimplifiedWebsite> for ModelWebsite {pub struct Searcher {url: String,optic_url: Option<String>,- summarizer: Arc<ExtractiveSummarizer>,}impl Searcher {@@ -190,7 +198,7 @@ impl Searcher {let mut websites = Vec::new();for website in res.webpages {- websites.push(SimplifiedWebsite::new(website, query, &self.summarizer));+ websites.push(SimplifiedWebsite::new(website));}tracing::debug!("search result: {:#?}", websites);@@ -202,7 +210,6 @@ impl Searcher {pub struct Alice {inner: Rc<RawModel>,tokenizer: Arc<Tokenizer>,- summarizer: Arc<ExtractiveSummarizer>,end_tokens: Vec<i64>,initial_state: ClonableTensor,encryption_key: Key<Aes256Gcm>,@@ -254,7 +261,6 @@ impl From<AliceAcceleratorConfig> for AcceleratorConfig {impl Alice {pub fn open<P: AsRef<Path>>(folder: P,- summarizer_path: P,accelerator: Option<AcceleratorConfig>,encryption_key: &[u8],) -> Result<Alice> {@@ -270,11 +276,6 @@ impl Alice {);}- let mut summarizer = ExtractiveSummarizer::open(summarizer_path, 1)?;- summarizer.set_window_size(50);-- let summarizer = Arc::new(summarizer);-let inner = Rc::new(model);let tokenizer = Arc::new(Tokenizer::open(folder.as_ref().join("tokenizer.json"))?);@@ -286,7 +287,6 @@ impl Alice {inner,tokenizer,end_tokens,- summarizer,initial_state,encryption_key,})@@ -348,7 +348,6 @@ impl Alice {let searcher = Searcher {url: search_url,optic_url,- summarizer: Arc::clone(&self.summarizer),};Ok(ActionExecutor::new(diff --git a/core/src/config/mod.rs b/core/src/config/mod.rsindex e0faa90a..3fac570f 100644--- a/core/src/config/mod.rs+++ b/core/src/config/mod.rs@@ -341,7 +341,6 @@ pub struct AliceLocalConfig {pub gossip_seed_nodes: Option<Vec<SocketAddr>>,pub gossip_addr: SocketAddr,pub host: SocketAddr,- pub summarizer_path: String,pub alice_path: String,pub accelerator: Option<AliceAcceleratorConfig>,diff --git a/core/src/entrypoint/alice.rs b/core/src/entrypoint/alice.rsindex 968a1949..823886ee 100644--- a/core/src/entrypoint/alice.rs+++ b/core/src/entrypoint/alice.rs@@ -174,7 +174,6 @@ pub async fn run(config: AliceLocalConfig) -> Result<(), anyhow::Error> {info!("starting alice");let alice = Alice::open(&config.alice_path,- &config.summarizer_path,config.accelerator.clone().map(|acc| acc.into()),&key,)?;diff --git a/core/src/qa_model.rs b/core/src/qa_model.rsindex 238b3639..a067648c 100644--- a/core/src/qa_model.rs+++ b/core/src/qa_model.rs@@ -69,7 +69,7 @@ impl QaModel {})}- pub fn run(&self, question: &str, contexts: &[&str]) -> Option<Answer> {+ pub fn run<S: AsRef<str>>(&self, question: &str, contexts: &[S]) -> Option<Answer> {if contexts.is_empty() {return None;}@@ -78,7 +78,7 @@ impl QaModel {let mut batches = Vec::with_capacity(bs);for context in contexts {- batches.push((question.to_string(), context.to_string()));+ batches.push((question.to_string(), context.as_ref().to_string()));}let encoded = self.tokenizer.encode_batch(batches, true).unwrap();diff --git a/core/src/search_prettifier/mod.rs b/core/src/search_prettifier/mod.rsindex 9f25e4f0..d651f4c2 100644--- a/core/src/search_prettifier/mod.rs+++ b/core/src/search_prettifier/mod.rs@@ -50,6 +50,15 @@ pub enum Snippet {},}+impl Snippet {+ pub fn text(&self) -> Option<&TextSnippet> {+ match self {+ Snippet::Normal { text, .. } => Some(text),+ _ => None,+ }+ }+}+#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]#[serde(rename_all = "camelCase")]pub struct HighlightedSpellCorrection {@@ -162,7 +171,6 @@ pub struct DisplayedWebpage {pub domain: String,pub pretty_url: String,pub snippet: Snippet,- pub body: String,pub ranking_signals: Option<HashMap<Signal, SignalScore>>,}@@ -174,7 +182,6 @@ pub struct DisplayedAnswer {pub pretty_url: String,pub snippet: String,pub answer: String,- pub body: String,}impl From<RetrievedWebpage> for DisplayedWebpage {@@ -192,7 +199,6 @@ impl From<RetrievedWebpage> for DisplayedWebpage {pretty_url,domain,snippet,- body: webpage.body,ranking_signals: None,}}diff --git a/core/src/searcher/api.rs b/core/src/searcher/api.rsindex 94734406..c20c3ad5 100644--- a/core/src/searcher/api.rs+++ b/core/src/searcher/api.rs@@ -407,22 +407,32 @@ impl ApiSearcher {let contexts: Vec<_> = webpages.iter().take(1)- .map(|webpage| webpage.body.as_str())+ .filter_map(|webpage| webpage.snippet.text())+ .map(|t| {+ t.fragments+ .iter()+ .map(|f| f.text.clone())+ .collect::<String>()+ }).collect();match qa_model.run(query, &contexts) {Some(answer) => {let answer_webpage = webpages.remove(answer.context_idx);+ let snip = answer_webpage+ .snippet+ .text()+ .unwrap()+ .fragments+ .iter()+ .map(|f| f.text.clone())+ .collect::<String>();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,+ snippet: generate_answer_snippet(&snip, answer.offset.clone()),+ answer: snip[answer.offset].to_string(),})}None => None,diff --git a/core/src/snippet.rs b/core/src/snippet.rsindex 0e0fa677..40fb40e0 100644--- a/core/src/snippet.rs+++ b/core/src/snippet.rs@@ -56,14 +56,14 @@ pub enum TextSnippetFragmentKind {#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, ToSchema)]#[serde(rename_all = "camelCase")]pub struct TextSnippetFragment {- kind: TextSnippetFragmentKind,- text: String,+ pub kind: TextSnippetFragmentKind,+ pub text: String,}#[derive(Default, Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, ToSchema)]#[serde(rename_all = "camelCase")]pub struct TextSnippet {- fragments: Vec<TextSnippetFragment>,+ pub fragments: Vec<TextSnippetFragment>,}struct SnippetBuilder {diff --git a/frontend/src/lib/api/schema.d.ts b/frontend/src/lib/api/schema.d.tsindex 5a407c77..18fef9b8 100644--- a/frontend/src/lib/api/schema.d.ts+++ b/frontend/src/lib/api/schema.d.ts@@ -97,7 +97,6 @@ export interface components {};DisplayedAnswer: {answer: string;- body: string;prettyUrl: string;snippet: string;title: string;@@ -113,7 +112,6 @@ export interface components {title: string;};DisplayedWebpage: {- body: string;domain: string;prettyUrl: string;rankingSignals?: {diff --git a/frontend/src/routes/search/Result.svelte b/frontend/src/routes/search/Result.svelteindex 778e11de..a860a8d7 100644--- a/frontend/src/routes/search/Result.svelte+++ b/frontend/src/routes/search/Result.svelte@@ -1,6 +1,6 @@<script lang="ts">import AdjustVertical from '~icons/heroicons/adjustments-vertical';- import type { Webpage, TextSnippet } from '$lib/api';+ import type { Webpage } from '$lib/api';import { createEventDispatcher } from 'svelte';import { clearSummary, summariesStore } from '$lib/stores';import Summary from './Summary.svelte';