Spaces:
Paused
Paused
matt HOFFNER
commited on
Commit
·
65c71c8
1
Parent(s):
405caf2
fix
Browse files- src/app/search/web/page.jsx +33 -46
- src/pages/api/llm.js +0 -1
- src/pages/api/stream.js +0 -1
src/app/search/web/page.jsx
CHANGED
|
@@ -5,66 +5,53 @@ import { MemoizedReactMarkdown } from '../../../components/MemoizedReactMarkdown
|
|
| 5 |
export default function WebSearchPage({ searchParams }) {
|
| 6 |
const [aiResponse, setAiResponse] = useState("");
|
| 7 |
const [searchTerm, setSearchTerm] = useState()
|
|
|
|
| 8 |
|
| 9 |
useEffect(() => {
|
| 10 |
setSearchTerm(searchParams.searchTerm)
|
| 11 |
}, [searchParams])
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
useEffect(() => {
|
| 14 |
-
const
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
async function fetchData() {
|
| 18 |
const response = await fetch('/api/llm', {
|
| 19 |
method: 'POST',
|
| 20 |
headers: { 'Content-Type': 'application/json' },
|
| 21 |
-
|
| 22 |
-
|
| 23 |
});
|
| 24 |
-
|
| 25 |
if (!response.ok) {
|
| 26 |
-
|
| 27 |
-
}
|
| 28 |
-
const reader = response.body.getReader();
|
| 29 |
-
const decoder = new TextDecoder();
|
| 30 |
-
let text = '';
|
| 31 |
-
|
| 32 |
-
while (true) {
|
| 33 |
-
const { value, done } = await reader.read();
|
| 34 |
-
|
| 35 |
-
if (value) {
|
| 36 |
-
text += decoder.decode(value, {stream: true});
|
| 37 |
-
let boundary = text.indexOf('\n');
|
| 38 |
-
|
| 39 |
-
while (boundary !== -1) {
|
| 40 |
-
const jsonStr = text.slice(0, boundary);
|
| 41 |
-
text = text.slice(boundary + 1);
|
| 42 |
-
try {
|
| 43 |
-
const json = JSON.parse(jsonStr);
|
| 44 |
-
console.log(json);
|
| 45 |
-
setAiResponse(prevResponse => [...prevResponse, json]);
|
| 46 |
-
} catch (error) {
|
| 47 |
-
console.error("Failed to parse JSON", error);
|
| 48 |
-
}
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
}
|
|
|
|
|
|
|
|
|
|
| 59 |
}
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
}, [searchParams, searchTerm]);
|
| 68 |
|
| 69 |
|
| 70 |
console.log(aiResponse);
|
|
|
|
| 5 |
export default function WebSearchPage({ searchParams }) {
|
| 6 |
const [aiResponse, setAiResponse] = useState("");
|
| 7 |
const [searchTerm, setSearchTerm] = useState()
|
| 8 |
+
const [text, setText] = useState('');
|
| 9 |
|
| 10 |
useEffect(() => {
|
| 11 |
setSearchTerm(searchParams.searchTerm)
|
| 12 |
}, [searchParams])
|
| 13 |
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
// Define the state for text
|
| 17 |
+
|
| 18 |
+
|
| 19 |
useEffect(() => {
|
| 20 |
+
const handleSend = async () => {
|
| 21 |
+
const body = JSON.stringify({ question: searchTerm });
|
| 22 |
+
const controller = new AbortController();
|
|
|
|
| 23 |
const response = await fetch('/api/llm', {
|
| 24 |
method: 'POST',
|
| 25 |
headers: { 'Content-Type': 'application/json' },
|
| 26 |
+
signal: controller.signal,
|
| 27 |
+
body
|
| 28 |
});
|
| 29 |
+
|
| 30 |
if (!response.ok) {
|
| 31 |
+
return;
|
| 32 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
const reader = response.body.getReader();
|
| 35 |
+
const decoder = new TextDecoder();
|
| 36 |
+
let resultText = '';
|
| 37 |
+
|
| 38 |
+
while (true) {
|
| 39 |
+
const { value, done } = await reader.read();
|
| 40 |
+
|
| 41 |
+
if (done) {
|
| 42 |
+
break;
|
| 43 |
}
|
| 44 |
+
|
| 45 |
+
const chunkValue = decoder.decode(value);
|
| 46 |
+
resultText += chunkValue;
|
| 47 |
}
|
| 48 |
+
|
| 49 |
+
setText(resultText);
|
| 50 |
+
};
|
| 51 |
+
|
| 52 |
+
handleSend();
|
| 53 |
+
}, []);
|
| 54 |
+
|
|
|
|
| 55 |
|
| 56 |
|
| 57 |
console.log(aiResponse);
|
src/pages/api/llm.js
CHANGED
|
@@ -2,7 +2,6 @@ import { GoogleCustomSearch } from "openai-function-calling-tools";
|
|
| 2 |
import { LLMError, LLMStream } from './stream';
|
| 3 |
|
| 4 |
const handler = async (req, res) => {
|
| 5 |
-
console.log(req.body);
|
| 6 |
try {
|
| 7 |
const googleCustomSearch = new GoogleCustomSearch({
|
| 8 |
apiKey: process.env.API_KEY,
|
|
|
|
| 2 |
import { LLMError, LLMStream } from './stream';
|
| 3 |
|
| 4 |
const handler = async (req, res) => {
|
|
|
|
| 5 |
try {
|
| 6 |
const googleCustomSearch = new GoogleCustomSearch({
|
| 7 |
apiKey: process.env.API_KEY,
|
src/pages/api/stream.js
CHANGED
|
@@ -68,7 +68,6 @@ export const LLMStream = async (
|
|
| 68 |
async start(controller) {
|
| 69 |
const onParse = async (event) => {
|
| 70 |
if (event.type === 'event') {
|
| 71 |
-
console.log(event);
|
| 72 |
const data = event.data;
|
| 73 |
try {
|
| 74 |
if (data === '[DONE]') {
|
|
|
|
| 68 |
async start(controller) {
|
| 69 |
const onParse = async (event) => {
|
| 70 |
if (event.type === 'event') {
|
|
|
|
| 71 |
const data = event.data;
|
| 72 |
try {
|
| 73 |
if (data === '[DONE]') {
|