substrate-similarsites.web.val.run
Readme

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

Find similar pages on HN and return a streaming markdown summary.

Powered by Exa and Substrate.

πŸͺ© To fork, sign up for Substrate to get your own API key and $50 free credits.

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
87
88
89
90
91
92
93
94
95
96
97
98
99
import urlHeader from "https://esm.town/v/substrate/urlHeader";
import Exa from "npm:exa-js";
import { ComputeText, GenerateImage, sb, Substrate } from "npm:substrate";
// NOTE: We've disabled the Exa API key in this demo due to high volume – you'll need to provide your own to use it.
const exa = new Exa(Deno.env.get("EXA_API_KEY"));
const substrate = new Substrate({ apiKey: Deno.env.get("SUBSTRATE_API_KEY") });
export default async function handler(req: Request): Promise<Response> {
if (new URL(req.url).pathname === "/robots.txt") {
return new Response("User-agent: *\nDisallow: /");
}
const url = new URL(req.url).searchParams.get("url") || "https://thesephist.com/posts/synth/";
const searchResults = await exa.findSimilarAndContents(url, {
text: { maxCharacters: 1000 },
category: "personal site",
});
const summary = new ComputeText({
prompt:
`I have a json list of results for similar sites to ${url}. Return a concise markdown summary in the following format:
<summary of common themes and highlights>
<list of similar sites in a table with a summary and the website url>
Just return the markdown. Do not introduce with "Here is" or explain the output.
Results:
${JSON.stringify(searchResults.results)}`,
model: "Llama3Instruct70B",
});
const stream = await substrate.stream(summary);
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>
${urlHeader(url)}
<div id="content"></div>
<script>
let content = document.getElementById('content');
let markdown = '';
// Override the link renderer
const renderer = new marked.Renderer();
renderer.link = function(href, title, text) {
if (typeof href === 'object') {
text = href.text || text;
title = href.title || '';
href = href.href || '#';
}
const newHref = \`/?url=\${encodeURIComponent(href)}\`;
return \`<span class="link-pair">
<a href="\${href}" target="_blank" target="_blank">\${text}</a>
<a href="\${newHref}">Find similar</a>
</span>\`;
};
marked.setOptions({ renderer: renderer });
</script>
`));
new Promise(async () => {
for await (const event of stream.get(summary)) {
if (event.object === "node.delta") {
const chunk = event.data.text || "";
writer.write(textEncoder.encode(`
<script>
markdown += ${JSON.stringify(chunk)};
content.innerHTML = marked.parse(markdown);
</script>
`));
}
}
writer.write(textEncoder.encode(`
</body>
</html>
`));
writer.close();
});
return new Response(readable, {
headers: {
"Content-Type": "text/html",
},
});
}
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 1, 2024