Spaces:
Paused
Paused
matt HOFFNER
commited on
Commit
·
d604675
1
Parent(s):
b446577
cleanup
Browse files- src/pages/api/llm.js +1 -1
- src/pages/api/stream.js +16 -12
src/pages/api/llm.js
CHANGED
@@ -21,7 +21,7 @@ const handler = async (req) => {
|
|
21 |
|
22 |
let promptToSend = "You are a helpful assistant";
|
23 |
|
24 |
-
const stream = await LLMStream({ id: "gpt-3.5-turbo-0613" }, promptToSend, 0.8,
|
25 |
|
26 |
return new Response(stream);
|
27 |
} catch (error) {
|
|
|
21 |
|
22 |
let promptToSend = "You are a helpful assistant";
|
23 |
|
24 |
+
const stream = await LLMStream({ id: "gpt-3.5-turbo-0613" }, promptToSend, 0.8, messages, functions);
|
25 |
|
26 |
return new Response(stream);
|
27 |
} catch (error) {
|
src/pages/api/stream.js
CHANGED
@@ -71,24 +71,28 @@ export const LLMStream = async (
|
|
71 |
if (event.type === 'event') {
|
72 |
console.log(event);
|
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();
|
79 |
return;
|
80 |
} else if (json.choices[0].finish_reason === "function_call") {
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
} else {
|
93 |
const text = json.choices[0].delta.content;
|
94 |
const queue = encoder.encode(text);
|
|
|
71 |
if (event.type === 'event') {
|
72 |
console.log(event);
|
73 |
const data = event.data;
|
74 |
+
if (data === '[DONE]') {
|
75 |
+
controller.close();
|
76 |
+
return;
|
77 |
+
}
|
78 |
|
79 |
try {
|
80 |
const json = JSON.parse(data);
|
81 |
+
if (json.choices[0].finish_reason === "stop") {
|
82 |
controller.close();
|
83 |
return;
|
84 |
} else if (json.choices[0].finish_reason === "function_call") {
|
85 |
+
const fnName = json.choices[0].message.function_call.name;
|
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);
|