/
functions.ts
119 lines (113 loc) · 3.11 KB
/
functions.ts
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { CompletionCreateParams } from "openai/resources/chat/index";
export const functions: CompletionCreateParams.Function[] = [
{
name: "get_top_stories",
description:
"Get the top stories from Hacker News. Also returns the Hacker News URL to each story.",
parameters: {
type: "object",
properties: {
limit: {
type: "number",
description: "The number of stories to return. Defaults to 10.",
},
},
required: [],
},
},
{
name: "get_story",
description:
"Get a story from Hacker News. Also returns the Hacker News URL to the story.",
parameters: {
type: "object",
properties: {
id: {
type: "number",
description: "The ID of the story",
},
},
required: ["id"],
},
},
{
name: "get_story_with_comments",
description:
"Get a story from Hacker News with comments. Also returns the Hacker News URL to the story and each comment.",
parameters: {
type: "object",
properties: {
id: {
type: "number",
description: "The ID of the story",
},
},
required: ["id"],
},
},
{
name: "summarize_top_story",
description:
"Summarize the top story from Hacker News, including both the story and its comments. Also returns the Hacker News URL to the story and each comment.",
parameters: {
type: "object",
properties: {},
required: [],
},
},
];
async function get_top_stories(limit: number = 10) {
const response = await fetch(
"https://hacker-news.firebaseio.com/v0/topstories.json",
);
const ids = await response.json();
const stories = await Promise.all(
ids.slice(0, limit).map((id: number) => get_story(id)),
);
return stories;
}
async function get_story(id: number) {
const response = await fetch(
`https://hacker-news.firebaseio.com/v0/item/${id}.json`,
);
const data = await response.json();
return {
...data,
hnUrl: `https://news.ycombinator.com/item?id=${id}`,
};
}
async function get_story_with_comments(id: number) {
const response = await fetch(
`https://hacker-news.firebaseio.com/v0/item/${id}.json`,
);
const data = await response.json();
const comments = await Promise.all(
data.kids.slice(0, 10).map((id: number) => get_story(id)),
);
return {
...data,
hnUrl: `https://news.ycombinator.com/item?id=${id}`,
comments: comments.map((comment: any) => ({
...comment,
hnUrl: `https://news.ycombinator.com/item?id=${comment.id}`,
})),
};
}
async function summarize_top_story() {
const topStory = await get_top_stories(1);
return await get_story_with_comments(topStory[0].id);
}
export async function runFunction(name: string, args: any) {
switch (name) {
case "get_top_stories":
return await get_top_stories();
case "get_story":
return await get_story(args["id"]);
case "get_story_with_comments":
return await get_story_with_comments(args["id"]);
case "summarize_top_story":
return await summarize_top_story();
default:
return null;
}
}