matt HOFFNER commited on
Commit
78354fc
β€’
1 Parent(s): 1c1c1be

embed serpapi

Browse files
Files changed (1) hide show
  1. pages/api/functions/index.ts +17 -3
pages/api/functions/index.ts CHANGED
@@ -64,7 +64,7 @@ const handleContentText = async (targetUrl: string) => {
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;
@@ -87,16 +87,30 @@ const surferApi = async ({ input }: any) => {
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) {
 
64
  return content;
65
  }
66
 
67
+ const surferEmbedApi = 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;
 
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
+ const serpEmbedApi = async ({ input }: any) => {
91
+ const content: string = await serpApi({ input })
92
+ const documents = await textSplitter.createDocuments([content]);
93
+ const vectorStore = await MemoryVectorStore.fromTexts(
94
+ // @ts-ignore
95
+ [...documents.map(doc => doc.pageContent)],
96
+ // @ts-ignore
97
+ [...documents.map((v, k) => k)],
98
+ model
99
+ )
100
+ const queryResult = await vectorStore.similaritySearch(input, VECTOR_STORE_SIZE);
101
+ return queryResult;
102
+ }
103
+
104
  export default async function handler(req: NextApiRequest, res: NextApiResponse) {
105
  const prompt = req.body.prompt as string;
106
  const functionName = req.body.name as string;
107
 
108
  try {
109
  if (functionName === 'serpApi') {
110
+ const result = await serpEmbedApi({ input: prompt });
111
  return res.status(200).send(result);
112
  } else {
113
+ const result = await surferEmbedApi({ input: prompt })
114
  return res.status(200).send(result);
115
  }
116
  } catch (error) {