summarylogtreecommitdiffstats
path: root/0002-Fix-json-parsing.patch
blob: 10b42afee64644c28504ce39f949ff4c9e9a3ac9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
From ffa902c68936cf362737d9edd1e9dcac6ac70fbc Mon Sep 17 00:00:00 2001
From: Liao Junxuan <mikeljx@126.com>
Date: Tue, 12 Dec 2023 21:24:10 +0800
Subject: [PATCH] Fix json parsing

See https://github.com/mckaywrigley/chatbot-ui/issues/580#issuecomment-1523023238
---
 utils/server/index.ts | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/utils/server/index.ts b/utils/server/index.ts
index d75b8f1..6a6abc3 100644
--- a/utils/server/index.ts
+++ b/utils/server/index.ts
@@ -90,17 +90,19 @@ export const OpenAIStream = async (
         if (event.type === 'event') {
           const data = event.data;
 
-          try {
-            const json = JSON.parse(data);
-            if (json.choices[0].finish_reason != null) {
-              controller.close();
-              return;
+          if (data !== '[DONE]') {
+            try {
+              const json = JSON.parse(data);
+              if (json.choices[0].finish_reason != null) {
+                controller.close();
+                return;
+              }
+              const text = json.choices[0].delta.content;
+              const queue = encoder.encode(text);
+              controller.enqueue(queue);
+            } catch (e) {
+              controller.error(e);
             }
-            const text = json.choices[0].delta.content;
-            const queue = encoder.encode(text);
-            controller.enqueue(queue);
-          } catch (e) {
-            controller.error(e);
           }
         }
       };
-- 
2.43.0