Skip to content

Commit

Permalink
fix(js/plugins/googleai): add lenient parsing of JSON in data part
Browse files Browse the repository at this point in the history
  • Loading branch information
cabljac committed Oct 3, 2024
1 parent b82af41 commit 8e4c6fc
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions js/plugins/googleai/src/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,39 @@
*/

import {
Content as GeminiMessage,
FileDataPart,
FunctionCallPart,
FunctionDeclaration,
FunctionDeclarationSchemaType,
FunctionResponsePart,
GenerateContentCandidate as GeminiCandidate,
Content as GeminiMessage,
Part as GeminiPart,
GenerateContentResponse,
GenerationConfig,
GoogleGenerativeAI,
InlineDataPart,
Part as GeminiPart,
RequestOptions,
StartChatParams,
Tool,
} from '@google/generative-ai';
import { GENKIT_CLIENT_HEADER, z } from 'genkit';
import { extractJson } from 'genkit/extract';
import {
CandidateData,
defineModel,
GenerationCommonConfigSchema,
getBasicUsageStats,
MediaPart,
MessageData,
ModelAction,
ModelMiddleware,
modelRef,
ModelReference,
Part,
ToolDefinitionSchema,
ToolRequestPart,
ToolResponsePart,
defineModel,
getBasicUsageStats,
modelRef,
} from 'genkit/model';
import {
downloadRequestMedia,
Expand Down Expand Up @@ -387,7 +388,7 @@ function toGeminiPart(part: Part): GeminiPart {

function fromGeminiPart(part: GeminiPart, jsonMode: boolean): Part {
if (jsonMode && part.text !== undefined) {
return { data: JSON.parse(part.text) };
return { data: extractJson(part.text) };
}
if (part.text !== undefined) return { text: part.text };
if (part.inlineData) return fromInlineData(part);
Expand Down

0 comments on commit 8e4c6fc

Please sign in to comment.