Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { sqlite } from "https://esm.town/v/std/sqlite";
import { parseFeed } from "jsr:@mikaelporttila/rss@*";
import { GoogleGenerativeAI, HarmBlockThreshold, HarmCategory } from "npm:@google/generative-ai";
import { sql } from "npm:drizzle-orm";
import { desc } from "npm:drizzle-orm";
import { drizzle } from "npm:drizzle-orm/libsql";
import { integer, sqliteTable, text } from "npm:drizzle-orm/sqlite-core";
// await sqlite.execute("CREATE TABLE IF NOT EXISTS tweets (published TEXT PRIMARY KEY, entry TEXT NOT NULL)");
const db = drizzle(sqlite as any);
const tweets = sqliteTable("tweets", {
key: text("published"),
value: text("entry").notNull(),
});
const result = await db.select({
tweet: tweets.value,
}).from(tweets).orderBy(desc(tweets.key)).limit(10);
const resultJson = JSON.stringify(result);
const apiKey = Deno.env.get("GEMINI_API_KEY");
if (!apiKey) {
throw new Error("GEMINI_API_KEY environment variable is not set");
}
const genAI = new GoogleGenerativeAI(apiKey);
const model = genAI.getGenerativeModel({
model: "gemini-1.5-flash",
systemInstruction: "あなたは優秀な日本語のアシスタントです。ツイートに関するRSSフィードを解析してください。",
});
const generationConfig = {
temperature: 1,
topP: 0.95,
topK: 64,
maxOutputTokens: 8192,
responseMimeType: "text/plain",
};
async function run() {
const chatSession = model.startChat({
generationConfig,
// safetySettings: Adjust safety settings
// See https://ai.google.dev/gemini-api/docs/safety-settings
history: [
{
role: "user",
parts: [
{
text: `I give you rss feed, then ask something. here is rss feed:
${resultJson}`,
},
],
},
{
role: "model",
parts: [
{
text: "Thank you. what should I do for you?",
},
],
},
],
});
const result = await chatSession.sendMessage(`
please analyze rss feed and write a html which is a summary website in Japanese foucsing following points:
- short summary of the tweet
- link to the tweet (you need to replace domain in the link from http://nitter.net/... to https//x.com/...)
- time tweeted
only return html, with no code enclosure \`\`\`,no yapping.`);
return result.response.text();
}
export default async function(req: Request): Promise<Response> {
const res = await run();
// return res as html Response
return new Response(res, {
headers: {
"content-type": "text/html; charset=UTF-8",
},
});
}
kj9-summary.web.val.run
August 16, 2024