matt HOFFNER commited on
Commit
15ae933
1 Parent(s): 3816441

hide chat and update llm url parsing (wip)

Browse files
Files changed (2) hide show
  1. components/Playground/index.tsx +34 -26
  2. utils/llm.ts +32 -29
components/Playground/index.tsx CHANGED
@@ -38,6 +38,7 @@ const Playground = () => {
38
  const [isModelInputVisible, setModelInputVisible] = useState(false);
39
  const [aiProvider, setAIProvider] = useState<string>("openai");
40
  const [urlOption, setUrlOption] = useState<string | any>(null);
 
41
 
42
  const [systemMessage, setSystemMessage] = useState(
43
  DEFAULT_PROMPT
@@ -140,6 +141,13 @@ const Playground = () => {
140
  >
141
  {isModelInputVisible ? '−' : '+'}
142
  </button>
 
 
 
 
 
 
 
143
 
144
  {isSystemInputVisible && (
145
  <textarea
@@ -176,32 +184,31 @@ const Playground = () => {
176
  </div>
177
  )}
178
 
179
- <form
180
- ref={formRef}
181
- onSubmit={modifiedHandleSubmit}
182
- className="relative w-full"
183
- >
184
-
185
- <textarea ref={inputRef} onChange={(e) => setInput(e.target.value)}
186
- placeholder="Enter your message"
187
- onKeyDown={(e) => {
188
- if (e.key === "Enter" && !e.shiftKey) {
189
- formRef.current?.requestSubmit();
190
- e.preventDefault();
191
- }
192
- }}
193
- spellCheck={false} className="textarea" value={input} />
194
- <button
195
- type="submit"
196
- className="absolute inset-y-0 right-3 my-auto flex h-8 w-8 items-center justify-center rounded-md transition-all"
197
- >
198
- <SendIcon
199
- className={"h-4 w-4"}
200
- />
201
- </button>
202
- </form>
203
  </div>
204
- <div className="flex flex-col items-start space-y-4 overflow-y-auto max-h-[20vh]">
 
205
  {messages?.map((message, index) => (
206
  <p key={index} className="messages-text">
207
  <ReactMarkdown
@@ -223,7 +230,8 @@ const Playground = () => {
223
  </ReactMarkdown>
224
  </p>
225
  ))}
226
- </div>
 
227
  </div>
228
  <Pane
229
  panelA={<InputCodeTab editorValue={editorValue} />}
 
38
  const [isModelInputVisible, setModelInputVisible] = useState(false);
39
  const [aiProvider, setAIProvider] = useState<string>("openai");
40
  const [urlOption, setUrlOption] = useState<string | any>(null);
41
+ const [isChatVisible, setIsChatVisible] = useState(true);
42
 
43
  const [systemMessage, setSystemMessage] = useState(
44
  DEFAULT_PROMPT
 
141
  >
142
  {isModelInputVisible ? '−' : '+'}
143
  </button>
144
+ <span className="text-lg font-semibold">Chat</span>
145
+ <button
146
+ onClick={() => setIsChatVisible(!isChatVisible)}
147
+ className="p-2 rounded-full hover:bg-gray-200 transition duration-300"
148
+ >
149
+ {isChatVisible ? '−' : '+'}
150
+ </button>
151
 
152
  {isSystemInputVisible && (
153
  <textarea
 
184
  </div>
185
  )}
186
 
187
+ {isChatVisible &&
188
+ <form ref={formRef} onSubmit={modifiedHandleSubmit} className="relative w-full">
189
+ <textarea ref={inputRef} onChange={(e) => setInput(e.target.value)}
190
+ placeholder="Enter your message"
191
+ onKeyDown={(e) => {
192
+ if (e.key === "Enter" && !e.shiftKey) {
193
+ formRef.current?.requestSubmit();
194
+ e.preventDefault();
195
+ }
196
+ }}
197
+ spellCheck={false} className="textarea" value={input} />
198
+ <button
199
+ type="submit"
200
+ className="absolute inset-y-0 right-3 my-auto flex h-8 w-8 items-center justify-center rounded-md transition-all"
201
+ >
202
+ <SendIcon
203
+ className={"h-4 w-4"}
204
+ />
205
+ </button>
206
+ </form>
207
+ }
208
+
 
 
209
  </div>
210
+ {isChatVisible && (
211
+ <div className="flex flex-col items-start space-y-4 overflow-y-auto max-h-[20vh]">
212
  {messages?.map((message, index) => (
213
  <p key={index} className="messages-text">
214
  <ReactMarkdown
 
230
  </ReactMarkdown>
231
  </p>
232
  ))}
233
+ </div>
234
+ )}
235
  </div>
236
  <Pane
237
  panelA={<InputCodeTab editorValue={editorValue} />}
utils/llm.ts CHANGED
@@ -53,36 +53,39 @@ export const LLMStream = async (baseUrl: string, messages: any[]) => {
53
  }
54
 
55
  const stream = new ReadableStream({
56
- async start(controller) {
57
- let accumulatedContent = ""; // To accumulate message content
58
-
59
- const onParse = (event: ParsedEvent | ReconnectInterval) => {
60
- if (event.type === 'event') {
61
- const data = event.data;
62
-
63
- if (data === '[DONE]') {
64
- const queue = encoder.encode(accumulatedContent);
65
- controller.enqueue(queue);
66
- controller.close();
67
- return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  }
69
- try {
70
- const parsedData = JSON.parse(data);
71
- const content = parsedData?.choices?.[0]?.message?.content;
72
- accumulatedContent += content ? content + " " : "";
73
- } catch (e) {
74
- controller.error(`Error parsing message: ${e}`);
75
- }
76
- }
77
- };
78
-
79
- const parser = createParser(onParse);
80
-
81
- for await (const chunk of res.body as any) {
82
- parser.feed(decoder.decode(chunk));
83
- }
84
- },
85
  });
86
-
87
  return stream;
88
  };
 
53
  }
54
 
55
  const stream = new ReadableStream({
56
+ async start(controller) {
57
+ const onParse = (event: ParsedEvent | ReconnectInterval) => {
58
+ if (event.type === 'event') {
59
+ const data = event.data;
60
+
61
+ if (data === "[DONE]") {
62
+ controller.close();
63
+ return;
64
+ }
65
+
66
+ try {
67
+ const json = JSON.parse(data);
68
+ if (json.choices[0].finish_reason != null) {
69
+ controller.close();
70
+ return;
71
+ }
72
+ const text = json.choices[0].delta.content;
73
+ const queue = encoder.encode(text);
74
+ controller.enqueue(queue);
75
+ } catch (e) {
76
+ controller.error(e);
77
+ }
78
+ }
79
+ };
80
+
81
+
82
+ const parser = createParser(onParse);
83
+
84
+ for await (const chunk of res.body as any) {
85
+ parser.feed(decoder.decode(chunk));
86
  }
87
+ },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  });
89
+
90
  return stream;
91
  };