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

openai[patch]: Bugfix: try catch around JSON stringify/parse when counting OpenAI function calling tokens #4799

Merged

Conversation

davidfant
Copy link
Contributor

If OpenAI doesn't return valid JSON in arguments (which is rare, but happens) the OpenAI invocation just throws because token counting fails. Counting the tokens without removing newlines/spaces (which I assume is needed bc it was already there, but I don't know for sure), should be a good enough approximation of token count

Copy link

vercel bot commented Mar 16, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchainjs-api-refs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 18, 2024 6:40pm
langchainjs-docs ✅ Ready (Inspect) Visit Preview Mar 18, 2024 6:40pm

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. auto:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Mar 16, 2024
@davidfant davidfant force-pushed the bugfix/try-catch-arg-token-counting branch from f4a5cb9 to 834d326 Compare March 16, 2024 10:54
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Mar 16, 2024
);
);
} catch (error) {
console.error("Error parsing function arguments", error, JSON.stringify(openAIMessage.additional_kwargs.function_call));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your call how you want to approach this kind of logging. I've had this as a patch-package patch for a while. Logging this is useful bc then I can easily see when arguments is invalid JSON

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this seems reasonable, thank you!

@jacoblee93 jacoblee93 changed the title Bugfix: try catch around JSON stringify/parse when counting OpenAI function calling tokens openai[patch]: Bugfix: try catch around JSON stringify/parse when counting OpenAI function calling tokens Mar 18, 2024
@jacoblee93
Copy link
Collaborator

Thank you for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature lgtm PRs that are ready to be merged as-is size:S This PR changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants