Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

NOTE: We've disabled the Exa API key in this demo due to high volume – you'll need to fork and provide your own to use it.

Search on Twitter (powered by Exa) and return a streaming markdown summary (powered by Substrate).

To fork, sign up for Substrate to get your own API key and $51 free credits.

You'll also need Exa, which comes with generous free credits and can be a much cheaper alternative to the Twitter API.

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
import Exa from "npm:exa-js";
import { ComputeJSON, ComputeText, sb, Substrate } from "npm:substrate";
import { z } from "npm:zod";
import { zodToJsonSchema } from "npm:zod-to-json-schema";
// NOTE: We've disabled the Exa API key in this demo due to high volume – you'll need to fork and provide your own to run it.
const exa = new Exa(Deno.env.get("EXA_API_KEY"));
const substrate = new Substrate({ apiKey: Deno.env.get("SUBSTRATE_API_KEY") });
const query = `"exa.ai" OR "@ExaAILabs"`;
// Search for tweets from the last week
const searchResults = await exa.searchAndContents(query, {
text: { maxCharacters: 1000 },
type: "keyword",
includeDomains: ["twitter.com"],
startPublishedDate: (new Date(Date.now() - 7 * 24 * 60 * 60 * 1000)).toISOString().split("T")[0],
});
// Extract summary, sentiment analysis, and other info for each tweet
const extractedTweet = z.object({
summary: z.string().describe(
"Summarize in a couple sentences: what is the text about and how it is related to the topic.",
),
isProfile: z.boolean().describe("True if the link is a twitter profile, not a post."),
sentiment: z.enum(["positive", "neutral", "negative"]).describe("Sentiment of the post. "),
url: z.string().describe("url"),
});
let summaries = [];
for (const result of searchResults.results) {
summaries.push(
new ComputeJSON({
prompt: `Summarize this tweet and how it relates to the topic: ${query}
Analyze the sentiment of the author about the topic.
COMMENT: ${JSON.stringify(result)}`,
json_schema: zodToJsonSchema(extractedTweet),
}, { cache_age: 60 * 60 * 24 }),
);
}
// Generate markdown summary
const markdown = new ComputeText({
prompt: sb.concat(
`Below is a list of summarized posts about ${query} on Twitter.
Generate concise markdown with bullets.
Group the results by sentiment and group from most positive to least positive.
Do not include a title or introduction.
Each bullet should be a summary, followed by sentiment in parens: - <summary> (positive).
After sentiment add a link to the tweet in this format: [<author handle>](https://twitter.com/<handle>/status/<id>)
Filter out tweets that are twitter profiles (isProfile = true).
Just return the markdown. Do not introduce with "Here is" or explain the output.
RESULTS:\n`, // @ts-ignore
...summaries.map((s) => sb.jq(s.future.json_object, "@json")),
),
model: "Llama3Instruct8B",
});
markdown.id = "markdown";
const stream = await substrate.stream(markdown);
// Render streaming markdown
export default async function handler(req: Request): Promise<Response> {
const renderMarkdown = (await import("https://esm.town/v/substrate/renderMarkdown")).default;
return renderMarkdown(stream);
}
levi-twitterrag.web.val.run
August 2, 2024