matt HOFFNER commited on
Commit
1c1c1be
β€’
1 Parent(s): f4e05b5
app/tools/serp-api.ts CHANGED
@@ -17,8 +17,9 @@ function createSerpApi({ apiKey }: { apiKey: string }) {
17
  q: input,
18
  location: "Seattle, Washington",
19
  });
20
-
21
- return JSON.stringify(response);
 
22
  } catch (error) {
23
  throw new Error(`Error in serpApi: ${error}`);
24
  }
 
17
  q: input,
18
  location: "Seattle, Washington",
19
  });
20
+
21
+ console.log(response);
22
+ return JSON.stringify(response);
23
  } catch (error) {
24
  throw new Error(`Error in serpApi: ${error}`);
25
  }
app/tools/surfer.ts CHANGED
@@ -8,29 +8,7 @@ function createUrlSurfer() {
8
  const name = 'surfer';
9
  const description = 'A custom URL navigator. Useful when a URL is provided with a question. Input should be a prompt with a URL. Outputs a JSON array of relevant results.';
10
 
11
- const execute = async ({ input }: z.infer<typeof paramsSchema>) => {
12
- try {
13
- const res = await fetch('/api/surfer', {
14
- method: 'POST',
15
- headers: {
16
- 'Content-Type': 'application/json',
17
- },
18
- body: JSON.stringify({ prompt: input }),
19
- });
20
-
21
- if (!res.ok) {
22
- throw new Error(`HTTP error! status: ${res.status}`);
23
- }
24
-
25
- const data = await res.json();
26
- return data;
27
- } catch (error) {
28
- // @ts-ignore
29
- throw new Error(`Error in UrlSurfer: ${error.message}`);
30
- }
31
- };
32
-
33
- return new Tool(paramsSchema, name, description, execute).tool;
34
  }
35
 
36
  export { createUrlSurfer };
 
8
  const name = 'surfer';
9
  const description = 'A custom URL navigator. Useful when a URL is provided with a question. Input should be a prompt with a URL. Outputs a JSON array of relevant results.';
10
 
11
+ return new Tool(paramsSchema, name, description, {} as any).tool;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  }
13
 
14
  export { createUrlSurfer };
pages/api/functions/index.ts CHANGED
@@ -64,39 +64,40 @@ const handleContentText = async (targetUrl: string) => {
64
  return content;
65
  }
66
 
67
- export default async function handler(req: NextApiRequest, res: NextApiResponse) {
68
- const prompt = req.body.prompt as string;
69
- const functionName = req.body.name as string;
70
- const urls = prompt.match(urlRegex);
71
  const targetUrl = urls ? urls[0] : null;
72
- const promptWithoutUrl = urls ? prompt.replace(urlRegex, '').trim() : prompt;
73
 
74
- if (!targetUrl) {
75
- return `Couldn't find url, here is the ${prompt}`;
 
76
  }
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  try {
79
  if (functionName === 'serpApi') {
80
  const result = await serpApi({ input: prompt });
81
  return res.status(200).send(result);
82
  } else {
83
- const content: string = await handleContentText(targetUrl)
84
- if (!content) {
85
- return `Couldn't find ${targetUrl}, here is the prompt: ${promptWithoutUrl}`;
86
- }
87
-
88
- const documents = await textSplitter.createDocuments([content]);
89
-
90
- const vectorStore = await MemoryVectorStore.fromTexts(
91
- // @ts-ignore
92
- [...documents.map(doc => doc.pageContent)],
93
- // @ts-ignore
94
- [...documents.map((v, k) => k)],
95
- model
96
- )
97
- const queryResult = await vectorStore.similaritySearch(promptWithoutUrl, VECTOR_STORE_SIZE);
98
- return res.status(200).send(
99
- `Here is the context: ${JSON.stringify(queryResult.map(result => result.pageContent))} from using the prompt to lookup relevant information. Here is the prompt: ${promptWithoutUrl}`);
100
  }
101
  } catch (error) {
102
  console.error(error);
 
64
  return content;
65
  }
66
 
67
+ const surferApi = async ({ input }: any) => {
68
+ const urls = input.match(urlRegex);
 
 
69
  const targetUrl = urls ? urls[0] : null;
70
+ const promptWithoutUrl = urls ? input.replace(urlRegex, '').trim() : input;
71
 
72
+ const content: string = await handleContentText(targetUrl)
73
+ if (!content) {
74
+ return `Couldn't find ${targetUrl}, here is the prompt: ${promptWithoutUrl}`;
75
  }
76
 
77
+ const documents = await textSplitter.createDocuments([content]);
78
+
79
+ const vectorStore = await MemoryVectorStore.fromTexts(
80
+ // @ts-ignore
81
+ [...documents.map(doc => doc.pageContent)],
82
+ // @ts-ignore
83
+ [...documents.map((v, k) => k)],
84
+ model
85
+ )
86
+ const queryResult = await vectorStore.similaritySearch(promptWithoutUrl, VECTOR_STORE_SIZE);
87
+ return `Here is the context: ${JSON.stringify(queryResult.map(result => result.pageContent))} from using the prompt to lookup relevant information. Here is the prompt: ${promptWithoutUrl}`;
88
+ }
89
+
90
+ export default async function handler(req: NextApiRequest, res: NextApiResponse) {
91
+ const prompt = req.body.prompt as string;
92
+ const functionName = req.body.name as string;
93
+
94
  try {
95
  if (functionName === 'serpApi') {
96
  const result = await serpApi({ input: prompt });
97
  return res.status(200).send(result);
98
  } else {
99
+ const result = await surferApi({ input: prompt })
100
+ return res.status(200).send(result);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  }
102
  } catch (error) {
103
  console.error(error);