Skip to content

Commit

Permalink
fix(js): disabled explicit json mode when using tools
Browse files Browse the repository at this point in the history
  • Loading branch information
cabljac committed Sep 12, 2024
1 parent 97b7223 commit 92deeb7
Show file tree
Hide file tree
Showing 5 changed files with 300 additions and 14 deletions.
29 changes: 15 additions & 14 deletions js/plugins/googleai/src/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,18 +534,6 @@ export function googleAIModel(
systemInstruction = toGeminiSystemInstruction(systemMessage);
}
}
const generationConfig: GenerationConfig = {
candidateCount: request.candidates || undefined,
temperature: request.config?.temperature,
maxOutputTokens: request.config?.maxOutputTokens,
topK: request.config?.topK,
topP: request.config?.topP,
stopSequences: request.config?.stopSequences,
responseMimeType:
request.output?.format === 'json' || request.output?.schema
? 'application/json'
: undefined,
};

const tools: Tool[] = [];

Expand All @@ -564,6 +552,21 @@ export function googleAIModel(
});
}

// cannot use tools with json mode
const jsonMode =
(request.output?.format === 'json' || !!request.output?.schema) &&
tools.length === 0;

const generationConfig: GenerationConfig = {
candidateCount: request.candidates || undefined,
temperature: request.config?.temperature,
maxOutputTokens: request.config?.maxOutputTokens,
topK: request.config?.topK,
topP: request.config?.topP,
stopSequences: request.config?.stopSequences,
responseMimeType: jsonMode ? 'application/json' : undefined,
};

const chatRequest = {
systemInstruction,
generationConfig,
Expand All @@ -574,8 +577,6 @@ export function googleAIModel(
safetySettings: request.config?.safetySettings,
} as StartChatParams;
const msg = toGeminiMessage(messages[messages.length - 1], model);
const jsonMode =
request.output?.format === 'json' || !!request.output?.schema;
const fromJSONModeScopedGeminiCandidate = (
candidate: GeminiCandidate
) => {
Expand Down
155 changes: 155 additions & 0 deletions js/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions js/testapps/basic-gemini/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "basic-gemini",
"version": "1.0.0",
"description": "",
"main": "lib/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node lib/index.js",
"build": "tsc",
"build:watch": "tsc --watch"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@genkit-ai/ai": "workspace:*",
"@genkit-ai/core": "workspace:*",
"@genkit-ai/dotprompt": "workspace:*",
"@genkit-ai/flow": "workspace:*",
"@genkit-ai/googleai": "workspace:*",
"express": "^4.20.0",
"zod": "^3.22.4"
},
"devDependencies": {
"typescript": "^5.6.2"
}
}
Loading

0 comments on commit 92deeb7

Please sign in to comment.