web search done right
Alice; show claim confidence level
Mikkel Denker18f7ef184271
diff --git a/configs/frontend.toml b/configs/frontend.tomlindex ca40cdf7..0930fcc0 100644--- a/configs/frontend.toml+++ b/configs/frontend.toml@@ -5,6 +5,7 @@ prometheus_host = "0.0.0.0:3001"gossip_addr = "0.0.0.0:3005"gossip_seed_nodes = ["0.0.0.0:3006", "0.0.0.0:3007"]crossencoder_model_path = "data/cross_encoder"+fact_check_model_path = "data/fact_model"# lambda_model_path = "data/lambdamart.txt"# qa_model_path = "data/qa_model"bangs_path = "data/bangs.json"diff --git a/core/src/alice/mod.rs b/core/src/alice/mod.rsindex 78046c6c..3f38a12e 100644--- a/core/src/alice/mod.rs+++ b/core/src/alice/mod.rs@@ -287,7 +287,7 @@ impl Alice {}let mut summarizer = ExtractiveSummarizer::open(summarizer_path, 1)?;- summarizer.set_window_size(30);+ summarizer.set_window_size(50);let summarizer = Arc::new(summarizer);diff --git a/core/src/api/fact_check.rs b/core/src/api/fact_check.rsnew file mode 100644index 00000000..49359a11--- /dev/null+++ b/core/src/api/fact_check.rs@@ -0,0 +1,42 @@+// Stract is an open source web search engine.+// Copyright (C) 2023 Stract 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::sync::Arc;++use axum::{extract, response::IntoResponse, Json};++#[derive(serde::Deserialize, serde::Serialize, Debug)]+pub struct Params {+ claim: String,+ evidence: String,+}++#[derive(serde::Deserialize, serde::Serialize, Debug)]+pub struct Response {+ score: f64,+}++pub async fn route(+ extract::State(state): extract::State<Arc<super::State>>,+ extract::Json(params): extract::Json<Params>,+) -> Result<impl IntoResponse, http::StatusCode> {+ let score = state+ .fact_checker+ .run(¶ms.claim, ¶ms.evidence)+ .map_err(|_| http::StatusCode::INTERNAL_SERVER_ERROR)?;++ Ok(Json(Response { score }))+}diff --git a/core/src/api/mod.rs b/core/src/api/mod.rsindex e0814e92..56344b3a 100644--- a/core/src/api/mod.rs+++ b/core/src/api/mod.rs@@ -25,6 +25,7 @@ use crate::{cluster::Cluster,member::{Member, Service},},+ fact_check_model::FactCheckModel,improvement::{store_improvements_loop, ImprovementEvent},leaky_queue::LeakyQueue,qa_model::QaModel,@@ -51,6 +52,7 @@ mod alice;mod autosuggest;mod chat;mod explore;+mod fact_check;pub mod improvement;mod index;mod metrics;@@ -71,6 +73,7 @@ pub struct State {pub search_counter_success: crate::metrics::Counter,pub search_counter_fail: crate::metrics::Counter,pub summarizer: Arc<Summarizer>,+ pub fact_checker: Arc<FactCheckModel>,pub improvement_queue: Option<Arc<Mutex<LeakyQueue<ImprovementEvent>>>>,pub cluster: Arc<Cluster>,}@@ -149,6 +152,7 @@ pub async fn router(search_counter_fail,remote_webgraph,summarizer: Arc::new(Summarizer::open(&config.summarizer_path)?),+ fact_checker: Arc::new(FactCheckModel::open(&config.fact_check_model_path)?),improvement_queue: query_store_queue,cluster,});@@ -195,7 +199,8 @@ pub async fn router().route("/beta/api/webgraph/knows_site", get(webgraph::knows_site)).route("/beta/api/alice", get(alice::route))- .route("/beta/api/alice/save_state", post(alice::save_state)),+ .route("/beta/api/alice/save_state", post(alice::save_state))+ .route("/beta/api/fact_check", post(fact_check::route)),).with_state(state))}diff --git a/core/src/fact_check_model.rs b/core/src/fact_check_model.rsnew file mode 100644index 00000000..e7d3f706--- /dev/null+++ b/core/src/fact_check_model.rs@@ -0,0 +1,97 @@+// Stract is an open source web search engine.+// Copyright (C) 2023 Stract 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::path::Path;++use itertools::Itertools;+use tch::Tensor;+use tokenizers::PaddingParams;+use tokenizers::TruncationParams;++const TRUNCATE_INPUT: usize = 512;++#[derive(thiserror::Error, Debug)]+pub enum Error {+ #[error("Tokenizer error")]+ Tokenizer(#[from] tokenizers::Error),+ #[error("Torch error")]+ Torch(#[from] tch::TchError),+}++pub struct FactCheckModel {+ tokenizer: tokenizers::Tokenizer,+ model: tch::CModule,+}++impl FactCheckModel {+ pub fn open<P: AsRef<Path>>(folder: P) -> Result<Self, Error> {+ 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 model = tch::CModule::load(folder.as_ref().join("model.pt"))?;++ Ok(Self { tokenizer, model })+ }++ pub fn run(&self, claim: &str, evidence: &str) -> Result<f64, Error> {+ let encoded = self.tokenizer.encode((claim, evidence), true)?;++ let ids = encoded.get_ids().iter().map(|i| *i as i64).collect_vec();++ let attention_mask = encoded+ .get_attention_mask()+ .iter()+ .map(|i| *i as i64)+ .collect_vec();++ let ids = Tensor::from_slice(&ids).unsqueeze(0);+ let attention_mask = Tensor::from_slice(&attention_mask).unsqueeze(0);++ let output = self.model.forward_ts(&[ids, attention_mask])?;++ Ok(output.double_value(&[0, 0]))+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn sanity_check() {+ let model =+ FactCheckModel::open("../data/fact_model").expect("Failed to find fact-check model");++ let score = model.run(+ "Albert Einstein work in the field of computer science",+ "Albert Einstein was a German-born theoretical physicist, widely acknowledged to be one of the greatest and most influential physicists of all time.",+ ).unwrap();++ assert!(score > 0.1);+ }+}diff --git a/core/src/lib.rs b/core/src/lib.rsindex f00d1384..3a21e077 100644--- a/core/src/lib.rs+++ b/core/src/lib.rs@@ -43,6 +43,7 @@ mod distributed;mod entity_index;mod enum_map;mod executor;+mod fact_check_model;mod fastfield_reader;mod human_website_annotations;mod hyperloglog;@@ -172,6 +173,7 @@ pub struct FrontendConfig {pub qa_model_path: Option<String>,pub bangs_path: String,pub summarizer_path: String,+ pub fact_check_model_path: String,pub query_store_db_host: Option<String>,pub cluster_id: String,pub gossip_seed_nodes: Option<Vec<SocketAddr>>,@@ -332,6 +334,9 @@ pub enum Error {#[error("Alice")]Alice(#[from] crate::alice::Error),++ #[error("Fact check model")]+ FactCheckModel(#[from] crate::fact_check_model::Error),}pub(crate) type Result<T> = std::result::Result<T, Error>;diff --git a/frontend/src/pages/chat.astro b/frontend/src/pages/chat.astroindex e17bf14d..e4f40b46 100644--- a/frontend/src/pages/chat.astro+++ b/frontend/src/pages/chat.astro@@ -145,11 +145,29 @@ import OpticSelector from "../components/OpticSelector.astro";type Message = {from: "user" | "alice",message: string,- queries: Query[]- sourceColors: Record<number, Record<number, string>> // queryId -> sourceId -> color+ queries: Query[],+ sourceColors: Record<number, ScoredColor> // idx -> color};type MessageWithElement = Message & {element: HTMLDivElement | null};+ type Color = {+ r: number,+ g: number,+ b: number,+ a: number+ };++ type ScoredColor = {+ color: Color,+ score: number+ };++ let colorGreen: Color = {r: 22, g: 163, b: 74, a: 1};+ let colorYellow: Color = {r: 234, g: 179, b: 8, a: 1};+ let colorRed: Color = {r: 220, g: 38, b: 38, a: 1};++ let scoreGradient: Color[] = colorGradient(colorRed, colorYellow, 5).concat(colorGradient(colorYellow, colorGreen, 5));+let numMessageLines = 1;let messageInputHeight = 24;let firstMessage = true;@@ -164,7 +182,7 @@ import OpticSelector from "../components/OpticSelector.astro";// "from": "user",// "message": "how does a car work",// "queries": [],- // "sourceColors": {},+ // "sourceColors": [],// "element": null// },// {@@ -195,7 +213,7 @@ import OpticSelector from "../components/OpticSelector.astro";// ]// }// ],- // "sourceColors": {},+ // "sourceColors": [],// "element": null// }// ];@@ -206,6 +224,24 @@ import OpticSelector from "../components/OpticSelector.astro";// renderMessage(message);// }+ function colorGradient(start: Color, end: Color, steps: number): Color[] {+ const gradient: Color[] = [];++ for (let i = 0; i < steps; i++) {+ const r = start.r + (end.r - start.r) * i / steps;+ const g = start.g + (end.g - start.g) * i / steps;+ const b = start.b + (end.b - start.b) * i / steps;+ const a = start.a + (end.a - start.a) * i / steps;+ gradient.push({r, g, b, a});+ }++ return gradient;+ }++ function colorToString(color: Color): string {+ return `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a})`;+ }+function updateMessageInputHeight() {const lines = messageInput.value.split("\n");numMessageLines = lines.length;@@ -377,7 +413,7 @@ import OpticSelector from "../components/OpticSelector.astro";from: from,message: message,queries: queries,- sourceColors: {},+ sourceColors: [],element: null});@@ -414,6 +450,7 @@ import OpticSelector from "../components/OpticSelector.astro";let source2Id: Record<number, Record<number, number>> = {}; // (i, j) -> id+ let sourceIdx = 0;while ((match = regex.exec(renderedMessage)) !== null) {const queryNum = parseInt(match[1]);const sourceNum = parseInt(match[2]);@@ -425,10 +462,10 @@ import OpticSelector from "../components/OpticSelector.astro";const source = query.results[j];// find text from '.' to match[0]- let text = renderedMessage.substring(renderedMessage.lastIndexOf(".", match.index) + 1, match.index).trim();+ let claim = renderedMessage.substring(renderedMessage.lastIndexOf(".", match.index) + 1, match.index).trim();- if (text.startsWith("<p>")) {- text = text.substring(3);+ if (claim.startsWith("<p>")) {+ claim = claim.substring(3);}@@ -441,18 +478,39 @@ import OpticSelector from "../components/OpticSelector.astro";nextSourceId += 1;}+ // check if color is already assigned+ if (!(sourceIdx in message.sourceColors)) {+ factCheck(claim, source.text, sourceIdx).then((res) => {+ if (res != null) {+ let [score, sourceIdx] = res;+ // s is a score from 0 to 1+ let colorId = Math.floor(score * (scoreGradient.length - 1));+ let color = scoreGradient[colorId];+++ message.sourceColors[sourceIdx] = {+ color: color,+ score: score+ };++ updateSourceColors(message);+ }+ })+ }+const id = source2Id[i][j];messageSources.push({query: query,source: source,- claim: text,+ claim: claim,id: id})- const link = `<span class="inline-source"><a href="` + source.url + `">` + id + `</a></span>`;+ const link = `<span class="inline-source inline-source-` + sourceIdx + `"><a href="` + source.url + `">` + id + `</a></span>`;newRenderedMessage = newRenderedMessage.replace(match[0], link);+ sourceIdx += 1;}// url and site of sources ordered by id@@ -521,11 +579,53 @@ import OpticSelector from "../components/OpticSelector.astro";message.element.appendChild(msgElem);}+ updateSourceColors(message);+// @ts-ignorehljs.highlightAll();removeBackgroundFromCode();}+ async function factCheck(claim: string, evidence: string, sourceIdx: number): Promise<[number, number] | null> {+ let url = "/beta/api/fact_check";++ const response = await fetch(url, {+ method: "POST",+ headers: {+ "Content-Type": "application/json"+ },+ body: JSON.stringify({+ claim: claim,+ evidence: evidence+ })+ });++ if (response.status == 200) {+ const data = await response.json();+ return [data.score, sourceIdx];+ } else {+ return null;+ }+ }++ function updateSourceColors(message: MessageWithElement) {+ if (message.element == null) {+ return;+ }++ // loop through the sourceColors+ for (const idx in message.sourceColors) {+ const scoredColor = message.sourceColors[idx];+ const source: HTMLSpanElement | null = message.element.querySelector(".inline-source-" + idx);++ if (source) {+ source.style.borderColor = colorToString(scoredColor.color);+ source.title = Math.floor(scoredColor.score*100) + "% confidence that source supports claim";+ }+ }++ }+function renderMarkdown(markdown: string) {// @ts-ignoreconst html = marked.parse(markdown);diff --git a/justfile b/justfileindex cea9b66e..44ef975d 100644--- a/justfile+++ b/justfile@@ -23,6 +23,7 @@./scripts/export_qa_model./scripts/export_abstractive_summary_model./scripts/export_dual_encoder+ ./scripts/export_fact_modelcd frontend; npm install; npm run buildcargo run --release --all-features -- configure {{ARGS}}diff --git a/scripts/export_fact_model b/scripts/export_fact_modelnew file mode 100755index 00000000..e14dd385--- /dev/null+++ b/scripts/export_fact_model@@ -0,0 +1,42 @@+#!.venv/bin/python3+import os+import transformers+import torch++MODEL = "Dzeniks/roberta-fact-check"++os.system("mkdir data/fact_model")++model = transformers.AutoModelForSequenceClassification.from_pretrained(MODEL)++model.eval()+for p in model.parameters():+ p.requires_grad_(False)++tokenizer = transformers.AutoTokenizer.from_pretrained(MODEL)+features = tokenizer(+ "Albert Einstein work in the field of computer science",+ "Albert Einstein was a German-born theoretical physicist, widely acknowledged to be one of the greatest and most influential physicists of all time.",+ padding="max_length",+ truncation=True,+ return_tensors="pt",+)+input_ids = features["input_ids"]+attention_mask = features["attention_mask"]++tokenizer.save_pretrained("data/fact_model")+++def run_model(input_ids, attention_mask):+ res = model(+ input_ids=input_ids,+ attention_mask=attention_mask,+ return_dict=False,+ )[0]+ res = torch.softmax(res, dim=1)+ return res+++traced_model = torch.jit.trace(run_model, ((input_ids, attention_mask)))++traced_model.save("data/fact_model/model.pt")