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

Update google params transform #91

Merged
merged 9 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions .changeset/violet-seas-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"llm-polyglot": patch
---

Removing "additional properties" property from function parameters in gemini requests
Binary file modified bun.lockb
Binary file not shown.
26 changes: 25 additions & 1 deletion public-packages/llm-client/src/providers/google/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ export class GoogleProvider extends GoogleGenerativeAI implements OpenAILikeClie
this.logger.addTransport(consoleTransport)
}

private cleanSchema(schema: Record<string, unknown>): Record<string, unknown> {
const { _additionalProperties, ...rest } = schema

if (rest["properties"] && typeof rest["properties"] === "object") {
rest["properties"] = Object.entries(rest["properties"]).reduce(
(acc, [key, value]) => {
acc[key] = typeof value === "object" && value !== null ? this.cleanSchema(value) : value
return acc
},
{} as Record<string, unknown>
)
}

if (rest["items"] && typeof rest["items"] === "object" && rest["items"] !== null) {
rest["items"] = this.cleanSchema(rest["items"] as Record<string, unknown>)
}

return rest
}

/**
* Transforms the OpenAI chat completion parameters into Google chat completion parameters.
* @param params - The OpenAI chat completion parameters.
Expand Down Expand Up @@ -107,10 +127,14 @@ export class GoogleProvider extends GoogleGenerativeAI implements OpenAILikeClie
function_declarations = tools.map(tool => {
allowedFunctionNames.push(tool.function.name)

const parameters = tool.function.parameters
? this.cleanSchema(tool.function.parameters)
: undefined

return {
name: tool.function.name ?? "",
description: tool.function.description ?? "",
parameters: tool.function.parameters
parameters
} as FunctionDeclarationsTool
})

Expand Down
Loading