Skip to content

Commit 9a02c50

Browse files
committed
Fix long tokens, split token based on curly brackets
1 parent f5d8244 commit 9a02c50

File tree

8 files changed

+2330
-1855
lines changed

8 files changed

+2330
-1855
lines changed

examples/02_colors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async function callGenerateColors(
3333
"Give me a palette of 5 gorgeous color with the hex code, name and a description.",
3434
},
3535
],
36-
model: "gpt-3.5-turbo", // OR "gpt-4"
36+
model: "gpt-4-turbo-preview", // OR "gpt-4"
3737
stream: true, // ENABLE STREAMING
3838
temperature: 1.3,
3939
functions: [

examples/03_colors_raw_json.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async function callGenerateColors(
2626
"Give me a palette of 5 gorgeous color with the hex code, name and a description.",
2727
},
2828
],
29-
model: "gpt-3.5-turbo", // OR "gpt-4"
29+
model: "gpt-4-turbo-preview", // OR "gpt-4"
3030
stream: true, // ENABLE STREAMING
3131
temperature: 1.3,
3232
functions: [

examples/05_postcodes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async function main() {
2323
content: "Give me 3 cities and their postcodes in California.",
2424
},
2525
],
26-
model: "gpt-3.5-turbo", // OR "gpt-4"
26+
model: "gpt-3.5-turbo", // OR "gpt-4" OR "gpt-4-turbo-preview"
2727
stream: true, // ENABLE STREAMING
2828
temperature: 1.1,
2929
functions: [
@@ -67,7 +67,7 @@ async function main() {
6767
// - StreamObjectKeyValue: (PROGRESSIVE) Stream of JSON objects and key value pairs
6868
// - StreamObject: (ONE-BY-ONE) Stream of JSON objects
6969
// - NoStream: (ALL-TOGETHER) All the data is returned at the end of the process
70-
const mode = StreamMode.StreamObjectKeyValueTokens;
70+
const mode = StreamMode.StreamObject;
7171

7272
// Create an instance of the handler
7373
const openAiHandler = new OpenAiHandler(mode);

package-lock.json

+2,254-1,840
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"packageManager": "[email protected]",
1818
"dependencies": {
1919
"@changesets/cli": "^2.26.2",
20+
"openai": "^4.26.0",
2021
"turbo": "^1.10.14"
2122
},
2223
"scripts": {

packages/openai-partial-stream/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"typescript": "^5.0.0"
4141
},
4242
"dependencies": {
43-
"openai": "^4.6.0",
43+
"openai": "^4.26.0",
4444
"zod": "^3.22.3",
4545
"zod-to-json-schema": "^3.21.4"
4646
},

packages/openai-partial-stream/src/openAiHandler.ts

+16-10
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,23 @@ export class OpenAiHandler {
2929
continue;
3030
}
3131

32-
const res = this.parser.parse(content);
33-
34-
if (
35-
this.mode === StreamMode.NoStream ||
36-
this.mode === StreamMode.Batch
37-
) {
38-
if (res) {
39-
this.noStreamBufferList.push(res);
32+
// TODO: Handle this in the stream parser
33+
// The stream parser should be able to return multi responses.
34+
const chunks = content.split(/(?<={|})/);
35+
36+
for (const chunk of chunks) {
37+
const res = this.parser.parse(chunk);
38+
39+
if (
40+
this.mode === StreamMode.NoStream ||
41+
this.mode === StreamMode.Batch
42+
) {
43+
if (res) {
44+
this.noStreamBufferList.push(res);
45+
}
46+
} else if (res) {
47+
yield res;
4048
}
41-
} else if (res) {
42-
yield res;
4349
}
4450
}
4551

packages/openai-partial-stream/tests/streamParser.test.ts

+54
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,57 @@ test("stream partial", async () => {
217217

218218
expect(results).toEqual(expected);
219219
});
220+
221+
// TODO: Improve the parer to handle this case
222+
// Parser should be able to return an array when their is mutli object closed
223+
// In the same chunk
224+
test.skip("stream partial longer tokens", async () => {
225+
const inputs = [
226+
`{ "colors": [{"hex": "#FF7F50`,
227+
`"},{`,
228+
`"he`,
229+
`x": "#465`,
230+
`2B4"}] }`,
231+
];
232+
233+
// TODO: Add another case
234+
// const inputs = [
235+
// `
236+
// { "colors": [{"a":1},{"b":2},{"c":3`,
237+
// `}`,
238+
// `] }`,
239+
// ];
240+
241+
const streamParser = new StreamParser(StreamMode.StreamObject);
242+
243+
const results = inputs.map((item) => streamParser.parse(item));
244+
245+
const expected = [
246+
null,
247+
[
248+
{
249+
index: 0,
250+
status: "COMPLETED",
251+
data: { colors: [{ hex: "#FF7F50" }] },
252+
},
253+
{
254+
index: 1,
255+
status: "PARTIAL",
256+
data: { colors: [{ hex: "#FF7F50" }, {}] },
257+
},
258+
],
259+
null,
260+
null,
261+
{
262+
index: 1,
263+
status: "COMPLETED",
264+
data: { colors: [{ hex: "#FF7F50" }, { hex: "#4652B4" }] },
265+
},
266+
];
267+
268+
results.forEach((result) => {
269+
console.log(result?.index, result?.status, result?.data);
270+
});
271+
272+
expect(results).toEqual(expected);
273+
});

0 commit comments

Comments
 (0)