Spaces:
Paused
Paused
matt HOFFNER
commited on
Commit
·
2a8011b
1
Parent(s):
82ddd60
lfg
Browse files- src/app/search/web/page.jsx +18 -31
- src/pages/api/llm.js +13 -4
- src/pages/api/search.js +0 -24
- src/pages/api/stream.js +14 -18
src/app/search/web/page.jsx
CHANGED
@@ -18,46 +18,33 @@ export default function WebSearchPage({ searchParams }) {
|
|
18 |
const response = await fetch('/api/llm', {
|
19 |
method: 'POST',
|
20 |
headers: { 'Content-Type': 'application/json' },
|
21 |
-
body: JSON.stringify({ question: searchTerm
|
22 |
signal,
|
23 |
});
|
24 |
-
|
25 |
-
if (!
|
26 |
-
|
27 |
-
}
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
console.log(json);
|
40 |
-
setAiResponse(json);
|
41 |
-
console.log("Stream complete");
|
42 |
-
break;
|
43 |
-
}
|
44 |
-
|
45 |
-
text += decoder.decode(value, {stream: true});
|
46 |
-
}
|
47 |
-
} catch (error) {
|
48 |
-
console.error("Failed to parse JSON", error);
|
49 |
}
|
50 |
}
|
51 |
-
}
|
52 |
|
53 |
-
fetchData()
|
54 |
-
console.error('Fetch failed: ', error);
|
55 |
-
});
|
56 |
|
57 |
return () => controller.abort();
|
58 |
}, [searchParams, searchTerm]);
|
59 |
|
60 |
-
|
61 |
console.log(aiResponse);
|
62 |
|
63 |
return (
|
|
|
18 |
const response = await fetch('/api/llm', {
|
19 |
method: 'POST',
|
20 |
headers: { 'Content-Type': 'application/json' },
|
21 |
+
body: JSON.stringify({ question: searchTerm }),
|
22 |
signal,
|
23 |
});
|
24 |
+
const data = response.body;
|
25 |
+
if (!data) {
|
26 |
+
return;
|
27 |
+
}
|
28 |
+
|
29 |
+
const reader = data.getReader();
|
30 |
+
const decoder = new TextDecoder();
|
31 |
+
let done = false;
|
32 |
+
|
33 |
+
while (!done) {
|
34 |
+
const { value, done: doneReading } = await reader.read();
|
35 |
+
done = doneReading;
|
36 |
+
const chunkValue = decoder.decode(value);
|
37 |
+
if (chunkValue) {
|
38 |
+
setAiResponse(chunkValue);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
}
|
40 |
}
|
41 |
+
};
|
42 |
|
43 |
+
fetchData();
|
|
|
|
|
44 |
|
45 |
return () => controller.abort();
|
46 |
}, [searchParams, searchTerm]);
|
47 |
|
|
|
48 |
console.log(aiResponse);
|
49 |
|
50 |
return (
|
src/pages/api/llm.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import { GoogleCustomSearch } from "openai-function-calling-tools";
|
2 |
import { LLMError, LLMStream } from './stream';
|
3 |
|
4 |
-
const handler = async (req) => {
|
5 |
try {
|
6 |
const { question } = (await req.body);
|
7 |
|
@@ -18,12 +18,21 @@ const handler = async (req) => {
|
|
18 |
];
|
19 |
|
20 |
const functions = {
|
21 |
-
googleCustomSearch
|
22 |
};
|
23 |
|
24 |
-
let promptToSend = "You are a helpful assistant";
|
25 |
const stream = await LLMStream({ id: "gpt-3.5-turbo-0613" }, promptToSend, 0.8, messages, functions);
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
} catch (error) {
|
28 |
console.error(error);
|
29 |
if (error instanceof LLMError) {
|
|
|
1 |
import { GoogleCustomSearch } from "openai-function-calling-tools";
|
2 |
import { LLMError, LLMStream } from './stream';
|
3 |
|
4 |
+
const handler = async (req, res) => {
|
5 |
try {
|
6 |
const { question } = (await req.body);
|
7 |
|
|
|
18 |
];
|
19 |
|
20 |
const functions = {
|
21 |
+
googleCustomSearch
|
22 |
};
|
23 |
|
24 |
+
let promptToSend = "You are a helpful assistant, a search term is provided and you are given search results to help provide a useful response.";
|
25 |
const stream = await LLMStream({ id: "gpt-3.5-turbo-0613" }, promptToSend, 0.8, messages, functions);
|
26 |
+
|
27 |
+
let data = '';
|
28 |
+
const decoder = new TextDecoder();
|
29 |
+
for await (const chunk of stream) {
|
30 |
+
data += decoder.decode(chunk);
|
31 |
+
res.write(data);
|
32 |
+
}
|
33 |
+
|
34 |
+
return res.end();
|
35 |
+
|
36 |
} catch (error) {
|
37 |
console.error(error);
|
38 |
if (error instanceof LLMError) {
|
src/pages/api/search.js
DELETED
@@ -1,24 +0,0 @@
|
|
1 |
-
// pages/api/search.js
|
2 |
-
|
3 |
-
export default async function handler(req, res) {
|
4 |
-
const { searchTerm, startIndex = "1" } = req.query;
|
5 |
-
|
6 |
-
if (!searchTerm) {
|
7 |
-
res.status(400).json({ error: 'Search term is required' });
|
8 |
-
return;
|
9 |
-
}
|
10 |
-
|
11 |
-
const response = await fetch(
|
12 |
-
`https://www.googleapis.com/customsearch/v1?key=${process.env.API_KEY}&cx=${process.env.CONTEXT_KEY}&q=${searchTerm}&start=${startIndex}`
|
13 |
-
);
|
14 |
-
|
15 |
-
if (!response.ok) {
|
16 |
-
res.status(response.status).json({ error: 'Failed to fetch search results' });
|
17 |
-
return;
|
18 |
-
}
|
19 |
-
|
20 |
-
const data = await response.json();
|
21 |
-
|
22 |
-
res.status(200).json(data);
|
23 |
-
}
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/pages/api/stream.js
CHANGED
@@ -40,7 +40,7 @@ export const LLMStream = async (
|
|
40 |
],
|
41 |
max_tokens: 1000,
|
42 |
temperature: temperature,
|
43 |
-
stream: true
|
44 |
}),
|
45 |
});
|
46 |
|
@@ -70,8 +70,9 @@ export const LLMStream = async (
|
|
70 |
const onParse = async (event) => {
|
71 |
if (event.type === 'event') {
|
72 |
const data = event.data;
|
|
|
73 |
try {
|
74 |
-
if (data ===
|
75 |
return;
|
76 |
}
|
77 |
const json = JSON.parse(data);
|
@@ -79,23 +80,18 @@ export const LLMStream = async (
|
|
79 |
controller.close();
|
80 |
return;
|
81 |
} else if (json.choices[0].finish_reason === "function_call") {
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
const fn = functions[fnName];
|
86 |
-
const functionResult = await fn(...Object.values(JSON.parse(args)));
|
87 |
-
|
88 |
-
console.log(`Function call: ${fnName}, Arguments: ${args}`);
|
89 |
-
console.log(`Calling Function ${fnName} Result: ` + functionResult);
|
90 |
-
|
91 |
-
const queue = encoder.encode(functionResult);
|
92 |
-
controller.enqueue(queue);
|
93 |
-
} else {
|
94 |
-
const text = json.choices[0].delta.content;
|
95 |
-
const queue = encoder.encode(text);
|
96 |
-
controller.enqueue(queue);
|
97 |
-
}
|
98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
} catch (e) {
|
100 |
console.log(e);
|
101 |
controller.error(e);
|
|
|
40 |
],
|
41 |
max_tokens: 1000,
|
42 |
temperature: temperature,
|
43 |
+
stream: true,
|
44 |
}),
|
45 |
});
|
46 |
|
|
|
70 |
const onParse = async (event) => {
|
71 |
if (event.type === 'event') {
|
72 |
const data = event.data;
|
73 |
+
|
74 |
try {
|
75 |
+
if (data === "[DONE]") {
|
76 |
return;
|
77 |
}
|
78 |
const json = JSON.parse(data);
|
|
|
80 |
controller.close();
|
81 |
return;
|
82 |
} else if (json.choices[0].finish_reason === "function_call") {
|
83 |
+
const fnName = json.choices[0].message.function_call.name;
|
84 |
+
const args = json.choices[0].message.function_call.arguments;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
+
const fn = functions[fnName];
|
87 |
+
const result = await fn(...Object.values(JSON.parse(args)));
|
88 |
+
|
89 |
+
console.log(`Function call: ${fnName}, Arguments: ${args}`);
|
90 |
+
console.log(`Calling Function ${fnName} Result: ` + result);
|
91 |
+
}
|
92 |
+
const text = json.choices[0].delta.content;
|
93 |
+
const queue = encoder.encode(text);
|
94 |
+
controller.enqueue(queue);
|
95 |
} catch (e) {
|
96 |
console.log(e);
|
97 |
controller.error(e);
|