web search done right
allow user to specify optic in alice
Mikkel Denkerf4a2395769ff
diff --git a/core/examples/alice.rs b/core/examples/alice.rsindex d2919483..5a846702 100644--- a/core/examples/alice.rs+++ b/core/examples/alice.rs@@ -36,15 +36,16 @@ async fn main() {std::io::stdin().read_line(&mut input).unwrap();let input = input.trim();- let mut gen = model+ let gen = model.new_executor(input,last_state.clone(),"http://localhost:3000/beta/api/search".to_string(),+ None,).unwrap();- while let Some(n) = gen.next().await {+ for n in gen {match n {ExecutionState::BeginSearch { query: _ } => {}ExecutionState::SearchResult {diff --git a/core/src/alice/mod.rs b/core/src/alice/mod.rsindex 51c26424..48cf8334 100644--- a/core/src/alice/mod.rs+++ b/core/src/alice/mod.rs@@ -642,6 +642,7 @@ impl Alice {user_question: &str,last_state: Option<EncryptedState>,search_url: String,+ optic_url: Option<String>,) -> Result<ActionExecutor> {let mut state = None;@@ -675,8 +676,10 @@ impl Alice {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),};@@ -765,6 +768,7 @@ impl AliceTokenGenerator {.softmax(-1, Kind::Float).squeeze().to(tch::Device::Cpu);+let next_token = llm_utils::sample_typical(probs, TEMP, TAU);self.states.push(ModelState {state: ClonableTensor(new_state),@@ -799,6 +803,10 @@ impl AliceTokenGenerator {}}+ pub fn reset_tokens_counter(&mut self) {+ self.num_generated_tokens = 0;+ }+pub fn set_banned_tokens(&mut self, tokens: &[i64]) {if self.banned_tokens != tokens {self.banned_tokens = tokens.to_vec();@@ -1102,24 +1110,28 @@ impl Iterator for RawActionGenerator {match &mut self.state {State::Thought { search, speaking } => {let token = self.token_generator.next()?;- self.token_generator.set_max_new_tokens(None);+ self.token_generator.set_max_new_tokens(Some(1024));if self.queries_performed >= self.max_num_queries {+ self.token_generator.reset_tokens_counter();self.force_speaking();} else if search.validate(token) {+ self.token_generator.reset_tokens_counter();self.state = State::new_query_build(&self.tokenizer);} else if speaking.validate(token) {+ self.token_generator.reset_tokens_counter();self.state = State::new_speaking(vec![]);}}State::QueryBuild { tokens, end } => {let token = self.token_generator.next()?;- self.token_generator.set_max_new_tokens(None);+ self.token_generator.set_max_new_tokens(Some(1024));tokens.push(token);if end.validate(token) {let query = tokens.clone();*tokens = Vec::new();+ self.token_generator.reset_tokens_counter();self.state = State::new_thought(&self.tokenizer);self.queries_performed += 1;return Some(RawAction::Search { query });@@ -1137,6 +1149,7 @@ impl Iterator for RawActionGenerator {if end.validate(token) {banned_tokens.reset_ttl();+ self.token_generator.reset_tokens_counter();self.state = State::new_thought(&self.tokenizer);return None;} else {@@ -1190,16 +1203,22 @@ impl From<SimplifiedWebsite> for ModelWebsite {struct Searcher {url: String,+ optic_url: Option<String>,summarizer: Arc<ExtractiveSummarizer>,}impl Searcher {- async fn raw_search(&self, query: &str) -> Result<WebsitesResult> {- let client = reqwest::Client::new();+ fn raw_search(&self, query: &str) -> Result<WebsitesResult> {+ let optic = self+ .optic_url+ .as_ref()+ .and_then(|url| reqwest::blocking::get(url).ok().and_then(|r| r.text().ok()));++ let client = reqwest::blocking::Client::new();let query = ApiSearchQuery {query: query.trim().to_string(),num_results: Some(3),- optic: None, // TODO: let user specify optic+ optic,page: None,selected_region: None,site_rankings: None,@@ -1208,13 +1227,7 @@ impl Searcher {};tracing::debug!("searching at {:?}: {:#?}", self.url, query);- let res: SearchResult = client- .post(&self.url)- .json(&query)- .send()- .await?- .json()- .await?;+ let res: SearchResult = client.post(&self.url).json(&query).send()?.json()?;match res {SearchResult::Websites(res) => Ok(res),@@ -1222,8 +1235,8 @@ impl Searcher {}}- async fn search(&self, query: &str) -> Result<Vec<SimplifiedWebsite>> {- let res = self.raw_search(query).await?;+ fn search(&self, query: &str) -> Result<Vec<SimplifiedWebsite>> {+ let res = self.raw_search(query)?;let mut websites = Vec::new();@@ -1422,8 +1435,12 @@ impl ActionExecutor {.unwrap();self.generator.raw.token_generator.load_tokens(&tokens);}+}- pub async fn next(&mut self) -> Option<ExecutionState> {+impl Iterator for ActionExecutor {+ type Item = ExecutionState;++ fn next(&mut self) -> Option<Self::Item> {if self.has_finished {return None;}@@ -1433,7 +1450,7 @@ impl ActionExecutor {self.generator.raw.token_generator.go_to_last_search();self.generator.raw.force_speaking();} else {- let res = self.searcher.search(&query).await.unwrap_or_default();+ let res = self.searcher.search(&query).unwrap_or_default();tracing::debug!("loading search results");self.load_search_result(&res);diff --git a/core/src/api/alice.rs b/core/src/api/alice.rsindex 107d8d4c..eaead227 100644--- a/core/src/api/alice.rs+++ b/core/src/api/alice.rs@@ -107,8 +107,9 @@ impl EncodedSavedState {#[derive(serde::Serialize, serde::Deserialize, Debug)]#[serde(rename_all = "camelCase")]pub struct Params {- pub message: String,- pub prev_state: Option<EncodedSavedState>,+ message: String,+ optic: Option<String>,+ prev_state: Option<EncodedSavedState>,}pub async fn route(@@ -166,6 +167,7 @@ pub async fn route(let params = entrypoint::alice::Params {message: params.message,prev_state: saved_state.map(|s| s.uuid),+ optic: params.optic,};let mut events = clientdiff --git a/core/src/api/chat.rs b/core/src/api/chat.rsindex 29ae666e..0b5302e4 100644--- a/core/src/api/chat.rs+++ b/core/src/api/chat.rs@@ -16,7 +16,10 @@use std::collections::HashMap;-use super::HtmlTemplate;+use super::{+ optics::{OpticLink, DEFAULT_OPTICS},+ HtmlTemplate,+};use askama::Template;use axum::{extract, response::IntoResponse};@@ -26,6 +29,7 @@ pub async fn route() -> impl IntoResponse {let template = ChatTemplate {query_url_part: serde_urlencoded::to_string(params).unwrap(),+ default_optics: DEFAULT_OPTICS.to_vec(),};HtmlTemplate(template)}@@ -34,4 +38,5 @@ pub async fn route(#[template(path = "chat/index.html")]struct ChatTemplate {query_url_part: String,+ default_optics: Vec<OpticLink>,}diff --git a/core/src/entrypoint/alice.rs b/core/src/entrypoint/alice.rsindex 8045a733..c6744e0b 100644--- a/core/src/entrypoint/alice.rs+++ b/core/src/entrypoint/alice.rs@@ -32,7 +32,7 @@ use tokio_stream::StreamExt as _;use tracing::info;use crate::{- alice::{Alice, EncryptedState},+ alice::{Alice, EncodedEncryptedState, EncryptedState},distributed::{cluster::Cluster,member::{Member, Service},@@ -51,7 +51,7 @@ 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(60)))),+ conv_states: Arc::new(Mutex::new(TTLCache::with_ttl(Duration::from_secs(10)))),});Router::new()@@ -62,23 +62,18 @@ fn router(alice: Alice, cluster: Cluster) -> Router {#[derive(serde::Serialize, serde::Deserialize, Debug)]pub struct SaveStateParams {- pub state: String,+ pub state: EncodedEncryptedState,}pub async fn save_state(extract::State(state): extract::State<Arc<State>>,extract::Json(params): extract::Json<SaveStateParams>,) -> Result<impl IntoResponse, http::StatusCode> {- let encrypted_state = base64::decode(params.state).map_err(|e| {+ let encrypted_state = params.state.decode().map_err(|e| {info!("error decoding state: {}", e);http::StatusCode::BAD_REQUEST})?;- let encrypted_state: EncryptedState = bincode::deserialize(&encrypted_state).map_err(|e| {- info!("error deserializing state: {}", e);- http::StatusCode::BAD_REQUEST- })?;-let uuid = uuid::Uuid::new_v4();state.conv_states.lock().await.insert(uuid, encrypted_state);@@ -90,6 +85,7 @@ pub async fn save_state(#[serde(rename_all = "camelCase")]pub struct Params {pub message: String,+ pub optic: Option<String>,pub prev_state: Option<uuid::Uuid>,}@@ -131,22 +127,23 @@ pub async fn route(http::StatusCode::INTERNAL_SERVER_ERROR})?;- let mut executor = state+ let executor = state.alice.new_executor(¶ms.message,prev_state,format!("http://{}/beta/api/search", search_addr),+ params.optic,).map_err(|e| {info!("error creating executor: {}", e);http::StatusCode::INTERNAL_SERVER_ERROR})?;- let (tx, mut rx) = tokio::sync::mpsc::channel(1);+ let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();- tokio::task::spawn(async move {- while let Some(msg) = executor.next().await {+ tokio::task::spawn_blocking(move || {+ for msg in executor {let msg = serde_json::to_string(&msg).map_err(|e| {info!("error serializing message: {}", e);@@ -154,7 +151,7 @@ pub async fn route(}).unwrap();- tx.send(msg).await.ok();+ tx.send(msg).ok();}});diff --git a/frontend/src/components/Footer.astro b/frontend/src/components/Footer.astroindex 7182b77f..e27a5ad4 100644--- a/frontend/src/components/Footer.astro+++ b/frontend/src/components/Footer.astro@@ -1,3 +1,3 @@-<div class="flex w-full items-center justify-center py-10 text-sm">+<div class="flex w-full items-center justify-center pt-5 pb-10 text-sm"><a href="/privacy-and-happy-lawyers">Terms & Privacy</a></div>diff --git a/frontend/src/pages/chat.astro b/frontend/src/pages/chat.astroindex a31637fd..3bac841f 100644--- a/frontend/src/pages/chat.astro+++ b/frontend/src/pages/chat.astro@@ -5,6 +5,7 @@ import Layout from "../layouts/Layout.astro";import Hero from "../components/Hero.astro";import ChatBubble from "../components/ChatBubble.astro";import { askama } from "../askama";+import OpticSelector from "../components/OpticSelector.astro";---<Layout title="Stract">@@ -16,13 +17,16 @@ import { askama } from "../askama";<div class="max-w-3xl w-full gap-y-5 flex flex-col h-full"><div id="message-container" class="flex px-5 flex-col gap-y-3 h-full"></div>- <div class="flex w-full border rounded-xl p-2 pl-3">- <textarea id="message-input" autofocus class="grow resize-none outline-none focus:ring-0" style="max-height: 200px;" placeholder="Type a message..."></textarea>- <div class="flex place-items-end">- <div id="send-button" class="w-6 h-6 rotate-90 text-brand_contrast/50 hover:text-brand_contrast cursor-pointer">- <Hero icon="paper-airplane" />+ <div class="w-full gap-y-2">+ <div class="flex w-full border rounded-xl p-2 pl-3">+ <textarea id="message-input" autofocus class="grow resize-none outline-none focus:ring-0" style="max-height: 200px;" placeholder="Type a message..."></textarea>+ <div class="flex place-items-end">+ <div id="send-button" class="w-6 h-6 rotate-90 text-brand_contrast/50 hover:text-brand_contrast cursor-pointer">+ <Hero icon="paper-airplane" />+ </div></div></div>+ <div class="w-full pt-2 text-gray-600 flex justify-end text-sm"><OpticSelector default_optics="default_optics" search_on_change={false} /></div></div></div></div>@@ -85,6 +89,7 @@ import { askama } from "../askama";const sendButton: HTMLDivElement = document.querySelector("#send-button")!;const messageContainer: HTMLDivElement = document.querySelector("#message-container")!;const statusTemplate: HTMLDivElement = document.querySelector(".status-template")!;+ const opticSelector: HTMLSelectElement = document.querySelector("#optics-selector")!;// let prevState: number[][] | null = null;let prevState: any | null = null;@@ -216,10 +221,16 @@ import { askama } from "../askama";async function sendMessageToAlice(message: string) {beginAliceStream();+ let chosenOptic = opticSelector.value;+var reqData: Record<string, string> = {'message': message,};+ if (chosenOptic != "") {+ reqData['optic'] = chosenOptic;+ }+if (prevState) {let savedState = await save_prev_state();reqData['prevState'] = savedState;