matt HOFFNER commited on
Commit
53763e2
·
1 Parent(s): 74edd3f

improvements

Browse files
src/app/search/web/page.jsx CHANGED
@@ -1,14 +1,25 @@
1
  "use client"
 
2
  import { useEffect, useState } from "react";
3
  import { MemoizedReactMarkdown } from '../../../components/MemoizedReactMarkdown'
4
  import { CodeBlock } from '../../../components/CodeBlock';
5
- import { useSearchParams } from 'next/navigation'
6
 
7
  export default function WebSearchPage() {
8
  const searchParams = useSearchParams()
9
 
10
  const [aiResponse, setAiResponse] = useState("");
11
  const [searchTerm, setSearchTerm] = useState()
 
 
 
 
 
 
 
 
 
 
12
 
13
  useEffect(() => {
14
  setSearchTerm(searchParams.get('searchTerm'))
@@ -53,7 +64,8 @@ export default function WebSearchPage() {
53
  }, [searchParams, searchTerm]);
54
 
55
  return (
56
- <div style={{ padding: "20px" }} className="flex flex-row">
 
57
  <MemoizedReactMarkdown
58
  className="prose dark:prose-invert flex-1"
59
  components={{
@@ -101,8 +113,9 @@ export default function WebSearchPage() {
101
  },
102
  }}
103
  >
104
- {aiResponse}
105
- </MemoizedReactMarkdown>
106
- </div>
 
107
  );
108
  }
 
1
  "use client"
2
+ import { useSearchParams } from 'next/navigation'
3
  import { useEffect, useState } from "react";
4
  import { MemoizedReactMarkdown } from '../../../components/MemoizedReactMarkdown'
5
  import { CodeBlock } from '../../../components/CodeBlock';
6
+ import WebSearchResults from '../../../components/WebSearchResults';
7
 
8
  export default function WebSearchPage() {
9
  const searchParams = useSearchParams()
10
 
11
  const [aiResponse, setAiResponse] = useState("");
12
  const [searchTerm, setSearchTerm] = useState()
13
+ const [searchResults, setSearchResults] = useState([]);
14
+
15
+ useEffect(() => {
16
+ try {
17
+ const streamArray = JSON.parse(aiResponse);
18
+ setSearchResults(streamArray);
19
+ } catch (err) {
20
+ return;
21
+ }
22
+ }, [aiResponse])
23
 
24
  useEffect(() => {
25
  setSearchTerm(searchParams.get('searchTerm'))
 
64
  }, [searchParams, searchTerm]);
65
 
66
  return (
67
+ <>
68
+ {!searchResults.length ? <div style={{ padding: "20px" }} className="flex flex-row">
69
  <MemoizedReactMarkdown
70
  className="prose dark:prose-invert flex-1"
71
  components={{
 
113
  },
114
  }}
115
  >
116
+ {aiResponse}
117
+ </MemoizedReactMarkdown>
118
+ </div> : <WebSearchResults results={searchResults} />}
119
+ </>
120
  );
121
  }
src/components/WebSearchResults.jsx CHANGED
@@ -1,72 +1,30 @@
1
- import { MemoizedReactMarkdown } from './MemoizedReactMarkdown';
 
 
2
 
3
- export default function WebSearchResults({ aiResponse, results }) {
4
  return (
5
  <div className="w-full mx-auto px-3 pb-40 sm:pb-24 sm:pl-[5%] md:pl-[14%] lg:pl-52">
6
- <div className="w-1/4">
7
- <div className="flex flex-row">
8
- <MemoizedReactMarkdown
9
- className="prose dark:prose-invert flex-1"
10
- components={{
11
- code({ node, inline, className, children, ...props }) {
12
- if (children.length) {
13
- if (children[0] == '▍') {
14
- return <span className="animate-pulse cursor-default mt-1">▍</span>
15
- }
16
- children[0] = (children[0]).replace("`▍`", "▍")
17
- }
18
- const match = /language-(\w+)/.exec(className || '');
19
- return !inline ? (
20
- <CodeBlock
21
- key={Math.random()}
22
- language={(match && match[1]) || ''}
23
- value={String(children).replace(/\n$/, '')}
24
- {...props}
25
- />
26
- ) : (
27
- <code className={className} {...props}>
28
- {children}
29
- </code>
30
- );
31
- },
32
- table({ children }) {
33
- return (
34
- <table className="border-collapse border border-black px-3 py-1 dark:border-white">
35
- {children}
36
- </table>
37
- );
38
- },
39
- th({ children }) {
40
- return (
41
- <th className="break-words border border-black bg-gray-500 px-3 py-1 text-white dark:border-white">
42
- {children}
43
- </th>
44
- );
45
- },
46
- td({ children }) {
47
- return (
48
- <td className="break-words border border-black px-3 py-1 dark:border-white">
49
- {children}
50
- </td>
51
- );
52
- },
53
- }}
54
- >
55
- {`${aiResponse}`}
56
- </MemoizedReactMarkdown>
57
  </div>
58
- </div>
 
59
  </div>
60
  );
61
  }
62
-
63
-
64
- export async function getStaticProps(context) {
65
- // You can access the query parameters of the request with context.query
66
- const searchParams = context.query;
67
-
68
- // TODO: Perform any necessary operations with searchParams
69
-
70
- // Return searchParams as a prop to your page
71
- return { props: { searchParams } };
72
- }
 
1
+ import Link from "next/link";
2
+ import Parser from "html-react-parser";
3
+ import PaginationButtons from "./PaginationButtons";
4
 
5
+ export default function WebSearchResults({ results }) {
6
  return (
7
  <div className="w-full mx-auto px-3 pb-40 sm:pb-24 sm:pl-[5%] md:pl-[14%] lg:pl-52">
8
+ <p className="text-gray-600 text-sm mb-5 mt-3">
9
+ About {results.length} results
10
+ </p>
11
+ {results.map((result) => (
12
+ <div className="mb-8 max-w-xl" key={result.link}>
13
+ <div className="group flex flex-col">
14
+ <Link className="text-sm truncate" href={result.link}>
15
+ {result.link}
16
+ </Link>
17
+ <Link
18
+ className="group-hover:underline decoration-blue-800 text-xl truncate font-medium text-blue-800"
19
+ href={result.link}
20
+ >
21
+ {result.title}
22
+ </Link>
23
+ </div>
24
+ <p className="text-gray-600">{Parser(result.snippet)}</p>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  </div>
26
+ ))}
27
+ <PaginationButtons />
28
  </div>
29
  );
30
  }
 
 
 
 
 
 
 
 
 
 
 
src/pages/api/llm.js CHANGED
@@ -23,11 +23,15 @@ const handler = async (req, res) => {
23
  const promptToSend = "You are a helpful assistant, a search term is provided and you are given search results to help provide a useful response.";
24
  const stream = await LLMStream({ id: "gpt-3.5-turbo-0613" }, promptToSend, 0.8, messages, functions);
25
 
26
- let data = '';
27
  const decoder = new TextDecoder();
28
  for await (const chunk of stream) {
29
- data += decoder.decode(chunk);
30
- res.write(data);
 
 
 
 
31
  }
32
 
33
  return res.end();
 
23
  const promptToSend = "You are a helpful assistant, a search term is provided and you are given search results to help provide a useful response.";
24
  const stream = await LLMStream({ id: "gpt-3.5-turbo-0613" }, promptToSend, 0.8, messages, functions);
25
 
26
+ let data = "";
27
  const decoder = new TextDecoder();
28
  for await (const chunk of stream) {
29
+ let text = decoder.decode(chunk);
30
+
31
+ if (text !== 'null') {
32
+ data += text;
33
+ res.write(text === 'null' ? 'lol' : data);
34
+ }
35
  }
36
 
37
  return res.end();
src/pages/api/stream.js CHANGED
@@ -107,7 +107,7 @@ export const LLMStream = async (
107
  // function call here using func_call
108
  const fn = functions[func_call.name][func_call.name];
109
  const funcResult = await fn(func_call.arguments);
110
- const serpQueue = encoder.encode(JSON.stringify(funcResult));
111
  controller.enqueue(serpQueue);
112
  }
113
 
 
107
  // function call here using func_call
108
  const fn = functions[func_call.name][func_call.name];
109
  const funcResult = await fn(func_call.arguments);
110
+ const serpQueue = encoder.encode(funcResult);
111
  controller.enqueue(serpQueue);
112
  }
113