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") });
const query = "langchain bad";
const headers = new Headers({ "X-API-KEY": Deno.env.get("SERPER_API_KEY"), "Content-Type": "application/json" });
headers.set("accept", "application/json");
const fetch = globalThis.fetch;
const resp = await fetch("https://google.serper.dev/search", {
method: "POST",
redirect: "follow",
headers: headers,
body: JSON.stringify({
"q": `${query} reddit`,
"num": 20,
"tbs": "qdr:m",
}),
});
const text = await resp.text();
const searchResults = JSON.parse(text);
const extract = z.object({
summary: z.string().describe(
"Summarize in a couple sentences: what is the post about and how it is related to the topic.",
),
sentiment: z.enum(["positive", "neutral", "negative"]).describe("Sentiment of the post. "),
url: z.string().describe("url from the link field."),
date: z.string().describe("date from the date field."),
});
let summaries = [];
for (const result of searchResults.organic) {
summaries.push(
new ComputeJSON({
prompt: `Summarize this search result and how it relates to the topic: ${query}
Analyze the sentiment of the author about the topic.
RESULT: ${JSON.stringify(result)}`,
json_schema: zodToJsonSchema(extract),
}, { cache_age: 60 * 60 * 24 }),
);
}
const markdown = new ComputeText({
prompt: sb.concat(
`Below is a list of summarized posts about "${query}".
Generate concise markdown with bulleted.
DO NOT INCLUDE a title or introduction.
Order from most negative to least negative sentiment.
Each bullet should be a summary, followed by sentiment in parens: - <summary> (negative).
After sentiment add a link to the original url in this format: [<date>](<url>).
Just return the markdown. Do not introduce with "Here is" or explain the output.`,
`RESULTS:\n`,
...summaries.map((s) => sb.jq(s.future.json_object, "@json")),
),
model: "Llama3Instruct70B",
});
const stream = await substrate.stream(markdown);
export default async function handler(req: Request): Promise<Response> {
if (new URL(req.url).pathname === "/robots.txt") {
return new Response("User-agent: *\nDisallow: /");
}
let { readable, writable } = new TransformStream();
let writer = writable.getWriter();
const textEncoder = new TextEncoder();
writer.write(textEncoder.encode(`<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<style>
.link-pair {
display: inline-block;
margin-right: 10px;
}
.link-pair a {
display: inline-block;
margin-right: 5px;
}
</style>
</head>
<body>
<div id="content"></div>
<script>
let content = document.getElementById('content');
let markdown = '';
</script>`));
new Promise(async () => {
for await (const event of stream.get(markdown)) {
if (event.object === "node.delta") {
const chunk = event.data.text || "";
writer.write(textEncoder.encode(`<script>
markdown += ${JSON.stringify(chunk)};
content.innerHTML = marked.parse(markdown);