Skip to content

Commit

Permalink
move functions to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Jun 25, 2023
1 parent 8c627bc commit 592161e
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 108 deletions.
106 changes: 106 additions & 0 deletions app/api/chat/functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
export const functions: {
name: string;
description: string;
parameters: object;
}[] = [
{
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: [],
},
},
];

export 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;
}

export 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}`,
};
}

export 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}`,
})),
};
}

export async function summarize_top_story() {
const topStory = await get_top_stories(1);
return await get_story_with_comments(topStory[0].id);
}
115 changes: 7 additions & 108 deletions app/api/chat/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Configuration, OpenAIApi } from "openai-edge";
import { OpenAIStream, StreamingTextResponse } from "ai";
import {
functions,
get_top_stories,
get_story,
get_story_with_comments,
summarize_top_story,
} from "./functions";

// Create an OpenAI API client (that's edge friendly!)
const config = new Configuration({
Expand All @@ -9,113 +16,6 @@ const openai = new OpenAIApi(config);

export const runtime = "edge";

const functions: {
name: string;
description: string;
parameters: object;
}[] = [
{
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 POST(req: Request) {
const { messages } = await req.json();

Expand All @@ -136,7 +36,6 @@ export async function POST(req: Request) {
initialResponseMessage.function_call;
const functionArgs = JSON.parse(args);
let functionResponse;
console.log({ initialResponseMessage });
if (functionName === "get_top_stories") {
functionResponse = await get_top_stories(functionArgs.limit);
} else if (functionName === "get_story") {
Expand Down

0 comments on commit 592161e

Please sign in to comment.