Compare commits

...

2 Commits

Author SHA1 Message Date
607ff8458d Switch to llama for summarization
Still not sure if this works at all, or how fast I'll be going over the
free tier limit with this. But, the bart-large-cnn is pretty awful so I
want to try something better.
2025-03-05 23:41:25 -05:00
2981b9cfff Update crates and enable logging 2025-03-05 23:40:23 -05:00
4 changed files with 62 additions and 34 deletions

18
Cargo.lock generated
View File

@@ -1,6 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
version = 4
[[package]]
name = "addr2line"
@@ -567,9 +567,9 @@ checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
[[package]]
name = "markdown"
version = "1.0.0-alpha.20"
version = "1.0.0-alpha.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "911a8325e6fb87b89890cd4529a2ab34c2669c026279e61c26b7140a3d821ccb"
checksum = "9047e0a37a596d4e15411a1ffbdabe71c328908cb90a721cb9bf8dcf3434e6d2"
dependencies = [
"unicode-id",
]
@@ -1524,9 +1524,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "worker"
version = "0.3.4"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3bd73bd2ea409ae91df99293cbed8b892d39c4c0df5039b646be7586df62c6b"
checksum = "727789ca7eff9733efbea9d0e97779edc1cf1926e98aee7d7d8afe32805458aa"
dependencies = [
"async-trait",
"bytes",
@@ -1570,9 +1570,9 @@ dependencies = [
[[package]]
name = "worker-macros"
version = "0.3.4"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bbf47d65e77652febb28abedac18b317d8dfe4e57f0d8d9998c4e991fca8e23"
checksum = "7d625c24570ba9207a2617476013335f28a95cbe513e59bb814ffba092a18058"
dependencies = [
"async-trait",
"proc-macro2",
@@ -1586,9 +1586,9 @@ dependencies = [
[[package]]
name = "worker-sys"
version = "0.3.4"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4fbb72a85a6509e5ac5dcd1361543468be089ff5ea5c932043b6d0aeac7b6a5"
checksum = "34563340d41016b4381257c5a16b0d2bc590dbe00500ecfbebcaa16f5f85ce90"
dependencies = [
"cfg-if",
"js-sys",

View File

@@ -21,8 +21,8 @@ crate-type = ["cdylib"]
[dependencies]
base64 = "0.22"
worker = { version = "0.3.4" }
worker-macros = { version = "0.3.4" }
worker = { version = "0.5.0" }
worker-macros = { version = "0.5.0" }
console_error_panic_hook = { version = "0.1.7" }
hex = "0.4"
hmac = "0.12"
@@ -31,4 +31,4 @@ sha2 = "0.10"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
futures = "0.3"
markdown = "1.0.0-alpha.20"
markdown = "1.0.0-alpha.23"

View File

@@ -94,11 +94,11 @@ async fn fetch_content(
Ok(content.content)
}
#[derive(Serialize)]
struct SummarizeRequest {
input_text: String,
max_length: u64,
}
// #[derive(Serialize)]
// struct SummarizeRequest {
// input_text: String,
// max_length: u64,
// }
#[derive(Serialize, Deserialize)]
struct Message {
@@ -106,27 +106,38 @@ struct Message {
content: String,
}
#[derive(Deserialize)]
struct SummarizeResult {
summary: String,
// #[derive(Deserialize)]
// struct SummarizeResult {
// summary: String,
// }
//
// #[derive(Deserialize)]
// struct SummarizeResponse {
// result: SummarizeResult,
// }
#[derive(Serialize)]
struct AIChatRequest {
messages: Vec<Message>,
max_tokens: u64,
}
#[derive(Deserialize)]
struct SummarizeResponse {
result: SummarizeResult,
#[derive(Serialize, Deserialize)]
struct AIChatResponse {
response: String,
}
async fn request_ai_summarization(
base_url: &str,
api_key: &str,
model: &str,
input: String,
messages: Vec<Message>,
) -> std::result::Result<String, Box<dyn std::error::Error>> {
console_log!("request_ai_summarization");
let client = reqwest::Client::new();
let request_body = SummarizeRequest {
input_text: input,
max_length: 2048,
let request_body = AIChatRequest {
messages,
max_tokens: 1024,
};
let response = client
@@ -139,8 +150,12 @@ async fn request_ai_summarization(
if response.status().is_success() {
console_log!("request_ai_summarization success");
let summarize_response: SummarizeResponse = response.json().await?;
Ok(summarize_response.result.summary)
let summarize_response: AIChatResponse = response.json().await?;
console_log!(
"request_ai_summarization response: {}",
summarize_response.response
);
Ok(summarize_response.response)
} else {
let error_message = response.text().await?;
console_log!("request_ai_summarization error: {}", error_message);
@@ -202,14 +217,23 @@ async fn generate_and_update_entry(
return Ok(());
}
}
let input = format!("{}\n\n{}", &entry.title, content);
// Generate summary
let messages = vec![
Message {
role: "system".to_string(),
content: "You are an experienced and knowledgeable internet blogger that writes short and easy-to-read summaries for articles from various RSS feeds. Please summarize the content of the article in 1000 words or less. Format your output in CommonMark compliant markdown. Do not give any extra comments, headers, or prefix. Only return the actual summary text. Similar to the blurbs on the back of books, highlight any aspects of the articles that may be of interest and grab the attention to any readers perusing.".to_string(),
},
Message {
role: "user".to_string(),
content: format!("{}\n\n{}", &entry.title, &entry.content),
},
];
if let Ok(summary) = request_ai_summarization(
&config.cloudflare_ai.url,
&config.cloudflare_ai.token,
&config.cloudflare_ai.model,
input,
messages,
)
.await
{

View File

@@ -4,7 +4,7 @@ compatibility_date = "2024-09-06"
[[kv_namespaces]]
binding = "entries"
id = "<insert-your-namespace-id>"
id = "265898697f934ce483e6ede9bc16edd1"
[build]
command = "cargo install -q worker-build && worker-build --release"
@@ -12,5 +12,9 @@ command = "cargo install -q worker-build && worker-build --release"
[triggers]
crons = ["*/15 * * * *"]
[observability]
enabled = true
head_sampling_rate = 1 # optional. default = 1.
[vars]
CF_AI_MODEL = "@cf/facebook/bart-large-cnn"
CF_AI_MODEL = "@hf/meta-llama/meta-llama-3-8b-instruct"