Spaces:
Paused
Paused
matt HOFFNER
commited on
Commit
·
15408a7
1
Parent(s):
2fbdf6a
streaming functions
Browse files- src/pages/api/stream.js +51 -18
src/pages/api/stream.js
CHANGED
@@ -20,6 +20,7 @@ export const LLMStream = async (
|
|
20 |
messages,
|
21 |
functions
|
22 |
) => {
|
|
|
23 |
let url = `${OPENAI_API_HOST}/v1/chat/completions`;
|
24 |
const res = await fetch(url, {
|
25 |
headers: {
|
@@ -40,6 +41,7 @@ export const LLMStream = async (
|
|
40 |
],
|
41 |
max_tokens: 1000,
|
42 |
temperature: temperature,
|
|
|
43 |
stream: true,
|
44 |
}),
|
45 |
});
|
@@ -67,6 +69,11 @@ export const LLMStream = async (
|
|
67 |
|
68 |
const stream = new ReadableStream({
|
69 |
async start(controller) {
|
|
|
|
|
|
|
|
|
|
|
70 |
const onParse = async (event) => {
|
71 |
if (event.type === 'event') {
|
72 |
const data = event.data;
|
@@ -75,38 +82,64 @@ export const LLMStream = async (
|
|
75 |
if (data === "[DONE]" || !data) {
|
76 |
return;
|
77 |
}
|
|
|
78 |
const json = JSON.parse(data);
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
console.log(
|
84 |
-
const
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
}
|
93 |
-
const text = json.choices[0].delta.content;
|
94 |
-
const queue = encoder.encode(text);
|
95 |
-
controller.enqueue(queue);
|
96 |
} catch (e) {
|
97 |
console.log(e);
|
98 |
controller.error(e);
|
99 |
}
|
100 |
}
|
101 |
};
|
102 |
-
|
103 |
const parser = createParser(onParse);
|
104 |
-
|
105 |
for await (const chunk of res.body) {
|
106 |
parser.feed(decoder.decode(chunk));
|
107 |
}
|
108 |
},
|
109 |
});
|
|
|
110 |
|
111 |
return stream;
|
112 |
};
|
|
|
20 |
messages,
|
21 |
functions
|
22 |
) => {
|
23 |
+
|
24 |
let url = `${OPENAI_API_HOST}/v1/chat/completions`;
|
25 |
const res = await fetch(url, {
|
26 |
headers: {
|
|
|
41 |
],
|
42 |
max_tokens: 1000,
|
43 |
temperature: temperature,
|
44 |
+
functions: [functions['googleCustomSearch']['googleCustomSearchSchema']],
|
45 |
stream: true,
|
46 |
}),
|
47 |
});
|
|
|
69 |
|
70 |
const stream = new ReadableStream({
|
71 |
async start(controller) {
|
72 |
+
let func_call = {
|
73 |
+
"name": null,
|
74 |
+
"arguments": "",
|
75 |
+
};
|
76 |
+
|
77 |
const onParse = async (event) => {
|
78 |
if (event.type === 'event') {
|
79 |
const data = event.data;
|
|
|
82 |
if (data === "[DONE]" || !data) {
|
83 |
return;
|
84 |
}
|
85 |
+
|
86 |
const json = JSON.parse(data);
|
87 |
+
console.log(json);
|
88 |
+
|
89 |
+
if (Array.isArray(json.choices) && json.choices.length > 0) {
|
90 |
+
const choice = json.choices[0];
|
91 |
+
console.log(choice);
|
92 |
+
const delta = choice.delta;
|
93 |
+
|
94 |
+
if (choice.finish_reason === "stop") {
|
95 |
+
controller.close();
|
96 |
+
return;
|
97 |
+
}
|
98 |
+
|
99 |
+
if (delta.hasOwnProperty("function_call")) {
|
100 |
+
if (delta.function_call.hasOwnProperty("name")) {
|
101 |
+
func_call["name"] = delta.function_call["name"];
|
102 |
+
}
|
103 |
+
if (delta.function_call.hasOwnProperty("arguments")) {
|
104 |
+
func_call["arguments"] += delta.function_call["arguments"];
|
105 |
+
}
|
106 |
+
}
|
107 |
+
|
108 |
+
if (choice.finish_reason === "function_call") {
|
109 |
+
// function call here using func_call
|
110 |
+
console.log('Function call:', func_call);
|
111 |
+
const fn = functions[func_call.name]['googleCustomSearch'];
|
112 |
+
console.log(fn);
|
113 |
+
const funcResult = await fn(JSON.parse(func_call.arguments));
|
114 |
+
console.log(funcResult);
|
115 |
+
const serpQueue = encoder.encode(funcResult);
|
116 |
+
controller.enqueue(serpQueue);
|
117 |
+
return;
|
118 |
+
}
|
119 |
+
|
120 |
+
if (delta && 'content' in delta) {
|
121 |
+
const text = delta.content;
|
122 |
+
const queue = encoder.encode(text);
|
123 |
+
controller.enqueue(queue);
|
124 |
+
}
|
125 |
+
} else {
|
126 |
+
console.error('No choices found in json');
|
127 |
}
|
|
|
|
|
|
|
128 |
} catch (e) {
|
129 |
console.log(e);
|
130 |
controller.error(e);
|
131 |
}
|
132 |
}
|
133 |
};
|
134 |
+
|
135 |
const parser = createParser(onParse);
|
136 |
+
|
137 |
for await (const chunk of res.body) {
|
138 |
parser.feed(decoder.decode(chunk));
|
139 |
}
|
140 |
},
|
141 |
});
|
142 |
+
|
143 |
|
144 |
return stream;
|
145 |
};
|