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
100
/** @jsx jsx */
import { jsx } from "https://deno.land/x/hono@v3.11.7/middleware.ts";
import { Hono } from "https://deno.land/x/hono@v3.11.7/mod.ts";
import { ai } from "https://esm.town/v/yawnxyz/ai";
const app = new Hono();
export const getContentFromUrl = async (url: string) => {
try {
if (url.includes("youtube")) {
url = `https://val.markdown.download/?url=${url}`;
}
url = "https://r.jina.ai/" + url
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.text();
return data;
} catch (error) {
console.error('Error fetching the URL:', error);
return 'Error fetching the content.';
}
};
export const getBlurbFromUrl = async (url: string, {
getSummary=true, getSummaryPrompt, getTags=true, getTagsPrompt, getEmbeddings} = {}) => {
const content = await getContentFromUrl(url);
if(!getSummary || !getTags) {
return content;
}
let summary, tags, embeddings
if(getSummary) {
summary = await getSummaryFn(getSummaryPrompt, content);
}
if(getTags) {
tags = await getTagsFn(getTagsPrompt, content);
}
if(getEmbeddings) {
embeddings = await getEmbeddingsFn(content);
}
return {
content,
summary: summary.text,
tags: tags.text,
embeddings
}
};
export const getSummaryFn = async (prompt, content) => {
prompt = prompt || "Summarize this article in 30 words, focusing on the primary author. Do not respond with JSON or XML or stage instructions. Only reply in text. Text:"
let result = await ai({
provider: "openai",
model: "gpt-3.5-turbo",
prompt: prompt + content
})
return result
}
export const getTagsFn = async (prompt, content) => {
prompt = prompt || "Return a comma separated list of 3-4 tags that broadly describes the text. Do not repeat the given text. Just return text and commas, no other symbols. Text to categorize:"
let result = await ai({
provider: "openai",
model: "gpt-3.5-turbo",
prompt: prompt + content
})
return result
}
export const getEmbeddingsFn = async (content) => {
let result = await ai({
provider: "openai",
embed:true,
value: content
})
return result
}
app.post("/", async (c) => {
// const body = await c.req.parseBody() || await c.req.json();
const body = await c.req.json();
const { url, getTags, getTagsPrompt, getSummary, getSummaryPrompt, getEmbeddings } = body;
const content = await getContentFromUrl(url);
if(!getSummary || !getTags) {
return c.text(content);
}
let summary, tags, embeddings
if(getSummary) {