Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: removing partial unicode when streaming #512

Merged
merged 6 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/ai-jsx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"repository": "fixie-ai/ai-jsx",
"bugs": "https://github.com/fixie-ai/ai-jsx/issues",
"homepage": "https://ai-jsx.com",
"version": "0.28.1",
"version": "0.28.2",
"volta": {
"extends": "../../package.json"
},
Expand Down
16 changes: 13 additions & 3 deletions packages/ai-jsx/src/lib/openai.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,19 @@ export async function* OpenAIChatModel(
argsJson += toolCall.function.arguments;
}

yield (
<FunctionCall id={id} partial name={name} args={JSON.parse(patchedUntruncateJson(argsJson || '{}'))} />
);
let partialArgs: Record<string, string | number | boolean | null> | undefined = undefined;
try {
partialArgs = JSON.parse(patchedUntruncateJson(argsJson || '{}'));
} catch (e: any) {
// If the JSON is incomplete and we get an error, we can ignore it.
const acceptedErrorPattern = /Unexpected .* JSON/;
if (!acceptedErrorPattern.test(e.message)) {
throw e;
}
}
if (partialArgs !== undefined) {
yield <FunctionCall id={id} partial name={name} args={partialArgs} />;
}

delta = await advance();
}
Expand Down
7 changes: 6 additions & 1 deletion packages/ai-jsx/src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ export function getEnvVar(name: string, shouldThrow: boolean = true) {
* There's an ESM issue with untruncate-json, so we need to do this to support running on both client & server.
*/
/** @hidden */
export const patchedUntruncateJson = 'default' in untruncateJson ? untruncateJson.default : untruncateJson;
const _patchedUntruncateJson = 'default' in untruncateJson ? untruncateJson.default : untruncateJson;

export function patchedUntruncateJson(str: string) {
// Remove partial unicode characters: e.g. "\\u5728\\u5fA" -> "\\u5728"
return _patchedUntruncateJson(str.replace(/\\u[\dA-F]{0,3}$/gi, ''));
}
19 changes: 18 additions & 1 deletion packages/ai-jsx/test/lib/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getEnvVar } from '../../dist/cjs/lib/util.cjs';
import { getEnvVar, patchedUntruncateJson } from '../../dist/cjs/lib/util.cjs';

process.env.EXISTS = 'exists';
process.env.REACT_APP_ONLY = 'react-value';
Expand Down Expand Up @@ -42,3 +42,20 @@ test('env is not defined', () => {

globalThis.process.env = originalEnv;
});

test('Basic untrucation of JSON', () => {
expect(patchedUntruncateJson('{"a":')).toEqual('{}');
expect(patchedUntruncateJson('{"a":"b')).toEqual('{"a":"b"}');
expect(patchedUntruncateJson('{"a":"b"')).toEqual('{"a":"b"}');
});

test('Partial unicode characters are removed', () => {
expect(patchedUntruncateJson('{"a":"\\u5728\\u5fA')).toEqual('{"a":"\\u5728"}');
expect(patchedUntruncateJson('"\\u5728\\u')).toEqual('"\\u5728"');
expect(patchedUntruncateJson('"\\u5728\\u0')).toEqual('"\\u5728"');
expect(patchedUntruncateJson('"\\u5728\\u5fA')).toEqual('"\\u5728"');
});

test('Unicode characters are allowed', () => {
expect(patchedUntruncateJson('{"a":"\\u5728什么是"}')).toEqual('{"a":"\\u5728什么是"}');
});
6 changes: 5 additions & 1 deletion packages/docs/docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

## 0.28.1
## 0.28.2

- Fix bug where partially streamed unicode characters (e.g. Chinese) would cause an error in OpenAI function calls.

## [0.28.1](https://github.com/fixie-ai/ai-jsx/tree/4c67d845f48585dc3f26e90a9a656471f40c82ed)

- Add `openai.finish_reason` span attribute for `OpenAIChatModel`

Expand Down