substrate-hackernewsrag.web.val.run
Readme

Find comments on HN (powered by Algolia), extract content and return a streaming markdown summary (powered by Substrate).

The RAG portion of this is 34 lines of Substrate code. Read the walkthrough: https://x.com/vprtwn/status/1812844236401762513

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

See also:

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
import { hnSearch } from "https://esm.town/v/stevekrouse/hnSearch";
import { ComputeJSON, ComputeText, sb, Substrate } from "npm:substrate";
import { z } from "npm:zod";
import { zodToJsonSchema } from "npm:zod-to-json-schema";
const substrate = new Substrate({ apiKey: Deno.env.get("SUBSTRATE_API_KEY") });
// search for HN comments using https://hn.algolia.com/api
const query = "langchain";
const searchResults = await hnSearch({
query: query,
numericFilters: `created_at_i>${Math.floor(Date.now() / 1000) - 60 * 60 * 24 * 7 * 4}`,
tags: "comment",
});
// Extract summary, sentiment, and metadata from each comment
const commentInfo = z.object({
summary: z.string().describe(
"Summarize in a couple sentences: who is commenting, what the comment is about, how it is related to the topic.",
),
storyTitle: z.string().describe("The story title."),
forHiring: z.boolean().describe(
"True if the story is a Ask HN post with Who is hiring, Who wants to be hired, or Seeking freelancer in the title",
),
sentiment: z.enum(["positive", "neutral", "negative"]).describe("Sentiment of the post."),
objectID: z.string().describe("objectID field"),
});
let summaries = [];
for (const hit of searchResults.hits) {
summaries.push(
new ComputeJSON({
prompt: `Summarize this comment and how it relates to the topic: ${query}
Use "negative" sentiment for posts about API, abstraction, documentation, tutorial, general quality, slowness, or performance issues.
COMMENT: ${JSON.stringify(hit)}`,
json_schema: zodToJsonSchema(commentInfo),
max_tokens: 1000,
}, { cache_age: 60 * 60 * 24 }),
);
}
// Generate a markdown summary
const markdown = new ComputeText({
prompt: sb.concat(
`Below is a list of summarized comments about ${query} on HackerNews in the last week.
Generate concise markdown summarizing the results.
DO NOT include a title or introduction.
Summarize the contents of the comment and the sentiment about ${query}.
Categorize results under sentiment headers.
Order from most negative to least negative within each category.
Add a link to the original story URL in this format: [<story title>](https://news.ycombinator.com/item?id=<objectID>)
Filter out posts that are for hiring (forHiring = true).
Filter out posts that do not seem to be about ${query}.
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: "Llama3Instruct70B",
max_tokens: 1000,
});
markdown.id = "markdown";
const stream = await substrate.stream(markdown);
// Render streaming markdown
export default async function handler(req: Request): Promise<Response> {
if (new URL(req.url).pathname === "/robots.txt") {
return new Response("User-agent: *\nDisallow: /");
}
const renderMarkdown = (await import("https://esm.town/v/substrate/renderMarkdown")).default;
return renderMarkdown(stream);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
August 2, 2024