Skip to content

Commit

Permalink
Trim trailing comma on tool calls (#1229)
Browse files Browse the repository at this point in the history
* feat: trim trailing comma on tools results

* feat: clearer tool call parsing failure message
  • Loading branch information
Saghen authored May 31, 2024
1 parent d58c7e3 commit 203e1c0
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/lib/server/textGeneration/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,14 @@ export async function* runTools(
// look for a code blocks of ```json and parse them
// if they're valid json, add them to the calls array
if (output.generated_text) {
const codeBlocks = Array.from(output.generated_text.matchAll(/```json\n(.*?)```/gs));
const codeBlocks = Array.from(output.generated_text.matchAll(/```json\n(.*?)```/gs))
.map(([, block]) => block)
// remove trailing comma
.map((block) => block.trim().replace(/,$/, ""));
if (codeBlocks.length === 0) continue;

// grab only the capture group from the regex match
for (const [, block] of codeBlocks) {
for (const block of codeBlocks) {
try {
calls.push(
...JSON5.parse(block).filter(isExternalToolCall).map(externalToToolCall).filter(Boolean)
Expand All @@ -157,7 +160,7 @@ export async function* runTools(
yield {
type: MessageUpdateType.Status,
status: MessageUpdateStatus.Error,
message: stringifyError(e),
message: "Error while parsing tool calls, please retry",
};
}
}
Expand Down

0 comments on commit 203e1c0

Please sign in to comment.