Skip to content

web search done right

alice

Mikkel Denkerb16a1b9629d4
diff --git a/.vscode/settings.json b/.vscode/settings.json
index e642d593..e3ae355b 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -10,6 +10,7 @@
},
"rust-analyzer.linkedProjects": [
"Cargo.toml",
- "optics-lsp/Cargo.toml"
+ "optics-lsp/Cargo.toml",
+ "./core/Cargo.toml"
]
}
\ No newline at end of file
diff --git a/Cargo.lock b/Cargo.lock
index 80b6b388..92248fe0 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -8,6 +8,16 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
+[[package]]
+name = "aead"
+version = "0.5.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0"
+dependencies = [
+ "crypto-common",
+ "generic-array",
+]
+
[[package]]
name = "aes"
version = "0.7.5"
@@ -15,11 +25,36 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e8b47f52ea9bae42228d07ec09eb676433d7c4ed1ebdf0f1d1c29ed446f1ab8"
dependencies = [
"cfg-if",
- "cipher",
+ "cipher 0.3.0",
"cpufeatures",
"opaque-debug",
]
+[[package]]
+name = "aes"
+version = "0.8.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "433cfd6710c9986c576a25ca913c39d66a6474107b406f34f91d4a8923395241"
+dependencies = [
+ "cfg-if",
+ "cipher 0.4.4",
+ "cpufeatures",
+]
+
+[[package]]
+name = "aes-gcm"
+version = "0.10.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "209b47e8954a928e1d72e86eca7000ebb6655fe1436d33eefc2201cad027e237"
+dependencies = [
+ "aead",
+ "aes 0.8.2",
+ "cipher 0.4.4",
+ "ctr",
+ "ghash",
+ "subtle",
+]
+
[[package]]
name = "ahash"
version = "0.7.6"
@@ -606,6 +641,16 @@ dependencies = [
"generic-array",
]
+[[package]]
+name = "cipher"
+version = "0.4.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad"
+dependencies = [
+ "crypto-common",
+ "inout",
+]
+
[[package]]
name = "clang-sys"
version = "1.6.1"
@@ -891,6 +936,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
+ "rand_core 0.6.4",
"typenum",
]
@@ -942,6 +988,15 @@ dependencies = [
"memchr",
]
+[[package]]
+name = "ctr"
+version = "0.9.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835"
+dependencies = [
+ "cipher 0.4.4",
+]
+
[[package]]
name = "cxx"
version = "1.0.94"
@@ -1217,6 +1272,17 @@ dependencies = [
"cc",
]
+[[package]]
+name = "eventsource-stream"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "74fef4569247a5f429d9156b9d0a2599914385dd189c539334c625d8099d90ab"
+dependencies = [
+ "futures-core",
+ "nom",
+ "pin-project-lite",
+]
+
[[package]]
name = "exr"
version = "1.6.3"
@@ -1320,7 +1386,7 @@ dependencies = [
"futures-sink",
"nanorand",
"pin-project",
- "spin 0.9.8",
+ "spin",
]
[[package]]
@@ -1533,6 +1599,16 @@ dependencies = [
"wasm-bindgen",
]
+[[package]]
+name = "ghash"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d930750de5717d2dd0b8c0d42c076c0e884c81a73e6cab859bbd2339c71e3e40"
+dependencies = [
+ "opaque-debug",
+ "polyval",
+]
+
[[package]]
name = "gif"
version = "0.12.0"
@@ -1581,6 +1657,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b4af3693f1b705df946e9fe5631932443781d0aabb423b62fcd4d73f6d2fd0"
dependencies = [
"crunchy",
+ "serde",
]
[[package]]
@@ -1852,6 +1929,15 @@ dependencies = [
"regex",
]
+[[package]]
+name = "inout"
+version = "0.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5"
+dependencies = [
+ "generic-array",
+]
+
[[package]]
name = "instant"
version = "0.1.12"
@@ -2905,6 +2991,18 @@ dependencies = [
"miniz_oxide 0.7.1",
]
+[[package]]
+name = "polyval"
+version = "0.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7ef234e08c11dfcb2e56f79fd70f6f2eb7f025c0ce2333e82f4f0518ecad30c6"
+dependencies = [
+ "cfg-if",
+ "cpufeatures",
+ "opaque-debug",
+ "universal-hash",
+]
+
[[package]]
name = "ppv-lite86"
version = "0.2.17"
@@ -3248,21 +3346,6 @@ dependencies = [
"winreg",
]
-[[package]]
-name = "ring"
-version = "0.16.20"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc"
-dependencies = [
- "cc",
- "libc",
- "once_cell",
- "spin 0.5.2",
- "untrusted",
- "web-sys",
- "winapi",
-]
-
[[package]]
name = "rkyv"
version = "0.7.41"
@@ -3337,18 +3420,6 @@ dependencies = [
"windows-sys 0.48.0",
]
-[[package]]
-name = "rustls"
-version = "0.20.8"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "fff78fc74d175294f4e83b28343315ffcfb114b156f0185e9741cb5570f50e2f"
-dependencies = [
- "log",
- "ring",
- "sct",
- "webpki",
-]
-
[[package]]
name = "rustversion"
version = "1.0.12"
@@ -3361,6 +3432,16 @@ version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041"
+[[package]]
+name = "safetensors"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a1d818a2cb3f564a1844be835011acf5c7ec8ad1986a47f73abc7b5fea91cc3a"
+dependencies = [
+ "serde",
+ "serde_json",
+]
+
[[package]]
name = "same-file"
version = "1.0.6"
@@ -3397,16 +3478,6 @@ version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1792db035ce95be60c3f8853017b3999209281c24e2ba5bc8e59bf97a0c590c1"
-[[package]]
-name = "sct"
-version = "0.7.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d53dcdb7c9f8158937a7981b48accfd39a43af418591a5d008c7b22b5e1b7ca4"
-dependencies = [
- "ring",
- "untrusted",
-]
-
[[package]]
name = "scylla"
version = "0.7.0"
@@ -3692,12 +3763,6 @@ dependencies = [
"winapi",
]
-[[package]]
-name = "spin"
-version = "0.5.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
-
[[package]]
name = "spin"
version = "0.9.8"
@@ -3735,6 +3800,7 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
name = "stract"
version = "0.1.0"
dependencies = [
+ "aes-gcm",
"anyhow",
"askama",
"async-stream",
@@ -3755,9 +3821,11 @@ dependencies = [
"csv",
"dashmap",
"encoding_rs",
+ "eventsource-stream",
"flate2",
"fst",
"futures",
+ "half 2.2.1",
"html-escape",
"http",
"image",
@@ -3786,6 +3854,7 @@ dependencies = [
"rkyv",
"rocksdb",
"rust-stemmers",
+ "safetensors",
"scylla",
"serde",
"serde_json",
@@ -4000,15 +4069,16 @@ dependencies = [
[[package]]
name = "tch"
-version = "0.11.0"
+version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d3394fea57e43ef3708196025beb3445dc13998b162f972233f11b9fdf091401"
+checksum = "9cbd9ce6fb581a1b918db880b649d1364b50f7f6717eda8497bcdc929cddd4b9"
dependencies = [
"half 2.2.1",
"lazy_static",
"libc",
"ndarray",
"rand 0.8.5",
+ "safetensors",
"thiserror",
"torch-sys",
"zip",
@@ -4322,14 +4392,13 @@ dependencies = [
[[package]]
name = "torch-sys"
-version = "0.11.0"
+version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6ff928d3e632acda675e388d8e0efe526136350620800cc770e3727c9abc6ea1"
+checksum = "42b2b81a479510717464df1d07c02cb4aebb26539a39b5db6637dda114a476cb"
dependencies = [
"anyhow",
"cc",
"libc",
- "ureq",
"zip",
]
@@ -4536,25 +4605,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e"
[[package]]
-name = "untrusted"
-version = "0.7.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
-
-[[package]]
-name = "ureq"
-version = "2.6.2"
+name = "universal-hash"
+version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "338b31dd1314f68f3aabf3ed57ab922df95ffcd902476ca7ba3c4ce7b908c46d"
+checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea"
dependencies = [
- "base64 0.13.1",
- "flate2",
- "log",
- "once_cell",
- "rustls",
- "url",
- "webpki",
- "webpki-roots",
+ "crypto-common",
+ "subtle",
]
[[package]]
@@ -4753,25 +4810,6 @@ dependencies = [
"wasm-bindgen",
]
-[[package]]
-name = "webpki"
-version = "0.22.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f095d78192e208183081cc07bc5515ef55216397af48b873e5edcd72637fa1bd"
-dependencies = [
- "ring",
- "untrusted",
-]
-
-[[package]]
-name = "webpki-roots"
-version = "0.22.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b6c71e40d7d2c34a5106301fb632274ca37242cd0c9d3e64dbece371a40a2d87"
-dependencies = [
- "webpki",
-]
-
[[package]]
name = "weezl"
version = "0.1.7"
@@ -5017,7 +5055,7 @@ version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0445d0fbc924bb93539b4316c11afb121ea39296f99a3c4c9edad09e3658cdef"
dependencies = [
- "aes",
+ "aes 0.7.5",
"byteorder",
"bzip2",
"constant_time_eq",
diff --git a/Cargo.toml b/Cargo.toml
index 495aedb5..c61062a9 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,6 +12,7 @@ lto = "thin"
debug = true
[workspace.dependencies]
+aes-gcm = "0.10.2"
bincode = "1.3.3"
clap = { version = "3.1.18", features = ["derive"] }
encoding_rs = "0.8.31"
@@ -19,7 +20,9 @@ flate2 = "1.0.23"
indicatif = { version = "0.16.2", features = ["rayon"] }
itertools = "0.10.3"
rayon = "1.5.3"
-reqwest = { version = "0.11.16", features = ["blocking", "stream"] }
+half = { version = "2.2.1", features = ["serde"] }
+eventsource-stream = "0.2.3"
+reqwest = { version = "0.11.16", features = ["blocking", "stream", "json"] }
serde = { version = "1.0.137", features = ["rc", "derive"] }
serde_json = "1.0.81"
tantivy = "0.19.2"
@@ -76,7 +79,8 @@ chitchat = "0.5.0"
memmap2 = "0.5.10"
rkyv = "0.7.41"
dashmap = "5.4.0"
-tch = "0.11.0"
-torch-sys = "0.11.0"
+tch = "0.13.0"
+torch-sys = "0.13.0"
libc = "0.2.142"
-lz-str = "0.2.1"
\ No newline at end of file
+lz-str = "0.2.1"
+safetensors = "0.3.1"
\ No newline at end of file
diff --git a/configs/alice.toml b/configs/alice.toml
new file mode 100644
index 00000000..6bb64070
--- /dev/null
+++ b/configs/alice.toml
@@ -0,0 +1,16 @@
+cluster_id = "dev_alice"
+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="
+
+[accelerator]
+layer_fraction = 1.0
+
+[accelerator.device]
+type = "mps"
+
+[accelerator.dtype]
+type = "float"
\ No newline at end of file
diff --git a/configs/frontend.toml b/configs/frontend.toml
index c263b2a9..ca40cdf7 100644
--- a/configs/frontend.toml
+++ b/configs/frontend.toml
@@ -2,8 +2,8 @@ cluster_id = "dev_frontend"
queries_csv_path = "data/queries_us.csv"
host = "0.0.0.0:3000"
prometheus_host = "0.0.0.0:3001"
-gossip_addr = "0.0.0.0:3004"
-gossip_seed_nodes = ["0.0.0.0:3005", "0.0.0.0:3006"]
+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"
# lambda_model_path = "data/lambdamart.txt"
# qa_model_path = "data/qa_model"
diff --git a/configs/search_server.toml b/configs/search_server.toml
index 4428732e..a86d144d 100644
--- a/configs/search_server.toml
+++ b/configs/search_server.toml
@@ -1,5 +1,6 @@
cluster_id = "dev_search"
-gossip_addr = "0.0.0.0:3005"
+gossip_addr = "0.0.0.0:3006"
+gossip_seed_nodes = ["0.0.0.0:3005"]
shard_id = 0
host = "0.0.0.0:3002"
index_path = "data/index"
diff --git a/configs/webgraph/server.toml b/configs/webgraph/server.toml
index 472ae19b..dd220804 100644
--- a/configs/webgraph/server.toml
+++ b/configs/webgraph/server.toml
@@ -1,5 +1,5 @@
host = "0.0.0.0:3003"
cluster_id = "dev_webgraph"
-gossip_addr = "0.0.0.0:3006"
+gossip_addr = "0.0.0.0:3007"
graph_path = "data/webgraph"
inbound_similarity_path = "data/centrality/inbound_similarity"
\ No newline at end of file
diff --git a/core/Cargo.toml b/core/Cargo.toml
index 5db6539e..85232e4c 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -81,6 +81,11 @@ tch = { workspace = true }
torch-sys = { workspace = true }
libc = { workspace = true }
lz-str = { workspace = true }
+safetensors = { workspace = true }
+eventsource-stream = { workspace = true }
+rand = { workspace = true }
+half = { workspace = true }
+aes-gcm = { workspace = true }
[build-dependencies]
lalrpop = { workspace = true }
@@ -88,7 +93,6 @@ lalrpop = { workspace = true }
[dev-dependencies]
criterion = { workspace = true }
maplit = { workspace = true }
-rand = { workspace = true }
[[bench]]
name = "search-preindexed"
diff --git a/core/benches/abstractive-summarizer.rs b/core/benches/abstractive-summarizer.rs
index 34cc354e..3ce01c74 100644
--- a/core/benches/abstractive-summarizer.rs
+++ b/core/benches/abstractive-summarizer.rs
@@ -1,5 +1,5 @@
use criterion::{criterion_group, criterion_main, Criterion};
-use stract::summarizer::{AbstractiveModel, AbstractiveSummarizer, GenerationConfig};
+use stract::summarizer::{AbstractiveModel, AbstractiveSummarizer};
pub fn criterion_benchmark(c: &mut Criterion) {
let model =
@@ -11,15 +11,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
Aristotle's views profoundly shaped medieval scholarship. The influence of physical science extended from Late Antiquity and the Early Middle Ages into the Renaissance, and were not replaced systematically until the Enlightenment and theories such as classical mechanics were developed. Some of Aristotle's zoological observations found in his biology, such as on the hectocotyl (reproductive) arm of the octopus, were disbelieved until the 19th century. He also influenced Judeo-Islamic philosophies during the Middle Ages, as well as Christian theology, especially the Neoplatonism of the Early Church and the scholastic tradition of the Catholic Church. Aristotle was revered among medieval Muslim scholars as "The First Teacher", and among medieval Christians like Thomas Aquinas as simply "The Philosopher", while the poet Dante called him "the master of those who know". His works contain the earliest known formal study of logic, and were studied by medieval scholars such as Peter Abelard and John Buridan. Aristotle's influence on logic continued well into the 19th century. In addition, his ethics, though always influential, gained renewed interest with the modern advent of virtue ethics."#;
c.bench_function("Summarize Aristotles wiki text", |b| {
- b.iter(|| {
- model.summarize(
- text,
- GenerationConfig {
- num_beams: 10,
- ..Default::default()
- },
- )
- })
+ b.iter(|| model.summarize(text))
});
}
diff --git a/core/examples/alice.rs b/core/examples/alice.rs
new file mode 100644
index 00000000..d2919483
--- /dev/null
+++ b/core/examples/alice.rs
@@ -0,0 +1,64 @@
+use std::io::Write;
+
+use stract::alice::{AcceleratorConfig, Alice, ExecutionState};
+
+#[tokio::main]
+async fn main() {
+ let subscriber = tracing_subscriber::FmtSubscriber::builder()
+ .with_max_level(tracing::Level::DEBUG)
+ .finish();
+ tracing::subscriber::set_global_default(subscriber).unwrap();
+
+ // dont use this in production
+ let key = base64::decode("URyJQTjwUjTq6FSRoZGdbUdTIvqs/QxkPacQio8Lhxc=").unwrap();
+
+ let model = Alice::open(
+ "data/alice",
+ "data/summarizer/dual_encoder",
+ Some(AcceleratorConfig {
+ layer_fraction: 1.0,
+ device: tch::Device::Mps,
+ kind: tch::Kind::Float,
+ }),
+ &key,
+ )
+ .unwrap();
+
+ tracing::debug!("model loaded");
+
+ let mut last_state = None;
+
+ loop {
+ println!();
+ print!("> ");
+ std::io::stdout().flush().unwrap();
+ let mut input = String::new();
+ std::io::stdin().read_line(&mut input).unwrap();
+ let input = input.trim();
+
+ let mut gen = model
+ .new_executor(
+ input,
+ last_state.clone(),
+ "http://localhost:3000/beta/api/search".to_string(),
+ )
+ .unwrap();
+
+ while let Some(n) = gen.next().await {
+ match n {
+ ExecutionState::BeginSearch { query: _ } => {}
+ ExecutionState::SearchResult {
+ query: _,
+ result: _,
+ } => {}
+ ExecutionState::Speaking { text } => {
+ print!("{}", text);
+ std::io::stdout().flush().unwrap();
+ }
+ ExecutionState::Done { state } => {
+ last_state = Some(state.decode().unwrap());
+ }
+ }
+ }
+ }
+}
diff --git a/core/src/alice/mod.rs b/core/src/alice/mod.rs
new file mode 100644
index 00000000..51c26424
--- /dev/null
+++ b/core/src/alice/mod.rs
@@ -0,0 +1,1601 @@
+// 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::{
+ fs::OpenOptions,
+ 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 safetensors::SafeTensors;
+use tch::{
+ nn::{embedding, layer_norm, Embedding, LayerNorm, LayerNormConfig, Linear, ModuleT, VarStore},
+ IndexOp, Kind, Tensor,
+};
+
+use crate::{
+ api::search::ApiSearchQuery,
+ leaky_queue::LeakyQueue,
+ llm_utils::{self, ClonableTensor},
+ search_prettifier::DisplayedWebpage,
+ searcher::{SearchResult, WebsitesResult},
+ summarizer::{self, ExtractiveSummarizer},
+ webpage::Url,
+ AcceleratorDevice, AcceleratorDtype, AliceAcceleratorConfig,
+};
+
+const NUM_TOKENS: i64 = 50277;
+
+const TAU: f64 = 0.8;
+const TEMP: f64 = 0.4;
+
+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|>"#;
+
+type Result<T> = std::result::Result<T, Error>;
+
+#[derive(thiserror::Error, Debug)]
+pub enum Error {
+ #[error("IO error: {0}")]
+ Io(#[from] std::io::Error),
+
+ #[error("Torch error: {0}")]
+ Torch(#[from] tch::TchError),
+
+ #[error("SafeTensors error: {0}")]
+ SafeTensors(#[from] safetensors::SafeTensorError),
+
+ #[error("Tokenizers error: {0}")]
+ Tokenizers(#[from] tokenizers::Error),
+
+ #[error("Empty input")]
+ EmptyInput,
+
+ #[error("Reqwest error: {0}")]
+ Reqwest(#[from] reqwest::Error),
+
+ #[error("Unexpected search result")]
+ UnexpectedSearchResult,
+
+ #[error("Summarizer: {0}")]
+ Summarizer(#[from] summarizer::Error),
+
+ #[error("Bincode: {0}")]
+ Bincode(#[from] bincode::Error),
+
+ #[error("Base64: {0}")]
+ Base64(#[from] base64::DecodeError),
+
+ #[error("Failed to decrypt")]
+ DecryptionFailed,
+
+ #[error("Cluster")]
+ Cluster(#[from] crate::distributed::cluster::Error),
+}
+
+fn load_linear(weights: &SafeTensors, prefix: &str) -> Result<Linear> {
+ let ws = weights.tensor(&format!("{prefix}.weight"))?.try_into()?;
+
+ let mut linear = Linear { ws, bs: None };
+
+ if let Ok(bias_tensor) = weights.tensor(&format!("{prefix}.bias")) {
+ linear.bs = Some(bias_tensor.try_into()?);
+ }
+
+ if let Some(bs) = &mut linear.bs {
+ *bs = bs.to_kind(Kind::Float);
+ }
+
+ linear.ws = linear.ws.to_kind(Kind::Float);
+
+ Ok(linear)
+}
+
+fn load_emb(weights: &SafeTensors, prefix: &str, emb_size: i64) -> Result<Embedding> {
+ let vars = VarStore::new(tch::Device::Cpu);
+
+ let ws = weights.tensor(&format!("{prefix}.weight"))?;
+ let ws: Tensor = ws.try_into()?;
+
+ vars.variables_
+ .lock()
+ .unwrap()
+ .named_variables
+ .insert("weight".to_string(), ws.shallow_clone());
+
+ let config = tch::nn::EmbeddingConfig::default();
+
+ let mut emb = embedding(vars.root(), NUM_TOKENS, emb_size, config);
+
+ emb.ws = ws.to_kind(Kind::Float);
+
+ Ok(emb)
+}
+
+fn load_ln(weights: &SafeTensors, prefix: &str, emb_size: i64) -> Result<LayerNorm> {
+ let vars = VarStore::new(tch::Device::Cpu);
+
+ if let Ok(ws) = weights.tensor(&format!("{prefix}.weight")) {
+ vars.variables_
+ .lock()
+ .unwrap()
+ .named_variables
+ .insert("weight".to_string(), ws.try_into()?);
+ }
+
+ if let Ok(bs) = weights.tensor(&format!("{prefix}.bias")) {
+ vars.variables_
+ .lock()
+ .unwrap()
+ .named_variables
+ .insert("bias".to_string(), bs.try_into()?);
+ }
+
+ let config = LayerNormConfig::default();
+
+ let mut ln = layer_norm(vars.root(), vec![emb_size], config);
+
+ if let Ok(ws) = weights.tensor(&format!("{prefix}.weight")) {
+ let ws: Tensor = ws.try_into()?;
+ ln.ws = Some(ws.to_kind(Kind::Float));
+ }
+
+ if let Ok(bs) = weights.tensor(&format!("{prefix}.bias")) {
+ let bs: Tensor = bs.try_into()?;
+ ln.bs = Some(bs.to_kind(Kind::Float));
+ }
+
+ ln.normalized_shape = vec![emb_size];
+
+ Ok(ln)
+}
+
+struct TimeMix {
+ time_decay: Tensor,
+ time_first: Tensor,
+
+ time_mix_k: Tensor,
+ time_mix_v: Tensor,
+ time_mix_r: Tensor,
+
+ key: Linear,
+ value: Linear,
+ receptance: Linear,
+ output: Linear,
+
+ block_idx: i64,
+}
+impl TimeMix {
+ fn forward(&self, x: &Tensor, state: &mut Tensor) -> Tensor {
+ let x = x.squeeze();
+ let xk = &x * &self.time_mix_k + state.i(5 * self.block_idx + 1) * (1 - &self.time_mix_k);
+ let xv = &x * &self.time_mix_v + state.i(5 * self.block_idx + 1) * (1 - &self.time_mix_v);
+ let xr = &x * &self.time_mix_r + state.i(5 * self.block_idx + 1) * (1 - &self.time_mix_r);
+
+ state.i(5 * self.block_idx + 1).copy_(&x);
+
+ let r = self.receptance.forward_t(&xr, false).sigmoid();
+ let k = self.key.forward_t(&xk, false);
+ let v = self.value.forward_t(&xv, false);
+
+ let aa = state.i(5 * self.block_idx + 2);
+ let bb = state.i(5 * self.block_idx + 3);
+ let pp = state.i(5 * self.block_idx + 4);
+
+ let ww = &self.time_first + &k;
+ let qq = pp.maximum(&ww);
+ let e1 = (&pp - &qq).exp();
+ let e2 = (ww - &qq).exp();
+
+ let a = &e1 * &aa + &e2 * &v;
+ let b = &e1 * &bb + &e2;
+ let wkv = a / b;
+ let ww = pp - self.time_decay.exp();
+ let qq = ww.maximum(&k);
+ let e1 = (ww - &qq).exp();
+ let e2 = (k - &qq).exp();
+
+ state
+ .i(5 * self.block_idx + 2)
+ .copy_(&(&e1 * &aa + &e2 * &v).squeeze());
+ state
+ .i(5 * self.block_idx + 3)
+ .copy_(&(&e1 * &bb + &e2).squeeze());
+ state.i(5 * self.block_idx + 4).copy_(&qq.squeeze());
+
+ self.output.forward_t(&(wkv * &r), false)
+ }
+
+ fn load_to_device(&mut self, device: tch::Device, kind: tch::Kind) {
+ self.time_decay = self.time_decay.to_kind(kind).to(device);
+ self.time_first = self.time_first.to_kind(kind).to(device);
+
+ self.time_mix_k = self.time_mix_k.to_kind(kind).to(device);
+ self.time_mix_v = self.time_mix_v.to_kind(kind).to(device);
+ self.time_mix_r = self.time_mix_r.to_kind(kind).to(device);
+
+ self.key.ws = self.key.ws.to_kind(kind).to(device);
+ self.key.bs = self.key.bs.as_ref().map(|t| t.to_kind(kind).to(device));
+
+ self.value.ws = self.value.ws.to_kind(kind).to(device);
+ self.value.bs = self.value.bs.as_ref().map(|t| t.to_kind(kind).to(device));
+
+ self.receptance.ws = self.receptance.ws.to_kind(kind).to(device);
+ self.receptance.bs = self
+ .receptance
+ .bs
+ .as_ref()
+ .map(|t| t.to_kind(kind).to(device));
+
+ self.output.ws = self.output.ws.to_kind(kind).to(device);
+ self.output.bs = self.output.bs.as_ref().map(|t| t.to_kind(kind).to(device));
+ }
+}
+
+struct ChannelMix {
+ time_mix_k: Tensor,
+ time_mix_r: Tensor,
+
+ key: Linear,
+ value: Linear,
+
+ receptance: Linear,
+
+ block_idx: i64,
+}
+impl ChannelMix {
+ fn forward(&self, x: &Tensor, state: &mut Tensor) -> Tensor {
+ let x = x.squeeze();
+ let xk = &x * &self.time_mix_k + state.i(5 * self.block_idx) * (1 - &self.time_mix_k);
+ let xr = &x * &self.time_mix_r + state.i(5 * self.block_idx) * (1 - &self.time_mix_r);
+
+ state.i(5 * self.block_idx).copy_(&x);
+
+ let r = self.receptance.forward_t(&xr, false).sigmoid();
+ let k = self.key.forward_t(&xk, false).relu().square();
+
+ r * self.value.forward_t(&k, false)
+ }
+
+ fn load_to_device(&mut self, device: tch::Device, kind: tch::Kind) {
+ self.time_mix_k = self.time_mix_k.to_kind(kind).to(device);
+ self.time_mix_r = self.time_mix_r.to_kind(kind).to(device);
+
+ self.key.ws = self.key.ws.to_kind(kind).to(device);
+ self.key.bs = self.key.bs.as_ref().map(|t| t.to_kind(kind).to(device));
+
+ self.value.ws = self.value.ws.to_kind(kind).to(device);
+ self.value.bs = self.value.bs.as_ref().map(|t| t.to_kind(kind).to(device));
+
+ self.receptance.ws = self.receptance.ws.to_kind(kind).to(device);
+ self.receptance.bs = self
+ .receptance
+ .bs
+ .as_ref()
+ .map(|t| t.to_kind(kind).to(device));
+ }
+}
+
+struct Block {
+ ln0: Option<LayerNorm>,
+ ln1: LayerNorm,
+ ln2: LayerNorm,
+
+ att: TimeMix,
+ ffn: ChannelMix,
+
+ device: tch::Device,
+}
+
+impl Block {
+ fn load(weights: &SafeTensors, prefix: &str, emb_size: i64, block_idx: i64) -> Result<Self> {
+ let mut ln0 = None;
+
+ let names = weights.names();
+
+ if names.contains(&&format!("{prefix}.ln0.weight")) {
+ ln0 = Some(load_ln(weights, &format!("{prefix}.ln0"), emb_size)?);
+ }
+
+ let ln1 = load_ln(weights, &format!("{prefix}.ln1"), emb_size)?;
+ let ln2 = load_ln(weights, &format!("{prefix}.ln2"), emb_size)?;
+
+ let att = TimeMix {
+ time_decay: weights
+ .tensor(&format!("{prefix}.att.time_decay"))?
+ .try_into()?,
+ time_first: weights
+ .tensor(&format!("{prefix}.att.time_first"))?
+ .try_into()?,
+ time_mix_k: weights
+ .tensor(&format!("{prefix}.att.time_mix_k"))?
+ .try_into()?,
+ time_mix_v: weights
+ .tensor(&format!("{prefix}.att.time_mix_v"))?
+ .try_into()?,
+ time_mix_r: weights
+ .tensor(&format!("{prefix}.att.time_mix_r"))?
+ .try_into()?,
+ key: load_linear(weights, &format!("{prefix}.att.key"))?,
+ value: load_linear(weights, &format!("{prefix}.att.value"))?,
+ receptance: load_linear(weights, &format!("{prefix}.att.receptance"))?,
+ output: load_linear(weights, &format!("{prefix}.att.output"))?,
+ block_idx,
+ };
+
+ let ffn = ChannelMix {
+ time_mix_k: weights
+ .tensor(&format!("{prefix}.ffn.time_mix_k"))?
+ .try_into()?,
+ time_mix_r: weights
+ .tensor(&format!("{prefix}.ffn.time_mix_r"))?
+ .try_into()?,
+ key: load_linear(weights, &format!("{prefix}.ffn.key"))?,
+ value: load_linear(weights, &format!("{prefix}.ffn.value"))?,
+ receptance: load_linear(weights, &format!("{prefix}.ffn.receptance"))?,
+ block_idx,
+ };
+
+ Ok(Self {
+ ln0,
+ ln1,
+ ln2,
+ att,
+ ffn,
+ device: tch::Device::Cpu,
+ })
+ }
+
+ fn forward(&self, mut x: Tensor, mut state: Tensor) -> (Tensor, Tensor) {
+ if x.device() != self.device {
+ x = x.to(self.device);
+ }
+
+ if state.device() != self.device {
+ state = state.to(self.device);
+ }
+
+ x = match &self.ln0 {
+ Some(ln) => ln.forward_t(&x, false),
+ None => x,
+ };
+
+ x += self
+ .att
+ .forward(&self.ln1.forward_t(&x, false), &mut state)
+ .reshape([1, -1]);
+ x += self
+ .ffn
+ .forward(&self.ln2.forward_t(&x, false), &mut state)
+ .reshape([1, -1]);
+
+ (x, state)
+ }
+
+ fn load_to_device(&mut self, device: tch::Device, kind: tch::Kind) {
+ self.device = device;
+
+ if let Some(ln) = &mut self.ln0 {
+ ln.ws = ln.ws.as_ref().map(|t| t.to_kind(kind).to(device));
+ ln.bs = ln.bs.as_ref().map(|t| t.to_kind(kind).to(device));
+ }
+
+ self.att.load_to_device(device, kind);
+ self.ffn.load_to_device(device, kind);
+
+ self.ln1.ws = self.ln1.ws.as_ref().map(|t| t.to_kind(kind).to(device));
+ self.ln1.bs = self.ln1.bs.as_ref().map(|t| t.to_kind(kind).to(device));
+
+ self.ln2.ws = self.ln2.ws.as_ref().map(|t| t.to_kind(kind).to(device));
+ self.ln2.bs = self.ln2.bs.as_ref().map(|t| t.to_kind(kind).to(device));
+ }
+}
+
+struct RawModel {
+ emb: Embedding,
+ blocks: Vec<Block>,
+ ln_out: LayerNorm,
+ head: Linear,
+}
+
+impl RawModel {
+ fn open<P: AsRef<Path>>(path: P) -> Result<RawModel> {
+ // SAFETY
+ // broadcasting: please don't modify the file while we're reading it.
+ let mmap = unsafe {
+ let file = OpenOptions::new()
+ .read(true)
+ .write(true)
+ .create(true)
+ .open(path)
+ .map_err(Error::Io)?;
+
+ memmap2::MmapOptions::new()
+ .map_mut(&file)
+ .map_err(Error::Io)?
+ };
+
+ let weights = SafeTensors::deserialize(&mmap)?;
+ let names = weights.names();
+
+ let num_layers = names
+ .iter()
+ .filter(|name| name.starts_with("blocks"))
+ .map(|name| name.split('.').nth(1).unwrap().parse::<usize>().unwrap())
+ .max()
+ .unwrap()
+ + 1;
+
+ let emb = weights.tensor("emb.weight")?;
+ let emb_size = emb.shape()[1] as i64;
+
+ let emb = load_emb(&weights, "emb", emb_size)?;
+
+ let mut blocks = Vec::with_capacity(num_layers);
+ for layer in 0..num_layers {
+ let prefix = format!("blocks.{}", layer);
+ blocks.push(Block::load(&weights, &prefix, emb_size, layer as i64)?);
+ }
+
+ Ok(Self {
+ emb,
+ blocks,
+ ln_out: load_ln(&weights, "ln_out", emb_size)?,
+ head: load_linear(&weights, "head")?,
+ })
+ }
+
+ fn load_to_device(&mut self, layer_fraction: f64, device: tch::Device, kind: tch::Kind) {
+ let layer_fraction = layer_fraction.max(0.0).min(1.0);
+
+ self.emb.ws = self.emb.ws.to_kind(kind).to(device);
+
+ let layers_to_move = (self.blocks.len() as f64 * layer_fraction).ceil() as usize;
+
+ for block in self.blocks.iter_mut().take(layers_to_move) {
+ block.load_to_device(device, kind);
+ }
+
+ if layer_fraction == 1.0 {
+ self.ln_out.ws = self.ln_out.ws.as_ref().map(|t| t.to_kind(kind).to(device));
+ self.ln_out.bs = self.ln_out.bs.as_ref().map(|t| t.to_kind(kind).to(device));
+
+ self.head.ws = self.head.ws.to_kind(kind).to(device);
+ self.head.bs = self.head.bs.as_ref().map(|t| t.to_kind(kind).to(device));
+ }
+ }
+
+ fn init_state(&self) -> Tensor {
+ let t = Tensor::zeros(
+ [self.blocks.len() as i64 * 5, self.emb.ws.size()[1]],
+ (tch::Kind::Float, tch::Device::Cpu),
+ );
+
+ for i in 0..self.blocks.len() as i64 {
+ t.i(5 * i + 4).copy_(&Tensor::from_slice(&[-1e30f32]))
+ }
+
+ t
+ }
+
+ fn forward(&self, token: i64, state: Option<&Tensor>) -> (Tensor, Tensor) {
+ tch::no_grad(|| self.forward_grad(token, state))
+ }
+
+ fn forward_grad(&self, token: i64, state: Option<&Tensor>) -> (Tensor, Tensor) {
+ let mut tokens = Tensor::from_slice(&[token]);
+
+ if self.emb.ws.device() != tokens.device() {
+ tokens = tokens.to(self.emb.ws.device());
+ }
+
+ let mut x = self.emb.forward_t(&tokens, false);
+
+ let mut state = match state {
+ Some(state) => state.shallow_clone(),
+ None => self.init_state(),
+ };
+
+ for block in &self.blocks {
+ (x, state) = block.forward(x, state);
+ }
+
+ x = self.ln_out.forward_t(&x, false);
+ x = self.head.forward_t(&x, false);
+
+ (x, state)
+ }
+}
+
+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,
+ ) -> 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,
+ summarizer: Arc::clone(&self.summarizer),
+ };
+
+ Ok(ActionExecutor::new(
+ action_gen,
+ searcher,
+ self.encryption_key,
+ ))
+ }
+}
+
+pub enum TokenGeneratorState {
+ InProgress,
+ Finished,
+}
+
+#[derive(Clone)]
+struct ModelState {
+ state: ClonableTensor,
+ next_token: i64,
+}
+
+pub struct AliceTokenGenerator {
+ model: Arc<RawModel>,
+ // state needs to be a queue of size 2 to have the ability
+ // to generate a new token if the current one is banned
+ states: LeakyQueue<ModelState>,
+ // the state of the model after each search result has been
+ // loaded. This allows us to go back to the end of the previous search
+ // and force speak if the model wants us to search for the same query again.
+ end_search_states: Vec<LeakyQueue<ModelState>>,
+ end_tokens: Vec<i64>,
+ generation_state: TokenGeneratorState,
+ max_new_tokens: Option<usize>,
+ num_generated_tokens: usize,
+ banned_tokens: Vec<i64>,
+}
+
+impl AliceTokenGenerator {
+ fn new(
+ model: Arc<RawModel>,
+ end_tokens: Vec<i64>,
+ state: Tensor,
+ tokens: &[i64],
+ max_new_tokens: Option<usize>,
+ ) -> Result<Self> {
+ if tokens.is_empty() {
+ return Err(Error::EmptyInput);
+ }
+
+ // this must be 2, otherwise `.front()` and `.back()` calls are wrong throughout the generation code
+ let num_states = 2;
+
+ let mut states = LeakyQueue::new(num_states);
+
+ let state = ClonableTensor(state);
+
+ for _ in 0..num_states {
+ states.push(ModelState {
+ state: state.clone(),
+ next_token: -1,
+ });
+ }
+
+ let mut generator = Self {
+ model,
+ states,
+ end_tokens,
+ generation_state: TokenGeneratorState::InProgress,
+ max_new_tokens,
+ num_generated_tokens: 0,
+ end_search_states: Vec::new(),
+ banned_tokens: Vec::new(),
+ };
+ generator.load_tokens(tokens);
+
+ Ok(generator)
+ }
+
+ pub fn load_tokens(&mut self, tokens: &[i64]) {
+ for token in tokens {
+ let (logits, new_state) = self
+ .model
+ .forward(*token, Some(&self.states.back().unwrap().state.0));
+ let probs = logits
+ .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),
+ next_token,
+ });
+
+ self.states.front_mut().unwrap().next_token = *token;
+ }
+ }
+
+ fn load_search_result(&mut self, tokens: &[i64]) {
+ self.load_tokens(tokens);
+ self.end_search_states.push(self.states.clone());
+ }
+
+ fn go_to_last_search(&mut self) {
+ if let Some(last_search) = self.end_search_states.pop() {
+ self.states = last_search;
+ }
+ }
+
+ pub fn set_end_tokens(&mut self, tokens: &[i64]) {
+ if self.end_tokens != tokens {
+ self.end_tokens = tokens.to_vec();
+ }
+ }
+
+ pub fn set_max_new_tokens(&mut self, max_new_tokens: Option<usize>) {
+ if self.max_new_tokens != max_new_tokens {
+ self.num_generated_tokens = 0;
+ self.max_new_tokens = max_new_tokens;
+ }
+ }
+
+ pub fn set_banned_tokens(&mut self, tokens: &[i64]) {
+ if self.banned_tokens != tokens {
+ self.banned_tokens = tokens.to_vec();
+ }
+ }
+}
+
+impl Iterator for AliceTokenGenerator {
+ type Item = i64;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ match self.generation_state {
+ TokenGeneratorState::InProgress => {
+ let s = self.states.back().unwrap();
+ let mut token = s.next_token;
+ let mut state = s.state.clone();
+
+ if self.end_tokens.contains(&token) {
+ self.generation_state = TokenGeneratorState::Finished;
+ return None;
+ }
+
+ if let Some(max_new_tokens) = self.max_new_tokens {
+ if self.num_generated_tokens >= max_new_tokens {
+ if let Some(tok) = self.end_tokens.first() {
+ token = *tok;
+ }
+
+ self.generation_state = TokenGeneratorState::Finished;
+ }
+ }
+
+ if self.banned_tokens.contains(&token) {
+ let (output, new_state) = {
+ let s = self.states.front().unwrap();
+ self.model.forward(s.next_token, Some(&s.state.0))
+ };
+
+ let output = output.squeeze();
+
+ let probs = output
+ .softmax(-1, Kind::Float)
+ .squeeze()
+ .to(tch::Device::Cpu);
+
+ for banned_token in &self.banned_tokens {
+ probs
+ .i(*banned_token)
+ .copy_(&Tensor::from_slice(&[0.0]).squeeze());
+ }
+
+ let next_token = llm_utils::sample_typical(probs, TEMP, TAU);
+ token = next_token;
+ state = ClonableTensor(new_state);
+ }
+
+ let (output, state) = self.model.forward(token, Some(&state.0));
+ let output = output.squeeze();
+
+ let probs = output
+ .softmax(-1, Kind::Float)
+ .squeeze()
+ .to(tch::Device::Cpu);
+
+ for banned_token in &self.banned_tokens {
+ probs
+ .i(*banned_token)
+ .copy_(&Tensor::from_slice(&[0.0]).squeeze());
+ }
+
+ let next_token = llm_utils::sample_typical(probs, TEMP, TAU);
+ self.states.push(ModelState {
+ state: ClonableTensor(state),
+ next_token,
+ });
+
+ self.num_generated_tokens += 1;
+
+ Some(token)
+ }
+ TokenGeneratorState::Finished => None,
+ }
+ }
+}
+
+pub struct AliceStringGenerator {
+ token_generator: AliceTokenGenerator,
+ tokenizer: Arc<Tokenizer>,
+ tokens: Vec<i64>,
+}
+
+impl Iterator for AliceStringGenerator {
+ type Item = String;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ loop {
+ match self.token_generator.next() {
+ Some(token) => {
+ self.tokens.push(token);
+ if let Some(s) = self.tokenizer.decode(&self.tokens).ok().and_then(|s| {
+ if !s.contains('\u{fffd}') {
+ // valid utf-8 string
+ Some(s)
+ } else {
+ None
+ }
+ }) {
+ self.tokens.clear();
+ return Some(s);
+ }
+ }
+ None if !self.tokens.is_empty() => {
+ let s = self.tokenizer.decode(&self.tokens).ok().and_then(|s| {
+ if !s.contains('\u{fffd}') {
+ Some(s)
+ } else {
+ None
+ }
+ });
+
+ self.tokens.clear();
+
+ return s;
+ }
+ None => return None,
+ }
+ }
+ }
+}
+
+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)]
+pub enum RawAction {
+ Search { query: Vec<i64> },
+ Speak { token: i64 },
+}
+
+enum State {
+ Thought {
+ search: TransitionValidator,
+ speaking: TransitionValidator,
+ },
+ QueryBuild {
+ tokens: Vec<i64>,
+ end: AnyTransitionValidator,
+ },
+ Speaking {
+ banned_tokens: TTLBannedTokens,
+ end: AnyTransitionValidator,
+ max_new_tokens: Option<usize>,
+ },
+}
+
+struct TTLBannedTokens {
+ tokens: Vec<i64>,
+ orig_ttl: usize,
+ ttl: usize,
+}
+
+impl TTLBannedTokens {
+ fn new(tokens: Vec<i64>, ttl: usize) -> Self {
+ Self {
+ tokens,
+ ttl,
+ orig_ttl: ttl,
+ }
+ }
+
+ fn reset_ttl(&mut self) {
+ self.ttl = self.orig_ttl;
+ }
+
+ fn tick(&mut self) {
+ if self.ttl > 0 {
+ self.ttl -= 1;
+ }
+ }
+
+ fn set_banned_tokens(&self, token_generator: &mut AliceTokenGenerator) {
+ if self.ttl > 0 {
+ token_generator.set_banned_tokens(&self.tokens);
+ } else {
+ token_generator.set_banned_tokens(&[]);
+ }
+ }
+}
+
+impl State {
+ fn new_thought(tokenizer: &Tokenizer) -> Self {
+ let search = TransitionValidator::new(tokenizer.encode("Search:".to_string()).unwrap());
+ let speaking = TransitionValidator::new(tokenizer.encode("Alice:".to_string()).unwrap());
+
+ Self::Thought { search, speaking }
+ }
+
+ fn new_query_build(tokenizer: &Tokenizer) -> Self {
+ let new_line = TransitionValidator::new(tokenizer.encode("\n".to_string()).unwrap());
+ let end_of_text =
+ TransitionValidator::new(tokenizer.encode("<|endoftext|>".to_string()).unwrap());
+ let end = AnyTransitionValidator::new(vec![new_line, end_of_text]);
+
+ Self::QueryBuild {
+ tokens: Vec::new(),
+ end,
+ }
+ }
+
+ fn new_speaking(initially_banned_tokens: Vec<i64>) -> Self {
+ let end = TransitionValidator::new(vec![0]);
+ let double_newline = TransitionValidator::new(vec![187, 187]);
+ let double_newline_single_tok = TransitionValidator::new(vec![535]);
+
+ let end = AnyTransitionValidator::new(vec![end, double_newline, double_newline_single_tok]);
+
+ Self::Speaking {
+ end,
+ max_new_tokens: Some(1024),
+ banned_tokens: TTLBannedTokens::new(initially_banned_tokens, 2), // the tokens should only be banned at the start of generation
+ }
+ }
+}
+
+pub struct RawActionGenerator {
+ state: State,
+ tokenizer: Arc<Tokenizer>,
+ token_generator: AliceTokenGenerator,
+ queries_performed: usize,
+ max_num_queries: usize,
+}
+
+impl RawActionGenerator {
+ fn new(token_generator: AliceTokenGenerator, tokenizer: Arc<Tokenizer>) -> Self {
+ let state = State::new_thought(&tokenizer);
+
+ let mut token_generator = token_generator;
+ token_generator.set_end_tokens(&[]);
+
+ Self {
+ state,
+ tokenizer,
+ token_generator,
+ queries_performed: 0,
+ max_num_queries: 3,
+ }
+ }
+
+ fn force_speaking(&mut self) {
+ self.token_generator.load_tokens(
+ &self
+ .tokenizer
+ .encode("Alice[thought]: I now know the final answer.\n\n".to_string())
+ .unwrap(),
+ );
+
+ self.state = State::new_speaking(vec![
+ self.tokenizer.encode(" [thought]:".to_string()).unwrap()[0],
+ self.tokenizer.encode("[thought]:".to_string()).unwrap()[0],
+ ]);
+
+ self.token_generator
+ .load_tokens(&self.tokenizer.encode("Alice:".to_string()).unwrap());
+ }
+}
+
+impl Iterator for RawActionGenerator {
+ type Item = RawAction;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ loop {
+ match &mut self.state {
+ State::Thought { search, speaking } => {
+ let token = self.token_generator.next()?;
+ self.token_generator.set_max_new_tokens(None);
+
+ if self.queries_performed >= self.max_num_queries {
+ self.force_speaking();
+ } else if search.validate(token) {
+ self.state = State::new_query_build(&self.tokenizer);
+ } else if speaking.validate(token) {
+ self.state = State::new_speaking(vec![]);
+ }
+ }
+ State::QueryBuild { tokens, end } => {
+ let token = self.token_generator.next()?;
+ self.token_generator.set_max_new_tokens(None);
+ tokens.push(token);
+
+ if end.validate(token) {
+ let query = tokens.clone();
+ *tokens = Vec::new();
+ self.state = State::new_thought(&self.tokenizer);
+ self.queries_performed += 1;
+ return Some(RawAction::Search { query });
+ }
+ }
+ State::Speaking {
+ end,
+ max_new_tokens,
+ banned_tokens,
+ } => {
+ self.token_generator.set_max_new_tokens(*max_new_tokens);
+ banned_tokens.set_banned_tokens(&mut self.token_generator);
+
+ let token = self.token_generator.next()?;
+
+ if end.validate(token) {
+ banned_tokens.reset_ttl();
+ self.state = State::new_thought(&self.tokenizer);
+ return None;
+ } else {
+ banned_tokens.tick();
+ return Some(RawAction::Speak { token });
+ }
+ }
+ }
+ }
+ }
+}
+
+#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
+pub struct SimplifiedWebsite {
+ pub title: String,
+ pub text: String,
+ pub url: String,
+ pub domain: String,
+}
+
+impl SimplifiedWebsite {
+ fn new(webpage: DisplayedWebpage, query: &str, summarizer: &ExtractiveSummarizer) -> Self {
+ let text = summarizer.summarize(query, &webpage.body);
+ let url = Url::from(webpage.url.to_string());
+
+ Self {
+ title: webpage.title,
+ text,
+ domain: url.domain().to_string(),
+ url: url.full(),
+ }
+ }
+}
+
+#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
+struct ModelWebsite {
+ pub title: String,
+ pub text: String,
+ pub domain: String,
+}
+
+impl From<SimplifiedWebsite> for ModelWebsite {
+ fn from(value: SimplifiedWebsite) -> Self {
+ Self {
+ title: value.title,
+ text: value.text,
+ domain: value.domain,
+ }
+ }
+}
+
+struct Searcher {
+ url: String,
+ summarizer: Arc<ExtractiveSummarizer>,
+}
+
+impl Searcher {
+ async fn raw_search(&self, query: &str) -> Result<WebsitesResult> {
+ let client = reqwest::Client::new();
+ let query = ApiSearchQuery {
+ query: query.trim().to_string(),
+ num_results: Some(3),
+ optic: None, // TODO: let user specify optic
+ page: None,
+ selected_region: None,
+ site_rankings: None,
+ return_ranking_signals: None,
+ flatten_response: Some(false),
+ };
+ tracing::debug!("searching at {:?}: {:#?}", self.url, query);
+
+ let res: SearchResult = client
+ .post(&self.url)
+ .json(&query)
+ .send()
+ .await?
+ .json()
+ .await?;
+
+ match res {
+ SearchResult::Websites(res) => Ok(res),
+ SearchResult::Bang(_) => Err(Error::UnexpectedSearchResult),
+ }
+ }
+
+ async fn search(&self, query: &str) -> Result<Vec<SimplifiedWebsite>> {
+ let res = self.raw_search(query).await?;
+
+ let mut websites = Vec::new();
+
+ for website in res.webpages {
+ websites.push(SimplifiedWebsite::new(website, query, &self.summarizer));
+ }
+
+ tracing::debug!("search result: {:#?}", websites);
+
+ Ok(websites)
+ }
+}
+
+enum Action {
+ Search { query: String },
+ Speak { text: String },
+}
+
+struct ActionGenerator {
+ raw: RawActionGenerator,
+ tokens_to_speak: Vec<i64>,
+}
+
+impl ActionGenerator {
+ fn new(raw_action_gen: RawActionGenerator) -> ActionGenerator {
+ Self {
+ raw: raw_action_gen,
+ tokens_to_speak: Vec::new(),
+ }
+ }
+}
+
+impl Iterator for ActionGenerator {
+ type Item = Action;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ loop {
+ let action = self.raw.next();
+
+ match action {
+ Some(RawAction::Search { query }) => {
+ return Some(Action::Search {
+ query: self
+ .raw
+ .tokenizer
+ .decode(&query)
+ .unwrap()
+ .trim()
+ .to_ascii_lowercase(),
+ });
+ }
+ Some(RawAction::Speak { token }) => {
+ self.tokens_to_speak.push(token);
+
+ if let Some(s) = self
+ .raw
+ .tokenizer
+ .decode(&self.tokens_to_speak)
+ .ok()
+ .and_then(|s| {
+ if !s.contains('\u{fffd}') {
+ // valid utf-8 string
+ Some(s)
+ } else {
+ None
+ }
+ })
+ {
+ self.tokens_to_speak.clear();
+ return Some(Action::Speak { text: s });
+ }
+ }
+ None if !self.tokens_to_speak.is_empty() => {
+ let s = self
+ .raw
+ .tokenizer
+ .decode(&self.tokens_to_speak)
+ .ok()
+ .and_then(|s| {
+ if !s.contains('\u{fffd}') {
+ Some(s)
+ } else {
+ None
+ }
+ });
+
+ self.tokens_to_speak.clear();
+
+ return s.map(|s| Action::Speak { text: s });
+ }
+ None => {
+ return None;
+ }
+ }
+ }
+ }
+}
+
+#[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, serde::Serialize, serde::Deserialize)]
+#[serde(tag = "@type", rename_all = "camelCase")]
+pub enum ExecutionState {
+ BeginSearch {
+ query: String,
+ },
+ SearchResult {
+ query: String,
+ result: Vec<SimplifiedWebsite>,
+ },
+ Speaking {
+ text: String,
+ },
+ Done {
+ state: EncodedEncryptedState,
+ },
+}
+
+pub struct ActionExecutor {
+ generator: ActionGenerator,
+ searcher: Searcher,
+ query_to_search: Option<String>,
+ queries_performed: Vec<String>,
+ has_finished: bool,
+ encryption_key: Key<Aes256Gcm>,
+}
+
+unsafe impl Send for ActionExecutor {}
+
+impl ActionExecutor {
+ fn new(
+ action_gen: ActionGenerator,
+ searcher: Searcher,
+ encryption_key: Key<Aes256Gcm>,
+ ) -> Self {
+ ActionExecutor {
+ generator: action_gen,
+ searcher,
+ query_to_search: None,
+ queries_performed: Vec::new(),
+ has_finished: false,
+ encryption_key,
+ }
+ }
+ pub fn state(&self) -> Option<Tensor> {
+ self.generator
+ .raw
+ .token_generator
+ .states
+ .back()
+ .map(|s| s.state.clone().0)
+ }
+
+ fn load_search_result(&mut self, result: &[SimplifiedWebsite]) {
+ let result = result
+ .iter()
+ .map(|r| ModelWebsite::from(r.clone()))
+ .collect::<Vec<_>>();
+
+ let json = serde_json::to_string(&result).unwrap();
+
+ let tokens = self
+ .generator
+ .raw
+ .tokenizer
+ .encode(format!("Result: {json}<|endoftext|>\n\n"))
+ .unwrap();
+ self.generator
+ .raw
+ .token_generator
+ .load_search_result(&tokens);
+
+ let tokens = self
+ .generator
+ .raw
+ .tokenizer
+ .encode("Alice[thought]:".to_string())
+ .unwrap();
+ self.generator.raw.token_generator.load_tokens(&tokens);
+ }
+
+ pub async fn next(&mut self) -> Option<ExecutionState> {
+ if self.has_finished {
+ return None;
+ }
+
+ if let Some(query) = self.query_to_search.take() {
+ if self.queries_performed.contains(&query) {
+ 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();
+
+ tracing::debug!("loading search results");
+ self.load_search_result(&res);
+ tracing::debug!("done loading search results");
+ self.queries_performed.push(query.clone());
+
+ return Some(ExecutionState::SearchResult { query, result: res });
+ }
+ }
+
+ let action = self.generator.next();
+
+ match action {
+ Some(Action::Search { query }) => {
+ self.query_to_search = Some(query.clone());
+ Some(ExecutionState::BeginSearch { query })
+ }
+ Some(Action::Speak { text }) => Some(ExecutionState::Speaking { text }),
+ None if !self.has_finished => {
+ self.has_finished = true;
+
+ let state: Vec<Vec<f32>> = self
+ .state()
+ .unwrap()
+ .to_device(tch::Device::Cpu)
+ .try_into()
+ .unwrap();
+
+ let encrypted = EncryptedState::encrypt(state, &self.encryption_key).unwrap();
+
+ Some(ExecutionState::Done {
+ state: EncodedEncryptedState::encode(encrypted),
+ })
+ }
+ None => None,
+ }
+ }
+}
+
+#[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>) -> Result<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();
+
+ Ok(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/api/alice.rs b/core/src/api/alice.rs
new file mode 100644
index 00000000..107d8d4c
--- /dev/null
+++ b/core/src/api/alice.rs
@@ -0,0 +1,187 @@
+// 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, sync::Arc};
+
+use axum::{
+ extract,
+ response::{sse::Event, IntoResponse, Sse},
+};
+use eventsource_stream::Eventsource;
+use http::StatusCode;
+use rand::seq::SliceRandom;
+use tokio_stream::{Stream, StreamExt};
+
+use crate::{
+ distributed::member::Service,
+ entrypoint::{self, alice::SaveStateParams},
+};
+
+#[derive(Debug, serde::Serialize, serde::Deserialize)]
+pub struct SavedState {
+ pub id: String,
+ pub uuid: uuid::Uuid,
+}
+
+pub async fn save_state(
+ extract::State(state): extract::State<Arc<super::State>>,
+ extract::Json(params): extract::Json<SaveStateParams>,
+) -> Result<impl IntoResponse, http::StatusCode> {
+ let client = reqwest::ClientBuilder::default()
+ .build()
+ .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
+
+ let mut members = state.cluster.members().await;
+
+ members.shuffle(&mut rand::thread_rng());
+
+ let (alice_addr, id) = members
+ .into_iter()
+ .find_map(|m| {
+ if let Service::Alice { host } = m.service {
+ Some((host, m.id))
+ } else {
+ None
+ }
+ })
+ .ok_or(StatusCode::INTERNAL_SERVER_ERROR)?;
+
+ let alice_url = format!("http://{}/save_state", alice_addr);
+
+ let res = client
+ .post(alice_url)
+ .json(&params)
+ .send()
+ .await
+ .map_err(|e| {
+ tracing::error!("Error contacting alice: {}", e);
+ StatusCode::INTERNAL_SERVER_ERROR
+ })?;
+
+ let uuid = res.text().await.map_err(|e| {
+ tracing::error!("Error contacting alice: {}", e);
+ StatusCode::INTERNAL_SERVER_ERROR
+ })?;
+
+ let saved_state = SavedState {
+ id,
+ uuid: uuid::Uuid::parse_str(&uuid).map_err(|e| {
+ tracing::error!("Error parsing uuid: {}", e);
+ StatusCode::INTERNAL_SERVER_ERROR
+ })?,
+ };
+
+ Ok(EncodedSavedState::encode(saved_state).0)
+}
+
+#[derive(serde::Serialize, serde::Deserialize, Debug)]
+pub struct EncodedSavedState(String);
+
+impl EncodedSavedState {
+ fn encode(saved_state: SavedState) -> Self {
+ let saved_state = bincode::serialize(&saved_state).unwrap();
+ let saved_state = base64::encode(saved_state);
+ Self(saved_state)
+ }
+
+ fn decode(self) -> Result<SavedState, crate::alice::Error> {
+ let saved_state = base64::decode(self.0)?;
+ let saved_state = bincode::deserialize(&saved_state)?;
+ Ok(saved_state)
+ }
+}
+
+#[derive(serde::Serialize, serde::Deserialize, Debug)]
+#[serde(rename_all = "camelCase")]
+pub struct Params {
+ pub message: String,
+ pub prev_state: Option<EncodedSavedState>,
+}
+
+pub async fn route(
+ extract::State(state): extract::State<Arc<super::State>>,
+ extract::Query(params): extract::Query<Params>,
+) -> std::result::Result<
+ Sse<impl Stream<Item = std::result::Result<axum::response::sse::Event, Infallible>>>,
+ StatusCode,
+> {
+ let mut saved_state: Option<SavedState> = None;
+
+ if let Some(prev_state) = params.prev_state {
+ saved_state = Some(prev_state.decode().map_err(|e| {
+ tracing::error!("Error decoding saved state: {}", e);
+ StatusCode::BAD_REQUEST
+ })?);
+ }
+
+ let client = reqwest::ClientBuilder::default()
+ .build()
+ .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
+
+ let mut members = state.cluster.members().await;
+
+ members.shuffle(&mut rand::thread_rng());
+
+ let alice_addr = if let Some(id) = saved_state.as_ref().map(|s| s.id.clone()) {
+ members
+ .into_iter()
+ .find_map(|m| {
+ if m.id == id {
+ if let Service::Alice { host } = m.service {
+ Some(host)
+ } else {
+ None
+ }
+ } else {
+ None
+ }
+ })
+ .ok_or(StatusCode::INTERNAL_SERVER_ERROR)?
+ } else {
+ members
+ .into_iter()
+ .find_map(|m| {
+ if let Service::Alice { host } = m.service {
+ Some(host)
+ } else {
+ None
+ }
+ })
+ .ok_or(StatusCode::INTERNAL_SERVER_ERROR)?
+ };
+
+ let params = entrypoint::alice::Params {
+ message: params.message,
+ prev_state: saved_state.map(|s| s.uuid),
+ };
+
+ let mut events = client
+ .get(format!("http://{}", alice_addr))
+ .query(&params)
+ .send()
+ .await
+ .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
+ .bytes_stream()
+ .eventsource();
+
+ let stream = async_stream::stream! {
+ while let Some(Ok(item)) = events.next().await {
+ yield Ok(Event::default().data(item.data));
+ }
+ };
+
+ Ok(Sse::new(stream))
+}
diff --git a/core/src/api/chat.rs b/core/src/api/chat.rs
new file mode 100644
index 00000000..29ae666e
--- /dev/null
+++ b/core/src/api/chat.rs
@@ -0,0 +1,37 @@
+// 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::collections::HashMap;
+
+use super::HtmlTemplate;
+use askama::Template;
+use axum::{extract, response::IntoResponse};
+
+#[allow(clippy::unused_async)]
+pub async fn route(
+ extract::Query(params): extract::Query<HashMap<String, String>>,
+) -> impl IntoResponse {
+ let template = ChatTemplate {
+ query_url_part: serde_urlencoded::to_string(params).unwrap(),
+ };
+ HtmlTemplate(template)
+}
+
+#[derive(Template)]
+#[template(path = "chat/index.html")]
+struct ChatTemplate {
+ query_url_part: String,
+}
diff --git a/core/src/api/mod.rs b/core/src/api/mod.rs
index f0031168..5b7fb696 100644
--- a/core/src/api/mod.rs
+++ b/core/src/api/mod.rs
@@ -47,7 +47,9 @@ use axum::{
use self::webgraph::RemoteWebgraph;
mod about;
+mod alice;
mod autosuggest;
+mod chat;
mod explore;
mod improvement;
mod index;
@@ -70,6 +72,7 @@ pub struct State {
pub search_counter_fail: crate::metrics::Counter,
pub summarizer: Arc<Summarizer>,
pub improvement_queue: Option<Arc<Mutex<LeakyQueue<ImprovementEvent>>>>,
+ pub cluster: Arc<Cluster>,
}
impl<T> IntoResponse for HtmlTemplate<T>
@@ -139,7 +142,8 @@ pub async fn router(
.await?,
);
let remote_webgraph = RemoteWebgraph::new(cluster.clone());
- let searcher = FrontendSearcher::new(cluster, crossencoder, lambda_model, qa_model, bangs);
+ let searcher =
+ FrontendSearcher::new(cluster.clone(), crossencoder, lambda_model, qa_model, bangs);
let state = Arc::new(State {
searcher,
@@ -149,6 +153,7 @@ pub async fn router(
remote_webgraph,
summarizer: Arc::new(Summarizer::open(&config.summarizer_path)?),
improvement_queue: query_store_queue,
+ cluster,
});
Ok(Router::new()
@@ -163,6 +168,7 @@ pub async fn router(
.route("/autosuggest/browser", get(autosuggest::browser))
.route("/favicon.ico", get(favicon))
.route("/explore", get(explore::route))
+ .route("/chat", get(chat::route))
.route("/explore/export", get(explore::export))
.route("/about", get(about::route))
.route("/settings", get(optics::route))
@@ -182,7 +188,9 @@ pub async fn router(
"/beta/api/webgraph/similar_sites",
post(webgraph::similar_sites),
)
- .route("/beta/api/webgraph/knows_site", get(webgraph::knows_site)),
+ .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)),
)
.with_state(state))
}
diff --git a/core/src/api/search.rs b/core/src/api/search.rs
index f9b8347e..2909a465 100644
--- a/core/src/api/search.rs
+++ b/core/src/api/search.rs
@@ -269,12 +269,13 @@ fn next_page_url(uri: &Uri, params: SearchParams, skip_pages: usize) -> String {
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct ApiSearchQuery {
pub query: String,
- pub page: usize,
- pub num_results: usize,
+ pub page: Option<usize>,
+ pub num_results: Option<usize>,
pub selected_region: Option<Region>,
pub optic: Option<String>,
pub site_rankings: Option<SiteRankings>,
- pub return_ranking_signals: bool,
+ pub return_ranking_signals: Option<bool>,
+ pub flatten_response: Option<bool>,
}
impl TryFrom<ApiSearchQuery> for SearchQuery {
@@ -287,19 +288,23 @@ impl TryFrom<ApiSearchQuery> for SearchQuery {
None
};
+ let default = SearchQuery::default();
+
Ok(SearchQuery {
query: api.query,
- page: api.page,
- num_results: api.num_results,
+ page: api.page.unwrap_or(default.page),
+ num_results: api.num_results.unwrap_or(default.num_results),
selected_region: api.selected_region,
optic,
site_rankings: api.site_rankings,
- return_ranking_signals: api.return_ranking_signals,
+ return_ranking_signals: api
+ .return_ranking_signals
+ .unwrap_or(default.return_ranking_signals),
})
}
}
-#[derive(Debug, serde::Serialize)]
+#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(tag = "@type", rename_all = "camelCase")]
pub enum ApiSearchResult {
Websites(WebsitesResult),
@@ -320,20 +325,27 @@ impl From<SearchResult> for ApiSearchResult {
#[debug_handler]
pub async fn api(
extract::State(state): extract::State<Arc<State>>,
- extract::Json(mut query): extract::Json<ApiSearchQuery>,
+ extract::Json(query): extract::Json<ApiSearchQuery>,
) -> Result<impl IntoResponse, StatusCode> {
- query.num_results = query.num_results.min(50 * NUM_RESULTS_PER_PAGE);
+ let flatten_result = query.flatten_response.unwrap_or(true);
let query = SearchQuery::try_from(query);
if let Err(err) = query {
tracing::error!("{:?}", err);
return Ok(err.to_string().into_response());
}
+ let mut query = query.unwrap();
- let query = query.unwrap();
+ query.num_results = query.num_results.min(100);
match state.searcher.search(&query).await {
- Ok(result) => Ok(Json(ApiSearchResult::from(result)).into_response()),
+ Ok(result) => {
+ if flatten_result {
+ Ok(Json(ApiSearchResult::from(result)).into_response())
+ } else {
+ Ok(Json(result).into_response())
+ }
+ }
Err(Error::DistributedSearcher(searcher::distributed::Error::EmptyQuery)) => {
Ok(searcher::distributed::Error::EmptyQuery
.to_string()
diff --git a/core/src/api/summarize.rs b/core/src/api/summarize.rs
index b32532db..083c5e18 100644
--- a/core/src/api/summarize.rs
+++ b/core/src/api/summarize.rs
@@ -14,30 +14,67 @@
// 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;
use std::sync::Arc;
use axum::extract;
+use axum::response::sse::KeepAlive;
+use axum::response::{sse::Event, Sse};
+use futures::stream::Stream;
use http::StatusCode;
use serde::Deserialize;
+use tokio::sync::mpsc::{unbounded_channel, UnboundedSender};
+use tokio_stream::StreamExt as _;
use super::State;
+use crate::Result;
-#[derive(Deserialize)]
+#[derive(Deserialize, Debug)]
pub struct Params {
pub url: String,
pub query: String,
}
+fn summarize_blocking(iter: impl Iterator<Item = String>, tx: UnboundedSender<String>) {
+ for tok in iter {
+ tx.send(tok).unwrap();
+ }
+}
+
+async fn summarize(
+ params: Params,
+ state: Arc<State>,
+) -> Result<Sse<impl Stream<Item = std::result::Result<Event, Infallible>>>> {
+ let webpage = state.searcher.get_webpage(&params.url).await?;
+ let (tx, mut rx) = unbounded_channel();
+
+ let summarizer = Arc::clone(&state.summarizer);
+ let it = summarizer.summarize_iter(&params.query, &webpage.body)?;
+
+ tokio::task::spawn_blocking(move || summarize_blocking(it, tx));
+
+ 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()),
+ )
+}
+
#[allow(clippy::unused_async)]
pub async fn route(
extract::Query(params): extract::Query<Params>,
extract::State(state): extract::State<Arc<State>>,
-) -> std::result::Result<String, StatusCode> {
- let webpage = state
- .searcher
- .get_webpage(&params.url)
- .await
- .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
-
- Ok(state.summarizer.summarize(&params.query, &webpage.body))
+) -> std::result::Result<Sse<impl Stream<Item = std::result::Result<Event, Infallible>>>, StatusCode>
+{
+ // err might actually happen if url contains more than 255 tokens
+ // as these might be dropped by tantivy.
+ match summarize(params, state).await {
+ Ok(stream) => Ok(stream),
+ Err(_) => Err(StatusCode::NO_CONTENT),
+ }
}
diff --git a/core/src/distributed/member.rs b/core/src/distributed/member.rs
index fd9e0a18..42c1b5d4 100644
--- a/core/src/distributed/member.rs
+++ b/core/src/distributed/member.rs
@@ -25,6 +25,7 @@ pub enum Service {
Searcher { host: SocketAddr, shard: ShardId },
Frontend { host: SocketAddr },
Webgraph { host: SocketAddr },
+ Alice { host: SocketAddr },
}
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
diff --git a/core/src/entrypoint/alice.rs b/core/src/entrypoint/alice.rs
new file mode 100644
index 00000000..8045a733
--- /dev/null
+++ b/core/src/entrypoint/alice.rs
@@ -0,0 +1,208 @@
+// 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 aes_gcm::{aead::OsRng, Aes256Gcm, KeyInit};
+use axum::{
+ extract,
+ response::{
+ sse::{Event, KeepAlive},
+ IntoResponse, Sse,
+ },
+ routing::{get, post},
+ Router,
+};
+use tokio::sync::Mutex;
+use tokio_stream::Stream;
+use tokio_stream::StreamExt as _;
+use tracing::info;
+
+use crate::{
+ alice::{Alice, EncryptedState},
+ distributed::{
+ cluster::Cluster,
+ member::{Member, Service},
+ },
+ ttl_cache::TTLCache,
+ AliceConfig,
+};
+
+pub struct State {
+ pub alice: Alice,
+ pub cluster: Cluster,
+ pub conv_states: Arc<Mutex<TTLCache<uuid::Uuid, EncryptedState>>>,
+}
+
+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)))),
+ });
+
+ Router::new()
+ .route("/", get(route))
+ .route("/save_state", post(save_state))
+ .with_state(state)
+}
+
+#[derive(serde::Serialize, serde::Deserialize, Debug)]
+pub struct SaveStateParams {
+ pub state: String,
+}
+
+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| {
+ 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);
+
+ Ok(uuid.to_string())
+}
+
+#[derive(serde::Serialize, serde::Deserialize, Debug)]
+#[serde(rename_all = "camelCase")]
+pub struct Params {
+ pub message: String,
+ pub prev_state: Option<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 mut prev_state = None;
+ if let Some(p) = params.prev_state {
+ prev_state = Some(
+ state
+ .conv_states
+ .lock()
+ .await
+ .get(&p)
+ .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(
+ &params.message,
+ prev_state,
+ format!("http://{}/beta/api/search", search_addr),
+ )
+ .map_err(|e| {
+ info!("error creating executor: {}", e);
+ http::StatusCode::INTERNAL_SERVER_ERROR
+ })?;
+
+ let (tx, mut rx) = tokio::sync::mpsc::channel(1);
+
+ tokio::task::spawn(async move {
+ while let 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()),
+ )
+}
+
+pub async fn run(config: AliceConfig) -> Result<(), crate::alice::Error> {
+ let addr: SocketAddr = config.host;
+ let key = base64::decode(config.encryption_key)?;
+
+ let alice = Alice::open(
+ &config.alice_path,
+ &config.summarizer_path,
+ config.accelerator.clone().map(|acc| acc.into()),
+ &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(())
+}
+
+pub fn generate_key() {
+ let key = Aes256Gcm::generate_key(OsRng);
+ println!("{}", base64::encode(key.as_slice()));
+}
diff --git a/core/src/entrypoint/mod.rs b/core/src/entrypoint/mod.rs
index 374c9dfb..3e38fece 100644
--- a/core/src/entrypoint/mod.rs
+++ b/core/src/entrypoint/mod.rs
@@ -13,6 +13,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/>.
+pub mod alice;
pub mod autosuggest_scrape;
mod centrality;
#[cfg(feature = "dev")]
diff --git a/core/src/inverted_index.rs b/core/src/inverted_index.rs
index 6d68e5b0..d63ae2cc 100644
--- a/core/src/inverted_index.rs
+++ b/core/src/inverted_index.rs
@@ -463,7 +463,7 @@ impl InvertedIndex {
let tv_searcher = self.reader.searcher();
let field = tv_searcher
.schema()
- .get_field(Field::Text(TextField::SiteNoTokenizer).name())
+ .get_field(Field::Text(TextField::SiteIfHomepageNoTokenizer).name())
.unwrap();
let term = tantivy::Term::from_field_text(field, url.site());
diff --git a/core/src/leaky_queue.rs b/core/src/leaky_queue.rs
index 4c922c19..01b2c3db 100644
--- a/core/src/leaky_queue.rs
+++ b/core/src/leaky_queue.rs
@@ -16,12 +16,13 @@
use std::collections::VecDeque;
-pub struct LeakyQueue<T> {
+#[derive(Clone)]
+pub struct LeakyQueue<T: Clone> {
inner: VecDeque<T>,
max_size: usize,
}
-impl<T> LeakyQueue<T> {
+impl<T: Clone> LeakyQueue<T> {
pub fn new(max_size: usize) -> Self {
Self {
inner: VecDeque::with_capacity(max_size),
@@ -40,6 +41,18 @@ impl<T> LeakyQueue<T> {
pub fn pop(&mut self) -> Option<T> {
self.inner.pop_front()
}
+
+ pub fn back(&self) -> Option<&T> {
+ self.inner.back()
+ }
+
+ pub fn front(&self) -> Option<&T> {
+ self.inner.front()
+ }
+
+ pub fn front_mut(&mut self) -> Option<&mut T> {
+ self.inner.front_mut()
+ }
}
#[cfg(test)]
diff --git a/core/src/lib.rs b/core/src/lib.rs
index 7c3ae9cc..1945ac4e 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -33,6 +33,7 @@ mod inverted_index;
pub mod mapreduce;
+pub mod alice;
mod api;
mod autosuggest;
mod bangs;
@@ -52,6 +53,7 @@ mod intmap;
mod kahan_sum;
mod kv;
mod leaky_queue;
+mod llm_utils;
mod metrics;
pub mod prehashed;
mod qa_model;
@@ -190,6 +192,42 @@ pub struct SearchServerConfig {
pub host: SocketAddr,
}
+#[derive(Debug, Serialize, Deserialize, Clone)]
+#[serde(tag = "type", rename_all = "snake_case")]
+pub enum AcceleratorDevice {
+ Cpu,
+ Cuda(usize),
+ Mps,
+}
+
+#[derive(Debug, Serialize, Deserialize, Clone)]
+#[serde(tag = "type", rename_all = "snake_case")]
+pub enum AcceleratorDtype {
+ Float,
+ Bf16,
+}
+
+#[derive(Debug, Serialize, Deserialize, Clone)]
+pub struct AliceAcceleratorConfig {
+ pub layer_fraction: f64,
+ pub device: AcceleratorDevice,
+ pub dtype: AcceleratorDtype,
+}
+
+#[derive(Debug, Serialize, Deserialize, Clone)]
+pub struct AliceConfig {
+ pub cluster_id: String,
+ pub gossip_seed_nodes: Option<Vec<SocketAddr>>,
+ pub gossip_addr: SocketAddr,
+ pub host: SocketAddr,
+
+ pub alice_path: String,
+ pub summarizer_path: String,
+ pub accelerator: Option<AliceAcceleratorConfig>,
+ /// base64 encoded
+ pub encryption_key: String,
+}
+
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct WebgraphServerConfig {
pub host: SocketAddr,
@@ -291,6 +329,9 @@ pub enum Error {
#[error("Summarizer")]
Summarizer(#[from] crate::summarizer::Error),
+
+ #[error("Alice")]
+ Alice(#[from] crate::alice::Error),
}
pub(crate) type Result<T> = std::result::Result<T, Error>;
diff --git a/core/src/llm_utils.rs b/core/src/llm_utils.rs
new file mode 100644
index 00000000..9c1d0046
--- /dev/null
+++ b/core/src/llm_utils.rs
@@ -0,0 +1,49 @@
+// 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 tch::{Kind, Tensor};
+
+pub fn sample_typical(probs: Tensor, temp: f64, tau: f64) -> i64 {
+ debug_assert!(probs.dim() == 1, "batch support not implemented");
+ let mut probs = probs;
+
+ let logits = -probs.log();
+ let ent = (&logits * &probs).nansum(-1, true, Kind::Float);
+ let shifted_logits = (&logits - ent).abs();
+ let sorted_ids = shifted_logits.argsort(-1, true);
+ let sorted_logits = shifted_logits.index_select(-1, &sorted_ids);
+ let sorted_probs = probs.index_select(-1, &sorted_ids);
+ let cumulative_probs = sorted_probs.cumsum(-1, Kind::Float);
+ let cutoff = cumulative_probs.lt(tau).sum(Kind::Int64).int64_value(&[]);
+
+ probs = probs.index_fill_(-1, &sorted_logits.gt(cutoff).to_kind(Kind::Int64), 0.0);
+
+ if temp != 1.0 {
+ let t = Tensor::from_slice(&[1.0 / temp]);
+ probs = probs.pow(&t);
+ }
+
+ probs.multinomial(1, true).int64_value(&[0])
+}
+
+pub struct ClonableTensor(pub Tensor);
+
+impl Clone for ClonableTensor {
+ fn clone(&self) -> Self {
+ let out = Tensor::empty(self.0.size(), (self.0.kind(), self.0.device()));
+ ClonableTensor(self.0.clone(&out))
+ }
+}
diff --git a/core/src/main.rs b/core/src/main.rs
index 3a8697b8..76910025 100644
--- a/core/src/main.rs
+++ b/core/src/main.rs
@@ -24,7 +24,7 @@ use stract::entrypoint::configure;
use stract::entrypoint::indexer::IndexPointer;
use stract::entrypoint::{self, frontend, search_server, webgraph_server};
use stract::webgraph::WebgraphBuilder;
-use stract::{FrontendConfig, SearchServerConfig, WebgraphServerConfig};
+use stract::{AliceConfig, FrontendConfig, SearchServerConfig, WebgraphServerConfig};
use tracing::Level;
use tracing_subscriber::FmtSubscriber;
@@ -38,6 +38,10 @@ struct Args {
#[derive(Subcommand)]
enum Commands {
+ Alice {
+ #[clap(subcommand)]
+ options: AliceOptions,
+ },
Indexer {
#[clap(subcommand)]
options: IndexingOptions,
@@ -86,6 +90,12 @@ enum Commands {
},
}
+#[derive(Subcommand)]
+enum AliceOptions {
+ Serve { config_path: String },
+ GenerateKey,
+}
+
#[derive(Subcommand)]
enum CentralityType {
All,
@@ -293,6 +303,17 @@ fn main() -> Result<()> {
online_harmonic_path,
output_path,
),
+ Commands::Alice { options } => match options {
+ AliceOptions::Serve { config_path } => {
+ let config: AliceConfig = load_toml_config(config_path);
+
+ tokio::runtime::Builder::new_multi_thread()
+ .enable_all()
+ .build()?
+ .block_on(entrypoint::alice::run(config))?
+ }
+ AliceOptions::GenerateKey => entrypoint::alice::generate_key(),
+ },
}
Ok(())
diff --git a/core/src/qa_model.rs b/core/src/qa_model.rs
index c95daf2c..237e5837 100644
--- a/core/src/qa_model.rs
+++ b/core/src/qa_model.rs
@@ -103,9 +103,9 @@ impl QaModel {
})
.collect_vec();
- let ids = Tensor::of_slice(&ids).reshape(&[bs as i64, num_tokens as i64]);
+ let ids = Tensor::from_slice(&ids).reshape([bs as i64, num_tokens as i64]);
let attention_mask =
- Tensor::of_slice(&attention_mask).reshape(&[bs as i64, num_tokens as i64]);
+ Tensor::from_slice(&attention_mask).reshape([bs as i64, num_tokens as i64]);
let output = self
.model
diff --git a/core/src/query_store.rs b/core/src/query_store.rs
index eea4f301..6f7bd0aa 100644
--- a/core/src/query_store.rs
+++ b/core/src/query_store.rs
@@ -35,7 +35,7 @@ enum Error {
/// Note that we don't store any information that can be used to link
/// the query and result back to the user performing the query. This is extremely important!
-#[derive(Debug)]
+#[derive(Debug, Clone)]
pub struct StoredQuery {
qid: Uuid,
query: String,
@@ -43,6 +43,7 @@ pub struct StoredQuery {
timestamp: Option<DateTime<Utc>>, // it is extremely important that we strip minutes, seconds and nanoseconds here for privacy
}
+#[derive(Clone)]
pub enum ImprovementEvent {
StoreQuery(StoredQuery),
Click { qid: Uuid, idx: usize },
diff --git a/core/src/ranking/models/cross_encoder.rs b/core/src/ranking/models/cross_encoder.rs
index 6dc5f9f1..7376a63a 100644
--- a/core/src/ranking/models/cross_encoder.rs
+++ b/core/src/ranking/models/cross_encoder.rs
@@ -126,10 +126,10 @@ impl CrossEncoder for CrossEncoderModel {
})
.collect_vec();
- let ids = Tensor::of_slice(&ids).reshape(&[bs as i64, num_tokens as i64]);
+ let ids = Tensor::from_slice(&ids).reshape([bs as i64, num_tokens as i64]);
let attention_mask =
- Tensor::of_slice(&attention_mask).reshape(&[bs as i64, num_tokens as i64]);
- let type_ids = Tensor::of_slice(&type_ids).reshape(&[bs as i64, num_tokens as i64]);
+ Tensor::from_slice(&attention_mask).reshape([bs as i64, num_tokens as i64]);
+ let type_ids = Tensor::from_slice(&type_ids).reshape([bs as i64, num_tokens as i64]);
let output = self
.model
diff --git a/core/src/searcher/mod.rs b/core/src/searcher/mod.rs
index 9204ac3a..a16e80ed 100644
--- a/core/src/searcher/mod.rs
+++ b/core/src/searcher/mod.rs
@@ -36,13 +36,13 @@ use crate::{
pub const NUM_RESULTS_PER_PAGE: usize = 20;
-#[derive(Debug, Serialize)]
+#[derive(Debug, Serialize, Deserialize)]
pub enum SearchResult {
Websites(WebsitesResult),
Bang(BangHit),
}
-#[derive(Debug, Serialize)]
+#[derive(Debug, Serialize, Deserialize)]
pub struct WebsitesResult {
pub spell_corrected_query: Option<HighlightedSpellCorrection>,
pub webpages: Vec<DisplayedWebpage>,
diff --git a/core/src/summarizer.rs b/core/src/summarizer.rs
index f7735918..d34bdad2 100644
--- a/core/src/summarizer.rs
+++ b/core/src/summarizer.rs
@@ -28,6 +28,7 @@ use tokenizers::{PaddingParams, TruncationParams};
use crate::{
ceil_char_boundary,
+ llm_utils::{self, ClonableTensor},
spell::word2vec::{Word2Vec, WordVec},
};
@@ -222,38 +223,38 @@ impl PassageScorer for Word2Vec {
}
}
-pub struct Summarizer {
- extractive_summarizer: DualEncoder,
- top_n_extractive_passages: usize,
- abstractive_summarizer: AbstractiveSummarizer,
+pub struct ExtractiveSummarizer {
+ passage_scorer: DualEncoder,
+ top_n_passages: usize,
+ window_size: usize,
+ overlap: usize,
}
-impl Summarizer {
- pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
+impl ExtractiveSummarizer {
+ pub fn open<P: AsRef<Path>>(path: P, top_n_passages: usize) -> Result<Self> {
Ok(Self {
- extractive_summarizer: DualEncoder::open(path.as_ref().join("dual_encoder").as_path())?,
- top_n_extractive_passages: 10,
- abstractive_summarizer: AbstractiveSummarizer {
- model: Arc::new(AbstractiveModel::open(
- path.as_ref().join("abstractive").as_path(),
- )?),
- },
+ passage_scorer: DualEncoder::open(path)?,
+ top_n_passages,
+ window_size: 100,
+ overlap: 10,
})
}
- fn extractive_query_specific(&self, query: &str, text: &str) -> Option<String> {
- let query_vectors = self.extractive_summarizer.embed_query(query)?;
+ pub fn set_window_size(&mut self, window_size: usize) {
+ self.window_size = window_size;
+ }
+
+ fn query_specific(&self, query: &str, text: &str) -> Option<String> {
+ let query_vectors = self.passage_scorer.embed_query(query)?;
let mut best_passages: BinaryHeap<Reverse<CandidatePassage<'_>>> =
- BinaryHeap::with_capacity(self.top_n_extractive_passages);
+ BinaryHeap::with_capacity(self.top_n_passages);
- let overlap_sents = OverlappingSents::new(text, 100, 10);
+ let overlap_sents = OverlappingSents::new(text, self.window_size, self.overlap);
for (index, (passage, range)) in overlap_sents.enumerate() {
- if let Some(passage_vec) = self.extractive_summarizer.embed_passage(passage) {
- let score = self
- .extractive_summarizer
- .score(&query_vectors, &passage_vec);
+ if let Some(passage_vec) = self.passage_scorer.embed_passage(passage) {
+ let score = self.passage_scorer.score(&query_vectors, &passage_vec);
let candidate = CandidatePassage {
passage,
@@ -262,7 +263,7 @@ impl Summarizer {
range,
};
- if best_passages.len() >= self.top_n_extractive_passages {
+ if best_passages.len() >= self.top_n_passages {
if let Some(mut worst) = best_passages.peek_mut() {
if worst.0.score < candidate.score {
*worst = Reverse(candidate);
@@ -310,19 +311,46 @@ impl Summarizer {
Some(res)
}
- pub fn extractive_summary(&self, query: &str, text: &str) -> String {
- self.extractive_query_specific(query, text)
+ pub fn summarize(&self, query: &str, text: &str) -> String {
+ self.query_specific(query, text)
.unwrap_or_else(|| intersperse(text.split_whitespace().take(1000), " ").collect())
}
+}
+
+pub struct Summarizer {
+ extractive: ExtractiveSummarizer,
+ abstractive: AbstractiveSummarizer,
+}
- pub fn abstractive_summary(&self, _query: &str, text: &str) -> String {
- self.abstractive_summarizer
- .summarize(text, GenerationConfig::default())
+impl Summarizer {
+ pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
+ Ok(Self {
+ extractive: ExtractiveSummarizer::open(
+ path.as_ref().join("dual_encoder").as_path(),
+ 50,
+ )?,
+ abstractive: AbstractiveSummarizer {
+ model: Arc::new(AbstractiveModel::open(
+ path.as_ref().join("abstractive").as_path(),
+ )?),
+ },
+ })
}
pub fn summarize(&self, query: &str, text: &str) -> String {
- let summary = self.extractive_summary(query, text);
- self.abstractive_summary(query, &summary)
+ let summary = self.extractive.summarize(query, text);
+ match self.abstractive.summarize(summary.as_str()) {
+ Ok(stream) => stream.collect(),
+ Err(err) => {
+ tracing::error!("Abstractive summarization failed: {}", err);
+ summary
+ }
+ }
+ }
+
+ pub fn summarize_iter(&self, query: &str, text: &str) -> Result<impl Iterator<Item = String>> {
+ let summary = self.extractive.summarize(query, text);
+ self.abstractive.summarize(&summary)
}
}
@@ -373,9 +401,9 @@ impl DualEncoder {
.map(|&id| id as i64)
.collect::<Vec<_>>();
- let ids = Tensor::of_slice(&ids).reshape(&[1, -1]);
- let types = Tensor::of_slice(&types).reshape(&[1, -1]);
- let mask = Tensor::of_slice(&mask).reshape(&[1, -1]);
+ let ids = Tensor::from_slice(&ids).reshape([1, -1]);
+ let types = Tensor::from_slice(&types).reshape([1, -1]);
+ let mask = Tensor::from_slice(&mask).reshape([1, -1]);
Ok(self.model.forward_ts(&[ids, types, mask])?.squeeze())
}
@@ -445,6 +473,60 @@ impl AbstractiveModel {
begin_decoder_token: 2,
})
}
+
+ fn parse_decoder_output(
+ &self,
+ output: IValue,
+ ) -> std::result::Result<(Tensor, Vec<Vec<ClonableTensor>>), Error> {
+ let mut output = if let IValue::Tuple(tup) = output {
+ Ok(tup)
+ } else {
+ Err(Error::UnexpectedOutputType)
+ }?;
+
+ if output.len() != 2 {
+ return Err(Error::UnexpectedOutputType);
+ }
+
+ let logits = if let IValue::Tensor(t) = output.remove(0) {
+ Ok(t)
+ } else {
+ Err(Error::UnexpectedOutputType)
+ }?;
+
+ let caches = if let IValue::Tuple(caches) = output.remove(0) {
+ Ok(caches)
+ } else {
+ Err(Error::UnexpectedOutputType)
+ }?;
+
+ let past_key_values = if caches.len() == 12 {
+ let mut new_caches = Vec::with_capacity(caches.len());
+
+ for cache in caches {
+ if let IValue::Tuple(cache) = cache {
+ let mut c = Vec::with_capacity(2);
+ for cache in cache.into_iter().take(2) {
+ if let IValue::Tensor(cache) = cache {
+ c.push(ClonableTensor(cache));
+ } else {
+ return Err(Error::UnexpectedOutputType);
+ }
+ }
+
+ new_caches.push(c);
+ } else {
+ return Err(Error::UnexpectedOutputType);
+ }
+ }
+
+ Ok(new_caches)
+ } else {
+ Err(Error::UnexpectedOutputType)
+ }?;
+
+ Ok((logits, past_key_values))
+ }
}
pub struct AbstractiveSummarizer {
@@ -457,7 +539,16 @@ impl AbstractiveSummarizer {
model: Arc::new(model),
}
}
- pub fn summarize(&self, text: &str, mut config: GenerationConfig) -> String {
+
+ pub fn summarize(&self, text: &str) -> Result<impl Iterator<Item = String>> {
+ self.summarize_with_max(text, Some(128))
+ }
+
+ pub fn summarize_with_max(
+ &self,
+ text: &str,
+ max_summary_tokens: Option<usize>,
+ ) -> Result<impl Iterator<Item = String>> {
let ids = self
.model
.tokenizer
@@ -468,120 +559,61 @@ impl AbstractiveSummarizer {
.map(|id| *id as i64)
.collect_vec();
- let ids = Tensor::of_slice(&ids).reshape(&[1, -1]);
+ let ids = Tensor::from_slice(&ids).reshape([1, -1]);
- if !config
- .banned_tokens
- .contains(&(self.model.bos_token_id as i64))
- {
- config.banned_tokens.push(self.model.bos_token_id as i64);
- }
+ let decoder_tokens = Tensor::from_slice(&[
+ self.model.begin_decoder_token as i64,
+ self.model.bos_token_id as i64,
+ ])
+ .reshape([1, -1]);
- if !config
- .end_tokens
- .contains(&(self.model.eos_token_id as i64))
- {
- config.end_tokens.push(self.model.eos_token_id as i64);
- }
-
- match self.model.beam_search(
- &ids,
- &Tensor::of_slice(&[
- self.model.begin_decoder_token as i64,
- self.model.bos_token_id as i64,
- ])
- .reshape(&[1, -1]),
- config,
- ) {
- Ok(res) => {
- let tokens = res.tokens.into_iter().map(|tok| tok as u32).collect_vec();
- self.model
- .tokenizer
- .decode(tokens, true)
- .unwrap_or_default()
- }
- Err(err) => {
- tracing::error!("Error while summarizing: {:?}", err);
- String::new()
- }
- }
+ self.generate(&ids, &decoder_tokens, max_summary_tokens)
}
-}
-struct ClonableTensor(Tensor);
+ fn generate(
+ &self,
+ encoder_ids: &Tensor,
+ decoder_start_tokens: &Tensor,
+ max_new_tokens: Option<usize>,
+ ) -> Result<impl Iterator<Item = String>> {
+ let tau = 0.8;
+ let temp = 1.0;
+
+ let encoded = self.model.initial(encoder_ids, decoder_start_tokens)?;
+
+ let token_it = TokenStreamingGenerator {
+ model: Arc::clone(&self.model),
+ tau,
+ temp,
+ banned_tokens: vec![self.model.bos_token_id as i64],
+ end_tokens: vec![self.model.eos_token_id as i64],
+ next_token_logits: Some(encoded.next_token_logits),
+ memory: encoded.memory,
+ num_tokens_generated: 0,
+ max_new_tokens,
+ };
+
+ let it = StringStreamingGenerator {
+ token_streamer: token_it,
+ tokens: Vec::new(),
+ };
-impl Clone for ClonableTensor {
- fn clone(&self) -> Self {
- let out = Tensor::empty(&self.0.size(), (Kind::Float, self.0.device()));
- ClonableTensor(self.0.clone(&out))
+ Ok(it)
}
}
#[derive(Clone)]
-struct BartMemory {
+pub struct BartMemory {
encoder_hidden_states: ClonableTensor,
past_key_values: Vec<Vec<ClonableTensor>>,
}
-fn parse_decoder_output(output: IValue) -> Result<(Tensor, Vec<Vec<ClonableTensor>>)> {
- let mut output = if let IValue::Tuple(tup) = output {
- Ok(tup)
- } else {
- Err(Error::UnexpectedOutputType)
- }?;
-
- if output.len() != 2 {
- return Err(Error::UnexpectedOutputType);
- }
-
- let logits = if let IValue::Tensor(t) = output.remove(0) {
- Ok(t)
- } else {
- Err(Error::UnexpectedOutputType)
- }?;
-
- let caches = if let IValue::Tuple(caches) = output.remove(0) {
- Ok(caches)
- } else {
- Err(Error::UnexpectedOutputType)
- }?;
-
- let past_key_values = if caches.len() == 12 {
- let mut new_caches = Vec::with_capacity(caches.len());
-
- for cache in caches {
- if let IValue::Tuple(cache) = cache {
- let mut c = Vec::with_capacity(2);
- for cache in cache.into_iter().take(2) {
- if let IValue::Tensor(cache) = cache {
- c.push(ClonableTensor(cache));
- } else {
- return Err(Error::UnexpectedOutputType);
- }
- }
-
- new_caches.push(c);
- } else {
- return Err(Error::UnexpectedOutputType);
- }
- }
-
- Ok(new_caches)
- } else {
- Err(Error::UnexpectedOutputType)
- }?;
-
- Ok((logits, past_key_values))
-}
-
-impl BeamSearch for AbstractiveModel {
- type Memory = BartMemory;
-
+impl AbstractiveModel {
fn initial(
&self,
encoder_ids: &Tensor,
decoder_ids: &Tensor,
- ) -> Result<DecoderOutput<Self::Memory>> {
+ ) -> std::result::Result<DecoderOutput, Error> {
let encoder_hidden_states = self.encoder.forward_ts(&[encoder_ids])?;
let decoder_output = self.decoder.forward_is(&[
@@ -589,7 +621,7 @@ impl BeamSearch for AbstractiveModel {
IValue::Tensor(encoder_hidden_states.shallow_clone()),
])?;
- let (logits, past_key_values) = parse_decoder_output(decoder_output)?;
+ let (logits, past_key_values) = self.parse_decoder_output(decoder_output)?;
// logits is [batch_size, seq_len, vocab_size]
// get last token logits
@@ -606,13 +638,13 @@ impl BeamSearch for AbstractiveModel {
fn step(
&self,
- prev_output: &DecoderInput<Self::Memory>,
- ) -> Result<DecoderOutput<Self::Memory>> {
- let ids = Tensor::of_slice(&[prev_output.last_token]).reshape(&[1, 1]);
+ last_token: i64,
+ memory: &BartMemory,
+ ) -> std::result::Result<DecoderOutput, Error> {
+ let ids = Tensor::from_slice(&[last_token]).reshape([1, 1]);
let past_key_value: IValue = IValue::Tuple(
- prev_output
- .memory
+ memory
.past_key_values
.iter()
.map(|c| {
@@ -627,11 +659,11 @@ impl BeamSearch for AbstractiveModel {
let decoder_output = self.decoder_with_past.forward_is(&[
IValue::Tensor(ids),
- IValue::Tensor(prev_output.memory.encoder_hidden_states.clone().0),
+ IValue::Tensor(memory.encoder_hidden_states.clone().0),
past_key_value,
])?;
- let (logits, past_key_values) = parse_decoder_output(decoder_output)?;
+ let (logits, past_key_values) = self.parse_decoder_output(decoder_output)?;
// logits is [batch_size, seq_len, vocab_size]
// get last token logits
@@ -640,243 +672,133 @@ impl BeamSearch for AbstractiveModel {
Ok(DecoderOutput {
next_token_logits,
memory: BartMemory {
- encoder_hidden_states: prev_output.memory.encoder_hidden_states.clone(),
+ encoder_hidden_states: memory.encoder_hidden_states.clone(),
past_key_values,
},
})
}
}
-struct DecoderOutput<M: Clone> {
- next_token_logits: Tensor,
- memory: M,
-}
-
-struct DecoderInput<M: Clone> {
- last_token: i64,
- memory: M,
-}
-
-#[derive(Clone)]
-struct Beam<M: Clone> {
- log_score: f64,
- length_penalty: f64,
- tokens: Vec<i64>,
+pub struct TokenStreamingGenerator {
+ model: Arc<AbstractiveModel>,
+ tau: f64,
+ temp: f64,
+ banned_tokens: Vec<i64>,
end_tokens: Vec<i64>,
- memory: M,
+ next_token_logits: Option<Tensor>,
+ memory: BartMemory,
+ num_tokens_generated: usize,
+ max_new_tokens: Option<usize>,
}
-impl<M: Clone> Beam<M> {
- fn is_finished(&self, config: &GenerationConfig) -> bool {
- if let Some(forced) = &config.force_min_tokens {
- if self.tokens.len() < *forced as usize {
- return false;
- }
- }
+impl Iterator for TokenStreamingGenerator {
+ type Item = i64;
- if let Some(early_stopping) = &config.early_stopping {
- match early_stopping {
- EarlyStopping::MaxTokens { max_new_tokens } => {
- if self.tokens.len() >= *max_new_tokens as usize {
- return true;
+ fn next(&mut self) -> Option<Self::Item> {
+ match &self.next_token_logits {
+ Some(next_token_logits) => {
+ if let Some(max_new_tokens) = self.max_new_tokens {
+ if self.num_tokens_generated >= max_new_tokens {
+ self.next_token_logits = None;
+ return None;
}
}
- }
- }
- if let Some(tok) = self.tokens.last() {
- if config.end_tokens.contains(tok) {
- return true;
- }
- }
+ let mut probs = next_token_logits.softmax(-1, Kind::Float).squeeze();
- false
- }
-
- fn score(&self) -> f64 {
- let length = match self.tokens.last() {
- Some(tok) => {
- if self.end_tokens.contains(tok) {
- self.tokens.len() - 1
- } else {
- self.tokens.len()
+ // remove banned tokens
+ for token in &self.banned_tokens {
+ let _ = probs.index_fill_(0, &Tensor::from_slice(&[*token]), 0.0);
}
- }
- None => 0,
- };
-
- self.log_score / ((length as f64).powf(self.length_penalty))
- }
-}
-
-impl<M: Clone> PartialOrd for Beam<M> {
- fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
- self.score().partial_cmp(&other.score())
- }
-}
-impl<M: Clone> PartialEq for Beam<M> {
- fn eq(&self, other: &Self) -> bool {
- self.score() == other.score()
- }
-}
+ let next_token = llm_utils::sample_typical(probs, self.temp, self.tau);
-impl<M: Clone> Ord for Beam<M> {
- fn cmp(&self, other: &Self) -> Ordering {
- self.partial_cmp(other).unwrap_or(Ordering::Equal)
- }
-}
-
-impl<M: Clone> Eq for Beam<M> {}
-
-trait BeamSearch {
- type Memory: Clone;
-
- fn initial(
- &self,
- encoder_ids: &Tensor,
- decoder_ids: &Tensor,
- ) -> Result<DecoderOutput<Self::Memory>>;
-
- fn step(&self, prev_output: &DecoderInput<Self::Memory>)
- -> Result<DecoderOutput<Self::Memory>>;
-
- fn beam_search(
- &self,
- encoder_ids: &Tensor,
- decoder_start_tokens: &Tensor,
- config: GenerationConfig,
- ) -> Result<Beam<Self::Memory>> {
- let encoded = self.initial(encoder_ids, decoder_start_tokens)?;
- let decoder_start_tokens = decoder_start_tokens.squeeze().iter::<i64>()?.collect_vec();
+ if self.end_tokens.contains(&next_token) {
+ self.next_token_logits = None;
+ return None;
+ }
- let scores = encoded.next_token_logits.softmax(-1, Kind::Float).squeeze();
+ let encoded = self.model.step(next_token, &self.memory).ok()?;
- let (next_scores, next_tokens) = scores.topk(config.num_beams as i64, -1, true, true);
+ self.next_token_logits = Some(encoded.next_token_logits);
+ self.memory = encoded.memory;
- let mut beams = BinaryHeap::with_capacity(config.num_beams as usize);
+ self.num_tokens_generated += 1;
- for (score, token) in next_scores.iter::<f64>()?.zip(next_tokens.iter::<i64>()?) {
- if config.banned_tokens.contains(&token) {
- continue;
+ Some(next_token)
}
+ None => None,
+ }
+ }
+}
- let mut toks = decoder_start_tokens.clone();
- toks.push(token);
-
- let beam = Reverse(Beam {
- log_score: score.log2(),
- tokens: toks,
- memory: encoded.memory.clone(),
- end_tokens: config.end_tokens.clone(),
- length_penalty: config.length_penalty,
- });
+pub struct StringStreamingGenerator {
+ token_streamer: TokenStreamingGenerator,
+ tokens: Vec<u32>,
+}
- if beams.len() < config.num_beams as usize {
- beams.push(beam);
- } else {
- let mut worst = beams.peek_mut().unwrap();
- if beam.0 > worst.0 {
- *worst = beam;
- }
- }
- }
+impl Iterator for StringStreamingGenerator {
+ type Item = String;
+ fn next(&mut self) -> Option<Self::Item> {
loop {
- let mut next_beams = BinaryHeap::with_capacity(config.num_beams as usize);
-
- for beam in beams.iter() {
- if beam.0.is_finished(&config) {
- if next_beams.len() < config.num_beams as usize {
- next_beams.push(beam.clone());
- } else {
- let mut worst = next_beams.peek_mut().unwrap();
- if beam.0 > worst.0 {
- *worst = beam.clone();
- }
+ match self.token_streamer.next() {
+ Some(token) => {
+ self.tokens.push(token as u32);
+
+ if let Some(s) = self
+ .token_streamer
+ .model
+ .tokenizer
+ .decode(self.tokens.clone(), true)
+ .ok()
+ .and_then(|s| {
+ if !s.contains('\u{fffd}') {
+ // valid utf-8 string
+ Some(s)
+ } else {
+ None
+ }
+ })
+ {
+ self.tokens.clear();
+ return Some(s);
}
-
- continue;
}
-
- let prev_output = DecoderInput {
- last_token: *beam.0.tokens.last().unwrap(),
- memory: beam.0.memory.clone(),
- };
-
- let decoded = self.step(&prev_output)?;
-
- let scores = decoded.next_token_logits.softmax(-1, Kind::Float);
-
- let (next_scores, next_tokens) =
- scores.topk(config.num_beams as i64, -1, true, true);
-
- for (score, token) in next_scores.iter::<f64>()?.zip(next_tokens.iter::<i64>()?) {
- let mut new_beam = beam.clone().0;
- new_beam.log_score += score.log2();
- new_beam.tokens.push(token);
- new_beam.memory = decoded.memory.clone();
-
- if next_beams.len() < config.num_beams as usize {
- next_beams.push(Reverse(new_beam));
+ None => {
+ if self.tokens.is_empty() {
+ return None;
} else {
- let mut worst = next_beams.peek_mut().unwrap();
- if new_beam > worst.0 {
- *worst = Reverse(new_beam);
+ match self
+ .token_streamer
+ .model
+ .tokenizer
+ .decode(self.tokens.clone(), true)
+ {
+ Ok(s) => {
+ self.tokens.clear();
+
+ if !s.contains('\u{fffd}') {
+ return Some(s);
+ } else {
+ return None;
+ }
+ }
+ Err(_) => {
+ self.tokens.clear();
+ return None;
+ }
}
}
}
}
-
- if next_beams.iter().all(|b| b.0.is_finished(&config)) {
- break;
- }
-
- beams = next_beams;
- }
-
- let mut beams = beams
- .into_sorted_vec()
- .into_iter()
- .map(|b| b.0)
- .collect_vec();
-
- beams.sort();
-
- Ok(beams.pop().unwrap())
- }
-}
-
-pub struct GenerationConfig {
- pub early_stopping: Option<EarlyStopping>,
- pub force_min_tokens: Option<u32>,
- pub end_tokens: Vec<i64>,
- pub banned_tokens: Vec<i64>,
- /// Exponential penalty to the length that is used with beam-based generation.
- /// It is applied as an exponent to the sequence length, which in turn is used to divide the score of the sequence.
- /// Since the score is the log likelihood of the sequence (i.e. negative),
- /// length_penalty > 0.0 promotes longer sequences, while length_penalty < 0.0 encourages shorter sequences
- pub length_penalty: f64,
- pub num_beams: u32,
-}
-
-impl Default for GenerationConfig {
- fn default() -> Self {
- Self {
- early_stopping: Some(EarlyStopping::MaxTokens {
- max_new_tokens: 128,
- }),
- end_tokens: vec![],
- banned_tokens: vec![],
- force_min_tokens: None,
- length_penalty: 1.0,
- num_beams: 3,
}
}
}
-pub enum EarlyStopping {
- MaxTokens { max_new_tokens: u32 },
+struct DecoderOutput {
+ next_token_logits: Tensor,
+ memory: BartMemory,
}
#[cfg(test)]
@@ -938,19 +860,15 @@ mod tests {
Though Aristotle wrote many elegant treatises and dialogues for publication, only around a third of his original output has survived, none of it intended for publication. Aristotle provided a complex synthesis of the various philosophies existing prior to him. It was above all from his teachings that the West inherited its intellectual lexicon, as well as problems and methods of inquiry. As a result, his philosophy has exerted a unique influence on almost every form of knowledge in the West and it continues to be a subject of contemporary philosophical discussion.
Aristotle's views profoundly shaped medieval scholarship. The influence of physical science extended from Late Antiquity and the Early Middle Ages into the Renaissance, and were not replaced systematically until the Enlightenment and theories such as classical mechanics were developed. Some of Aristotle's zoological observations found in his biology, such as on the hectocotyl (reproductive) arm of the octopus, were disbelieved until the 19th century. He also influenced Judeo-Islamic philosophies during the Middle Ages, as well as Christian theology, especially the Neoplatonism of the Early Church and the scholastic tradition of the Catholic Church. Aristotle was revered among medieval Muslim scholars as "The First Teacher", and among medieval Christians like Thomas Aquinas as simply "The Philosopher", while the poet Dante called him "the master of those who know". His works contain the earliest known formal study of logic, and were studied by medieval scholars such as Peter Abelard and John Buridan. Aristotle's influence on logic continued well into the 19th century. In addition, his ethics, though always influential, gained renewed interest with the modern advent of virtue ethics."#;
- let config = GenerationConfig {
- num_beams: 2,
- early_stopping: Some(EarlyStopping::MaxTokens { max_new_tokens: 16 }),
- length_penalty: 1.0,
- ..Default::default()
- };
-
let start = std::time::Instant::now();
- let summary = summarizer.summarize(text, config);
+ let summary = summarizer
+ .summarize_with_max(text, Some(16))
+ .unwrap()
+ .collect::<String>();
+
println!("Elapsed: {:?}", start.elapsed());
println!("{:?}", &summary);
assert!(summary.len() > 50);
- assert!(summary.contains("Aristotle"));
}
}
diff --git a/core/src/ttl_cache.rs b/core/src/ttl_cache.rs
index 65151d4d..cfc65522 100644
--- a/core/src/ttl_cache.rs
+++ b/core/src/ttl_cache.rs
@@ -24,11 +24,14 @@ pub struct TTLCache<K, V> {
data: HashMap<K, V>,
insertion_order: VecDeque<K>,
insertion_times: HashMap<K, SystemTime>,
- max_size: usize,
+ max_size: Option<usize>,
}
impl<K: Hash + Eq + Clone, V> TTLCache<K, V> {
- pub fn with_ttl_and_max_size(ttl: Duration, max_size: usize) -> Self {
+ pub fn with_ttl(ttl: Duration) -> Self {
+ Self::with_ttl_and_max_size(ttl, None)
+ }
+ pub fn with_ttl_and_max_size(ttl: Duration, max_size: Option<usize>) -> Self {
Self {
ttl,
data: HashMap::new(),
@@ -72,10 +75,12 @@ impl<K: Hash + Eq + Clone, V> TTLCache<K, V> {
fn prune_old_entries(&mut self) {
let current_time = SystemTime::now();
- while self.data.len() >= self.max_size {
- let front = self.insertion_order.pop_front().unwrap();
- self.insertion_times.remove(&front);
- self.data.remove(&front);
+ if let Some(max_size) = self.max_size {
+ while self.data.len() >= max_size {
+ let front = self.insertion_order.pop_front().unwrap();
+ self.insertion_times.remove(&front);
+ self.data.remove(&front);
+ }
}
while let Some(front) = self.insertion_order.front() {
@@ -100,7 +105,7 @@ mod tests {
#[test]
fn simple() {
- let mut cache = TTLCache::with_ttl_and_max_size(Duration::from_millis(50), 5);
+ let mut cache = TTLCache::with_ttl_and_max_size(Duration::from_millis(50), None);
cache.insert(0, 0);
std::thread::sleep(Duration::from_millis(30));
@@ -128,7 +133,7 @@ mod tests {
#[test]
fn max_size() {
- let mut cache = TTLCache::with_ttl_and_max_size(Duration::from_millis(50), 1);
+ let mut cache = TTLCache::with_ttl_and_max_size(Duration::from_millis(50), Some(1));
cache.insert(0, 0);
cache.insert(1, 1);
diff --git a/core/src/webpage/mod.rs b/core/src/webpage/mod.rs
index be948834..be9b76b2 100644
--- a/core/src/webpage/mod.rs
+++ b/core/src/webpage/mod.rs
@@ -1175,12 +1175,12 @@ impl Html {
})
}
- pub fn description(&self) -> Option<String> {
+ pub fn og_description(&self) -> Option<String> {
self.metadata()
.into_iter()
.find(|metadata| {
if let Some(property) = metadata.get("property") {
- property == &String::from("og:description")
+ property.as_str() == "og:description"
} else {
false
}
@@ -1188,6 +1188,24 @@ impl Html {
.and_then(|metadata| metadata.get("content").cloned())
}
+ pub fn metadata_description(&self) -> Option<String> {
+ self.metadata()
+ .into_iter()
+ .find(|metadata| {
+ if let Some(name) = metadata.get("name") {
+ name.as_str() == "description" || name.as_str() == "Description"
+ } else {
+ false
+ }
+ })
+ .and_then(|metadata| metadata.get("content").cloned())
+ }
+
+ pub fn description(&self) -> Option<String> {
+ self.og_description()
+ .or_else(|| self.metadata_description())
+ }
+
pub fn og_title(&self) -> Option<String> {
self.metadata()
.into_iter()
diff --git a/core/src/webpage/url.rs b/core/src/webpage/url.rs
index 2823b3e6..db9cea4b 100644
--- a/core/src/webpage/url.rs
+++ b/core/src/webpage/url.rs
@@ -357,4 +357,25 @@ mod tests {
let url: Url = "test.example.com/test/test".to_string().into();
assert_eq!(url.without_protocol(), "test.example.com/test/test");
}
+
+ #[test]
+ fn url_is_homepage() {
+ let url: Url = "https://test.example.com".to_string().into();
+ assert!(url.is_homepage());
+
+ let url: Url = "https://test.example.com/".to_string().into();
+ assert!(url.is_homepage());
+
+ let url: Url = "https://test.example.com/test".to_string().into();
+ assert!(!url.is_homepage());
+
+ let url: Url = "https://podcasts.apple.com/fr/podcast/beyond-2-cest-quoi-un-planneur-strat%C3%A9gique/id1492683918?i=1000460534325".to_string().into();
+ assert!(!url.is_homepage());
+
+ let url: Url = "https://podcasts.apple.com".to_string().into();
+ assert!(url.is_homepage());
+
+ let url: Url = "podcasts.apple.com".to_string().into();
+ assert!(url.is_homepage());
+ }
}
diff --git a/core/src/widgets/calculator.rs b/core/src/widgets/calculator.rs
index c8211a09..522b576d 100644
--- a/core/src/widgets/calculator.rs
+++ b/core/src/widgets/calculator.rs
@@ -16,14 +16,14 @@
use crate::widgets::{Error, Result};
use lalrpop_util::lalrpop_mod;
-use serde::Serialize;
+use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Display, Formatter};
lalrpop_mod!(pub parser, "/widgets/calculator.rs");
pub static PARSER: once_cell::sync::Lazy<parser::ExprParser> =
once_cell::sync::Lazy::new(parser::ExprParser::new);
-#[derive(Serialize)]
+#[derive(Serialize, Deserialize)]
pub enum Expr {
Number(f64),
Op(Box<Expr>, Opcode, Box<Expr>),
@@ -47,7 +47,7 @@ impl Expr {
}
}
-#[derive(Copy, Clone, Serialize)]
+#[derive(Copy, Clone, Serialize, Deserialize)]
pub enum Opcode {
Mul,
Div,
@@ -106,7 +106,7 @@ fn parse(expr: &str) -> Result<Box<Expr>> {
}
}
-#[derive(Debug, Serialize)]
+#[derive(Debug, Serialize, Deserialize)]
pub struct Calculation {
pub input: String,
pub expr: Box<Expr>,
diff --git a/core/src/widgets/mod.rs b/core/src/widgets/mod.rs
index 03052f24..fb1becf0 100644
--- a/core/src/widgets/mod.rs
+++ b/core/src/widgets/mod.rs
@@ -14,7 +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 serde::Serialize;
+use serde::{Deserialize, Serialize};
use thiserror::Error;
use self::calculator::{try_calculate, Calculation};
@@ -29,7 +29,7 @@ pub enum Error {
CalculatorParse,
}
-#[derive(Debug, Serialize)]
+#[derive(Debug, Serialize, Deserialize)]
pub enum Widget {
Calculator(Calculation),
}
diff --git a/frontend/public/js/marked.min.js b/frontend/public/js/marked.min.js
new file mode 100644
index 00000000..1727d095
--- /dev/null
+++ b/frontend/public/js/marked.min.js
@@ -0,0 +1,6 @@
+/**
+ * marked v5.0.2 - a markdown parser
+ * Copyright (c) 2011-2023, Christopher Jeffrey. (MIT Licensed)
+ * https://github.com/markedjs/marked
+ */
+!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).marked={})}(this,function(r){"use strict";function i(e,t){for(var u=0;u<t.length;u++){var n=t[u];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,function(e){e=function(e,t){if("object"!=typeof e||null===e)return e;var u=e[Symbol.toPrimitive];if(void 0===u)return("string"===t?String:Number)(e);u=u.call(e,t||"default");if("object"!=typeof u)return u;throw new TypeError("@@toPrimitive must return a primitive value.")}(e,"string");return"symbol"==typeof e?e:String(e)}(n.key),n)}}function d(){return(d=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var u,n=arguments[t];for(u in n)Object.prototype.hasOwnProperty.call(n,u)&&(e[u]=n[u])}return e}).apply(this,arguments)}function s(e,t){(null==t||t>e.length)&&(t=e.length);for(var u=0,n=new Array(t);u<t;u++)n[u]=e[u];return n}function D(e,t){var u,n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(n)return(n=n.call(e)).next.bind(n);if(Array.isArray(e)||(n=function(e,t){var u;if(e)return"string"==typeof e?s(e,t):"Map"===(u="Object"===(u=Object.prototype.toString.call(e).slice(8,-1))&&e.constructor?e.constructor.name:u)||"Set"===u?Array.from(e):"Arguments"===u||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(u)?s(e,t):void 0}(e))||t&&e&&"number"==typeof e.length)return n&&(e=n),u=0,function(){return u>=e.length?{done:!0}:{done:!1,value:e[u++]}};throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function e(){return{async:!1,baseUrl:null,breaks:!1,extensions:null,gfm:!0,headerIds:!0,headerPrefix:"",highlight:null,hooks:null,langPrefix:"language-",mangle:!0,pedantic:!1,renderer:null,sanitize:!1,sanitizer:null,silent:!1,smartypants:!1,tokenizer:null,walkTokens:null,xhtml:!1}}r.defaults=e();function u(e){return t[e]}var n=/[&<>"']/,a=new RegExp(n.source,"g"),o=/[<>"']|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/,l=new RegExp(o.source,"g"),t={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"};function A(e,t){if(t){if(n.test(e))return e.replace(a,u)}else if(o.test(e))return e.replace(l,u);return e}var c=/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/gi;function m(e){return e.replace(c,function(e,t){return"colon"===(t=t.toLowerCase())?":":"#"===t.charAt(0)?"x"===t.charAt(1)?String.fromCharCode(parseInt(t.substring(2),16)):String.fromCharCode(+t.substring(1)):""})}var p=/(^|[^\[])\^/g;function h(u,e){u="string"==typeof u?u:u.source,e=e||"";var n={replace:function(e,t){return t=(t=t.source||t).replace(p,"$1"),u=u.replace(e,t),n},getRegex:function(){return new RegExp(u,e)}};return n}var j=/[^\w:]/g,Z=/^$|^[a-z][a-z0-9+.-]*:|^[?#]/i;function f(e,t,u){if(e){try{n=decodeURIComponent(m(u)).replace(j,"").toLowerCase()}catch(e){return null}if(0===n.indexOf("javascript:")||0===n.indexOf("vbscript:")||0===n.indexOf("data:"))return null}var n;t&&!Z.test(u)&&(e=u,g[" "+(n=t)]||(O.test(n)?g[" "+n]=n+"/":g[" "+n]=C(n,"/",!0)),t=-1===(n=g[" "+n]).indexOf(":"),u="//"===e.substring(0,2)?t?e:n.replace(q,"$1")+e:"/"===e.charAt(0)?t?e:n.replace(P,"$1")+e:n+e);try{u=encodeURI(u).replace(/%25/g,"%")}catch(e){return null}return u}var g={},O=/^[^:]+:\/*[^/]*$/,q=/^([^:]+:)[\s\S]*$/,P=/^([^:]+:\/*[^/]*)[\s\S]*$/;var F={exec:function(){}};function k(e,t){var u=e.replace(/\|/g,function(e,t,u){for(var n=!1,r=t;0<=--r&&"\\"===u[r];)n=!n;return n?"|":" |"}).split(/ \|/),n=0;if(u[0].trim()||u.shift(),0<u.length&&!u[u.length-1].trim()&&u.pop(),u.length>t)u.splice(t);else for(;u.length<t;)u.push("");for(;n<u.length;n++)u[n]=u[n].trim().replace(/\\\|/g,"|");return u}function C(e,t,u){var n=e.length;if(0===n)return"";for(var r=0;r<n;){var i=e.charAt(n-r-1);if((i!==t||u)&&(i===t||!u))break;r++}return e.slice(0,n-r)}function E(e,t){if(t<1)return"";for(var u="";1<t;)1&t&&(u+=e),t>>=1,e+=e;return u+e}function x(e,t,u,n){var r=t.href,t=t.title?A(t.title):null,i=e[1].replace(/\\([\[\]])/g,"$1");return"!"!==e[0].charAt(0)?(n.state.inLink=!0,e={type:"link",raw:u,href:r,title:t,text:i,tokens:n.inlineTokens(i)},n.state.inLink=!1,e):{type:"image",raw:u,href:r,title:t,text:A(i)}}var b=function(){function e(e){this.options=e||r.defaults}var t=e.prototype;return t.space=function(e){e=this.rules.block.newline.exec(e);if(e&&0<e[0].length)return{type:"space",raw:e[0]}},t.code=function(e){var t,e=this.rules.block.code.exec(e);if(e)return t=e[0].replace(/^ {1,4}/gm,""),{type:"code",raw:e[0],codeBlockStyle:"indented",text:this.options.pedantic?t:C(t,"\n")}},t.fences=function(e){var t,u,n,r,e=this.rules.block.fences.exec(e);if(e)return t=e[0],u=t,n=e[3]||"",u=null===(u=t.match(/^(\s+)(?:```)/))?n:(r=u[1],n.split("\n").map(function(e){var t=e.match(/^\s+/);return null!==t&&t[0].length>=r.length?e.slice(r.length):e}).join("\n")),{type:"code",raw:t,lang:e[2]&&e[2].trim().replace(this.rules.inline._escapes,"$1"),text:u}},t.heading=function(e){var t,u,e=this.rules.block.heading.exec(e);if(e)return t=e[2].trim(),/#$/.test(t)&&(u=C(t,"#"),!this.options.pedantic&&u&&!/ $/.test(u)||(t=u.trim())),{type:"heading",raw:e[0],depth:e[1].length,text:t,tokens:this.lexer.inline(t)}},t.hr=function(e){e=this.rules.block.hr.exec(e);if(e)return{type:"hr",raw:e[0]}},t.blockquote=function(e){var t,u,n,e=this.rules.block.blockquote.exec(e);if(e)return t=e[0].replace(/^ *>[ \t]?/gm,""),u=this.lexer.state.top,this.lexer.state.top=!0,n=this.lexer.blockTokens(t),this.lexer.state.top=u,{type:"blockquote",raw:e[0],tokens:n,text:t}},t.list=function(e){var t=this.rules.block.list.exec(e);if(t){var u,n,r,i,s,a,o,l,D,c,p,h=1<(g=t[1].trim()).length,f={type:"list",raw:"",ordered:h,start:h?+g.slice(0,-1):"",loose:!1,items:[]},g=h?"\\d{1,9}\\"+g.slice(-1):"\\"+g;this.options.pedantic&&(g=h?g:"[*+-]");for(var F=new RegExp("^( {0,3}"+g+")((?:[\t ][^\\n]*)?(?:\\n|$))");e&&(p=!1,t=F.exec(e))&&!this.rules.block.hr.test(e);){if(u=t[0],e=e.substring(u.length),o=t[2].split("\n",1)[0].replace(/^\t+/,function(e){return" ".repeat(3*e.length)}),l=e.split("\n",1)[0],this.options.pedantic?(i=2,c=o.trimLeft()):(i=t[2].search(/[^ ]/),c=o.slice(i=4<i?1:i),i+=t[1].length),s=!1,!o&&/^ *$/.test(l)&&(u+=l+"\n",e=e.substring(l.length+1),p=!0),!p)for(var d=new RegExp("^ {0,"+Math.min(3,i-1)+"}(?:[*+-]|\\d{1,9}[.)])((?:[ \t][^\\n]*)?(?:\\n|$))"),A=new RegExp("^ {0,"+Math.min(3,i-1)+"}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)"),k=new RegExp("^ {0,"+Math.min(3,i-1)+"}(?:```|~~~)"),C=new RegExp("^ {0,"+Math.min(3,i-1)+"}#");e&&(l=D=e.split("\n",1)[0],this.options.pedantic&&(l=l.replace(/^ {1,4}(?=( {4})*[^ ])/g," ")),!k.test(l))&&!C.test(l)&&!d.test(l)&&!A.test(e);){if(l.search(/[^ ]/)>=i||!l.trim())c+="\n"+l.slice(i);else{if(s)break;if(4<=o.search(/[^ ]/))break;if(k.test(o))break;if(C.test(o))break;if(A.test(o))break;c+="\n"+l}s||l.trim()||(s=!0),u+=D+"\n",e=e.substring(D.length+1),o=l.slice(i)}f.loose||(a?f.loose=!0:/\n *\n *$/.test(u)&&(a=!0)),this.options.gfm&&(n=/^\[[ xX]\] /.exec(c))&&(r="[ ] "!==n[0],c=c.replace(/^\[[ xX]\] +/,"")),f.items.push({type:"list_item",raw:u,task:!!n,checked:r,loose:!1,text:c}),f.raw+=u}f.items[f.items.length-1].raw=u.trimRight(),f.items[f.items.length-1].text=c.trimRight(),f.raw=f.raw.trimRight();for(var E,m=f.items.length,x=0;x<m;x++)this.lexer.state.top=!1,f.items[x].tokens=this.lexer.blockTokens(f.items[x].text,[]),f.loose||(E=0<(E=f.items[x].tokens.filter(function(e){return"space"===e.type})).length&&E.some(function(e){return/\n.*\n/.test(e.raw)}),f.loose=E);if(f.loose)for(x=0;x<m;x++)f.items[x].loose=!0;return f}},t.html=function(e){var t,e=this.rules.block.html.exec(e);if(e)return t={type:"html",block:!0,raw:e[0],pre:!this.options.sanitizer&&("pre"===e[1]||"script"===e[1]||"style"===e[1]),text:e[0]},this.options.sanitize&&(e=this.options.sanitizer?this.options.sanitizer(e[0]):A(e[0]),t.type="paragraph",t.text=e,t.tokens=this.lexer.inline(e)),t},t.def=function(e){var t,u,n,e=this.rules.block.def.exec(e);if(e)return t=e[1].toLowerCase().replace(/\s+/g," "),u=e[2]?e[2].replace(/^<(.*)>$/,"$1").replace(this.rules.inline._escapes,"$1"):"",n=e[3]&&e[3].substring(1,e[3].length-1).replace(this.rules.inline._escapes,"$1"),{type:"def",tag:t,raw:e[0],href:u,title:n}},t.table=function(e){e=this.rules.block.table.exec(e);if(e){var t={type:"table",header:k(e[1]).map(function(e){return{text:e}}),align:e[2].replace(/^ *|\| *$/g,"").split(/ *\| */),rows:e[3]&&e[3].trim()?e[3].replace(/\n[ \t]*$/,"").split("\n"):[]};if(t.header.length===t.align.length){t.raw=e[0];for(var u,n,r,i=t.align.length,s=0;s<i;s++)/^ *-+: *$/.test(t.align[s])?t.align[s]="right":/^ *:-+: *$/.test(t.align[s])?t.align[s]="center":/^ *:-+ *$/.test(t.align[s])?t.align[s]="left":t.align[s]=null;for(i=t.rows.length,s=0;s<i;s++)t.rows[s]=k(t.rows[s],t.header.length).map(function(e){return{text:e}});for(i=t.header.length,u=0;u<i;u++)t.header[u].tokens=this.lexer.inline(t.header[u].text);for(i=t.rows.length,u=0;u<i;u++)for(r=t.rows[u],n=0;n<r.length;n++)r[n].tokens=this.lexer.inline(r[n].text);return t}}},t.lheading=function(e){e=this.rules.block.lheading.exec(e);if(e)return{type:"heading",raw:e[0],depth:"="===e[2].charAt(0)?1:2,text:e[1],tokens:this.lexer.inline(e[1])}},t.paragraph=function(e){var t,e=this.rules.block.paragraph.exec(e);if(e)return t="\n"===e[1].charAt(e[1].length-1)?e[1].slice(0,-1):e[1],{type:"paragraph",raw:e[0],text:t,tokens:this.lexer.inline(t)}},t.text=function(e){e=this.rules.block.text.exec(e);if(e)return{type:"text",raw:e[0],text:e[0],tokens:this.lexer.inline(e[0])}},t.escape=function(e){e=this.rules.inline.escape.exec(e);if(e)return{type:"escape",raw:e[0],text:A(e[1])}},t.tag=function(e){e=this.rules.inline.tag.exec(e);if(e)return!this.lexer.state.inLink&&/^<a /i.test(e[0])?this.lexer.state.inLink=!0:this.lexer.state.inLink&&/^<\/a>/i.test(e[0])&&(this.lexer.state.inLink=!1),!this.lexer.state.inRawBlock&&/^<(pre|code|kbd|script)(\s|>)/i.test(e[0])?this.lexer.state.inRawBlock=!0:this.lexer.state.inRawBlock&&/^<\/(pre|code|kbd|script)(\s|>)/i.test(e[0])&&(this.lexer.state.inRawBlock=!1),{type:this.options.sanitize?"text":"html",raw:e[0],inLink:this.lexer.state.inLink,inRawBlock:this.lexer.state.inRawBlock,block:!1,text:this.options.sanitize?this.options.sanitizer?this.options.sanitizer(e[0]):A(e[0]):e[0]}},t.link=function(e){e=this.rules.inline.link.exec(e);if(e){var t=e[2].trim();if(!this.options.pedantic&&/^</.test(t)){if(!/>$/.test(t))return;var u=C(t.slice(0,-1),"\\");if((t.length-u.length)%2==0)return}else{u=function(e,t){if(-1!==e.indexOf(t[1]))for(var u=e.length,n=0,r=0;r<u;r++)if("\\"===e[r])r++;else if(e[r]===t[0])n++;else if(e[r]===t[1]&&--n<0)return r;return-1}(e[2],"()");-1<u&&(r=(0===e[0].indexOf("!")?5:4)+e[1].length+u,e[2]=e[2].substring(0,u),e[0]=e[0].substring(0,r).trim(),e[3]="")}var n,u=e[2],r="";return this.options.pedantic?(n=/^([^'"]*[^\s])\s+(['"])(.*)\2/.exec(u))&&(u=n[1],r=n[3]):r=e[3]?e[3].slice(1,-1):"",u=u.trim(),x(e,{href:(u=/^</.test(u)?this.options.pedantic&&!/>$/.test(t)?u.slice(1):u.slice(1,-1):u)&&u.replace(this.rules.inline._escapes,"$1"),title:r&&r.replace(this.rules.inline._escapes,"$1")},e[0],this.lexer)}},t.reflink=function(e,t){var u;if(u=(u=this.rules.inline.reflink.exec(e))||this.rules.inline.nolink.exec(e))return(e=t[(e=(u[2]||u[1]).replace(/\s+/g," ")).toLowerCase()])?x(u,e,u[0],this.lexer):{type:"text",raw:t=u[0].charAt(0),text:t}},t.emStrong=function(e,t,u){void 0===u&&(u="");var n=this.rules.inline.emStrong.lDelim.exec(e);if(n&&(!n[3]||!u.match(/(?:[0-9A-Za-z\xAA\xB2\xB3\xB5\xB9\xBA\xBC-\xBE\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u052F\u0531-\u0556\u0559\u0560-\u0588\u05D0-\u05EA\u05EF-\u05F2\u0620-\u064A\u0660-\u0669\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07C0-\u07EA\u07F4\u07F5\u07FA\u0800-\u0815\u081A\u0824\u0828\u0840-\u0858\u0860-\u086A\u0870-\u0887\u0889-\u088E\u08A0-\u08C9\u0904-\u0939\u093D\u0950\u0958-\u0961\u0966-\u096F\u0971-\u0980\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09E6-\u09F1\u09F4-\u09F9\u09FC\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A66-\u0A6F\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0AE6-\u0AEF\u0AF9\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3D\u0B5C\u0B5D\u0B5F-\u0B61\u0B66-\u0B6F\u0B71-\u0B77\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BD0\u0BE6-\u0BF2\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C39\u0C3D\u0C58-\u0C5A\u0C5D\u0C60\u0C61\u0C66-\u0C6F\u0C78-\u0C7E\u0C80\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDD\u0CDE\u0CE0\u0CE1\u0CE6-\u0CEF\u0CF1\u0CF2\u0D04-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D\u0D4E\u0D54-\u0D56\u0D58-\u0D61\u0D66-\u0D78\u0D7A-\u0D7F\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0DE6-\u0DEF\u0E01-\u0E30\u0E32\u0E33\u0E40-\u0E46\u0E50-\u0E59\u0E81\u0E82\u0E84\u0E86-\u0E8A\u0E8C-\u0EA3\u0EA5\u0EA7-\u0EB0\u0EB2\u0EB3\u0EBD\u0EC0-\u0EC4\u0EC6\u0ED0-\u0ED9\u0EDC-\u0EDF\u0F00\u0F20-\u0F33\u0F40-\u0F47\u0F49-\u0F6C\u0F88-\u0F8C\u1000-\u102A\u103F-\u1049\u1050-\u1055\u105A-\u105D\u1061\u1065\u1066\u106E-\u1070\u1075-\u1081\u108E\u1090-\u1099\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1369-\u137C\u1380-\u138F\u13A0-\u13F5\u13F8-\u13FD\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F8\u1700-\u1711\u171F-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u17E0-\u17E9\u17F0-\u17F9\u1810-\u1819\u1820-\u1878\u1880-\u1884\u1887-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191E\u1946-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\u19D0-\u19DA\u1A00-\u1A16\u1A20-\u1A54\u1A80-\u1A89\u1A90-\u1A99\u1AA7\u1B05-\u1B33\u1B45-\u1B4C\u1B50-\u1B59\u1B83-\u1BA0\u1BAE-\u1BE5\u1C00-\u1C23\u1C40-\u1C49\u1C4D-\u1C7D\u1C80-\u1C88\u1C90-\u1CBA\u1CBD-\u1CBF\u1CE9-\u1CEC\u1CEE-\u1CF3\u1CF5\u1CF6\u1CFA\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2070\u2071\u2074-\u2079\u207F-\u2089\u2090-\u209C\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2150-\u2189\u2460-\u249B\u24EA-\u24FF\u2776-\u2793\u2C00-\u2CE4\u2CEB-\u2CEE\u2CF2\u2CF3\u2CFD\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2E2F\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303C\u3041-\u3096\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312F\u3131-\u318E\u3192-\u3195\u31A0-\u31BF\u31F0-\u31FF\u3220-\u3229\u3248-\u324F\u3251-\u325F\u3280-\u3289\u32B1-\u32BF\u3400-\u4DBF\u4E00-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA62B\uA640-\uA66E\uA67F-\uA69D\uA6A0-\uA6EF\uA717-\uA71F\uA722-\uA788\uA78B-\uA7CA\uA7D0\uA7D1\uA7D3\uA7D5-\uA7D9\uA7F2-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA830-\uA835\uA840-\uA873\uA882-\uA8B3\uA8D0-\uA8D9\uA8F2-\uA8F7\uA8FB\uA8FD\uA8FE\uA900-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF-\uA9D9\uA9E0-\uA9E4\uA9E6-\uA9FE\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA50-\uAA59\uAA60-\uAA76\uAA7A\uAA7E-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB69\uAB70-\uABE2\uABF0-\uABF9\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE70-\uFE74\uFE76-\uFEFC\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]|\uD800[\uDC00-\uDC0B\uDC0D-\uDC26\uDC28-\uDC3A\uDC3C\uDC3D\uDC3F-\uDC4D\uDC50-\uDC5D\uDC80-\uDCFA\uDD07-\uDD33\uDD40-\uDD78\uDD8A\uDD8B\uDE80-\uDE9C\uDEA0-\uDED0\uDEE1-\uDEFB\uDF00-\uDF23\uDF2D-\uDF4A\uDF50-\uDF75\uDF80-\uDF9D\uDFA0-\uDFC3\uDFC8-\uDFCF\uDFD1-\uDFD5]|\uD801[\uDC00-\uDC9D\uDCA0-\uDCA9\uDCB0-\uDCD3\uDCD8-\uDCFB\uDD00-\uDD27\uDD30-\uDD63\uDD70-\uDD7A\uDD7C-\uDD8A\uDD8C-\uDD92\uDD94\uDD95\uDD97-\uDDA1\uDDA3-\uDDB1\uDDB3-\uDDB9\uDDBB\uDDBC\uDE00-\uDF36\uDF40-\uDF55\uDF60-\uDF67\uDF80-\uDF85\uDF87-\uDFB0\uDFB2-\uDFBA]|\uD802[\uDC00-\uDC05\uDC08\uDC0A-\uDC35\uDC37\uDC38\uDC3C\uDC3F-\uDC55\uDC58-\uDC76\uDC79-\uDC9E\uDCA7-\uDCAF\uDCE0-\uDCF2\uDCF4\uDCF5\uDCFB-\uDD1B\uDD20-\uDD39\uDD80-\uDDB7\uDDBC-\uDDCF\uDDD2-\uDE00\uDE10-\uDE13\uDE15-\uDE17\uDE19-\uDE35\uDE40-\uDE48\uDE60-\uDE7E\uDE80-\uDE9F\uDEC0-\uDEC7\uDEC9-\uDEE4\uDEEB-\uDEEF\uDF00-\uDF35\uDF40-\uDF55\uDF58-\uDF72\uDF78-\uDF91\uDFA9-\uDFAF]|\uD803[\uDC00-\uDC48\uDC80-\uDCB2\uDCC0-\uDCF2\uDCFA-\uDD23\uDD30-\uDD39\uDE60-\uDE7E\uDE80-\uDEA9\uDEB0\uDEB1\uDF00-\uDF27\uDF30-\uDF45\uDF51-\uDF54\uDF70-\uDF81\uDFB0-\uDFCB\uDFE0-\uDFF6]|\uD804[\uDC03-\uDC37\uDC52-\uDC6F\uDC71\uDC72\uDC75\uDC83-\uDCAF\uDCD0-\uDCE8\uDCF0-\uDCF9\uDD03-\uDD26\uDD36-\uDD3F\uDD44\uDD47\uDD50-\uDD72\uDD76\uDD83-\uDDB2\uDDC1-\uDDC4\uDDD0-\uDDDA\uDDDC\uDDE1-\uDDF4\uDE00-\uDE11\uDE13-\uDE2B\uDE3F\uDE40\uDE80-\uDE86\uDE88\uDE8A-\uDE8D\uDE8F-\uDE9D\uDE9F-\uDEA8\uDEB0-\uDEDE\uDEF0-\uDEF9\uDF05-\uDF0C\uDF0F\uDF10\uDF13-\uDF28\uDF2A-\uDF30\uDF32\uDF33\uDF35-\uDF39\uDF3D\uDF50\uDF5D-\uDF61]|\uD805[\uDC00-\uDC34\uDC47-\uDC4A\uDC50-\uDC59\uDC5F-\uDC61\uDC80-\uDCAF\uDCC4\uDCC5\uDCC7\uDCD0-\uDCD9\uDD80-\uDDAE\uDDD8-\uDDDB\uDE00-\uDE2F\uDE44\uDE50-\uDE59\uDE80-\uDEAA\uDEB8\uDEC0-\uDEC9\uDF00-\uDF1A\uDF30-\uDF3B\uDF40-\uDF46]|\uD806[\uDC00-\uDC2B\uDCA0-\uDCF2\uDCFF-\uDD06\uDD09\uDD0C-\uDD13\uDD15\uDD16\uDD18-\uDD2F\uDD3F\uDD41\uDD50-\uDD59\uDDA0-\uDDA7\uDDAA-\uDDD0\uDDE1\uDDE3\uDE00\uDE0B-\uDE32\uDE3A\uDE50\uDE5C-\uDE89\uDE9D\uDEB0-\uDEF8]|\uD807[\uDC00-\uDC08\uDC0A-\uDC2E\uDC40\uDC50-\uDC6C\uDC72-\uDC8F\uDD00-\uDD06\uDD08\uDD09\uDD0B-\uDD30\uDD46\uDD50-\uDD59\uDD60-\uDD65\uDD67\uDD68\uDD6A-\uDD89\uDD98\uDDA0-\uDDA9\uDEE0-\uDEF2\uDF02\uDF04-\uDF10\uDF12-\uDF33\uDF50-\uDF59\uDFB0\uDFC0-\uDFD4]|\uD808[\uDC00-\uDF99]|\uD809[\uDC00-\uDC6E\uDC80-\uDD43]|\uD80B[\uDF90-\uDFF0]|[\uD80C\uD81C-\uD820\uD822\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879\uD880-\uD883\uD885-\uD887][\uDC00-\uDFFF]|\uD80D[\uDC00-\uDC2F\uDC41-\uDC46]|\uD811[\uDC00-\uDE46]|\uD81A[\uDC00-\uDE38\uDE40-\uDE5E\uDE60-\uDE69\uDE70-\uDEBE\uDEC0-\uDEC9\uDED0-\uDEED\uDF00-\uDF2F\uDF40-\uDF43\uDF50-\uDF59\uDF5B-\uDF61\uDF63-\uDF77\uDF7D-\uDF8F]|\uD81B[\uDE40-\uDE96\uDF00-\uDF4A\uDF50\uDF93-\uDF9F\uDFE0\uDFE1\uDFE3]|\uD821[\uDC00-\uDFF7]|\uD823[\uDC00-\uDCD5\uDD00-\uDD08]|\uD82B[\uDFF0-\uDFF3\uDFF5-\uDFFB\uDFFD\uDFFE]|\uD82C[\uDC00-\uDD22\uDD32\uDD50-\uDD52\uDD55\uDD64-\uDD67\uDD70-\uDEFB]|\uD82F[\uDC00-\uDC6A\uDC70-\uDC7C\uDC80-\uDC88\uDC90-\uDC99]|\uD834[\uDEC0-\uDED3\uDEE0-\uDEF3\uDF60-\uDF78]|\uD835[\uDC00-\uDC54\uDC56-\uDC9C\uDC9E\uDC9F\uDCA2\uDCA5\uDCA6\uDCA9-\uDCAC\uDCAE-\uDCB9\uDCBB\uDCBD-\uDCC3\uDCC5-\uDD05\uDD07-\uDD0A\uDD0D-\uDD14\uDD16-\uDD1C\uDD1E-\uDD39\uDD3B-\uDD3E\uDD40-\uDD44\uDD46\uDD4A-\uDD50\uDD52-\uDEA5\uDEA8-\uDEC0\uDEC2-\uDEDA\uDEDC-\uDEFA\uDEFC-\uDF14\uDF16-\uDF34\uDF36-\uDF4E\uDF50-\uDF6E\uDF70-\uDF88\uDF8A-\uDFA8\uDFAA-\uDFC2\uDFC4-\uDFCB\uDFCE-\uDFFF]|\uD837[\uDF00-\uDF1E\uDF25-\uDF2A]|\uD838[\uDC30-\uDC6D\uDD00-\uDD2C\uDD37-\uDD3D\uDD40-\uDD49\uDD4E\uDE90-\uDEAD\uDEC0-\uDEEB\uDEF0-\uDEF9]|\uD839[\uDCD0-\uDCEB\uDCF0-\uDCF9\uDFE0-\uDFE6\uDFE8-\uDFEB\uDFED\uDFEE\uDFF0-\uDFFE]|\uD83A[\uDC00-\uDCC4\uDCC7-\uDCCF\uDD00-\uDD43\uDD4B\uDD50-\uDD59]|\uD83B[\uDC71-\uDCAB\uDCAD-\uDCAF\uDCB1-\uDCB4\uDD01-\uDD2D\uDD2F-\uDD3D\uDE00-\uDE03\uDE05-\uDE1F\uDE21\uDE22\uDE24\uDE27\uDE29-\uDE32\uDE34-\uDE37\uDE39\uDE3B\uDE42\uDE47\uDE49\uDE4B\uDE4D-\uDE4F\uDE51\uDE52\uDE54\uDE57\uDE59\uDE5B\uDE5D\uDE5F\uDE61\uDE62\uDE64\uDE67-\uDE6A\uDE6C-\uDE72\uDE74-\uDE77\uDE79-\uDE7C\uDE7E\uDE80-\uDE89\uDE8B-\uDE9B\uDEA1-\uDEA3\uDEA5-\uDEA9\uDEAB-\uDEBB]|\uD83C[\uDD00-\uDD0C]|\uD83E[\uDFF0-\uDFF9]|\uD869[\uDC00-\uDEDF\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF39\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0]|\uD87E[\uDC00-\uDE1D]|\uD884[\uDC00-\uDF4A\uDF50-\uDFFF]|\uD888[\uDC00-\uDFAF])/))){var r=n[1]||n[2]||"";if(!r||""===u||this.rules.inline.punctuation.exec(u)){var i=n[0].length-1,s=i,a=0,o="*"===n[0][0]?this.rules.inline.emStrong.rDelimAst:this.rules.inline.emStrong.rDelimUnd;for(o.lastIndex=0,t=t.slice(-1*e.length+i);null!=(n=o.exec(t));){var l,D=n[1]||n[2]||n[3]||n[4]||n[5]||n[6];if(D)if(l=D.length,n[3]||n[4])s+=l;else if((n[5]||n[6])&&i%3&&!((i+l)%3))a+=l;else if(!(0<(s-=l)))return l=Math.min(l,l+s+a),D=e.slice(0,i+n.index+(n[0].length-D.length)+l),Math.min(i,l)%2?(l=D.slice(1,-1),{type:"em",raw:D,text:l,tokens:this.lexer.inlineTokens(l)}):(l=D.slice(2,-2),{type:"strong",raw:D,text:l,tokens:this.lexer.inlineTokens(l)})}}}},t.codespan=function(e){var t,u,n,e=this.rules.inline.code.exec(e);if(e)return n=e[2].replace(/\n/g," "),t=/[^ ]/.test(n),u=/^ /.test(n)&&/ $/.test(n),n=A(n=t&&u?n.substring(1,n.length-1):n,!0),{type:"codespan",raw:e[0],text:n}},t.br=function(e){e=this.rules.inline.br.exec(e);if(e)return{type:"br",raw:e[0]}},t.del=function(e){e=this.rules.inline.del.exec(e);if(e)return{type:"del",raw:e[0],text:e[2],tokens:this.lexer.inlineTokens(e[2])}},t.autolink=function(e,t){var u,e=this.rules.inline.autolink.exec(e);if(e)return t="@"===e[2]?"mailto:"+(u=A(this.options.mangle?t(e[1]):e[1])):u=A(e[1]),{type:"link",raw:e[0],text:u,href:t,tokens:[{type:"text",raw:u,text:u}]}},t.url=function(e,t){var u,n,r,i;if(u=this.rules.inline.url.exec(e)){if("@"===u[2])r="mailto:"+(n=A(this.options.mangle?t(u[0]):u[0]));else{for(;i=u[0],u[0]=this.rules.inline._backpedal.exec(u[0])[0],i!==u[0];);n=A(u[0]),r="www."===u[1]?"http://"+u[0]:u[0]}return{type:"link",raw:u[0],text:n,href:r,tokens:[{type:"text",raw:n,text:n}]}}},t.inlineText=function(e,t){e=this.rules.inline.text.exec(e);if(e)return t=this.lexer.state.inRawBlock?this.options.sanitize?this.options.sanitizer?this.options.sanitizer(e[0]):A(e[0]):e[0]:A(this.options.smartypants?t(e[0]):e[0]),{type:"text",raw:e[0],text:t}},e}(),B={newline:/^(?: *(?:\n|$))+/,code:/^( {4}[^\n]+(?:\n(?: *(?:\n|$))*)?)+/,fences:/^ {0,3}(`{3,}(?=[^`\n]*(?:\n|$))|~{3,})([^\n]*)(?:\n|$)(?:|([\s\S]*?)(?:\n|$))(?: {0,3}\1[~`]* *(?=\n|$)|$)/,hr:/^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/,heading:/^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/,blockquote:/^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,list:/^( {0,3}bull)([ \t][^\n]+?)?(?:\n|$)/,html:"^ {0,3}(?:<(script|pre|style|textarea)[\\s>][\\s\\S]*?(?:</\\1>[^\\n]*\\n+|$)|comment[^\\n]*(\\n+|$)|<\\?[\\s\\S]*?(?:\\?>\\n*|$)|<![A-Z][\\s\\S]*?(?:>\\n*|$)|<!\\[CDATA\\[[\\s\\S]*?(?:\\]\\]>\\n*|$)|</?(tag)(?: +|\\n|/?>)[\\s\\S]*?(?:(?:\\n *)+\\n|$)|<(?!script|pre|style|textarea)([a-z][\\w-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n *)+\\n|$)|</(?!script|pre|style|textarea)[a-z][\\w-]*\\s*>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n *)+\\n|$))",def:/^ {0,3}\[(label)\]: *(?:\n *)?([^<\s][^\s]*|<.*?>)(?:(?: +(?:\n *)?| *\n *)(title))? *(?:\n+|$)/,table:F,lheading:/^((?:.|\n(?!\n))+?)\n {0,3}(=+|-+) *(?:\n+|$)/,_paragraph:/^([^\n]+(?:\n(?!hr|heading|lheading|blockquote|fences|list|html|table| +\n)[^\n]+)*)/,text:/^[^\n]+/,_label:/(?!\s*\])(?:\\.|[^\[\]\\])+/,_title:/(?:"(?:\\"?|[^"\\])*"|'[^'\n]*(?:\n[^'\n]+)*\n?'|\([^()]*\))/},w=(B.def=h(B.def).replace("label",B._label).replace("title",B._title).getRegex(),B.bullet=/(?:[*+-]|\d{1,9}[.)])/,B.listItemStart=h(/^( *)(bull) */).replace("bull",B.bullet).getRegex(),B.list=h(B.list).replace(/bull/g,B.bullet).replace("hr","\\n+(?=\\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$))").replace("def","\\n+(?="+B.def.source+")").getRegex(),B._tag="address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul",B._comment=/<!--(?!-?>)[\s\S]*?(?:-->|$)/,B.html=h(B.html,"i").replace("comment",B._comment).replace("tag",B._tag).replace("attribute",/ +[a-zA-Z:_][\w.:-]*(?: *= *"[^"\n]*"| *= *'[^'\n]*'| *= *[^\s"'=<>`]+)?/).getRegex(),B.paragraph=h(B._paragraph).replace("hr",B.hr).replace("heading"," {0,3}#{1,6} ").replace("|lheading","").replace("|table","").replace("blockquote"," {0,3}>").replace("fences"," {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n").replace("list"," {0,3}(?:[*+-]|1[.)]) ").replace("html","</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)").replace("tag",B._tag).getRegex(),B.blockquote=h(B.blockquote).replace("paragraph",B.paragraph).getRegex(),B.normal=d({},B),B.gfm=d({},B.normal,{table:"^ *([^\\n ].*\\|.*)\\n {0,3}(?:\\| *)?(:?-+:? *(?:\\| *:?-+:? *)*)(?:\\| *)?(?:\\n((?:(?! *\\n|hr|heading|blockquote|code|fences|list|html).*(?:\\n|$))*)\\n*|$)"}),B.gfm.table=h(B.gfm.table).replace("hr",B.hr).replace("heading"," {0,3}#{1,6} ").replace("blockquote"," {0,3}>").replace("code"," {4}[^\\n]").replace("fences"," {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n").replace("list"," {0,3}(?:[*+-]|1[.)]) ").replace("html","</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)").replace("tag",B._tag).getRegex(),B.gfm.paragraph=h(B._paragraph).replace("hr",B.hr).replace("heading"," {0,3}#{1,6} ").replace("|lheading","").replace("table",B.gfm.table).replace("blockquote"," {0,3}>").replace("fences"," {0,3}(?:`{3,}(?=[^`\\n]*\\n)|~{3,})[^\\n]*\\n").replace("list"," {0,3}(?:[*+-]|1[.)]) ").replace("html","</?(?:tag)(?: +|\\n|/?>)|<(?:script|pre|style|textarea|!--)").replace("tag",B._tag).getRegex(),B.pedantic=d({},B.normal,{html:h("^ *(?:comment *(?:\\n|\\s*$)|<(tag)[\\s\\S]+?</\\1> *(?:\\n{2,}|\\s*$)|<tag(?:\"[^\"]*\"|'[^']*'|\\s[^'\"/>\\s]*)*?/?> *(?:\\n{2,}|\\s*$))").replace("comment",B._comment).replace(/tag/g,"(?!(?:a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)\\b)\\w+(?!:|[^\\w\\s@]*@)\\b").getRegex(),def:/^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +(["(][^\n]+[")]))? *(?:\n+|$)/,heading:/^(#{1,6})(.*)(?:\n+|$)/,fences:F,lheading:/^(.+?)\n {0,3}(=+|-+) *(?:\n+|$)/,paragraph:h(B.normal._paragraph).replace("hr",B.hr).replace("heading"," *#{1,6} *[^\n]").replace("lheading",B.lheading).replace("blockquote"," {0,3}>").replace("|fences","").replace("|list","").replace("|html","").getRegex()}),{escape:/^\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/,autolink:/^<(scheme:[^\s\x00-\x1f<>]*|email)>/,url:F,tag:"^comment|^</[a-zA-Z][\\w:-]*\\s*>|^<[a-zA-Z][\\w-]*(?:attribute)*?\\s*/?>|^<\\?[\\s\\S]*?\\?>|^<![a-zA-Z]+\\s[\\s\\S]*?>|^<!\\[CDATA\\[[\\s\\S]*?\\]\\]>",link:/^!?\[(label)\]\(\s*(href)(?:\s+(title))?\s*\)/,reflink:/^!?\[(label)\]\[(ref)\]/,nolink:/^!?\[(ref)\](?:\[\])?/,reflinkSearch:"reflink|nolink(?!\\()",emStrong:{lDelim:/^(?:\*+(?:([punct_])|[^\s*]))|^_+(?:([punct*])|([^\s_]))/,rDelimAst:/^(?:[^_*\\]|\\.)*?\_\_(?:[^_*\\]|\\.)*?\*(?:[^_*\\]|\\.)*?(?=\_\_)|(?:[^*\\]|\\.)+(?=[^*])|[punct_](\*+)(?=[\s]|$)|(?:[^punct*_\s\\]|\\.)(\*+)(?=[punct_\s]|$)|[punct_\s](\*+)(?=[^punct*_\s])|[\s](\*+)(?=[punct_])|[punct_](\*+)(?=[punct_])|(?:[^punct*_\s\\]|\\.)(\*+)(?=[^punct*_\s])/,rDelimUnd:/^(?:[^_*\\]|\\.)*?\*\*(?:[^_*\\]|\\.)*?\_(?:[^_*\\]|\\.)*?(?=\*\*)|(?:[^_\\]|\\.)+(?=[^_])|[punct*](\_+)(?=[\s]|$)|(?:[^punct*_\s\\]|\\.)(\_+)(?=[punct*\s]|$)|[punct*\s](\_+)(?=[^punct*_\s])|[\s](\_+)(?=[punct*])|[punct*](\_+)(?=[punct*])/},code:/^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/,br:/^( {2,}|\\)\n(?!\s*$)/,del:F,text:/^(`+|[^`])(?:(?= {2,}\n)|[\s\S]*?(?:(?=[\\<!\[`*_]|\b_|$)|[^ ](?= {2,}\n)))/,punctuation:/^([\spunctuation])/});function L(e){return e.replace(/---/g,"—").replace(/--/g,"–").replace(/(^|[-\u2014/(\[{"\s])'/g,"$1‘").replace(/'/g,"’").replace(/(^|[-\u2014/(\[{\u2018\s])"/g,"$1“").replace(/"/g,"”").replace(/\.{3}/g,"…")}function v(e){for(var t,u="",n=e.length,r=0;r<n;r++)t=e.charCodeAt(r),u+="&#"+(t=.5<Math.random()?"x"+t.toString(16):t)+";";return u}w._punctuation="!\"#$%&'()+\\-.,/:;<=>?@\\[\\]`^{|}~",w.punctuation=h(w.punctuation).replace(/punctuation/g,w._punctuation).getRegex(),w.blockSkip=/\[[^\]]*?\]\([^\)]*?\)|`[^`]*?`|<[^>]*?>/g,w.escapedEmSt=/(?:^|[^\\])(?:\\\\)*\\[*_]/g,w._comment=h(B._comment).replace("(?:--\x3e|$)","--\x3e").getRegex(),w.emStrong.lDelim=h(w.emStrong.lDelim).replace(/punct/g,w._punctuation).getRegex(),w.emStrong.rDelimAst=h(w.emStrong.rDelimAst,"g").replace(/punct/g,w._punctuation).getRegex(),w.emStrong.rDelimUnd=h(w.emStrong.rDelimUnd,"g").replace(/punct/g,w._punctuation).getRegex(),w._escapes=/\\([!"#$%&'()*+,\-./:;<=>?@\[\]\\^_`{|}~])/g,w._scheme=/[a-zA-Z][a-zA-Z0-9+.-]{1,31}/,w._email=/[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+(@)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(?![-_])/,w.autolink=h(w.autolink).replace("scheme",w._scheme).replace("email",w._email).getRegex(),w._attribute=/\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/,w.tag=h(w.tag).replace("comment",w._comment).replace("attribute",w._attribute).getRegex(),w._label=/(?:\[(?:\\.|[^\[\]\\])*\]|\\.|`[^`]*`|[^\[\]\\`])*?/,w._href=/<(?:\\.|[^\n<>\\])+>|[^\s\x00-\x1f]*/,w._title=/"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/,w.link=h(w.link).replace("label",w._label).replace("href",w._href).replace("title",w._title).getRegex(),w.reflink=h(w.reflink).replace("label",w._label).replace("ref",B._label).getRegex(),w.nolink=h(w.nolink).replace("ref",B._label).getRegex(),w.reflinkSearch=h(w.reflinkSearch,"g").replace("reflink",w.reflink).replace("nolink",w.nolink).getRegex(),w.normal=d({},w),w.pedantic=d({},w.normal,{strong:{start:/^__|\*\*/,middle:/^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,endAst:/\*\*(?!\*)/g,endUnd:/__(?!_)/g},em:{start:/^_|\*/,middle:/^()\*(?=\S)([\s\S]*?\S)\*(?!\*)|^_(?=\S)([\s\S]*?\S)_(?!_)/,endAst:/\*(?!\*)/g,endUnd:/_(?!_)/g},link:h(/^!?\[(label)\]\((.*?)\)/).replace("label",w._label).getRegex(),reflink:h(/^!?\[(label)\]\s*\[([^\]]*)\]/).replace("label",w._label).getRegex()}),w.gfm=d({},w.normal,{escape:h(w.escape).replace("])","~|])").getRegex(),_extended_email:/[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/,url:/^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/,_backpedal:/(?:[^?!.,:;*_'"~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_'"~)]+(?!$))+/,del:/^(~~?)(?=[^\s~])([\s\S]*?[^\s~])\1(?=[^~]|$)/,text:/^([`~]+|[^`~])(?:(?= {2,}\n)|(?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)|[\s\S]*?(?:(?=[\\<!\[`*~_]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-](?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)))/}),w.gfm.url=h(w.gfm.url,"i").replace("email",w.gfm._extended_email).getRegex(),w.breaks=d({},w.gfm,{br:h(w.br).replace("{2,}","*").getRegex(),text:h(w.gfm.text).replace("\\b_","\\b_| {2,}\\n").replace(/\{2,\}/g,"*").getRegex()});var y=function(){function u(e){this.tokens=[],this.tokens.links=Object.create(null),this.options=e||r.defaults,this.options.tokenizer=this.options.tokenizer||new b,this.tokenizer=this.options.tokenizer,this.tokenizer.options=this.options,(this.tokenizer.lexer=this).inlineQueue=[],this.state={inLink:!1,inRawBlock:!1,top:!0};e={block:B.normal,inline:w.normal};this.options.pedantic?(e.block=B.pedantic,e.inline=w.pedantic):this.options.gfm&&(e.block=B.gfm,this.options.breaks?e.inline=w.breaks:e.inline=w.gfm),this.tokenizer.rules=e}u.lex=function(e,t){return new u(t).lex(e)},u.lexInline=function(e,t){return new u(t).inlineTokens(e)};var e,t,n=u.prototype;return n.lex=function(e){var t;for(e=e.replace(/\r\n|\r/g,"\n"),this.blockTokens(e,this.tokens);t=this.inlineQueue.shift();)this.inlineTokens(t.src,t.tokens);return this.tokens},n.blockTokens=function(r,i){var s,a,o,l,D=this;for(void 0===i&&(i=[]),r=this.options.pedantic?r.replace(/\t/g," ").replace(/^ +$/gm,""):r.replace(/^( *)(\t+)/gm,function(e,t,u){return t+" ".repeat(u.length)});r;){var e=function(){if(D.options.extensions&&D.options.extensions.block&&D.options.extensions.block.some(function(e){return!!(s=e.call({lexer:D},r,i))&&(r=r.substring(s.raw.length),i.push(s),!0)}))return"continue";if(s=D.tokenizer.space(r))return r=r.substring(s.raw.length),1===s.raw.length&&0<i.length?i[i.length-1].raw+="\n":i.push(s),"continue";if(s=D.tokenizer.code(r))return r=r.substring(s.raw.length),!(a=i[i.length-1])||"paragraph"!==a.type&&"text"!==a.type?i.push(s):(a.raw+="\n"+s.raw,a.text+="\n"+s.text,D.inlineQueue[D.inlineQueue.length-1].src=a.text),"continue";if(s=D.tokenizer.fences(r))return r=r.substring(s.raw.length),i.push(s),"continue";if(s=D.tokenizer.heading(r))return r=r.substring(s.raw.length),i.push(s),"continue";if(s=D.tokenizer.hr(r))return r=r.substring(s.raw.length),i.push(s),"continue";if(s=D.tokenizer.blockquote(r))return r=r.substring(s.raw.length),i.push(s),"continue";if(s=D.tokenizer.list(r))return r=r.substring(s.raw.length),i.push(s),"continue";if(s=D.tokenizer.html(r))return r=r.substring(s.raw.length),i.push(s),"continue";if(s=D.tokenizer.def(r))return r=r.substring(s.raw.length),!(a=i[i.length-1])||"paragraph"!==a.type&&"text"!==a.type?D.tokens.links[s.tag]||(D.tokens.links[s.tag]={href:s.href,title:s.title}):(a.raw+="\n"+s.raw,a.text+="\n"+s.raw,D.inlineQueue[D.inlineQueue.length-1].src=a.text),"continue";if(s=D.tokenizer.table(r))return r=r.substring(s.raw.length),i.push(s),"continue";if(s=D.tokenizer.lheading(r))return r=r.substring(s.raw.length),i.push(s),"continue";var t,u,n;if(o=r,D.options.extensions&&D.options.extensions.startBlock&&(t=1/0,u=r.slice(1),D.options.extensions.startBlock.forEach(function(e){"number"==typeof(n=e.call({lexer:this},u))&&0<=n&&(t=Math.min(t,n))}),t<1/0)&&0<=t&&(o=r.substring(0,t+1)),D.state.top&&(s=D.tokenizer.paragraph(o)))return a=i[i.length-1],l&&"paragraph"===a.type?(a.raw+="\n"+s.raw,a.text+="\n"+s.text,D.inlineQueue.pop(),D.inlineQueue[D.inlineQueue.length-1].src=a.text):i.push(s),l=o.length!==r.length,r=r.substring(s.raw.length),"continue";if(s=D.tokenizer.text(r))return r=r.substring(s.raw.length),(a=i[i.length-1])&&"text"===a.type?(a.raw+="\n"+s.raw,a.text+="\n"+s.text,D.inlineQueue.pop(),D.inlineQueue[D.inlineQueue.length-1].src=a.text):i.push(s),"continue";if(r){var e="Infinite loop on byte: "+r.charCodeAt(0);if(D.options.silent)return console.error(e),"break";throw new Error(e)}}();if("continue"!==e&&"break"===e)break}return this.state.top=!0,i},n.inline=function(e,t){return this.inlineQueue.push({src:e,tokens:t=void 0===t?[]:t}),t},n.inlineTokens=function(r,i){var s,a,o,e,l,D,c=this,p=(void 0===i&&(i=[]),r);if(this.tokens.links){var t=Object.keys(this.tokens.links);if(0<t.length)for(;null!=(e=this.tokenizer.rules.inline.reflinkSearch.exec(p));)t.includes(e[0].slice(e[0].lastIndexOf("[")+1,-1))&&(p=p.slice(0,e.index)+"["+E("a",e[0].length-2)+"]"+p.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex))}for(;null!=(e=this.tokenizer.rules.inline.blockSkip.exec(p));)p=p.slice(0,e.index)+"["+E("a",e[0].length-2)+"]"+p.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);for(;null!=(e=this.tokenizer.rules.inline.escapedEmSt.exec(p));)p=p.slice(0,e.index+e[0].length-2)+"++"+p.slice(this.tokenizer.rules.inline.escapedEmSt.lastIndex),this.tokenizer.rules.inline.escapedEmSt.lastIndex--;for(;r;){var u=function(){if(l||(D=""),l=!1,c.options.extensions&&c.options.extensions.inline&&c.options.extensions.inline.some(function(e){return!!(s=e.call({lexer:c},r,i))&&(r=r.substring(s.raw.length),i.push(s),!0)}))return"continue";if(s=c.tokenizer.escape(r))return r=r.substring(s.raw.length),i.push(s),"continue";if(s=c.tokenizer.tag(r))return r=r.substring(s.raw.length),(a=i[i.length-1])&&"text"===s.type&&"text"===a.type?(a.raw+=s.raw,a.text+=s.text):i.push(s),"continue";if(s=c.tokenizer.link(r))return r=r.substring(s.raw.length),i.push(s),"continue";if(s=c.tokenizer.reflink(r,c.tokens.links))return r=r.substring(s.raw.length),(a=i[i.length-1])&&"text"===s.type&&"text"===a.type?(a.raw+=s.raw,a.text+=s.text):i.push(s),"continue";if(s=c.tokenizer.emStrong(r,p,D))return r=r.substring(s.raw.length),i.push(s),"continue";if(s=c.tokenizer.codespan(r))return r=r.substring(s.raw.length),i.push(s),"continue";if(s=c.tokenizer.br(r))return r=r.substring(s.raw.length),i.push(s),"continue";if(s=c.tokenizer.del(r))return r=r.substring(s.raw.length),i.push(s),"continue";if(s=c.tokenizer.autolink(r,v))return r=r.substring(s.raw.length),i.push(s),"continue";if(!c.state.inLink&&(s=c.tokenizer.url(r,v)))return r=r.substring(s.raw.length),i.push(s),"continue";var t,u,n;if(o=r,c.options.extensions&&c.options.extensions.startInline&&(t=1/0,u=r.slice(1),c.options.extensions.startInline.forEach(function(e){"number"==typeof(n=e.call({lexer:this},u))&&0<=n&&(t=Math.min(t,n))}),t<1/0)&&0<=t&&(o=r.substring(0,t+1)),s=c.tokenizer.inlineText(o,L))return r=r.substring(s.raw.length),"_"!==s.raw.slice(-1)&&(D=s.raw.slice(-1)),l=!0,(a=i[i.length-1])&&"text"===a.type?(a.raw+=s.raw,a.text+=s.text):i.push(s),"continue";if(r){var e="Infinite loop on byte: "+r.charCodeAt(0);if(c.options.silent)return console.error(e),"break";throw new Error(e)}}();if("continue"!==u&&"break"===u)break}return i},n=u,t=[{key:"rules",get:function(){return{block:B,inline:w}}}],(e=null)&&i(n.prototype,e),t&&i(n,t),Object.defineProperty(n,"prototype",{writable:!1}),u}(),_=function(){function e(e){this.options=e||r.defaults}var t=e.prototype;return t.code=function(e,t,u){var n,t=(t||"").match(/\S*/)[0];return this.options.highlight&&null!=(n=this.options.highlight(e,t))&&n!==e&&(u=!0,e=n),e=e.replace(/\n$/,"")+"\n",t?'<pre><code class="'+this.options.langPrefix+A(t)+'">'+(u?e:A(e,!0))+"</code></pre>\n":"<pre><code>"+(u?e:A(e,!0))+"</code></pre>\n"},t.blockquote=function(e){return"<blockquote>\n"+e+"</blockquote>\n"},t.html=function(e,t){return e},t.heading=function(e,t,u,n){return this.options.headerIds?"<h"+t+' id="'+(this.options.headerPrefix+n.slug(u))+'">'+e+"</h"+t+">\n":"<h"+t+">"+e+"</h"+t+">\n"},t.hr=function(){return this.options.xhtml?"<hr/>\n":"<hr>\n"},t.list=function(e,t,u){var n=t?"ol":"ul";return"<"+n+(t&&1!==u?' start="'+u+'"':"")+">\n"+e+"</"+n+">\n"},t.listitem=function(e){return"<li>"+e+"</li>\n"},t.checkbox=function(e){return"<input "+(e?'checked="" ':"")+'disabled="" type="checkbox"'+(this.options.xhtml?" /":"")+"> "},t.paragraph=function(e){return"<p>"+e+"</p>\n"},t.table=function(e,t){return"<table>\n<thead>\n"+e+"</thead>\n"+(t=t&&"<tbody>"+t+"</tbody>")+"</table>\n"},t.tablerow=function(e){return"<tr>\n"+e+"</tr>\n"},t.tablecell=function(e,t){var u=t.header?"th":"td";return(t.align?"<"+u+' align="'+t.align+'">':"<"+u+">")+e+"</"+u+">\n"},t.strong=function(e){return"<strong>"+e+"</strong>"},t.em=function(e){return"<em>"+e+"</em>"},t.codespan=function(e){return"<code>"+e+"</code>"},t.br=function(){return this.options.xhtml?"<br/>":"<br>"},t.del=function(e){return"<del>"+e+"</del>"},t.link=function(e,t,u){return null===(e=f(this.options.sanitize,this.options.baseUrl,e))?u:(e='<a href="'+e+'"',t&&(e+=' title="'+t+'"'),e+">"+u+"</a>")},t.image=function(e,t,u){return null===(e=f(this.options.sanitize,this.options.baseUrl,e))?u:(e='<img src="'+e+'" alt="'+u+'"',t&&(e+=' title="'+t+'"'),e+(this.options.xhtml?"/>":">"))},t.text=function(e){return e},e}(),z=function(){function e(){}var t=e.prototype;return t.strong=function(e){return e},t.em=function(e){return e},t.codespan=function(e){return e},t.del=function(e){return e},t.html=function(e){return e},t.text=function(e){return e},t.link=function(e,t,u){return""+u},t.image=function(e,t,u){return""+u},t.br=function(){return""},e}(),$=function(){function e(){this.seen={}}var t=e.prototype;return t.serialize=function(e){return e.toLowerCase().trim().replace(/<[!\/a-z].*?>/gi,"").replace(/[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,./:;<=>?@[\]^`{|}~]/g,"").replace(/\s/g,"-")},t.getNextSafeSlug=function(e,t){var u=e,n=0;if(this.seen.hasOwnProperty(u))for(n=this.seen[e];u=e+"-"+ ++n,this.seen.hasOwnProperty(u););return t||(this.seen[e]=n,this.seen[u]=0),u},t.slug=function(e,t){void 0===t&&(t={});e=this.serialize(e);return this.getNextSafeSlug(e,t.dryrun)},e}(),S=function(){function u(e){this.options=e||r.defaults,this.options.renderer=this.options.renderer||new _,this.renderer=this.options.renderer,this.renderer.options=this.options,this.textRenderer=new z,this.slugger=new $}u.parse=function(e,t){return new u(t).parse(e)},u.parseInline=function(e,t){return new u(t).parseInline(e)};var e=u.prototype;return e.parse=function(e,t){void 0===t&&(t=!0);for(var u,n,r,i,s,a,o,l,D,c,p,h,f,g,F,d,A="",k=e.length,C=0;C<k;C++)if(l=e[C],this.options.extensions&&this.options.extensions.renderers&&this.options.extensions.renderers[l.type]&&(!1!==(d=this.options.extensions.renderers[l.type].call({parser:this},l))||!["space","hr","heading","code","table","blockquote","list","html","paragraph","text"].includes(l.type)))A+=d||"";else switch(l.type){case"space":continue;case"hr":A+=this.renderer.hr();continue;case"heading":A+=this.renderer.heading(this.parseInline(l.tokens),l.depth,m(this.parseInline(l.tokens,this.textRenderer)),this.slugger);continue;case"code":A+=this.renderer.code(l.text,l.lang,l.escaped);continue;case"table":for(a=D="",r=l.header.length,u=0;u<r;u++)a+=this.renderer.tablecell(this.parseInline(l.header[u].tokens),{header:!0,align:l.align[u]});for(D+=this.renderer.tablerow(a),o="",r=l.rows.length,u=0;u<r;u++){for(a="",i=(s=l.rows[u]).length,n=0;n<i;n++)a+=this.renderer.tablecell(this.parseInline(s[n].tokens),{header:!1,align:l.align[n]});o+=this.renderer.tablerow(a)}A+=this.renderer.table(D,o);continue;case"blockquote":o=this.parse(l.tokens),A+=this.renderer.blockquote(o);continue;case"list":for(D=l.ordered,E=l.start,c=l.loose,r=l.items.length,o="",u=0;u<r;u++)f=(h=l.items[u]).checked,g=h.task,p="",h.task&&(F=this.renderer.checkbox(f),c?0<h.tokens.length&&"paragraph"===h.tokens[0].type?(h.tokens[0].text=F+" "+h.tokens[0].text,h.tokens[0].tokens&&0<h.tokens[0].tokens.length&&"text"===h.tokens[0].tokens[0].type&&(h.tokens[0].tokens[0].text=F+" "+h.tokens[0].tokens[0].text)):h.tokens.unshift({type:"text",text:F}):p+=F),p+=this.parse(h.tokens,c),o+=this.renderer.listitem(p,g,f);A+=this.renderer.list(o,D,E);continue;case"html":A+=this.renderer.html(l.text,l.block);continue;case"paragraph":A+=this.renderer.paragraph(this.parseInline(l.tokens));continue;case"text":for(o=l.tokens?this.parseInline(l.tokens):l.text;C+1<k&&"text"===e[C+1].type;)o+="\n"+((l=e[++C]).tokens?this.parseInline(l.tokens):l.text);A+=t?this.renderer.paragraph(o):o;continue;default:var E='Token with "'+l.type+'" type was not found.';if(this.options.silent)return void console.error(E);throw new Error(E)}return A},e.parseInline=function(e,t){t=t||this.renderer;for(var u,n,r="",i=e.length,s=0;s<i;s++)if(u=e[s],this.options.extensions&&this.options.extensions.renderers&&this.options.extensions.renderers[u.type]&&(!1!==(n=this.options.extensions.renderers[u.type].call({parser:this},u))||!["escape","html","link","image","strong","em","codespan","br","del","text"].includes(u.type)))r+=n||"";else switch(u.type){case"escape":r+=t.text(u.text);break;case"html":r+=t.html(u.text);break;case"link":r+=t.link(u.href,u.title,this.parseInline(u.tokens,t));break;case"image":r+=t.image(u.href,u.title,u.text);break;case"strong":r+=t.strong(this.parseInline(u.tokens,t));break;case"em":r+=t.em(this.parseInline(u.tokens,t));break;case"codespan":r+=t.codespan(u.text);break;case"br":r+=t.br();break;case"del":r+=t.del(this.parseInline(u.tokens,t));break;case"text":r+=t.text(u.text);break;default:var a='Token with "'+u.type+'" type was not found.';if(this.options.silent)return void console.error(a);throw new Error(a)}return r},u}(),T=function(){function e(e){this.options=e||r.defaults}var t=e.prototype;return t.preprocess=function(e){return e},t.postprocess=function(e){return e},e}();function R(g,F){return function(e,u,n){"function"==typeof u&&(n=u,u=null);var r,i,s,t,a=d({},u),o=(u=d({},I.defaults,a),r=u.silent,i=u.async,s=n,function(e){var t;if(e.message+="\nPlease report this to https://github.com/markedjs/marked.",r)return t="<p>An error occurred:</p><pre>"+A(e.message+"",!0)+"</pre>",i?Promise.resolve(t):s?void s(null,t):t;if(i)return Promise.reject(e);if(!s)throw e;s(e)});if(null==e)return o(new Error("marked(): input parameter is undefined or null"));if("string"!=typeof e)return o(new Error("marked(): input parameter is of type "+Object.prototype.toString.call(e)+", string expected"));if(a=n,(t=u)&&!t.silent&&(a&&console.warn("marked(): callback is deprecated since version 5.0.0, should not be used and will be removed in the future. Read more here: https://marked.js.org/using_pro#async"),(t.sanitize||t.sanitizer)&&console.warn("marked(): sanitize and sanitizer parameters are deprecated since version 0.7.0, should not be used and will be removed in the future. Read more here: https://marked.js.org/#/USING_ADVANCED.md#options"),!t.highlight&&"language-"===t.langPrefix||console.warn("marked(): highlight and langPrefix parameters are deprecated since version 5.0.0, should not be used and will be removed in the future. Instead use https://www.npmjs.com/package/marked-highlight."),t.mangle&&console.warn("marked(): mangle parameter is enabled by default, but is deprecated since version 5.0.0, and will be removed in the future. To clear this warning, install https://www.npmjs.com/package/marked-mangle, or disable by setting `{mangle: false}`."),t.baseUrl&&console.warn("marked(): baseUrl parameter is deprecated since version 5.0.0, should not be used and will be removed in the future. Instead use https://www.npmjs.com/package/marked-base-url."),t.smartypants&&console.warn("marked(): smartypants parameter is deprecated since version 5.0.0, should not be used and will be removed in the future. Instead use https://www.npmjs.com/package/marked-smartypants."),t.xhtml&&console.warn("marked(): xhtml parameter is deprecated since version 5.0.0, should not be used and will be removed in the future. Instead use https://www.npmjs.com/package/marked-xhtml."),t.headerIds||t.headerPrefix)&&console.warn("marked(): headerIds and headerPrefix parameters enabled by default, but are deprecated since version 5.0.0, and will be removed in the future. To clear this warning, install https://www.npmjs.com/package/marked-gfm-heading-id, or disable by setting `{headerIds: false}`."),u.hooks&&(u.hooks.options=u),n){var l,D=u.highlight;try{u.hooks&&(e=u.hooks.preprocess(e)),l=g(e,u)}catch(e){return o(e)}var c,p=function(t){var e;if(!t)try{u.walkTokens&&I.walkTokens(l,u.walkTokens),e=F(l,u),u.hooks&&(e=u.hooks.postprocess(e))}catch(e){t=e}return u.highlight=D,t?o(t):n(null,e)};return!D||D.length<3?p():(delete u.highlight,l.length?(c=0,I.walkTokens(l,function(u){"code"===u.type&&(c++,setTimeout(function(){D(u.text,u.lang,function(e,t){if(e)return p(e);null!=t&&t!==u.text&&(u.text=t,u.escaped=!0),0===--c&&p()})},0))}),void(0===c&&p())):p())}if(u.async)return Promise.resolve(u.hooks?u.hooks.preprocess(e):e).then(function(e){return g(e,u)}).then(function(e){return u.walkTokens?Promise.all(I.walkTokens(e,u.walkTokens)).then(function(){return e}):e}).then(function(e){return F(e,u)}).then(function(e){return u.hooks?u.hooks.postprocess(e):e}).catch(o);try{u.hooks&&(e=u.hooks.preprocess(e));var h=g(e,u),f=(u.walkTokens&&I.walkTokens(h,u.walkTokens),F(h,u));return f=u.hooks?u.hooks.postprocess(f):f}catch(e){return o(e)}}}function I(e,t,u){return R(y.lex,S.parse)(e,t,u)}T.passThroughHooks=new Set(["preprocess","postprocess"]),I.options=I.setOptions=function(e){return I.defaults=d({},I.defaults,e),e=I.defaults,r.defaults=e,I},I.getDefaults=e,I.defaults=r.defaults,I.use=function(){for(var D=I.defaults.extensions||{renderers:{},childTokens:{}},e=arguments.length,t=new Array(e),u=0;u<e;u++)t[u]=arguments[u];t.forEach(function(s){var u,e=d({},s);if(e.async=I.defaults.async||e.async||!1,s.extensions&&(s.extensions.forEach(function(r){if(!r.name)throw new Error("extension name required");var i;if(r.renderer&&(i=D.renderers[r.name],D.renderers[r.name]=i?function(){for(var e=arguments.length,t=new Array(e),u=0;u<e;u++)t[u]=arguments[u];var n=r.renderer.apply(this,t);return n=!1===n?i.apply(this,t):n}:r.renderer),r.tokenizer){if(!r.level||"block"!==r.level&&"inline"!==r.level)throw new Error("extension level must be 'block' or 'inline'");D[r.level]?D[r.level].unshift(r.tokenizer):D[r.level]=[r.tokenizer],r.start&&("block"===r.level?D.startBlock?D.startBlock.push(r.start):D.startBlock=[r.start]:"inline"===r.level&&(D.startInline?D.startInline.push(r.start):D.startInline=[r.start]))}r.childTokens&&(D.childTokens[r.name]=r.childTokens)}),e.extensions=D),s.renderer){var t,a=I.defaults.renderer||new _;for(t in s.renderer)!function(r){var i=a[r];a[r]=function(){for(var e=arguments.length,t=new Array(e),u=0;u<e;u++)t[u]=arguments[u];var n=s.renderer[r].apply(a,t);return n=!1===n?i.apply(a,t):n}}(t);e.renderer=a}if(s.tokenizer){var n,o=I.defaults.tokenizer||new b;for(n in s.tokenizer)!function(r){var i=o[r];o[r]=function(){for(var e=arguments.length,t=new Array(e),u=0;u<e;u++)t[u]=arguments[u];var n=s.tokenizer[r].apply(o,t);return n=!1===n?i.apply(o,t):n}}(n);e.tokenizer=o}if(s.hooks){var r,l=I.defaults.hooks||new T;for(r in s.hooks)!function(r){var i=l[r];T.passThroughHooks.has(r)?l[r]=function(e){return I.defaults.async?Promise.resolve(s.hooks[r].call(l,e)).then(function(e){return i.call(l,e)}):(e=s.hooks[r].call(l,e),i.call(l,e))}:l[r]=function(){for(var e=arguments.length,t=new Array(e),u=0;u<e;u++)t[u]=arguments[u];var n=s.hooks[r].apply(l,t);return n=!1===n?i.apply(l,t):n}}(r);e.hooks=l}s.walkTokens&&(u=I.defaults.walkTokens,e.walkTokens=function(e){var t=[];return t.push(s.walkTokens.call(this,e)),t=u?t.concat(u.call(this,e)):t}),I.setOptions(e)})},I.walkTokens=function(e,a){for(var o,l=[],t=D(e);!(o=t()).done;)!function(){var t=o.value;switch(l=l.concat(a.call(I,t)),t.type){case"table":for(var e=D(t.header);!(u=e()).done;){var u=u.value;l=l.concat(I.walkTokens(u.tokens,a))}for(var n,r=D(t.rows);!(n=r()).done;)for(var i=D(n.value);!(s=i()).done;){var s=s.value;l=l.concat(I.walkTokens(s.tokens,a))}break;case"list":l=l.concat(I.walkTokens(t.items,a));break;default:I.defaults.extensions&&I.defaults.extensions.childTokens&&I.defaults.extensions.childTokens[t.type]?I.defaults.extensions.childTokens[t.type].forEach(function(e){l=l.concat(I.walkTokens(t[e],a))}):t.tokens&&(l=l.concat(I.walkTokens(t.tokens,a)))}}();return l},I.parseInline=R(y.lexInline,S.parseInline),I.Parser=S,I.parser=S.parse,I.Renderer=_,I.TextRenderer=z,I.Lexer=y,I.lexer=y.lex,I.Tokenizer=b,I.Slugger=$,I.Hooks=T;var F=(I.parse=I).options,U=I.setOptions,Q=I.use,M=I.walkTokens,N=I.parseInline,H=I,X=S.parse,G=y.lex;r.Hooks=T,r.Lexer=y,r.Parser=S,r.Renderer=_,r.Slugger=$,r.TextRenderer=z,r.Tokenizer=b,r.getDefaults=e,r.lexer=G,r.marked=I,r.options=F,r.parse=H,r.parseInline=N,r.parser=X,r.setOptions=U,r.use=Q,r.walkTokens=M});
\ No newline at end of file
diff --git a/frontend/public/js/purify.min.js b/frontend/public/js/purify.min.js
new file mode 100644
index 00000000..6baa5c90
--- /dev/null
+++ b/frontend/public/js/purify.min.js
@@ -0,0 +1,3 @@
+/*! @license DOMPurify 3.0.3 | (c) Cure53 and other contributors | Released under the Apache license 2.0 and Mozilla Public License 2.0 | github.com/cure53/DOMPurify/blob/3.0.3/LICENSE */
+!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).DOMPurify=t()}(this,(function(){"use strict";const{entries:e,setPrototypeOf:t,isFrozen:n,getPrototypeOf:o,getOwnPropertyDescriptor:r}=Object;let{freeze:i,seal:a,create:l}=Object,{apply:c,construct:s}="undefined"!=typeof Reflect&&Reflect;c||(c=function(e,t,n){return e.apply(t,n)}),i||(i=function(e){return e}),a||(a=function(e){return e}),s||(s=function(e,t){return new e(...t)});const m=b(Array.prototype.forEach),u=b(Array.prototype.pop),f=b(Array.prototype.push),p=b(String.prototype.toLowerCase),d=b(String.prototype.toString),h=b(String.prototype.match),g=b(String.prototype.replace),T=b(String.prototype.indexOf),y=b(String.prototype.trim),E=b(RegExp.prototype.test),A=(_=TypeError,function(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];return s(_,t)});var _;function b(e){return function(t){for(var n=arguments.length,o=new Array(n>1?n-1:0),r=1;r<n;r++)o[r-1]=arguments[r];return c(e,t,o)}}function N(e,o,r){var i;r=null!==(i=r)&&void 0!==i?i:p,t&&t(e,null);let a=o.length;for(;a--;){let t=o[a];if("string"==typeof t){const e=r(t);e!==t&&(n(o)||(o[a]=e),t=e)}e[t]=!0}return e}function S(t){const n=l(null);for(const[o,r]of e(t))n[o]=r;return n}function R(e,t){for(;null!==e;){const n=r(e,t);if(n){if(n.get)return b(n.get);if("function"==typeof n.value)return b(n.value)}e=o(e)}return function(e){return console.warn("fallback value for",e),null}}const w=i(["a","abbr","acronym","address","area","article","aside","audio","b","bdi","bdo","big","blink","blockquote","body","br","button","canvas","caption","center","cite","code","col","colgroup","content","data","datalist","dd","decorator","del","details","dfn","dialog","dir","div","dl","dt","element","em","fieldset","figcaption","figure","font","footer","form","h1","h2","h3","h4","h5","h6","head","header","hgroup","hr","html","i","img","input","ins","kbd","label","legend","li","main","map","mark","marquee","menu","menuitem","meter","nav","nobr","ol","optgroup","option","output","p","picture","pre","progress","q","rp","rt","ruby","s","samp","section","select","shadow","small","source","spacer","span","strike","strong","style","sub","summary","sup","table","tbody","td","template","textarea","tfoot","th","thead","time","tr","track","tt","u","ul","var","video","wbr"]),D=i(["svg","a","altglyph","altglyphdef","altglyphitem","animatecolor","animatemotion","animatetransform","circle","clippath","defs","desc","ellipse","filter","font","g","glyph","glyphref","hkern","image","line","lineargradient","marker","mask","metadata","mpath","path","pattern","polygon","polyline","radialgradient","rect","stop","style","switch","symbol","text","textpath","title","tref","tspan","view","vkern"]),L=i(["feBlend","feColorMatrix","feComponentTransfer","feComposite","feConvolveMatrix","feDiffuseLighting","feDisplacementMap","feDistantLight","feDropShadow","feFlood","feFuncA","feFuncB","feFuncG","feFuncR","feGaussianBlur","feImage","feMerge","feMergeNode","feMorphology","feOffset","fePointLight","feSpecularLighting","feSpotLight","feTile","feTurbulence"]),v=i(["animate","color-profile","cursor","discard","font-face","font-face-format","font-face-name","font-face-src","font-face-uri","foreignobject","hatch","hatchpath","mesh","meshgradient","meshpatch","meshrow","missing-glyph","script","set","solidcolor","unknown","use"]),x=i(["math","menclose","merror","mfenced","mfrac","mglyph","mi","mlabeledtr","mmultiscripts","mn","mo","mover","mpadded","mphantom","mroot","mrow","ms","mspace","msqrt","mstyle","msub","msup","msubsup","mtable","mtd","mtext","mtr","munder","munderover","mprescripts"]),C=i(["maction","maligngroup","malignmark","mlongdiv","mscarries","mscarry","msgroup","mstack","msline","msrow","semantics","annotation","annotation-xml","mprescripts","none"]),k=i(["#text"]),O=i(["accept","action","align","alt","autocapitalize","autocomplete","autopictureinpicture","autoplay","background","bgcolor","border","capture","cellpadding","cellspacing","checked","cite","class","clear","color","cols","colspan","controls","controlslist","coords","crossorigin","datetime","decoding","default","dir","disabled","disablepictureinpicture","disableremoteplayback","download","draggable","enctype","enterkeyhint","face","for","headers","height","hidden","high","href","hreflang","id","inputmode","integrity","ismap","kind","label","lang","list","loading","loop","low","max","maxlength","media","method","min","minlength","multiple","muted","name","nonce","noshade","novalidate","nowrap","open","optimum","pattern","placeholder","playsinline","poster","preload","pubdate","radiogroup","readonly","rel","required","rev","reversed","role","rows","rowspan","spellcheck","scope","selected","shape","size","sizes","span","srclang","start","src","srcset","step","style","summary","tabindex","title","translate","type","usemap","valign","value","width","xmlns","slot"]),I=i(["accent-height","accumulate","additive","alignment-baseline","ascent","attributename","attributetype","azimuth","basefrequency","baseline-shift","begin","bias","by","class","clip","clippathunits","clip-path","clip-rule","color","color-interpolation","color-interpolation-filters","color-profile","color-rendering","cx","cy","d","dx","dy","diffuseconstant","direction","display","divisor","dur","edgemode","elevation","end","fill","fill-opacity","fill-rule","filter","filterunits","flood-color","flood-opacity","font-family","font-size","font-size-adjust","font-stretch","font-style","font-variant","font-weight","fx","fy","g1","g2","glyph-name","glyphref","gradientunits","gradienttransform","height","href","id","image-rendering","in","in2","k","k1","k2","k3","k4","kerning","keypoints","keysplines","keytimes","lang","lengthadjust","letter-spacing","kernelmatrix","kernelunitlength","lighting-color","local","marker-end","marker-mid","marker-start","markerheight","markerunits","markerwidth","maskcontentunits","maskunits","max","mask","media","method","mode","min","name","numoctaves","offset","operator","opacity","order","orient","orientation","origin","overflow","paint-order","path","pathlength","patterncontentunits","patterntransform","patternunits","points","preservealpha","preserveaspectratio","primitiveunits","r","rx","ry","radius","refx","refy","repeatcount","repeatdur","restart","result","rotate","scale","seed","shape-rendering","specularconstant","specularexponent","spreadmethod","startoffset","stddeviation","stitchtiles","stop-color","stop-opacity","stroke-dasharray","stroke-dashoffset","stroke-linecap","stroke-linejoin","stroke-miterlimit","stroke-opacity","stroke","stroke-width","style","surfacescale","systemlanguage","tabindex","targetx","targety","transform","transform-origin","text-anchor","text-decoration","text-rendering","textlength","type","u1","u2","unicode","values","viewbox","visibility","version","vert-adv-y","vert-origin-x","vert-origin-y","width","word-spacing","wrap","writing-mode","xchannelselector","ychannelselector","x","x1","x2","xmlns","y","y1","y2","z","zoomandpan"]),M=i(["accent","accentunder","align","bevelled","close","columnsalign","columnlines","columnspan","denomalign","depth","dir","display","displaystyle","encoding","fence","frame","height","href","id","largeop","length","linethickness","lspace","lquote","mathbackground","mathcolor","mathsize","mathvariant","maxsize","minsize","movablelimits","notation","numalign","open","rowalign","rowlines","rowspacing","rowspan","rspace","rquote","scriptlevel","scriptminsize","scriptsizemultiplier","selection","separator","separators","stretchy","subscriptshift","supscriptshift","symmetric","voffset","width","xmlns"]),U=i(["xlink:href","xml:id","xlink:title","xml:space","xmlns:xlink"]),P=a(/\{\{[\w\W]*|[\w\W]*\}\}/gm),F=a(/<%[\w\W]*|[\w\W]*%>/gm),H=a(/\${[\w\W]*}/gm),z=a(/^data-[\-\w.\u00B7-\uFFFF]/),B=a(/^aria-[\-\w]+$/),W=a(/^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i),G=a(/^(?:\w+script|data):/i),Y=a(/[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205F\u3000]/g),j=a(/^html$/i);var q=Object.freeze({__proto__:null,MUSTACHE_EXPR:P,ERB_EXPR:F,TMPLIT_EXPR:H,DATA_ATTR:z,ARIA_ATTR:B,IS_ALLOWED_URI:W,IS_SCRIPT_OR_DATA:G,ATTR_WHITESPACE:Y,DOCTYPE_NAME:j});const X=()=>"undefined"==typeof window?null:window,K=function(e,t){if("object"!=typeof e||"function"!=typeof e.createPolicy)return null;let n=null;const o="data-tt-policy-suffix";t&&t.hasAttribute(o)&&(n=t.getAttribute(o));const r="dompurify"+(n?"#"+n:"");try{return e.createPolicy(r,{createHTML:e=>e,createScriptURL:e=>e})}catch(e){return console.warn("TrustedTypes policy "+r+" could not be created."),null}};var V=function t(){let n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:X();const o=e=>t(e);if(o.version="3.0.3",o.removed=[],!n||!n.document||9!==n.document.nodeType)return o.isSupported=!1,o;const r=n.document,a=r.currentScript;let{document:l}=n;const{DocumentFragment:c,HTMLTemplateElement:s,Node:_,Element:b,NodeFilter:P,NamedNodeMap:F=n.NamedNodeMap||n.MozNamedAttrMap,HTMLFormElement:H,DOMParser:z,trustedTypes:B}=n,G=b.prototype,Y=R(G,"cloneNode"),V=R(G,"nextSibling"),$=R(G,"childNodes"),Z=R(G,"parentNode");if("function"==typeof s){const e=l.createElement("template");e.content&&e.content.ownerDocument&&(l=e.content.ownerDocument)}let J,Q="";const{implementation:ee,createNodeIterator:te,createDocumentFragment:ne,getElementsByTagName:oe}=l,{importNode:re}=r;let ie={};o.isSupported="function"==typeof e&&"function"==typeof Z&&ee&&void 0!==ee.createHTMLDocument;const{MUSTACHE_EXPR:ae,ERB_EXPR:le,TMPLIT_EXPR:ce,DATA_ATTR:se,ARIA_ATTR:me,IS_SCRIPT_OR_DATA:ue,ATTR_WHITESPACE:fe}=q;let{IS_ALLOWED_URI:pe}=q,de=null;const he=N({},[...w,...D,...L,...x,...k]);let ge=null;const Te=N({},[...O,...I,...M,...U]);let ye=Object.seal(Object.create(null,{tagNameCheck:{writable:!0,configurable:!1,enumerable:!0,value:null},attributeNameCheck:{writable:!0,configurable:!1,enumerable:!0,value:null},allowCustomizedBuiltInElements:{writable:!0,configurable:!1,enumerable:!0,value:!1}})),Ee=null,Ae=null,_e=!0,be=!0,Ne=!1,Se=!0,Re=!1,we=!1,De=!1,Le=!1,ve=!1,xe=!1,Ce=!1,ke=!0,Oe=!1;const Ie="user-content-";let Me=!0,Ue=!1,Pe={},Fe=null;const He=N({},["annotation-xml","audio","colgroup","desc","foreignobject","head","iframe","math","mi","mn","mo","ms","mtext","noembed","noframes","noscript","plaintext","script","style","svg","template","thead","title","video","xmp"]);let ze=null;const Be=N({},["audio","video","img","source","image","track"]);let We=null;const Ge=N({},["alt","class","for","id","label","name","pattern","placeholder","role","summary","title","value","style","xmlns"]),Ye="http://www.w3.org/1998/Math/MathML",je="http://www.w3.org/2000/svg",qe="http://www.w3.org/1999/xhtml";let Xe=qe,Ke=!1,Ve=null;const $e=N({},[Ye,je,qe],d);let Ze;const Je=["application/xhtml+xml","text/html"],Qe="text/html";let et,tt=null;const nt=l.createElement("form"),ot=function(e){return e instanceof RegExp||e instanceof Function},rt=function(e){if(!tt||tt!==e){if(e&&"object"==typeof e||(e={}),e=S(e),Ze=Ze=-1===Je.indexOf(e.PARSER_MEDIA_TYPE)?Qe:e.PARSER_MEDIA_TYPE,et="application/xhtml+xml"===Ze?d:p,de="ALLOWED_TAGS"in e?N({},e.ALLOWED_TAGS,et):he,ge="ALLOWED_ATTR"in e?N({},e.ALLOWED_ATTR,et):Te,Ve="ALLOWED_NAMESPACES"in e?N({},e.ALLOWED_NAMESPACES,d):$e,We="ADD_URI_SAFE_ATTR"in e?N(S(Ge),e.ADD_URI_SAFE_ATTR,et):Ge,ze="ADD_DATA_URI_TAGS"in e?N(S(Be),e.ADD_DATA_URI_TAGS,et):Be,Fe="FORBID_CONTENTS"in e?N({},e.FORBID_CONTENTS,et):He,Ee="FORBID_TAGS"in e?N({},e.FORBID_TAGS,et):{},Ae="FORBID_ATTR"in e?N({},e.FORBID_ATTR,et):{},Pe="USE_PROFILES"in e&&e.USE_PROFILES,_e=!1!==e.ALLOW_ARIA_ATTR,be=!1!==e.ALLOW_DATA_ATTR,Ne=e.ALLOW_UNKNOWN_PROTOCOLS||!1,Se=!1!==e.ALLOW_SELF_CLOSE_IN_ATTR,Re=e.SAFE_FOR_TEMPLATES||!1,we=e.WHOLE_DOCUMENT||!1,ve=e.RETURN_DOM||!1,xe=e.RETURN_DOM_FRAGMENT||!1,Ce=e.RETURN_TRUSTED_TYPE||!1,Le=e.FORCE_BODY||!1,ke=!1!==e.SANITIZE_DOM,Oe=e.SANITIZE_NAMED_PROPS||!1,Me=!1!==e.KEEP_CONTENT,Ue=e.IN_PLACE||!1,pe=e.ALLOWED_URI_REGEXP||W,Xe=e.NAMESPACE||qe,ye=e.CUSTOM_ELEMENT_HANDLING||{},e.CUSTOM_ELEMENT_HANDLING&&ot(e.CUSTOM_ELEMENT_HANDLING.tagNameCheck)&&(ye.tagNameCheck=e.CUSTOM_ELEMENT_HANDLING.tagNameCheck),e.CUSTOM_ELEMENT_HANDLING&&ot(e.CUSTOM_ELEMENT_HANDLING.attributeNameCheck)&&(ye.attributeNameCheck=e.CUSTOM_ELEMENT_HANDLING.attributeNameCheck),e.CUSTOM_ELEMENT_HANDLING&&"boolean"==typeof e.CUSTOM_ELEMENT_HANDLING.allowCustomizedBuiltInElements&&(ye.allowCustomizedBuiltInElements=e.CUSTOM_ELEMENT_HANDLING.allowCustomizedBuiltInElements),Re&&(be=!1),xe&&(ve=!0),Pe&&(de=N({},[...k]),ge=[],!0===Pe.html&&(N(de,w),N(ge,O)),!0===Pe.svg&&(N(de,D),N(ge,I),N(ge,U)),!0===Pe.svgFilters&&(N(de,L),N(ge,I),N(ge,U)),!0===Pe.mathMl&&(N(de,x),N(ge,M),N(ge,U))),e.ADD_TAGS&&(de===he&&(de=S(de)),N(de,e.ADD_TAGS,et)),e.ADD_ATTR&&(ge===Te&&(ge=S(ge)),N(ge,e.ADD_ATTR,et)),e.ADD_URI_SAFE_ATTR&&N(We,e.ADD_URI_SAFE_ATTR,et),e.FORBID_CONTENTS&&(Fe===He&&(Fe=S(Fe)),N(Fe,e.FORBID_CONTENTS,et)),Me&&(de["#text"]=!0),we&&N(de,["html","head","body"]),de.table&&(N(de,["tbody"]),delete Ee.tbody),e.TRUSTED_TYPES_POLICY){if("function"!=typeof e.TRUSTED_TYPES_POLICY.createHTML)throw A('TRUSTED_TYPES_POLICY configuration option must provide a "createHTML" hook.');if("function"!=typeof e.TRUSTED_TYPES_POLICY.createScriptURL)throw A('TRUSTED_TYPES_POLICY configuration option must provide a "createScriptURL" hook.');J=e.TRUSTED_TYPES_POLICY,Q=J.createHTML("")}else void 0===J&&(J=K(B,a)),null!==J&&"string"==typeof Q&&(Q=J.createHTML(""));i&&i(e),tt=e}},it=N({},["mi","mo","mn","ms","mtext"]),at=N({},["foreignobject","desc","title","annotation-xml"]),lt=N({},["title","style","font","a","script"]),ct=N({},D);N(ct,L),N(ct,v);const st=N({},x);N(st,C);const mt=function(e){let t=Z(e);t&&t.tagName||(t={namespaceURI:Xe,tagName:"template"});const n=p(e.tagName),o=p(t.tagName);return!!Ve[e.namespaceURI]&&(e.namespaceURI===je?t.namespaceURI===qe?"svg"===n:t.namespaceURI===Ye?"svg"===n&&("annotation-xml"===o||it[o]):Boolean(ct[n]):e.namespaceURI===Ye?t.namespaceURI===qe?"math"===n:t.namespaceURI===je?"math"===n&&at[o]:Boolean(st[n]):e.namespaceURI===qe?!(t.namespaceURI===je&&!at[o])&&(!(t.namespaceURI===Ye&&!it[o])&&(!st[n]&&(lt[n]||!ct[n]))):!("application/xhtml+xml"!==Ze||!Ve[e.namespaceURI]))},ut=function(e){f(o.removed,{element:e});try{e.parentNode.removeChild(e)}catch(t){e.remove()}},ft=function(e,t){try{f(o.removed,{attribute:t.getAttributeNode(e),from:t})}catch(e){f(o.removed,{attribute:null,from:t})}if(t.removeAttribute(e),"is"===e&&!ge[e])if(ve||xe)try{ut(t)}catch(e){}else try{t.setAttribute(e,"")}catch(e){}},pt=function(e){let t,n;if(Le)e="<remove></remove>"+e;else{const t=h(e,/^[\r\n\t ]+/);n=t&&t[0]}"application/xhtml+xml"===Ze&&Xe===qe&&(e='<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body>'+e+"</body></html>");const o=J?J.createHTML(e):e;if(Xe===qe)try{t=(new z).parseFromString(o,Ze)}catch(e){}if(!t||!t.documentElement){t=ee.createDocument(Xe,"template",null);try{t.documentElement.innerHTML=Ke?Q:o}catch(e){}}const r=t.body||t.documentElement;return e&&n&&r.insertBefore(l.createTextNode(n),r.childNodes[0]||null),Xe===qe?oe.call(t,we?"html":"body")[0]:we?t.documentElement:r},dt=function(e){return te.call(e.ownerDocument||e,e,P.SHOW_ELEMENT|P.SHOW_COMMENT|P.SHOW_TEXT,null,!1)},ht=function(e){return e instanceof H&&("string"!=typeof e.nodeName||"string"!=typeof e.textContent||"function"!=typeof e.removeChild||!(e.attributes instanceof F)||"function"!=typeof e.removeAttribute||"function"!=typeof e.setAttribute||"string"!=typeof e.namespaceURI||"function"!=typeof e.insertBefore||"function"!=typeof e.hasChildNodes)},gt=function(e){return"object"==typeof _?e instanceof _:e&&"object"==typeof e&&"number"==typeof e.nodeType&&"string"==typeof e.nodeName},Tt=function(e,t,n){ie[e]&&m(ie[e],(e=>{e.call(o,t,n,tt)}))},yt=function(e){let t;if(Tt("beforeSanitizeElements",e,null),ht(e))return ut(e),!0;const n=et(e.nodeName);if(Tt("uponSanitizeElement",e,{tagName:n,allowedTags:de}),e.hasChildNodes()&&!gt(e.firstElementChild)&&(!gt(e.content)||!gt(e.content.firstElementChild))&&E(/<[/\w]/g,e.innerHTML)&&E(/<[/\w]/g,e.textContent))return ut(e),!0;if(!de[n]||Ee[n]){if(!Ee[n]&&At(n)){if(ye.tagNameCheck instanceof RegExp&&E(ye.tagNameCheck,n))return!1;if(ye.tagNameCheck instanceof Function&&ye.tagNameCheck(n))return!1}if(Me&&!Fe[n]){const t=Z(e)||e.parentNode,n=$(e)||e.childNodes;if(n&&t){for(let o=n.length-1;o>=0;--o)t.insertBefore(Y(n[o],!0),V(e))}}return ut(e),!0}return e instanceof b&&!mt(e)?(ut(e),!0):"noscript"!==n&&"noembed"!==n||!E(/<\/no(script|embed)/i,e.innerHTML)?(Re&&3===e.nodeType&&(t=e.textContent,t=g(t,ae," "),t=g(t,le," "),t=g(t,ce," "),e.textContent!==t&&(f(o.removed,{element:e.cloneNode()}),e.textContent=t)),Tt("afterSanitizeElements",e,null),!1):(ut(e),!0)},Et=function(e,t,n){if(ke&&("id"===t||"name"===t)&&(n in l||n in nt))return!1;if(be&&!Ae[t]&&E(se,t));else if(_e&&E(me,t));else if(!ge[t]||Ae[t]){if(!(At(e)&&(ye.tagNameCheck instanceof RegExp&&E(ye.tagNameCheck,e)||ye.tagNameCheck instanceof Function&&ye.tagNameCheck(e))&&(ye.attributeNameCheck instanceof RegExp&&E(ye.attributeNameCheck,t)||ye.attributeNameCheck instanceof Function&&ye.attributeNameCheck(t))||"is"===t&&ye.allowCustomizedBuiltInElements&&(ye.tagNameCheck instanceof RegExp&&E(ye.tagNameCheck,n)||ye.tagNameCheck instanceof Function&&ye.tagNameCheck(n))))return!1}else if(We[t]);else if(E(pe,g(n,fe,"")));else if("src"!==t&&"xlink:href"!==t&&"href"!==t||"script"===e||0!==T(n,"data:")||!ze[e]){if(Ne&&!E(ue,g(n,fe,"")));else if(n)return!1}else;return!0},At=function(e){return e.indexOf("-")>0},_t=function(e){let t,n,r,i;Tt("beforeSanitizeAttributes",e,null);const{attributes:a}=e;if(!a)return;const l={attrName:"",attrValue:"",keepAttr:!0,allowedAttributes:ge};for(i=a.length;i--;){t=a[i];const{name:c,namespaceURI:s}=t;if(n="value"===c?t.value:y(t.value),r=et(c),l.attrName=r,l.attrValue=n,l.keepAttr=!0,l.forceKeepAttr=void 0,Tt("uponSanitizeAttribute",e,l),n=l.attrValue,l.forceKeepAttr)continue;if(ft(c,e),!l.keepAttr)continue;if(!Se&&E(/\/>/i,n)){ft(c,e);continue}Re&&(n=g(n,ae," "),n=g(n,le," "),n=g(n,ce," "));const m=et(e.nodeName);if(Et(m,r,n)){if(!Oe||"id"!==r&&"name"!==r||(ft(c,e),n=Ie+n),J&&"object"==typeof B&&"function"==typeof B.getAttributeType)if(s);else switch(B.getAttributeType(m,r)){case"TrustedHTML":n=J.createHTML(n);break;case"TrustedScriptURL":n=J.createScriptURL(n)}try{s?e.setAttributeNS(s,c,n):e.setAttribute(c,n),u(o.removed)}catch(e){}}}Tt("afterSanitizeAttributes",e,null)},bt=function e(t){let n;const o=dt(t);for(Tt("beforeSanitizeShadowDOM",t,null);n=o.nextNode();)Tt("uponSanitizeShadowNode",n,null),yt(n)||(n.content instanceof c&&e(n.content),_t(n));Tt("afterSanitizeShadowDOM",t,null)};return o.sanitize=function(e){let t,n,i,a,l=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(Ke=!e,Ke&&(e="\x3c!--\x3e"),"string"!=typeof e&&!gt(e)){if("function"!=typeof e.toString)throw A("toString is not a function");if("string"!=typeof(e=e.toString()))throw A("dirty is not a string, aborting")}if(!o.isSupported)return e;if(De||rt(l),o.removed=[],"string"==typeof e&&(Ue=!1),Ue){if(e.nodeName){const t=et(e.nodeName);if(!de[t]||Ee[t])throw A("root node is forbidden and cannot be sanitized in-place")}}else if(e instanceof _)t=pt("\x3c!----\x3e"),n=t.ownerDocument.importNode(e,!0),1===n.nodeType&&"BODY"===n.nodeName||"HTML"===n.nodeName?t=n:t.appendChild(n);else{if(!ve&&!Re&&!we&&-1===e.indexOf("<"))return J&&Ce?J.createHTML(e):e;if(t=pt(e),!t)return ve?null:Ce?Q:""}t&&Le&&ut(t.firstChild);const s=dt(Ue?e:t);for(;i=s.nextNode();)yt(i)||(i.content instanceof c&&bt(i.content),_t(i));if(Ue)return e;if(ve){if(xe)for(a=ne.call(t.ownerDocument);t.firstChild;)a.appendChild(t.firstChild);else a=t;return(ge.shadowroot||ge.shadowrootmod)&&(a=re.call(r,a,!0)),a}let m=we?t.outerHTML:t.innerHTML;return we&&de["!doctype"]&&t.ownerDocument&&t.ownerDocument.doctype&&t.ownerDocument.doctype.name&&E(j,t.ownerDocument.doctype.name)&&(m="<!DOCTYPE "+t.ownerDocument.doctype.name+">\n"+m),Re&&(m=g(m,ae," "),m=g(m,le," "),m=g(m,ce," ")),J&&Ce?J.createHTML(m):m},o.setConfig=function(e){rt(e),De=!0},o.clearConfig=function(){tt=null,De=!1},o.isValidAttribute=function(e,t,n){tt||rt({});const o=et(e),r=et(t);return Et(o,r,n)},o.addHook=function(e,t){"function"==typeof t&&(ie[e]=ie[e]||[],f(ie[e],t))},o.removeHook=function(e){if(ie[e])return u(ie[e])},o.removeHooks=function(e){ie[e]&&(ie[e]=[])},o.removeAllHooks=function(){ie={}},o}();return V}));
+//# sourceMappingURL=purify.min.js.map
diff --git a/frontend/src/components/ChatBubble.astro b/frontend/src/components/ChatBubble.astro
new file mode 100644
index 00000000..39d4027b
--- /dev/null
+++ b/frontend/src/components/ChatBubble.astro
@@ -0,0 +1,70 @@
+---
+export interface Props {
+ shown?: boolean;
+}
+
+const { shown = true } = Astro.props;
+const shownClass = !shown ? 'hidden' : '';
+---
+
+<div class={"chat-bubble "+shownClass}>
+ <div class="typing">
+ <div class="dot"></div>
+ <div class="dot"></div>
+ <div class="dot"></div>
+ </div>
+</div>
+
+<style>
+ .chat-bubble {
+ padding-top: 5px;
+ padding-left: 10px;
+ }
+
+ .typing {
+ align-items: center;
+ display: flex;
+ height: 17px;
+ }
+
+ .typing .dot {
+ animation: mercuryTypingAnimation 1.8s infinite ease-in-out;
+ background-color: rgba(11, 123, 255, .7);
+ border-radius: 50%;
+ height: 7px;
+ margin-right: 4px;
+ vertical-align: middle;
+ width: 7px;
+ }
+
+ .typing .dot:nth-child(1) {
+ animation-delay: 200ms;
+ }
+
+ .typing .dot:nth-child(2) {
+ animation-delay: 300ms;
+ }
+
+ .typing .dot:nth-child(3) {
+ animation-delay: 400ms;
+ }
+
+ .typing .dot:last-child {
+ margin-right: 0;
+ }
+
+ @keyframes mercuryTypingAnimation {
+ 0% {
+ transform: translateY(0px);
+ background-color:rgba(11, 123, 255, .7);
+ }
+ 28% {
+ transform: translateY(-7px);
+ background-color:rgba(11, 123, 255, .4);
+ }
+ 44% {
+ transform: translateY(0px);
+ background-color: rgba(11, 123, 255, .2);
+ }
+ }
+</style>
\ No newline at end of file
diff --git a/frontend/src/components/IndexBar.astro b/frontend/src/components/IndexBar.astro
index b3d3d2a6..289886c2 100644
--- a/frontend/src/components/IndexBar.astro
+++ b/frontend/src/components/IndexBar.astro
@@ -2,6 +2,7 @@
const links = [
{ url: "/search", title: "Search" },
{ url: "/explore", title: "Explore" },
+ { url: "/chat", title: "Chat" },
];
export interface Props {
diff --git a/frontend/src/components/SearchModal.astro b/frontend/src/components/SearchModal.astro
index 509dbbd9..9afeae09 100644
--- a/frontend/src/components/SearchModal.astro
+++ b/frontend/src/components/SearchModal.astro
@@ -209,6 +209,7 @@ import Hero from "./Hero.astro";
"summary": {
"text": "",
"hasStarted": false,
+ "hasFinished": false,
}};
}
}
@@ -225,7 +226,6 @@ import Hero from "./Hero.astro";
return;
}
var snippetText = snippetElem.querySelector(".snippet-text");
- var snippetBubble = snippetElem.querySelector(".chat-bubble");
var idx = parseInt(currentIdx);
var snip = snippets[idx];
@@ -233,18 +233,18 @@ import Hero from "./Hero.astro";
snip["summaryShown"] = !snip["summaryShown"];
if (snip["summaryShown"]) {
btnSummarize.classList.add("active");
- summarize(idx, snippetBubble, snippetText);
+ summarize(idx, snippetText);
} else {
- snippetBubble.classList.add("hidden");
btnSummarize.classList.remove("active");
snippetText.innerHTML = snip["origSnippet"];
+ snippetText.classList.remove("blinking-cursor");
}
}
- function summarize(idx, snippetBubble, snippetText) {
+ function summarize(idx, snippetText) {
var snip = snippets[idx];
snippetText.innerHTML = snip['summary']["text"];
@@ -258,29 +258,37 @@ import Hero from "./Hero.astro";
var queryData = new URLSearchParams(reqData).toString();
- if (snip["summary"]["text"] == "") {
- snippetBubble.classList.remove("hidden");
+ if (!snip["summary"]["hasFinished"]) {
+ snippetText.classList.add("blinking-cursor");
}
if (!snip["summary"]["hasStarted"]) {
snip["summary"]["hasStarted"] = true;
- fetch("/beta/api/summarize?" + queryData, {
- method: "GET",
- }).then((e) => {
- snippetBubble.classList.add("hidden");
+ var source = new EventSource("/beta/api/summarize?" + queryData);
- if (e.status != 200) {
- console.log("summarization error", e);
- snippetText.innerHTML = snip["origSnippet"];
+ source.onmessage = function (event) {
+ console.log(event);
+ snip["summary"]["text"] += event.data;
+
+ if (snip["summaryShown"]) {
+ snippetText.innerHTML = snip["summary"]["text"];
+ }
+
+ if (event.data == "") {
+ source.close();
+ snip["summary"]["hasFinished"] = true;
+ snippetText.classList.remove("blinking-cursor");
return;
}
+ };
- return e.text();
- }).then((text) => {
- snip["summary"]["text"] = text;
- snippetText.innerHTML = snip["summary"]["text"];
- });
+ // this is called when the connection is closed by the server
+ source.onerror = function (event) {
+ snip["summary"]["hasFinished"] = true;
+ snippetText.classList.remove("blinking-cursor");
+ source.close();
+ };
}
}
</script>
diff --git a/frontend/src/components/Snippet.astro b/frontend/src/components/Snippet.astro
index 94bff9e3..baa33bdf 100644
--- a/frontend/src/components/Snippet.astro
+++ b/frontend/src/components/Snippet.astro
@@ -1,6 +1,7 @@
---
import Hero from "./Hero.astro";
import { askama } from "../askama";
+import ChatBubble from "./ChatBubble.astro";
export interface Props {
snippet: string;
@@ -16,17 +17,10 @@ export interface Props {
{askama.if_("let Some(date) = date", () => (
<span class="text-gray-500">{askama`date $ 2. May 2022`}</span>
))}
- <div id={"snippet-" + askama`loop.index0`}>
+ <div id={"snippet-" + askama`loop.index0`} class="inline">
<span id="snippet-text" class="[&:nth-child(2)]:before:content-['—'] snippet-text">
{askama`text|safe $ {{lorem.lines}}`}
</span>
- <div class="chat-bubble hidden">
- <div class="typing">
- <div class="dot"></div>
- <div class="dot"></div>
- <div class="dot"></div>
- </div>
- </div>
</div>
</div>
),
@@ -93,50 +87,18 @@ export interface Props {
</div>
<style>
- .chat-bubble {
- padding-top: 5px;
- padding-left: 10px;
- }
-
- .typing {
- align-items: center;
- display: flex;
- height: 17px;
- }
- .typing .dot {
- animation: mercuryTypingAnimation 1.8s infinite ease-in-out;
- background-color: rgba(20, 105, 69, .7);
- border-radius: 50%;
- height: 7px;
- margin-right: 4px;
- vertical-align: middle;
- width: 7px;
- }
- .typing .dot:nth-child(1) {
- animation-delay: 200ms;
- }
- .typing .dot:nth-child(2) {
- animation-delay: 300ms;
- }
- .typing .dot:nth-child(3) {
- animation-delay: 400ms;
- }
- .typing .dot:last-child {
- margin-right: 0;
- }
-
- @keyframes mercuryTypingAnimation {
+ @keyframes cursor-blink {
0% {
- transform: translateY(0px);
- background-color:rgba(20, 105, 69, .7);
- }
- 28% {
- transform: translateY(-7px);
- background-color:rgba(20, 105, 69, .4);
- }
- 44% {
- transform: translateY(0px);
- background-color: rgba(20, 105, 69, .2);
+ opacity: 0;
}
}
+
+ .blinking-cursor::after {
+ content: "";
+ width: 2px;
+ height: 1rem;
+ background: #292d30;
+ display: inline-block;
+ animation: cursor-blink 1s steps(2) infinite;
+ }
</style>
\ No newline at end of file
diff --git a/frontend/src/layouts/Markdown.astro b/frontend/src/layouts/Markdown.astro
index 09d50e6a..e496566b 100644
--- a/frontend/src/layouts/Markdown.astro
+++ b/frontend/src/layouts/Markdown.astro
@@ -12,9 +12,9 @@ const { frontmatter } = Astro.props;
<Layout title={frontmatter.title}>
<div class="flex h-full w-full flex-col">
- <Header icon = {false} />
+ <Header />
- <div class="flex h-fit w-full flex-col items-center pt-10">
+ <div class="flex h-fit w-full flex-col items-center pt-10 px-5">
<div
class="prose prose-sm prose-headings:text-center prose-headings:font-medium prose-h4:float-left prose-h4:my-0 prose-h4:mr-3 prose-h4:leading-7"
>
diff --git a/frontend/src/pages/chat.astro b/frontend/src/pages/chat.astro
new file mode 100644
index 00000000..a31637fd
--- /dev/null
+++ b/frontend/src/pages/chat.astro
@@ -0,0 +1,305 @@
+---
+import Footer from "../components/Footer.astro";
+import Header from "../components/Header.astro";
+import Layout from "../layouts/Layout.astro";
+import Hero from "../components/Hero.astro";
+import ChatBubble from "../components/ChatBubble.astro";
+import { askama } from "../askama";
+---
+
+<Layout title="Stract">
+ <div class="relative grid h-full grid-rows-[auto_1fr_auto]">
+ <div id="header" class="row-start-1">
+ <Header showDivider={true} activeIndex={2} queryUrlPart={askama`query_url_part $ ""`} />
+ </div>
+ <div class="flex flex-col row-start-2 mt-10 px-2 items-center">
+ <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>
+ </div>
+ </div>
+ </div>
+ </div>
+ <div id="footer" class="row-start-3">
+ <Footer />
+ </div>
+ </div>
+<div class="status-template hidden">
+ <ChatBubble />
+ <div id="info" class="text-gray-500"></div>
+</div>
+</Layout>
+
+<style is:inline>
+ .user-message-container {
+ width: 100%;
+ display: flex;
+ justify-content: flex-end;
+ padding-left: 1.25rem;
+ }
+
+ .user-message {
+ padding: 0.5rem;
+ background-color: rgba(11, 123, 255, 0.05);
+ border-color: rgba(11, 123, 255, 0.3);
+ width: fit-content;
+ height: fit-content;
+ border-width: 1px;
+ border-radius: 0.75rem;
+ border-bottom-right-radius: 0;
+ }
+
+ .alice-message {
+ padding: 0.5rem;
+ background-color: rgba(0, 0, 0, 0.025);
+ border-color: rgba(0, 0, 0, 0.15);
+ width: fit-content;
+ height: fit-content;
+ border-width: 1px;
+ border-radius: 0.75rem;
+ border-bottom-left-radius: 0;
+ }
+
+ .alice-message-container {
+ width: 100%;
+ display: flex;
+ justify-content: flex-start;
+ padding-right: 1.25rem;
+ }
+</style>
+
+<script is:inline src="/js/marked.min.js"></script>
+<script is:inline src="/js/purify.min.js"></script>
+
+<link rel="stylesheet" href="/css/highlightjs-default.min.css" />
+<script is:inline src="/js/highlight.min.js"></script>
+
+<script>
+ const messageInput: HTMLTextAreaElement = document.querySelector("#message-input")!;
+ const sendButton: HTMLDivElement = document.querySelector("#send-button")!;
+ const messageContainer: HTMLDivElement = document.querySelector("#message-container")!;
+ const statusTemplate: HTMLDivElement = document.querySelector(".status-template")!;
+
+ // let prevState: number[][] | null = null;
+ let prevState: any | null = null;
+
+ let originalSendButtonColor = sendButton.style.color;
+
+ let numMessageLines = 1;
+ let messageInputHeight = 24;
+ let messages: {from: string, message: string, queries: {query: string, results: {text: string, url: string, domain: string}[]}[]}[] = [];
+ let aliceResponding = false;
+ let aliceStreamingMessage = "";
+ let currentQueries: {query: string, results: {text: string, url: string, domain: string}[]}[] = [];
+
+ function updateMessageInputHeight() {
+ const lines = messageInput.value.split("\n");
+ numMessageLines = lines.length;
+ messageInput.style.height = `${messageInputHeight*numMessageLines}px`;
+ }
+
+ updateMessageInputHeight();
+
+ function scrollToBottom() {
+ window.scrollTo(0, document.body.scrollHeight);}
+
+ sendButton.addEventListener("click", sendMessage);
+
+ messageInput.addEventListener("keydown", (event) => {
+ if (event.key === "Enter" && !event.shiftKey) {
+ event.preventDefault();
+ sendMessage().then(() => {
+ updateMessageInputHeight();
+ })
+ }
+ });
+
+ messageInput.addEventListener("input", (_) => {
+ updateMessageInputHeight();
+ });
+
+ async function sendMessage() {
+ const message = messageInput.value;
+
+ if (message.length == 0) {
+ return;
+ }
+
+ addMessage("user", message);
+ await sendMessageToAlice(message);
+ messageInput.value = "";
+ }
+
+ async function save_prev_state(): Promise<string> {
+ let url = "/beta/api/alice/save_state";
+ var reqData: Record<string, string> = {
+ 'state': prevState,
+ };
+
+ // post request using fetch api
+ let response = await fetch(url, {
+ method: 'POST',
+ body: JSON.stringify(reqData),
+ headers: {
+ 'Content-Type': 'application/json'
+ }
+ });
+
+ return await response.text();
+ }
+
+ function beginAliceStream() {
+ aliceResponding = true;
+ aliceStreamingMessage = "";
+
+ // disable input
+ messageInput.disabled = true;
+ sendButton.style.color = "rgba(0, 0, 0, 0.25)";
+
+ scrollToBottom();
+
+ // show status
+ const status = statusTemplate.cloneNode(true) as HTMLDivElement;
+ status.classList.remove("status-template");
+ status.classList.remove("hidden");
+
+ status.id = "active-status"
+
+ messageContainer.appendChild(status);
+ }
+
+ function streamMessage(data: any) {
+ if (!aliceResponding) {
+ return;
+ }
+
+ if (data["@type"] == "speaking") {
+ if (aliceStreamingMessage == "") {
+ addMessage("alice", "", currentQueries);
+ currentQueries = [];
+ }
+ aliceStreamingMessage += data["text"];
+
+ const lastMessage = messages[messages.length - 1];
+ lastMessage.message = aliceStreamingMessage;
+ renderMessages();
+ } 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>`;
+ } else if (data["@type"] == "searchResult") {
+ currentQueries.push({
+ query: data["query"],
+ results: data["result"]
+ });
+ } else if (data["@type"] == "done") {
+ prevState = data["state"];
+ endAliceStream();
+ }
+ }
+
+ function endAliceStream() {
+ aliceResponding = false;
+ aliceStreamingMessage = "";
+
+ // enable input
+ messageInput.disabled = false;
+ sendButton.style.color = originalSendButtonColor;
+ messageInput.focus();
+ }
+
+ async function sendMessageToAlice(message: string) {
+ beginAliceStream();
+
+ var reqData: Record<string, string> = {
+ 'message': message,
+ };
+
+ if (prevState) {
+ let savedState = await save_prev_state();
+ reqData['prevState'] = savedState;
+ }
+
+ var queryData = new URLSearchParams(reqData).toString();
+ var source = new EventSource("/beta/api/alice?" + queryData);
+
+ source.onmessage = function(event) {
+ const data = JSON.parse(event.data);
+
+ streamMessage(data);
+ };
+
+ // also called when the connection is closed from the server
+ source.onerror = function(_) {
+ endAliceStream();
+ source.close();
+ };
+ }
+
+ function addMessage(from: string, message: string, queries: {query: string, results: {text: string, url: string, domain: string}[]}[] = []) {
+ messages.push({
+ from: from,
+ message: message,
+ queries: queries
+ });
+
+ renderMessages();
+ scrollToBottom();
+ }
+
+ function renderMessages() {
+ messageContainer.innerHTML = "";
+
+ for (const message of messages) {
+ let renderedMessage = renderMarkdown(message.message);
+
+ if (message.from === "user") {
+ const msg = document.createElement("div");
+ msg.classList.add("user-message-container");
+
+ const msgElem = document.createElement("div");
+ msgElem.classList.add("user-message");
+ msgElem.innerHTML = renderedMessage;
+
+ msg.appendChild(msgElem);
+
+ messageContainer.appendChild(msg);
+ } else {
+ const msg = document.createElement("div");
+ msg.classList.add("alice-message-container");
+
+ const msgElem = document.createElement("div");
+ msgElem.classList.add("alice-message");
+ msgElem.innerHTML = renderedMessage;
+
+ msg.appendChild(msgElem);
+
+ messageContainer.appendChild(msg);
+ }
+ }
+
+ // @ts-ignore
+ hljs.highlightAll();
+ removeBackgroundFromCode();
+ }
+
+ function renderMarkdown(markdown: string) {
+ // @ts-ignore
+ const html = marked.parse(markdown);
+ // @ts-ignore
+ const sanitized = DOMPurify.sanitize(html);
+ return sanitized;
+ }
+
+ function removeBackgroundFromCode() {
+ const codeBlocks = document.querySelectorAll("code");
+ for (const codeBlock of codeBlocks) {
+ codeBlock.style.background = "none";
+ }
+ }
+</script>
\ No newline at end of file
diff --git a/frontend/src/pages/explore.astro b/frontend/src/pages/explore.astro
index afc76bc6..82ca0949 100644
--- a/frontend/src/pages/explore.astro
+++ b/frontend/src/pages/explore.astro
@@ -7,9 +7,11 @@ import { askama } from "../askama";
---
<Layout title="Stract">
- <Header showDivider={true} activeIndex={1} queryUrlPart={askama`query_url_part $ ""`} />
<div class="relative grid h-full grid-rows-[auto_1fr_auto]">
- <div class="flex mt-10 px-5 justify-center">
+ <div class="row-start-1">
+ <Header showDivider={true} activeIndex={1} queryUrlPart={askama`query_url_part $ ""`} />
+ </div>
+ <div class="row-start-2 flex mt-10 px-5 justify-center">
<div class="flex flex-col grow max-w-3xl">
<noscript>
<div class="text-red-600">Unfortunately, this site requires javascript to function.</div>
diff --git a/frontend/src/pages/index.astro b/frontend/src/pages/index.astro
index e4725fc2..7b749424 100644
--- a/frontend/src/pages/index.astro
+++ b/frontend/src/pages/index.astro
@@ -7,7 +7,7 @@ import OpticSelector from "../components/OpticSelector.astro";
---
<Layout title="Stract">
- <div class="relative grid h-full grid-rows-[auto_1fr_auto]">
+ <div class="grid h-full grid-rows-[auto_1fr_auto]">
<Header showLogo={false} />
<div class="absolute top-1/4 grid w-full place-items-center">
<div
diff --git a/frontend/src/pages/search.astro b/frontend/src/pages/search.astro
index 061b34af..79c0aa8a 100644
--- a/frontend/src/pages/search.astro
+++ b/frontend/src/pages/search.astro
@@ -267,6 +267,7 @@ import Searchbar from "../components/Searchbar.astro";
var improvementSent = new Set();
var urls = [];
+
for (const elem of titleLinks) {
urls.push(elem.href);
}
diff --git a/frontend/src/pages/settings/privacy.astro b/frontend/src/pages/settings/privacy.astro
index d5dfe2cd..ef28eb1d 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/query_store.rs#L36-#L49">here</a>.
+ In fact, you can see exactly what gets stored <a href="https://github.com/StractOrg/Stract/blob/main/core/src/query_store.rs#L36-#L50">here</a>.
</div>
<div class="text-sm">
diff --git a/justfile b/justfile
index 9e8ca4e7..cea9b66e 100644
--- a/justfile
+++ b/justfile
@@ -8,7 +8,7 @@
@frontend-rerun *ARGS:
cd frontend; npm run build
- bash scripts/run_frontend.sh {{ARGS}}
+ ./scripts/run_frontend.py {{ARGS}}
@frontend *ARGS:
cd frontend; npm install
diff --git a/scripts/pth_to_safetensors.py b/scripts/pth_to_safetensors.py
new file mode 100644
index 00000000..5aa01e52
--- /dev/null
+++ b/scripts/pth_to_safetensors.py
@@ -0,0 +1,29 @@
+import torch
+from safetensors.torch import save_file, save_model
+import argparse
+
+parser = argparse.ArgumentParser(description="Convert a PyTorch model to SafeTensors.")
+parser.add_argument("model", type=str, help="The model to convert.")
+args = parser.parse_args()
+
+args.model = args.model.replace(".pth", "")
+
+
+def main():
+ input = f"{args.model}.pth"
+ output = f"{args.model}.safetensors"
+ print(f"* Loading with Torch: {input}")
+ weights = torch.load(input, map_location="cpu")
+
+ # fix shared weights
+ for name in weights:
+ weights[name] = weights[name].clone().contiguous()
+
+ print(f"* Saving with SafeTensors: {output}")
+ save_file(weights, output)
+
+ print("* Done.")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/scripts/requirements.txt b/scripts/requirements.txt
index 896e4cc2..f41096d8 100644
--- a/scripts/requirements.txt
+++ b/scripts/requirements.txt
@@ -2,4 +2,5 @@ transformers
tensorboard>1.15.0
torch>=2.0.0
datasets
-gensim
\ No newline at end of file
+gensim
+safetensors
diff --git a/scripts/run_frontend.py b/scripts/run_frontend.py
new file mode 100755
index 00000000..f412b79e
--- /dev/null
+++ b/scripts/run_frontend.py
@@ -0,0 +1,57 @@
+#!.venv/bin/python3
+import argparse
+import subprocess
+
+parser = argparse.ArgumentParser()
+
+parser.add_argument("--release", action="store_true")
+parser.add_argument("--alice", action="store_true")
+
+args = parser.parse_args()
+
+release = ["--release"] if args.release else []
+
+processes = []
+
+processes.append(
+ subprocess.Popen(
+ ["cargo", "run"] + release + ["--", "frontend", "configs/frontend.toml"]
+ )
+)
+processes.append(
+ subprocess.Popen(
+ ["cargo", "run"]
+ + release
+ + ["--", "search-server", "configs/search_server.toml"]
+ )
+)
+processes.append(
+ subprocess.Popen(
+ ["cargo", "run"]
+ + release
+ + [
+ "--",
+ "webgraph",
+ "server",
+ "configs/webgraph/server.toml",
+ ]
+ )
+)
+
+if args.alice:
+ processes.append(
+ subprocess.Popen(
+ ["cargo", "run"] + release + ["--", "alice", "serve", "configs/alice.toml"]
+ )
+ )
+
+# kill processes on ctrl-c
+import time
+
+while True:
+ try:
+ time.sleep(1)
+ except KeyboardInterrupt:
+ for p in processes:
+ p.kill()
+ break
diff --git a/scripts/run_frontend.sh b/scripts/run_frontend.sh
deleted file mode 100644
index c4792f3a..00000000
--- a/scripts/run_frontend.sh
+++ /dev/null
@@ -1,5 +0,0 @@
-#!/bin/bash
-trap 'kill $(jobs -p)' EXIT
-cargo run $1 -- frontend configs/frontend.toml &
-cargo run $1 -- search-server configs/search_server.toml &
-cargo run $1 -- webgraph server configs/webgraph/server.toml