matt HOFFNER commited on
Commit
bf8ef54
·
1 Parent(s): 284c9cc
Files changed (1) hide show
  1. src/pages/api/stream.js +14 -7
src/pages/api/stream.js CHANGED
@@ -73,6 +73,10 @@ export const LLMStream = async (
73
  const data = event.data;
74
 
75
  try {
 
 
 
 
76
  const json = JSON.parse(data);
77
  if (json.choices[0].finish_reason === "stop") {
78
  controller.close();
@@ -82,16 +86,19 @@ export const LLMStream = async (
82
  const args = json.choices[0].message.function_call.arguments;
83
 
84
  const fn = functions[fnName];
85
- const result = await fn(...Object.values(JSON.parse(args)));
86
 
87
  console.log(`Function call: ${fnName}, Arguments: ${args}`);
88
- console.log(`Calling Function ${fnName} Result: ` + result);
89
-
90
 
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);
 
73
  const data = event.data;
74
 
75
  try {
76
+ if (data === '[DONE]') {
77
+ controller.close();
78
+ return;
79
+ }
80
  const json = JSON.parse(data);
81
  if (json.choices[0].finish_reason === "stop") {
82
  controller.close();
 
86
  const args = json.choices[0].message.function_call.arguments;
87
 
88
  const fn = functions[fnName];
89
+ const functionResult = await fn(...Object.values(JSON.parse(args)));
90
 
91
  console.log(`Function call: ${fnName}, Arguments: ${args}`);
92
+ console.log(`Calling Function ${fnName} Result: ` + functionResult);
 
93
 
94
+ const queue = encoder.encode(functionResult);
95
+ controller.enqueue(queue);
96
+ } else {
97
+ const text = json.choices[0].delta.content;
98
+ const queue = encoder.encode(text);
99
+ controller.enqueue(queue);
100
+ }
101
+
102
  } catch (e) {
103
  console.log(e);
104
  controller.error(e);