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";
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"`;
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],
});
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 }),
);
}
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`,
...summaries.map((s) => sb.jq(s.future.json_object, "@json")),
),
model: "Llama3Instruct70B",
}); markdown.id = "markdown";
const stream = await substrate.stream(markdown);
export default async function handler(req: Request): Promise<Response> {
const renderMarkdown = (await import("https://esm.town/v/substrate/renderMarkdown")).default;
return renderMarkdown(stream);
}