web search done right
Alice; render citations in frontend
Mikkel Denker87d87bed8da2
diff --git a/core/examples/alice.rs b/core/examples/alice.rsindex 5e73bb48..c4d03012 100644--- a/core/examples/alice.rs+++ b/core/examples/alice.rs@@ -1,8 +1,7 @@use std::io::Write;use stract::alice::{- local::{AcceleratorConfig, Alice},- ExecutionState,+ ExecutionState, {AcceleratorConfig, Alice},};#[tokio::main]@@ -12,7 +11,7 @@ async fn main() {.finish();tracing::subscriber::set_global_default(subscriber).unwrap();- // dont use this in production+ // dont use this key in productionlet key = base64::decode("URyJQTjwUjTq6FSRoZGdbUdTIvqs/QxkPacQio8Lhxc=").unwrap();let model = Alice::open(diff --git a/core/src/alice/local/generate.rs b/core/src/alice/generate.rssimilarity index 99%rename from core/src/alice/local/generate.rsrename to core/src/alice/generate.rsindex 8fa675bf..f69d777c 100644--- a/core/src/alice/local/generate.rs+++ b/core/src/alice/generate.rs@@ -32,8 +32,8 @@ use super::{use crate::alice::{ModelWebsite, SimplifiedWebsite};// const TAU: f64 = 0.3;-const TEMP: f64 = 0.4;-const TOP_P: f64 = 0.5;+const TEMP: f64 = 0.4; // 0.4+const TOP_P: f64 = 0.7; //0.5const MAX_NUM_QUERIES: usize = 3;diff --git a/core/src/alice/local/mod.rs b/core/src/alice/local/mod.rsdeleted file mode 100644index a7eb8a33..00000000--- a/core/src/alice/local/mod.rs+++ /dev/null@@ -1,393 +0,0 @@-// 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::io::{Read, Write};-use std::{path::Path, sync::Arc};--use aes_gcm::aead::{Aead, OsRng};-use aes_gcm::{AeadCore, Aes256Gcm, Key, KeyInit, Nonce};-use flate2::bufread::GzDecoder;-use flate2::write::GzEncoder;-use flate2::Compression;-use half::bf16;-use tch::Tensor;--use crate::{- llm_utils::ClonableTensor, summarizer::ExtractiveSummarizer, AcceleratorDevice,- AcceleratorDtype, AliceAcceleratorConfig,-};--use crate::alice::Result;--use self::generate::{ActionExecutor, ActionGenerator, AliceTokenGenerator, RawActionGenerator};-use self::raw_model::RawModel;--use super::{Error, Searcher};--pub mod generate;-mod raw_model;--const PROMPT_PREFIX: &str = r#"System: Your name is Alice, and you are an AI assistant trained by Stract. Below is a conversation between you and a user. Help the user as best you can.-You can lookup information on the web in the following format:-Alice[thought]: I should look up "<keyword>" on the web.-Search: <query><|endoftext|>-Result: <search result>--This Thought/Search/Result can repeat N times.-When you are ready to answer the user, use the following format:-Alice: <answer><|endoftext|>"#;--pub struct Alice {- inner: Arc<RawModel>,- tokenizer: Arc<Tokenizer>,- summarizer: Arc<ExtractiveSummarizer>,- end_tokens: Vec<i64>,- initial_state: ClonableTensor,- encryption_key: Key<Aes256Gcm>,-}--/// SAFETY:-/// Alice is thread-safe because it never mutates it's internal state.-/// It only ever spawns executors that keeps track of the generation state. These are not threadsafe.-unsafe impl Send for Alice {}-unsafe impl Sync for Alice {}--pub struct AcceleratorConfig {- pub layer_fraction: f64,- pub device: tch::Device,- pub kind: tch::Kind,-}--impl From<AcceleratorDevice> for tch::Device {- fn from(value: AcceleratorDevice) -> Self {- match value {- AcceleratorDevice::Cpu => tch::Device::Cpu,- AcceleratorDevice::Cuda(d) => tch::Device::Cuda(d),- AcceleratorDevice::Mps => tch::Device::Mps,- }- }-}--impl From<AcceleratorDtype> for tch::Kind {- fn from(value: AcceleratorDtype) -> Self {- match value {- AcceleratorDtype::Float => tch::Kind::Float,- AcceleratorDtype::Bf16 => tch::Kind::BFloat16,- }- }-}--impl From<AliceAcceleratorConfig> for AcceleratorConfig {- fn from(value: AliceAcceleratorConfig) -> Self {- Self {- layer_fraction: value.layer_fraction,- device: value.device.into(),- kind: value.dtype.into(),- }- }-}--impl Alice {- pub fn open<P: AsRef<Path>>(- folder: P,- summarizer_path: P,- accelerator: Option<AcceleratorConfig>,- encryption_key: &[u8],- ) -> Result<Alice> {- let encryption_key = *Key::<Aes256Gcm>::from_slice(encryption_key);- let mut model = RawModel::open(folder.as_ref().join("model.safetensors"))?;-- if let Some(accelerator) = accelerator {- model.load_to_device(- accelerator.layer_fraction,- accelerator.device,- accelerator.kind,- );- }-- let mut summarizer = ExtractiveSummarizer::open(summarizer_path, 1)?;- summarizer.set_window_size(30);-- let summarizer = Arc::new(summarizer);-- let inner = Arc::new(model);- let tokenizer = Arc::new(Tokenizer::open(folder.as_ref().join("tokenizer.json"))?);-- let end_tokens = vec![tokenizer.tokenizer.token_to_id("<|endoftext|>").unwrap() as i64];-- let initial_state = Alice::prepare_initial_state(&inner, &tokenizer)?;-- Ok(Self {- inner,- tokenizer,- end_tokens,- summarizer,- initial_state,- encryption_key,- })- }-- fn prepare_initial_state(model: &RawModel, tokenizer: &Tokenizer) -> Result<ClonableTensor> {- let tokens = tokenizer.encode(PROMPT_PREFIX.to_string())?;-- let mut state = model.init_state();-- for token in tokens {- let (_, new_state) = model.forward(token, Some(&state));- state = new_state;- }-- Ok(ClonableTensor(state))- }-- pub fn new_executor(- &self,- user_question: &str,- last_state: Option<EncryptedState>,- search_url: String,- optic_url: Option<String>,- ) -> Result<ActionExecutor> {- let mut state = None;-- if let Some(s) = last_state {- let decrypted_state = s.decrypt(&self.encryption_key)?;- state = Some(Tensor::from_slice2(&decrypted_state));- }-- let (tokens, state) = match state {- Some(state) => {- let input = format!("User: {user_question}\n\nAlice[thought]:");- let tokens = self.tokenizer.encode(input).unwrap();-- (tokens, state)- }- None => {- let input = format!("\n\nUser: {user_question}\n\nAlice[thought]:");- let tokens = self.tokenizer.encode(input).unwrap();-- (tokens, self.initial_state.clone().0)- }- };-- let token_generator = AliceTokenGenerator::new(- self.inner.clone(),- self.end_tokens.clone(),- state,- &tokens,- None,- )?;-- let raw_action_gen = RawActionGenerator::new(token_generator, self.tokenizer.clone());- let action_gen = ActionGenerator::new(raw_action_gen);-- let searcher = Searcher {- url: search_url,- optic_url,- summarizer: Arc::clone(&self.summarizer),- };-- Ok(ActionExecutor::new(- action_gen,- searcher,- self.encryption_key,- ))- }-}--#[derive(Clone)]-pub struct ModelState {- state: ClonableTensor,- next_token: i64,-}--pub struct Tokenizer {- tokenizer: tokenizers::Tokenizer,-}--impl Tokenizer {- pub fn open<P: AsRef<Path>>(path: P) -> Result<Tokenizer> {- let tokenizer = tokenizers::Tokenizer::from_file(path)?;-- Ok(Self { tokenizer })- }-- pub fn encode(&self, input: String) -> Result<Vec<i64>> {- let encoding = self.tokenizer.encode(input, false)?;-- let ids = encoding- .get_ids()- .iter()- .map(|&id| id as i64)- .collect::<Vec<_>>();-- Ok(ids)- }-- pub fn decode(&self, tokens: &[i64]) -> Result<String> {- let tokens = tokens.iter().map(|&id| id as u32).collect::<Vec<_>>();-- let output = self.tokenizer.decode(tokens, true)?;-- Ok(output)- }-}--#[derive(Debug, serde::Serialize, serde::Deserialize)]-/// base64 encoded `EncryptedState`-pub struct EncodedEncryptedState(String);--impl EncodedEncryptedState {- pub fn encode(state: EncryptedState) -> Self {- let bytes = bincode::serialize(&state).unwrap();- let encoded = base64::encode(bytes);-- Self(encoded)- }-- pub fn decode(self) -> Result<EncryptedState> {- let bytes = base64::decode(self.0)?;- let state = bincode::deserialize(&bytes)?;-- Ok(state)- }-}--#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]-pub struct EncryptedState {- nonce: Vec<u8>,- // ciphertext is bincoded `CompressedState` encrypted with `nonce` and a key- ciphertext: Vec<u8>,-}--impl EncryptedState {- fn encrypt(state: Vec<Vec<f32>>, key: &Key<Aes256Gcm>) -> Self {- let compressed = compress_state(state);-- let cipher = Aes256Gcm::new(key);- let nonce = Aes256Gcm::generate_nonce(&mut OsRng);-- let bytes = bincode::serialize(&compressed).unwrap();-- let ciphertext = cipher.encrypt(&nonce, bytes.as_slice()).unwrap();-- Self {- nonce: nonce.to_vec(),- ciphertext,- }- }-- fn decrypt(&self, key: &Key<Aes256Gcm>) -> Result<Vec<Vec<f32>>> {- let cipher = Aes256Gcm::new(key);- let nonce = Nonce::from_slice(&self.nonce);-- let decrypted = cipher- .decrypt(nonce, self.ciphertext.as_slice())- .map_err(|_| Error::DecryptionFailed)?;-- let compressed: CompressedState = bincode::deserialize(&decrypted)?;-- decompress_state(compressed)- }-}--/// base64 encoded gzipped state-#[derive(Debug, serde::Serialize, serde::Deserialize)]-pub struct CompressedState(String);--pub fn compress_state(state: Vec<Vec<f32>>) -> CompressedState {- let state: Vec<Vec<bf16>> = state- .into_iter()- .map(|v| v.into_iter().map(bf16::from_f32).collect())- .collect();-- let mut encoder = GzEncoder::new(Vec::new(), Compression::best());-- encoder- .write_all(&bincode::serialize(&state).unwrap())- .unwrap();-- let compressed = encoder.finish().unwrap();-- // base64 encode the compressed state- CompressedState(base64::encode(compressed))-}--pub fn decompress_state(state: CompressedState) -> Result<Vec<Vec<f32>>> {- let state = base64::decode(state.0)?;-- let mut decoder = GzDecoder::new(&state[..]);-- let mut state = Vec::new();- decoder.read_to_end(&mut state)?;-- let state: Vec<Vec<bf16>> = bincode::deserialize(&state)?;-- Ok(state- .into_iter()- .map(|v| v.into_iter().map(f32::from).collect())- .collect())-}--#[derive(Debug)]-struct AnyTransitionValidator {- validators: Vec<TransitionValidator>,-}--impl AnyTransitionValidator {- fn new(validators: Vec<TransitionValidator>) -> Self {- Self { validators }- }-- fn validate(&mut self, token: i64) -> bool {- for validator in &mut self.validators {- if validator.validate(token) {- return true;- }- }-- false- }-}--#[derive(Debug)]-struct TransitionValidator {- tokens: Vec<i64>,- next_match: usize,-}--impl TransitionValidator {- fn new(tokens: Vec<i64>) -> Self {- Self {- tokens,- next_match: 0,- }- }-- fn validate(&mut self, token: i64) -> bool {- if self.tokens[self.next_match] == token {- self.next_match += 1;- if self.next_match == self.tokens.len() {- self.next_match = 0;- true- } else {- false- }- } else {- self.next_match = 0;- false- }- }-}diff --git a/core/src/alice/mod.rs b/core/src/alice/mod.rsindex 19dae59b..78046c6c 100644--- a/core/src/alice/mod.rs+++ b/core/src/alice/mod.rs@@ -14,18 +14,47 @@// 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 std::{+ io::{Read, Write},+ path::Path,+ sync::Arc,+};++use aes_gcm::{+ aead::{Aead, OsRng},+ AeadCore, Aes256Gcm, Key, KeyInit, Nonce,+};+use flate2::{bufread::GzDecoder, write::GzEncoder, Compression};+use half::bf16;+use tch::Tensor;use crate::{api::search::ApiSearchQuery,+ llm_utils::ClonableTensor,search_prettifier::DisplayedWebpage,searcher::{SearchResult, WebsitesResult},summarizer::{self, ExtractiveSummarizer},webpage::Url,+ AcceleratorDevice, AcceleratorDtype, AliceAcceleratorConfig,+};++use self::{+ generate::{ActionExecutor, ActionGenerator, AliceTokenGenerator, RawActionGenerator},+ raw_model::RawModel,};-pub mod local;-pub mod openai;+const PROMPT_PREFIX: &str = r#"System: Your name is Alice, and you are an AI assistant trained by Stract. Below is a conversation between you and a user. Help the user as best you can.+You can lookup information on the web in the following format:+Alice[thought]: I should look up "<keyword>" on the web.+Search: <query><|endoftext|>+Result: <search result>++This Thought/Search/Result can repeat N times.+When you are ready to answer the user, use the following format:+Alice: <answer><|endoftext|>"#;++pub mod generate;+mod raw_model;type Result<T> = std::result::Result<T, Error>;@@ -88,7 +117,7 @@ pub struct SimplifiedWebsite {pub title: String,pub text: String,pub url: String,- pub domain: String,+ pub site: String,}impl SimplifiedWebsite {@@ -99,7 +128,7 @@ impl SimplifiedWebsite {Self {title: webpage.title,text,- domain: url.domain().to_string(),+ site: url.site().to_string(),url: url.full(),}}@@ -119,7 +148,7 @@ pub enum ExecutionState {text: String,},Done {- state: local::EncodedEncryptedState,+ state: EncodedEncryptedState,},}@@ -127,7 +156,7 @@ pub enum ExecutionState {struct ModelWebsite {pub title: String,pub text: String,- pub domain: String,+ pub site: String,}impl From<SimplifiedWebsite> for ModelWebsite {@@ -135,7 +164,7 @@ impl From<SimplifiedWebsite> for ModelWebsite {Self {title: value.title,text: value.text,- domain: value.domain,+ site: value.site,}}}@@ -187,56 +216,346 @@ impl Searcher {Ok(websites)}+}- async fn raw_search_async(&self, query: &str) -> Result<WebsitesResult> {- let mut optic = None;+pub struct Alice {+ inner: Arc<RawModel>,+ tokenizer: Arc<Tokenizer>,+ summarizer: Arc<ExtractiveSummarizer>,+ end_tokens: Vec<i64>,+ initial_state: ClonableTensor,+ encryption_key: Key<Aes256Gcm>,+}- if let Some(url) = &self.optic_url {- if let Ok(r) = reqwest::get(url).await {- if let Ok(text) = r.text().await {- optic = Some(text);- }- }+/// SAFETY:+/// Alice is thread-safe because it never mutates it's internal state.+/// It only ever spawns executors that keeps track of the generation state. These are not threadsafe.+unsafe impl Send for Alice {}+unsafe impl Sync for Alice {}++pub struct AcceleratorConfig {+ pub layer_fraction: f64,+ pub device: tch::Device,+ pub kind: tch::Kind,+}++impl From<AcceleratorDevice> for tch::Device {+ fn from(value: AcceleratorDevice) -> Self {+ match value {+ AcceleratorDevice::Cpu => tch::Device::Cpu,+ AcceleratorDevice::Cuda(d) => tch::Device::Cuda(d),+ AcceleratorDevice::Mps => tch::Device::Mps,}+ }+}- let client = reqwest::Client::new();- let query = ApiSearchQuery {- query: query.trim().to_string(),- num_results: Some(3),- optic,- page: None,- selected_region: None,- site_rankings: None,- return_ranking_signals: None,- flatten_response: Some(false),+impl From<AcceleratorDtype> for tch::Kind {+ fn from(value: AcceleratorDtype) -> Self {+ match value {+ AcceleratorDtype::Float => tch::Kind::Float,+ AcceleratorDtype::Bf16 => tch::Kind::BFloat16,+ }+ }+}++impl From<AliceAcceleratorConfig> for AcceleratorConfig {+ fn from(value: AliceAcceleratorConfig) -> Self {+ Self {+ layer_fraction: value.layer_fraction,+ device: value.device.into(),+ kind: value.dtype.into(),+ }+ }+}++impl Alice {+ pub fn open<P: AsRef<Path>>(+ folder: P,+ summarizer_path: P,+ accelerator: Option<AcceleratorConfig>,+ encryption_key: &[u8],+ ) -> Result<Alice> {+ let encryption_key = *Key::<Aes256Gcm>::from_slice(encryption_key);+ let mut model = RawModel::open(folder.as_ref().join("model.safetensors"))?;++ if let Some(accelerator) = accelerator {+ model.load_to_device(+ accelerator.layer_fraction,+ accelerator.device,+ accelerator.kind,+ );+ }++ let mut summarizer = ExtractiveSummarizer::open(summarizer_path, 1)?;+ summarizer.set_window_size(30);++ let summarizer = Arc::new(summarizer);++ let inner = Arc::new(model);+ let tokenizer = Arc::new(Tokenizer::open(folder.as_ref().join("tokenizer.json"))?);++ let end_tokens = vec![tokenizer.tokenizer.token_to_id("<|endoftext|>").unwrap() as i64];++ let initial_state = Alice::prepare_initial_state(&inner, &tokenizer)?;++ Ok(Self {+ inner,+ tokenizer,+ end_tokens,+ summarizer,+ initial_state,+ encryption_key,+ })+ }++ fn prepare_initial_state(model: &RawModel, tokenizer: &Tokenizer) -> Result<ClonableTensor> {+ let tokens = tokenizer.encode(PROMPT_PREFIX.to_string())?;++ let mut state = model.init_state();++ for token in tokens {+ let (_, new_state) = model.forward(token, Some(&state));+ state = new_state;+ }++ Ok(ClonableTensor(state))+ }++ pub fn new_executor(+ &self,+ user_question: &str,+ last_state: Option<EncryptedState>,+ search_url: String,+ optic_url: Option<String>,+ ) -> Result<ActionExecutor> {+ let mut state = None;++ if let Some(s) = last_state {+ let decrypted_state = s.decrypt(&self.encryption_key)?;+ state = Some(Tensor::from_slice2(&decrypted_state));+ }++ let (tokens, state) = match state {+ Some(state) => {+ let input = format!("User: {user_question}\n\nAlice[thought]:");+ let tokens = self.tokenizer.encode(input).unwrap();++ (tokens, state)+ }+ None => {+ let input = format!("\n\nUser: {user_question}\n\nAlice[thought]:");+ let tokens = self.tokenizer.encode(input).unwrap();++ (tokens, self.initial_state.clone().0)+ }};- tracing::debug!("searching at {:?}: {:#?}", self.url, query);- let res: SearchResult = client- .post(&self.url)- .json(&query)- .send()- .await?- .json()- .await?;+ let token_generator = AliceTokenGenerator::new(+ self.inner.clone(),+ self.end_tokens.clone(),+ state,+ &tokens,+ None,+ )?;++ let raw_action_gen = RawActionGenerator::new(token_generator, self.tokenizer.clone());+ let action_gen = ActionGenerator::new(raw_action_gen);++ let searcher = Searcher {+ url: search_url,+ optic_url,+ summarizer: Arc::clone(&self.summarizer),+ };- match res {- SearchResult::Websites(res) => Ok(res),- SearchResult::Bang(_) => Err(Error::UnexpectedSearchResult),+ Ok(ActionExecutor::new(+ action_gen,+ searcher,+ self.encryption_key,+ ))+ }+}++#[derive(Clone)]+pub struct ModelState {+ state: ClonableTensor,+ next_token: i64,+}++pub struct Tokenizer {+ tokenizer: tokenizers::Tokenizer,+}++impl Tokenizer {+ pub fn open<P: AsRef<Path>>(path: P) -> Result<Tokenizer> {+ let tokenizer = tokenizers::Tokenizer::from_file(path)?;++ Ok(Self { tokenizer })+ }++ pub fn encode(&self, input: String) -> Result<Vec<i64>> {+ let encoding = self.tokenizer.encode(input, false)?;++ let ids = encoding+ .get_ids()+ .iter()+ .map(|&id| id as i64)+ .collect::<Vec<_>>();++ Ok(ids)+ }++ pub fn decode(&self, tokens: &[i64]) -> Result<String> {+ let tokens = tokens.iter().map(|&id| id as u32).collect::<Vec<_>>();++ let output = self.tokenizer.decode(tokens, true)?;++ Ok(output)+ }+}++#[derive(Debug, serde::Serialize, serde::Deserialize)]+/// base64 encoded `EncryptedState`+pub struct EncodedEncryptedState(String);++impl EncodedEncryptedState {+ pub fn encode(state: EncryptedState) -> Self {+ let bytes = bincode::serialize(&state).unwrap();+ let encoded = base64::encode(bytes);++ Self(encoded)+ }++ pub fn decode(self) -> Result<EncryptedState> {+ let bytes = base64::decode(self.0)?;+ let state = bincode::deserialize(&bytes)?;++ Ok(state)+ }+}++#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]+pub struct EncryptedState {+ nonce: Vec<u8>,+ // ciphertext is bincoded `CompressedState` encrypted with `nonce` and a key+ ciphertext: Vec<u8>,+}++impl EncryptedState {+ fn encrypt(state: Vec<Vec<f32>>, key: &Key<Aes256Gcm>) -> Self {+ let compressed = compress_state(state);++ let cipher = Aes256Gcm::new(key);+ let nonce = Aes256Gcm::generate_nonce(&mut OsRng);++ let bytes = bincode::serialize(&compressed).unwrap();++ let ciphertext = cipher.encrypt(&nonce, bytes.as_slice()).unwrap();++ Self {+ nonce: nonce.to_vec(),+ ciphertext,}}- async fn search_async(&self, query: &str) -> Result<Vec<SimplifiedWebsite>> {- let res = self.raw_search_async(query).await?;+ fn decrypt(&self, key: &Key<Aes256Gcm>) -> Result<Vec<Vec<f32>>> {+ let cipher = Aes256Gcm::new(key);+ let nonce = Nonce::from_slice(&self.nonce);- let mut websites = Vec::new();+ let decrypted = cipher+ .decrypt(nonce, self.ciphertext.as_slice())+ .map_err(|_| Error::DecryptionFailed)?;- for website in res.webpages {- websites.push(SimplifiedWebsite::new(website, query, &self.summarizer));+ let compressed: CompressedState = bincode::deserialize(&decrypted)?;++ decompress_state(compressed)+ }+}++/// base64 encoded gzipped state+#[derive(Debug, serde::Serialize, serde::Deserialize)]+pub struct CompressedState(String);++pub fn compress_state(state: Vec<Vec<f32>>) -> CompressedState {+ let state: Vec<Vec<bf16>> = state+ .into_iter()+ .map(|v| v.into_iter().map(bf16::from_f32).collect())+ .collect();++ let mut encoder = GzEncoder::new(Vec::new(), Compression::best());++ encoder+ .write_all(&bincode::serialize(&state).unwrap())+ .unwrap();++ let compressed = encoder.finish().unwrap();++ // base64 encode the compressed state+ CompressedState(base64::encode(compressed))+}++pub fn decompress_state(state: CompressedState) -> Result<Vec<Vec<f32>>> {+ let state = base64::decode(state.0)?;++ let mut decoder = GzDecoder::new(&state[..]);++ let mut state = Vec::new();+ decoder.read_to_end(&mut state)?;++ let state: Vec<Vec<bf16>> = bincode::deserialize(&state)?;++ Ok(state+ .into_iter()+ .map(|v| v.into_iter().map(f32::from).collect())+ .collect())+}++#[derive(Debug)]+struct AnyTransitionValidator {+ validators: Vec<TransitionValidator>,+}++impl AnyTransitionValidator {+ fn new(validators: Vec<TransitionValidator>) -> Self {+ Self { validators }+ }++ fn validate(&mut self, token: i64) -> bool {+ for validator in &mut self.validators {+ if validator.validate(token) {+ return true;+ }}- tracing::debug!("search result: {:#?}", websites);+ false+ }+}- Ok(websites)+#[derive(Debug)]+struct TransitionValidator {+ tokens: Vec<i64>,+ next_match: usize,+}++impl TransitionValidator {+ fn new(tokens: Vec<i64>) -> Self {+ Self {+ tokens,+ next_match: 0,+ }+ }++ fn validate(&mut self, token: i64) -> bool {+ if self.tokens[self.next_match] == token {+ self.next_match += 1;+ if self.next_match == self.tokens.len() {+ self.next_match = 0;+ true+ } else {+ false+ }+ } else {+ self.next_match = 0;+ false+ }}}diff --git a/core/src/alice/openai.rs b/core/src/alice/openai.rsdeleted file mode 100644index ccec6388..00000000--- a/core/src/alice/openai.rs+++ /dev/null@@ -1,385 +0,0 @@-// 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 std::sync::Arc;--use reqwest_eventsource::EventSource;-use tokio_stream::StreamExt;--use crate::summarizer::ExtractiveSummarizer;--use super::Error;-use super::ExecutionState;-use super::ModelWebsite;-use super::Result;-use super::Searcher;-use super::SimplifiedWebsite;--const MAX_NUM_SEARCHES: usize = 3;--#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]-#[serde(tag = "@type", rename_all = "camelCase")]-pub enum Message {- User {- message: String,- },- Alice {- message: String,- queries: Vec<String>,- },-}--fn prompt(user_question: &str, conv: &[Message]) -> String {- let mut prompt = r#"System: Your name is Alice, and you are an AI assistant developed by Stract. Below is a conversation between you and a user. Help the user as best you can.-You can lookup information on the web in the following format:-Alice[thought]: I should look up "<keywords>" on the web.-Search i: <query>-Result: <search result>--This Thought/Search/Result can repeat N times.-You should always cite the source of your statements using the format [search i source j] at the end of the statement.--The search results from your previous messages have been truncated.--When you are ready to answer the user, use the following format:-Alice[thought]: I now know enough to answer the user.-Alice: <answer>--You should only use the information you find on the web. If you couldn't find a good answer to the user, you should tell them and ask for clarification.--Begin!"#.to_string();-- // truncate conv- let conv = if conv.len() > 4 {- &conv[conv.len() - 4..]- } else {- conv- };-- for msg in conv {- match msg {- Message::User { message } => {- prompt.push_str("\n\n");- prompt.push_str(format!("User: {}", message).as_str());- }- Message::Alice { message, queries } => {- prompt.push('\n');- for query in queries {- prompt.push('\n');- prompt.push_str(&format!(- "Alice[thought]: I should look up \"{query}\" on the web."- ));- prompt.push('\n');- prompt.push_str(&format!("Search i: {query}\n"));- prompt.push_str("Result: <truncated>");- }- prompt.push('\n');- prompt.push_str("Alice[thought]: I now know enough to answer the user.");- prompt.push('\n');- prompt.push_str(&format!("Alice: {}", message));- }- }- }-- prompt.push_str("\n\n");- prompt.push_str(format!("User: {}", user_question).as_str());-- prompt-}--#[derive(serde::Serialize, serde::Deserialize)]-struct OpenaiParams {- model: String,- prompt: String,- max_tokens: usize,- temperature: f32,- top_p: f32,- stream: bool,- stop: Vec<String>,-}--async fn thoughts(prompt: &str, api_key: &str) -> Result<String> {- let client = reqwest::Client::new();- let res: serde_json::Value = client- .post("https://api.openai.com/v1/completions")- .bearer_auth(api_key)- .json(&OpenaiParams {- model: "text-davinci-003".to_string(),- prompt: prompt.to_string(),- max_tokens: 100,- temperature: 0.7,- top_p: 0.7,- stream: false,- stop: vec!["Result:".to_string(), "Alice:".to_string()],- })- .send()- .await?- .json()- .await?;-- let text = res["choices"][0]["text"].as_str().unwrap().to_string();-- Ok(dbg!(text))-}--async fn speak(prompt: &str, api_key: &str) -> Result<EventSource> {- let client = reqwest::Client::new();- let req = client- .post("https://api.openai.com/v1/completions")- .bearer_auth(api_key)- .json(&OpenaiParams {- model: "text-davinci-003".to_string(),- prompt: prompt.to_string(),- max_tokens: 512,- temperature: 0.7,- top_p: 0.7,- stream: true,- stop: vec!["<|endoftext|>".to_string()],- });-- let res = EventSource::new(req)?;-- Ok(res)-}--#[derive(Debug)]-enum State {- BeginSearch { query: String },- PerformSearch { query: String },-- Thinking,- Speaking,-}--fn next_state(completion: &str) -> Result<State> {- let completion = completion.trim();-- if completion.ends_with("Alice:")- || completion.contains("I now know enough to answer the user.")- {- Ok(State::Speaking)- } else if completion.contains("Search") {- // find last "Search \d: <query>" and extract query- let mut query = None;- for line in completion.lines().rev() {- if line.starts_with("Search") {- query = Some(line.split(':').nth(1).unwrap().trim());- break;- }- }-- if let Some(query) = query {- Ok(State::BeginSearch {- query: query.to_string(),- })- } else {- Err(Error::UnexpectedCompletion)- }- } else {- Err(Error::UnexpectedCompletion)- }-}--pub struct Alice {- api_key: String,- summarizer: Arc<ExtractiveSummarizer>,-}--impl Alice {- pub fn open<P: AsRef<Path>>(summarizer_path: P, api_key: &str) -> Result<Self> {- let mut summarizer = ExtractiveSummarizer::open(summarizer_path, 1)?;- summarizer.set_window_size(30);-- let summarizer = Arc::new(summarizer);-- Ok(Self {- api_key: api_key.to_string(),- summarizer,- })- }-- pub fn new_executor(- &self,- conversation: Vec<Message>,- optic_url: Option<String>,- search_url: String,- ) -> Result<Executor> {- let user_question = match conversation.last() {- Some(Message::User { message }) => message.clone(),- _ => return Err(Error::LastMessageNotUser),- };-- let prev_conv = conversation[..conversation.len() - 1].to_vec();-- let searcher = Searcher {- url: search_url,- optic_url,- summarizer: self.summarizer.clone(),- };-- Ok(Executor::new(- prev_conv,- self.api_key.clone(),- user_question,- searcher,- ))- }-}--#[derive(Clone)]-struct Search {- query: String,- results: Vec<SimplifiedWebsite>,-}--pub struct Executor {- api_key: String,- prev_conv: Vec<Message>,- current_searches: Vec<Search>,- user_question: String,- state: State,- searcher: Searcher,- speaking_stream: Option<EventSource>,- performed_searches: usize,-}--impl Executor {- fn new(- prev_conv: Vec<Message>,- api_key: String,- user_question: String,- searcher: Searcher,- ) -> Self {- Self {- api_key,- state: State::Thinking,- prev_conv,- user_question,- current_searches: Vec::new(),- speaking_stream: None,- searcher,- performed_searches: 0,- }- }-- fn build_prompt(&self, speaking: bool) -> String {- let mut prompt = prompt(&self.user_question, &self.prev_conv);-- for (i, search) in self.current_searches.clone().into_iter().enumerate() {- prompt.push_str("\n\n");- prompt.push_str(- format!(- "Alice[thought]: I should look up \"{}\" on the web.",- search.query- )- .as_str(),- );- prompt.push_str(format!("\nSearch {}: {}", i + 1, search.query).as_str());- let res = search- .results- .into_iter()- .take(3)- .map(ModelWebsite::from)- .collect::<Vec<_>>();-- prompt- .push_str(format!("\nResponse: {}", serde_json::to_string(&res).unwrap()).as_str());- }-- prompt.push_str("\n\n");-- if speaking {- prompt.push_str("Alice[thought]: I now know enough to answer the user.");- prompt.push_str("\n\n");- prompt.push_str("Alice:");- } else {- prompt.push_str("Alice[thought]:");- }-- dbg!(prompt)- }-- pub async fn next(&mut self) -> Result<Option<ExecutionState>> {- loop {- match &mut self.state {- State::BeginSearch { query } => {- let query = query.clone();-- if self.current_searches.iter().any(|s| s.query == query) {- self.state = State::Speaking;- } else {- self.state = State::PerformSearch {- query: query.clone(),- };-- return Ok(Some(ExecutionState::BeginSearch { query }));- }- }- State::PerformSearch { query } => {- let query = query.to_string();- let res = self.searcher.search_async(&query).await?;-- self.current_searches.push(Search {- query: query.clone(),- results: res.clone(),- });-- self.performed_searches += 1;-- if self.performed_searches >= MAX_NUM_SEARCHES {- self.state = State::Speaking;- } else {- self.state = State::Thinking;- }-- return Ok(Some(ExecutionState::SearchResult { query, result: res }));- }- State::Thinking => {- let prompt = self.build_prompt(false);- let cur_thoughts = thoughts(&prompt, &self.api_key).await?;- let next_state = dbg!(next_state(&cur_thoughts))?;- self.state = next_state;- }- State::Speaking => {- if self.speaking_stream.is_none() {- let prompt = self.build_prompt(true);- self.speaking_stream = Some(speak(&prompt, &self.api_key).await?);- }-- let stream = self.speaking_stream.as_mut().unwrap();- match stream.next().await {- Some(event) => {- let event = event?;-- match event {- reqwest_eventsource::Event::Open => {}- reqwest_eventsource::Event::Message(msg) => {- let res: serde_json::Value = serde_json::from_str(&msg.data)?;- let text =- res["choices"][0]["text"].as_str().unwrap().to_string();-- return Ok(Some(ExecutionState::Speaking { text }));- }- }- }- None => return Ok(None),- }- }- }- }- }-}diff --git a/core/src/alice/local/raw_model.rs b/core/src/alice/raw_model.rssimilarity index 100%rename from core/src/alice/local/raw_model.rsrename to core/src/alice/raw_model.rsdiff --git a/core/src/api/alice.rs b/core/src/api/alice.rsindex efc5308b..cdba6cc4 100644--- a/core/src/api/alice.rs+++ b/core/src/api/alice.rs@@ -27,7 +27,7 @@ use tokio_stream::{Stream, StreamExt};use crate::{distributed::member::Service,- entrypoint::{self, alice::openai::SaveStateParams},+ entrypoint::{self, alice::SaveStateParams},};#[derive(Debug, serde::Serialize, serde::Deserialize)]@@ -164,7 +164,7 @@ pub async fn route(.ok_or(StatusCode::INTERNAL_SERVER_ERROR)?};- let params = entrypoint::alice::local::Params {+ let params = entrypoint::alice::Params {message: params.message,prev_state: saved_state.map(|s| s.uuid),optic: params.optic,diff --git a/core/src/api/improvement.rs b/core/src/api/improvement.rsindex c0f3d844..a993807a 100644--- a/core/src/api/improvement.rs+++ b/core/src/api/improvement.rs@@ -21,7 +21,7 @@ use axum::{extract, response::IntoResponse};use serde::Deserialize;use uuid::Uuid;-use crate::improvement::{AliceConversation, ImprovementEvent, StoredQuery};+use crate::improvement::{AliceMessage, ImprovementEvent, StoredQuery};use super::{HtmlTemplate, State};@@ -76,6 +76,12 @@ pub async fn store(}}+#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]+pub struct AliceConversation {+ pub id: Uuid,+ pub messages: Vec<AliceMessage>,+}+#[allow(clippy::unused_async)]pub async fn new_chat_id() -> impl IntoResponse {Uuid::new_v4().to_string()@@ -87,7 +93,9 @@ pub async fn store_chat(extract::Json(params): extract::Json<AliceConversation>,) {if let Some(q) = state.improvement_queue.as_ref() {- q.lock().await.push(ImprovementEvent::Chat { chat: params })+ q.lock().await.push(ImprovementEvent::Chat {+ chat: params.into(),+ })}}diff --git a/core/src/entrypoint/alice/local.rs b/core/src/entrypoint/alice.rssimilarity index 95%rename from core/src/entrypoint/alice/local.rsrename to core/src/entrypoint/alice.rsindex a7032932..b1a3171e 100644--- a/core/src/entrypoint/alice/local.rs+++ b/core/src/entrypoint/alice.rs@@ -14,6 +14,7 @@// 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 aes_gcm::{aead::OsRng, Aes256Gcm, KeyInit};use std::{convert::Infallible, net::SocketAddr, sync::Arc, time::Duration};use axum::{@@ -31,7 +32,7 @@ use tokio_stream::StreamExt as _;use tracing::info;use crate::{- alice::local::{Alice, EncodedEncryptedState, EncryptedState},+ alice::{Alice, EncodedEncryptedState, EncryptedState},distributed::{cluster::Cluster,member::{Member, Service},@@ -197,3 +198,8 @@ pub async fn run(config: AliceLocalConfig) -> Result<(), crate::alice::Error> {Ok(())}++pub fn generate_key() {+ let key = Aes256Gcm::generate_key(OsRng);+ println!("{}", base64::encode(key.as_slice()));+}diff --git a/core/src/entrypoint/alice/mod.rs b/core/src/entrypoint/alice/mod.rsdeleted file mode 100644index bad2db74..00000000--- a/core/src/entrypoint/alice/mod.rs+++ /dev/null@@ -1,28 +0,0 @@-// 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 aes_gcm::{aead::OsRng, Aes256Gcm, KeyInit};--pub mod local;--/// This is currently not used, but let's keep it around for now-/// in case we want to use it later.-pub mod openai;--pub fn generate_key() {- let key = Aes256Gcm::generate_key(OsRng);- println!("{}", base64::encode(key.as_slice()));-}diff --git a/core/src/entrypoint/alice/openai.rs b/core/src/entrypoint/alice/openai.rsdeleted file mode 100644index d70bef54..00000000--- a/core/src/entrypoint/alice/openai.rs+++ /dev/null@@ -1,186 +0,0 @@-// 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::{convert::Infallible, net::SocketAddr, sync::Arc, time::Duration};--use axum::{- extract,- response::{- sse::{Event, KeepAlive},- IntoResponse, Sse,- },- routing::{get, post},- Router,-};-use tokio::sync::Mutex;-use tokio_stream::{Stream, StreamExt};-use tracing::info;--use crate::{- alice::openai::{Alice, Message},- distributed::{- cluster::Cluster,- member::{Member, Service},- },- ttl_cache::TTLCache,- AliceOpenaiConfig,-};--pub struct State {- pub alice: Alice,- pub cluster: Cluster,- pub conv_states: Arc<Mutex<TTLCache<uuid::Uuid, Vec<Message>>>>,-}--fn router(alice: Alice, cluster: Cluster) -> Router {- let state = Arc::new(State {- alice,- cluster,- conv_states: Arc::new(Mutex::new(TTLCache::with_ttl(Duration::from_secs(10)))),- });-- Router::new()- .route("/", get(route))- .route("/save_state", post(save_state))- .with_state(state)-}--#[derive(serde::Serialize, serde::Deserialize)]-pub struct SaveStateParams {- pub conversation: Vec<Message>,-}--pub async fn save_state(- extract::State(state): extract::State<Arc<State>>,- extract::Json(params): extract::Json<SaveStateParams>,-) -> impl IntoResponse {- let uuid = uuid::Uuid::new_v4();-- state- .conv_states- .lock()- .await- .insert(uuid, params.conversation);-- uuid.to_string()-}--#[derive(serde::Serialize, serde::Deserialize, Debug)]-#[serde(rename_all = "camelCase")]-pub struct Params {- pub optic: Option<String>,- pub conversation: uuid::Uuid,-}--pub async fn route(- extract::State(state): extract::State<Arc<State>>,- extract::Query(params): extract::Query<Params>,-) -> std::result::Result<- Sse<impl Stream<Item = std::result::Result<Event, Infallible>>>,- http::StatusCode,-> {- let conversation = state- .conv_states- .lock()- .await- .get(¶ms.conversation)- .cloned()- .ok_or(http::StatusCode::BAD_REQUEST)?;-- let search_addr = state- .cluster- .members()- .await- .into_iter()- .find_map(|m| {- if let Service::Frontend { host } = m.service {- Some(host)- } else {- None- }- })- .ok_or_else(|| {- info!("no frontend found");-- http::StatusCode::INTERNAL_SERVER_ERROR- })?;-- let mut executor = state- .alice- .new_executor(- conversation,- params.optic,- format!("http://{}/beta/api/search", search_addr),- )- .map_err(|e| {- info!("error creating executor: {}", e);- http::StatusCode::BAD_REQUEST- })?;-- let (tx, mut rx) = tokio::sync::mpsc::channel(1);-- tokio::task::spawn(async move {- while let Ok(Some(msg)) = executor.next().await {- let msg = serde_json::to_string(&msg)- .map_err(|e| {- info!("error serializing message: {}", e);- e- })- .unwrap();-- tx.send(msg).await.ok();- }- });-- let stream = async_stream::stream! {- while let Some(item) = rx.recv().await {- yield item;- }- };-- Ok(- Sse::new(stream.map(|term| Event::default().data(term)).map(Ok))- .keep_alive(KeepAlive::default()),- )-}--/// This is currently not used, but let's keep it around for now-/// in case we want to use it later.-pub async fn run(config: AliceOpenaiConfig) -> Result<(), crate::alice::Error> {- let addr: SocketAddr = config.host;-- let alice = Alice::open(&config.summarizer_path, &config.api_key)?;-- let cluster = Cluster::join(- Member {- id: config.cluster_id,- service: Service::Alice { host: config.host },- },- config.gossip_addr,- config.gossip_seed_nodes.unwrap_or_default(),- )- .await?;-- let app = router(alice, cluster);-- info!("alice is ready to accept requests on {}", addr);- axum::Server::bind(&addr)- .serve(app.into_make_service())- .await- .unwrap();-- Ok(())-}diff --git a/core/src/improvement.rs b/core/src/improvement.rsindex 98b49de3..b708824d 100644--- a/core/src/improvement.rs+++ b/core/src/improvement.rs@@ -16,13 +16,13 @@use std::{sync::Arc, time::Duration};-use chrono::{DateTime, Timelike, Utc};+use chrono::{DateTime, NaiveDate, Timelike, Utc};use scylla::{prepared_statement::PreparedStatement, SessionBuilder};use thiserror::Error;use tokio::{sync::Mutex, time};use uuid::Uuid;-use crate::{leaky_queue::LeakyQueue, webpage::Url};+use crate::{api::improvement::AliceConversation, leaky_queue::LeakyQueue, webpage::Url};#[derive(Debug, Error)]enum Error {@@ -44,29 +44,42 @@ pub struct StoredQuery {}#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]-pub struct AliceConversation {+pub struct StoredAliceConversation {pub id: Uuid,messages: Vec<AliceMessage>,+ date: NaiveDate,}#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]-struct AliceMessage {+pub struct AliceMessage {from: String,message: String,queries: Vec<AliceQuery>,}#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]-struct AliceQuery {+pub struct AliceQuery {query: String,results: Vec<crate::alice::SimplifiedWebsite>,}+impl From<AliceConversation> for StoredAliceConversation {+ fn from(value: AliceConversation) -> Self {+ let date = chrono::Utc::now().date_naive();++ Self {+ id: value.id,+ messages: value.messages,+ date,+ }+ }+}+#[derive(Clone)]pub enum ImprovementEvent {StoreQuery(StoredQuery),Click { qid: Uuid, idx: usize },- Chat { chat: AliceConversation },+ Chat { chat: StoredAliceConversation },}impl StoredQuery {@@ -155,7 +168,7 @@ impl ScyllaConn {session.query(- "CREATE TABLE IF NOT EXISTS ks.chats (id uuid, json_encoded text, primary key (qid))",+ "CREATE TABLE IF NOT EXISTS ks.chats (id uuid, json_encoded text, date date, primary key (qid))",&[],).await?;@@ -169,7 +182,7 @@ impl ScyllaConn {.await?;let prepared_chat: PreparedStatement = session- .prepare("INSERT INTO ks.chats (id, json_encoded) VALUES(?, ?)")+ .prepare("INSERT INTO ks.chats (id, json_encoded, date) VALUES(?, ?, ?)").await?;Ok(Self {@@ -214,11 +227,11 @@ impl ScyllaConn {}}- async fn store_chat(&self, chat: AliceConversation) {+ async fn store_chat(&self, chat: StoredAliceConversation) {if let Ok(json_encoded) = serde_json::to_string(&chat) {let res = self.session- .execute(&self.prepared_chat, (chat.id, json_encoded))+ .execute(&self.prepared_chat, (chat.id, json_encoded, chat.date)).await;if let Err(err) = res {diff --git a/core/src/lib.rs b/core/src/lib.rsindex cbd6b691..f00d1384 100644--- a/core/src/lib.rs+++ b/core/src/lib.rs@@ -228,17 +228,6 @@ pub struct AliceLocalConfig {pub encryption_key: String,}-#[derive(Debug, Serialize, Deserialize, Clone)]-pub struct AliceOpenaiConfig {- pub cluster_id: String,- pub gossip_seed_nodes: Option<Vec<SocketAddr>>,- pub gossip_addr: SocketAddr,- pub host: SocketAddr,- pub summarizer_path: String,-- pub api_key: String,-}-#[derive(Debug, Serialize, Deserialize, Clone)]pub struct WebgraphServerConfig {pub host: SocketAddr,diff --git a/core/src/main.rs b/core/src/main.rsindex 43f92acf..6292fe63 100644--- a/core/src/main.rs+++ b/core/src/main.rs@@ -322,7 +322,7 @@ fn main() -> Result<()> {tokio::runtime::Builder::new_multi_thread().enable_all().build()?- .block_on(entrypoint::alice::local::run(config))?+ .block_on(entrypoint::alice::run(config))?}AliceOptions::GenerateKey => entrypoint::alice::generate_key(),},diff --git a/frontend/src/pages/chat.astro b/frontend/src/pages/chat.astroindex 751b66f2..e17bf14d 100644--- a/frontend/src/pages/chat.astro+++ b/frontend/src/pages/chat.astro@@ -52,7 +52,7 @@ import OpticSelector from "../components/OpticSelector.astro";padding-left: 1.25rem;}- .user-message {+ .user-message-text {padding: 0.5rem;background-color: rgba(11, 123, 255, 0.05);border-color: rgba(11, 123, 255, 0.3);@@ -64,7 +64,7 @@ import OpticSelector from "../components/OpticSelector.astro";border-bottom-right-radius: 0;}- .alice-message {+ .alice-message-text {padding: 0.5rem;background-color: rgba(0, 0, 0, 0.025);border-color: rgba(0, 0, 0, 0.15);@@ -82,6 +82,38 @@ import OpticSelector from "../components/OpticSelector.astro";justify-content: flex-start;padding-right: 1.25rem;}++ .inline-source {+ display: inline-flex;+ border-radius: 50%;+ background-color: white;+ text-align: center;+ justify-content: center;+ border: 1px solid rgba(0, 0, 0, 0.4);+ padding-left: 0.22rem;+ padding-right: 0.22rem;+ font-size: 0.6rem;+ height: 0.9rem;+ line-height: 0.85rem;+ }++ .message-sources {+ display: flex;+ flex-wrap: wrap;+ column-gap: 1.5rem;+ row-gap: 0.5rem;+ margin-top: 0.25rem;+ }++ .message-source {+ display: flex;+ align-items: center;+ gap: 0.25rem;+ }++ #message-container {+ line-height: 22px;+ }</style><script is:inline src="/js/marked.min.js"></script>@@ -105,12 +137,74 @@ import OpticSelector from "../components/OpticSelector.astro";let originalSendButtonColor = sendButton.style.color;+ type Query = {+ query: string,+ results: {text: string, url: string, site: string, title: string}[]+ }++ type Message = {+ from: "user" | "alice",+ message: string,+ queries: Query[]+ sourceColors: Record<number, Record<number, string>> // queryId -> sourceId -> color+ };+ type MessageWithElement = Message & {element: HTMLDivElement | null};+let numMessageLines = 1;let messageInputHeight = 24;- let messages: {from: string, message: string, queries: {query: string, results: {text: string, url: string, title: string}[]}[]}[] = [];+ let firstMessage = true;+ let messages: MessageWithElement[] = [];+let aliceResponding = false;let aliceStreamingMessage = "";- let currentQueries: {query: string, results: {text: string, url: string, title: string}[]}[] = [];+ let currentQueries: Query[] = [];++ // messages = [+ // {+ // "from": "user",+ // "message": "how does a car work",+ // "queries": [],+ // "sourceColors": {},+ // "element": null+ // },+ // {+ // "from": "alice",+ // "message": " Yes, you can rent a car in Santa Cruz de la Sierra from $34 per day[query 1 source 1]. The car rental rates vary depending on the type of vehicle you choose, the duration of the rental, and any additional fees. You can also find cheap car rental deals in Santa Cruz de la Sierra from $24 - $35 per day[query 1 source 1]. Additionally, you can rent a car for a month for just $34[query 1 source 2]. It's important to note that driving in Santa Cruz de la Sierra can be challenging due to the country's challenging road conditions and drivers who may not follow traffic laws[query 1 source 3]. It's recommended that you hire a licensed driver and take extra precautions when driving in the city[query 1 source 3].",+ // "queries": [+ // {+ // "query": "how does a car work",+ // "results": [+ // {+ // "title": "Cheap Car Rental Deals in Santa Cruz de la Sierra from $34 - Cheapflights.com",+ // "text": "month, costing you anywhere between $24 and $35 to fill up a car’s gas tank. Can I rent a car for a month in Santa Cruz de la Sierra? Yes,",+ // "url": "https://www.cheapflights.com/car-rentals/santa-cruz/",+ // "site": "cheapflights.com"+ // },+ // {+ // "title": "What a Character Blogathon: How Arthur Kennedy Changed my Cinematic Life – The Wonderful World of Cinema",+ // "text": "us smile, he’s just driving a car, but simply the fact that he’s here and that I was beginning to know him more and more. You know, just as if",+ // "url": "https://thewonderfulworldofcinema.wordpress.com/2016/12/17/what-a-character-blogathon-how-arthur-kennedy-changed-my-cinematic-life/",+ // "site": "thewonderfulworldofcinema.wordpress.com"+ // },+ // {+ // "title": "Cop Killer Shouts Obscenities In Court, He Also Killed His Baby Mama… – Conversations Of A Sistah",+ // "text": "likely. The devil didn’t make him steal his dads car, the devil didn’t make him kill those folks BUT now that he is up chit creek he is free to",+ // "url": "https://conversationsofasistah.com/2012/01/31/cop-killer-shouts-obscenities-in-court-he-also-killed-his-baby-mama/",+ // "site": "conversationsofasistah.com"+ // }+ // ]+ // }+ // ],+ // "sourceColors": {},+ // "element": null+ // }+ // ];++ // messageContainer.innerHTML = "";++ // for (const message of messages) {+ // renderMessage(message);+ // }function updateMessageInputHeight() {const lines = messageInput.value.split("\n");@@ -145,12 +239,17 @@ import OpticSelector from "../components/OpticSelector.astro";return;}+ if (firstMessage) {+ firstMessage = false;+ messageContainer.innerHTML = "";+ }+addMessage("user", message);await sendMessageToAlice(message);messageInput.value = "";}- async function save_prev_state(): Promise<string> {+ async function savePrevState(): Promise<string> {let url = "/beta/api/alice/save_state";var reqData: Record<string, string> = {'state': prevState,@@ -194,6 +293,12 @@ import OpticSelector from "../components/OpticSelector.astro";}if (data["@type"] == "speaking") {+ const status = document.querySelector("#active-status");++ if (status) {+ status.remove();+ }+if (aliceStreamingMessage == "") {addMessage("alice", "", currentQueries);currentQueries = [];@@ -202,7 +307,7 @@ import OpticSelector from "../components/OpticSelector.astro";const lastMessage = messages[messages.length - 1];lastMessage.message = aliceStreamingMessage;- renderMessages();+ renderLastMessage();} else if (data["@type"] == "beginSearch") {const status = document.querySelector("#active-status")!;status.querySelector("#info")!.innerHTML = `Looking up <span style="font-weight: bold;">${data["query"]}</span>`;@@ -243,7 +348,7 @@ import OpticSelector from "../components/OpticSelector.astro";}if (prevState) {- let savedState = await save_prev_state();+ let savedState = await savePrevState();reqData['prevState'] = savedState;}@@ -263,46 +368,157 @@ import OpticSelector from "../components/OpticSelector.astro";};}- function addMessage(from: string, message: string, queries: {query: string, results: {text: string, url: string, title: string}[]}[] = []) {+ function addMessage(from: string, message: string, queries: Query[] = []) {+ if (from != "user" && from != "alice") {+ throw new Error("Invalid message sender");+ }+messages.push({from: from,message: message,- queries: queries+ queries: queries,+ sourceColors: {},+ element: null});- renderMessages();+ renderLastMessage();scrollToBottom();}- function renderMessages() {- messageContainer.innerHTML = "";+ function renderLastMessage() {+ if (messages.length == 0) {+ return;+ }- for (const message of messages) {- let renderedMessage = renderMarkdown(message.message);+ const message = messages[messages.length - 1];+ renderMessage(message);+ }- if (message.from === "user") {- const msg = document.createElement("div");- msg.classList.add("user-message-container");+ function renderMessage(message: MessageWithElement) {+ if (message.element == null) {+ message.element = document.createElement("div");+ messageContainer.appendChild(message.element);+ }++ message.element.innerHTML = "";++ let renderedMessage = renderMarkdown(message.message);++ // replace [query i source j] with a link to the source+ // find using regex+ const regex = /\[query (\d+) source (\d+)\]/g;+ let match: RegExpExecArray | null;+ let newRenderedMessage = renderedMessage;+ let messageSources = [];+ let nextSourceId = 1;- const msgElem = document.createElement("div");- msgElem.classList.add("user-message");- msgElem.innerHTML = renderedMessage;+ let source2Id: Record<number, Record<number, number>> = {}; // (i, j) -> id- msg.appendChild(msgElem);+ while ((match = regex.exec(renderedMessage)) !== null) {+ const queryNum = parseInt(match[1]);+ const sourceNum = parseInt(match[2]);- messageContainer.appendChild(msg);- } else {- const msg = document.createElement("div");- msg.classList.add("alice-message-container");+ let i = queryNum - 1;+ let j = sourceNum - 1;- const msgElem = document.createElement("div");- msgElem.classList.add("alice-message");- msgElem.innerHTML = renderedMessage;+ const query = message.queries[i];+ const source = query.results[j];++ // find text from '.' to match[0]+ let text = renderedMessage.substring(renderedMessage.lastIndexOf(".", match.index) + 1, match.index).trim();++ if (text.startsWith("<p>")) {+ text = text.substring(3);+ }- msg.appendChild(msgElem);- messageContainer.appendChild(msg);+ if (!(i in source2Id)) {+ source2Id[i] = {};}++ if (!(j in source2Id[i])) {+ source2Id[i][j] = nextSourceId;+ nextSourceId += 1;+ }++ const id = source2Id[i][j];++ messageSources.push({+ query: query,+ source: source,+ claim: text,+ id: id+ })++ const link = `<span class="inline-source"><a href="` + source.url + `">` + id + `</a></span>`;++ newRenderedMessage = newRenderedMessage.replace(match[0], link);+ }++ // url and site of sources ordered by id+ let sourceUrls: {url: string, site: string}[] = [];++ for (let i = 0; i < nextSourceId; i++) {+ const source = messageSources.find(s => s.id == i + 1);++ if (source) {+ sourceUrls.push({+ url: source.source.url,+ site : source.source.site+ });+ }+ }++ renderedMessage = newRenderedMessage;++ if (message.from === "user") {+ message.element.classList.add("user-message-container");++ const msgElem = document.createElement("div");+ msgElem.classList.add("user-message-text");+ msgElem.innerHTML = renderedMessage;+ message.element.appendChild(msgElem);+ } else {+ message.element.classList.add("alice-message-container");++ const msgElem = document.createElement("div");+ msgElem.classList.add("alice-message");++ const msgText = document.createElement("div");+ msgText.classList.add("alice-message-text");+ msgText.innerHTML = renderedMessage;++ msgElem.appendChild(msgText);++ // add sources+ if (sourceUrls.length > 0) {+ const sources = document.createElement("div");+ sources.classList.add("message-sources");++ for (var i = 0; i < sourceUrls.length; i++) {+ let id = i + 1;+ const source = sourceUrls[i];++ const sourceLink = document.createElement("a");+ sourceLink.text = source.site;+ sourceLink.href = source.url;++ const sourceId = document.createElement("span");+ sourceId.classList.add("inline-source");+ sourceId.innerHTML = `<a href="` + source.url + `">` + id.toString() + `</a>`;++ const sourceContainer = document.createElement("div");+ sourceContainer.classList.add("message-source");+ sourceContainer.appendChild(sourceId);+ sourceContainer.appendChild(sourceLink);++ sources.appendChild(sourceContainer);+ }++ msgElem.appendChild(sources);+ }++ message.element.appendChild(msgElem);}// @ts-ignore@@ -366,6 +582,4 @@ import OpticSelector from "../components/OpticSelector.astro";storeChat();}}--</script>\ No newline at end of filediff --git a/frontend/src/pages/settings/privacy.astro b/frontend/src/pages/settings/privacy.astroindex 4ba7c67b..0cb2b1cf 100644--- a/frontend/src/pages/settings/privacy.astro+++ b/frontend/src/pages/settings/privacy.astro@@ -11,7 +11,7 @@ import SettingsLayout from "../../layouts/Settings.astro";<div class="text-sm">It is important to note, that <b class="font-extrabold">no</b> personal information is stored about your search. We don't store your IP, browser fingerprint or even a precise timestamp.- In fact, you can see exactly what gets stored <a href="https://github.com/StractOrg/Stract/blob/main/core/src/improvement.rs#L36-#L70">here</a>.+ In fact, you can see exactly what gets stored <a href="https://github.com/StractOrg/Stract/blob/main/core/src/improvement.rs#L36-#L64">here</a>.</div><div class="text-sm">