Spaces:
Running
Running
Liam Dyer
commited on
Trim trailing comma on tool calls (#1229)
Browse files* feat: trim trailing comma on tools results
* feat: clearer tool call parsing failure message
src/lib/server/textGeneration/tools.ts
CHANGED
@@ -143,11 +143,14 @@ export async function* runTools(
|
|
143 |
// look for a code blocks of ```json and parse them
|
144 |
// if they're valid json, add them to the calls array
|
145 |
if (output.generated_text) {
|
146 |
-
const codeBlocks = Array.from(output.generated_text.matchAll(/```json\n(.*?)```/gs))
|
|
|
|
|
|
|
147 |
if (codeBlocks.length === 0) continue;
|
148 |
|
149 |
// grab only the capture group from the regex match
|
150 |
-
for (const
|
151 |
try {
|
152 |
calls.push(
|
153 |
...JSON5.parse(block).filter(isExternalToolCall).map(externalToToolCall).filter(Boolean)
|
@@ -157,7 +160,7 @@ export async function* runTools(
|
|
157 |
yield {
|
158 |
type: MessageUpdateType.Status,
|
159 |
status: MessageUpdateStatus.Error,
|
160 |
-
message:
|
161 |
};
|
162 |
}
|
163 |
}
|
|
|
143 |
// look for a code blocks of ```json and parse them
|
144 |
// if they're valid json, add them to the calls array
|
145 |
if (output.generated_text) {
|
146 |
+
const codeBlocks = Array.from(output.generated_text.matchAll(/```json\n(.*?)```/gs))
|
147 |
+
.map(([, block]) => block)
|
148 |
+
// remove trailing comma
|
149 |
+
.map((block) => block.trim().replace(/,$/, ""));
|
150 |
if (codeBlocks.length === 0) continue;
|
151 |
|
152 |
// grab only the capture group from the regex match
|
153 |
+
for (const block of codeBlocks) {
|
154 |
try {
|
155 |
calls.push(
|
156 |
...JSON5.parse(block).filter(isExternalToolCall).map(externalToToolCall).filter(Boolean)
|
|
|
160 |
yield {
|
161 |
type: MessageUpdateType.Status,
|
162 |
status: MessageUpdateStatus.Error,
|
163 |
+
message: "Error while parsing tool calls, please retry",
|
164 |
};
|
165 |
}
|
166 |
}
|