Skip to content

fix groq and cerebras clients #672

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
130 changes: 69 additions & 61 deletions lib/llm/CerebrasClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ export class CerebrasClient extends LLMClient {
? [
{
role: "system" as const,
content: `IMPORTANT: Your response must be valid JSON that matches this schema: ${JSON.stringify(options.response_model.schema)}`,
content: `IMPORTANT: Your response must be valid JSON that matches this schema: ${JSON.stringify(
options.response_model.schema,
)}`,
},
]
: []),
Expand Down Expand Up @@ -234,77 +236,83 @@ export class CerebrasClient extends LLMClient {
},
});

if (options.response_model) {
// First try standard function calling format
const toolCall = response.choices[0]?.message?.tool_calls?.[0];
if (toolCall?.function?.arguments) {
try {
const result = JSON.parse(toolCall.function.arguments);
if (this.enableCaching) {
this.cache.set(cacheOptions, result, options.requestId);
}
return result as T;
} catch (e) {
// If JSON parse fails, the model might be returning a different format
logger({
category: "cerebras",
message: "failed to parse tool call arguments as JSON, retrying",
level: 0,
auxiliary: {
error: {
value: e.message,
type: "string",
},
},
});
}
// If we have no response model, just return the entire LLMResponse
if (!options.response_model) {
if (this.enableCaching) {
await this.cache.set(cacheOptions, response, options.requestId);
}
return response as T;
}

// If we have content but no tool calls, try to parse the content as JSON
const content = response.choices[0]?.message?.content;
if (content) {
try {
// Try to extract JSON from the content
const jsonMatch = content.match(/\{[\s\S]*\}/);
if (jsonMatch) {
const result = JSON.parse(jsonMatch[0]);
if (this.enableCaching) {
this.cache.set(cacheOptions, result, options.requestId);
}
return result as T;
}
} catch (e) {
logger({
category: "cerebras",
message: "failed to parse content as JSON",
level: 0,
auxiliary: {
error: {
value: e.message,
type: "string",
},
},
});
// If we have a response model, parse JSON from tool calls or content
const toolCall = response.choices[0]?.message?.tool_calls?.[0];
if (toolCall?.function?.arguments) {
try {
const result = JSON.parse(toolCall.function.arguments);
const finalResponse = {
data: result,
usage: response.usage,
};
if (this.enableCaching) {
await this.cache.set(cacheOptions, finalResponse, options.requestId);
}
return finalResponse as T;
} catch (e) {
logger({
category: "cerebras",
message: "failed to parse tool call arguments as JSON, retrying",
level: 0,
auxiliary: {
error: {
value: e.message,
type: "string",
},
},
});
}
}

// If we still haven't found valid JSON and have retries left, try again
if (!retries || retries < 5) {
return this.createChatCompletion({
options,
logger,
retries: (retries ?? 0) + 1,
// If we have content but no tool calls, try to parse the content as JSON
const content = response.choices[0]?.message?.content;
if (content) {
try {
const jsonMatch = content.match(/\{[\s\S]*\}/);
if (jsonMatch) {
Comment on lines +283 to +284
Copy link

Choose a reason for hiding this comment

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

logic: regex pattern /{[\s\S]*}/ could match nested JSON objects incorrectly or match the first {} it finds instead of the complete JSON object

const result = JSON.parse(jsonMatch[0]);
const finalResponse = {
data: result,
usage: response.usage,
};
if (this.enableCaching) {
await this.cache.set(cacheOptions, finalResponse, options.requestId);
}
return finalResponse as T;
}
} catch (e) {
logger({
category: "cerebras",
message: "failed to parse content as JSON",
level: 0,
auxiliary: {
error: {
value: e.message,
type: "string",
},
},
});
}

throw new CreateChatCompletionResponseError("Invalid response schema");
}

if (this.enableCaching) {
this.cache.set(cacheOptions, response, options.requestId);
// If we still haven't found valid JSON and have retries left, try again
if (!retries || retries < 5) {
return this.createChatCompletion({
options,
logger,
retries: (retries ?? 0) + 1,
});
}

return response as T;
throw new CreateChatCompletionResponseError("Invalid response schema");
} catch (error) {
logger({
category: "cerebras",
Expand Down
131 changes: 70 additions & 61 deletions lib/llm/GroqClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ export class GroqClient extends LLMClient {
? [
{
role: "system" as const,
content: `IMPORTANT: Your response must be valid JSON that matches this schema: ${JSON.stringify(options.response_model.schema)}`,
content: `IMPORTANT: Your response must be valid JSON that matches this schema: ${JSON.stringify(
options.response_model.schema,
)}`,
},
]
: []),
Expand Down Expand Up @@ -234,77 +236,84 @@ export class GroqClient extends LLMClient {
},
});

if (options.response_model) {
// First try standard function calling format
const toolCall = response.choices[0]?.message?.tool_calls?.[0];
if (toolCall?.function?.arguments) {
try {
const result = JSON.parse(toolCall.function.arguments);
if (this.enableCaching) {
this.cache.set(cacheOptions, result, options.requestId);
}
return result as T;
} catch (e) {
// If JSON parse fails, the model might be returning a different format
logger({
category: "groq",
message: "failed to parse tool call arguments as JSON, retrying",
level: 0,
auxiliary: {
error: {
value: e.message,
type: "string",
},
},
});
}
// If there's no response model, return the entire response object
if (!options.response_model) {
if (this.enableCaching) {
await this.cache.set(cacheOptions, response, options.requestId);
}
return response as T;
}

// If we have content but no tool calls, try to parse the content as JSON
const content = response.choices[0]?.message?.content;
if (content) {
try {
// Try to extract JSON from the content
const jsonMatch = content.match(/\{[\s\S]*\}/);
if (jsonMatch) {
const result = JSON.parse(jsonMatch[0]);
if (this.enableCaching) {
this.cache.set(cacheOptions, result, options.requestId);
}
return result as T;
}
} catch (e) {
logger({
category: "groq",
message: "failed to parse content as JSON",
level: 0,
auxiliary: {
error: {
value: e.message,
type: "string",
},
},
});
// Otherwise, try parsing the JSON from the tool call or content
const toolCall = response.choices[0]?.message?.tool_calls?.[0];
if (toolCall?.function?.arguments) {
try {
const result = JSON.parse(toolCall.function.arguments);
const finalResponse = {
data: result,
usage: response.usage,
};
if (this.enableCaching) {
await this.cache.set(cacheOptions, finalResponse, options.requestId);
}
return finalResponse as T;
} catch (e) {
logger({
category: "groq",
message: "failed to parse tool call arguments as JSON, retrying",
level: 0,
auxiliary: {
error: {
value: e.message,
type: "string",
},
},
});
}
}

// If we still haven't found valid JSON and have retries left, try again
if (!retries || retries < 5) {
return this.createChatCompletion({
options,
logger,
retries: (retries ?? 0) + 1,
// If we have content but no tool calls, try to parse the content as JSON
const content = response.choices[0]?.message?.content;
if (content) {
try {
// Try to extract JSON from the content
const jsonMatch = content.match(/\{[\s\S]*\}/);
Copy link

Choose a reason for hiding this comment

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

logic: Regex pattern /{[\s\S]*}/ may match nested JSON objects incorrectly, leading to invalid parsing

if (jsonMatch) {
const result = JSON.parse(jsonMatch[0]);
const finalResponse = {
data: result,
usage: response.usage,
};
if (this.enableCaching) {
await this.cache.set(cacheOptions, finalResponse, options.requestId);
}
return finalResponse as T;
}
} catch (e) {
logger({
category: "groq",
message: "failed to parse content as JSON",
level: 0,
auxiliary: {
error: {
value: e.message,
type: "string",
},
},
});
}

throw new CreateChatCompletionResponseError("Invalid response schema");
}

if (this.enableCaching) {
this.cache.set(cacheOptions, response, options.requestId);
// If we still haven't found valid JSON and have retries left, try again
if (!retries || retries < 5) {
return this.createChatCompletion({
options,
logger,
retries: (retries ?? 0) + 1,
});
}

return response as T;
throw new CreateChatCompletionResponseError("Invalid response schema");
} catch (error) {
logger({
category: "groq",
Expand Down
Loading