-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[issue-5931] [FE/P SDK] fix: render Google ADK attachments in prompt parts #5967
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
Draft
ollieagent
wants to merge
3
commits into
main
Choose a base branch
from
ollie/issue-5931-render-attachments-correctly-879056
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 86 additions & 0 deletions
86
apps/opik-frontend/src/shared/PrettyLLMMessage/llmMessages/providers/google/detector.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { FormatDetector } from "../../types"; | ||
|
|
||
| interface GooglePart { | ||
| text?: string; | ||
| inline_data?: { | ||
| data?: string; | ||
| mime_type?: string; | ||
| }; | ||
| function_call?: unknown; | ||
| function_response?: unknown; | ||
| } | ||
|
|
||
| interface GoogleContent { | ||
| role?: string; | ||
| parts?: GooglePart[]; | ||
| } | ||
|
|
||
| interface GoogleCandidate { | ||
| content?: GoogleContent; | ||
| finish_reason?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if an object looks like a Google GenAI Content item | ||
| */ | ||
| const isGoogleContent = (item: unknown): item is GoogleContent => { | ||
| if (!item || typeof item !== "object") return false; | ||
| const c = item as Record<string, unknown>; | ||
|
|
||
| if (!Array.isArray(c.parts)) return false; | ||
|
|
||
| const validRoles = ["user", "model", "function", "system"]; | ||
| if (c.role !== undefined && !validRoles.includes(c.role as string)) | ||
| return false; | ||
|
|
||
| return true; | ||
| }; | ||
|
|
||
| /** | ||
| * Detects Google GenAI input format: | ||
| * { contents: [{ role: "user", parts: [{ text: "..." } | { inline_data: {...} }] }] } | ||
| */ | ||
| const hasGoogleInputFormat = (data: unknown): boolean => { | ||
| if (!data || typeof data !== "object") return false; | ||
| const d = data as Record<string, unknown>; | ||
|
|
||
| if (!Array.isArray(d.contents)) return false; | ||
| if (d.contents.length === 0) return false; | ||
|
|
||
| return d.contents.every(isGoogleContent); | ||
| }; | ||
|
|
||
| /** | ||
| * Detects Google GenAI output format: | ||
| * { candidates: [{ content: { role: "model", parts: [...] } }] } | ||
| */ | ||
| const hasGoogleOutputFormat = (data: unknown): boolean => { | ||
| if (!data || typeof data !== "object") return false; | ||
| const d = data as Record<string, unknown>; | ||
|
|
||
| if (!Array.isArray(d.candidates)) return false; | ||
| if (d.candidates.length === 0) return false; | ||
|
|
||
| return d.candidates.every((candidate: unknown) => { | ||
| if (!candidate || typeof candidate !== "object") return false; | ||
| const c = candidate as GoogleCandidate; | ||
| return c.content === undefined || isGoogleContent(c.content); | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * Detects if the provided data is in Google GenAI (ADK) format. | ||
| */ | ||
| export const detectGoogleFormat: FormatDetector = (data, prettifyConfig) => { | ||
| if (!data) return false; | ||
|
|
||
| const isInput = prettifyConfig?.fieldType === "input"; | ||
| const isOutput = prettifyConfig?.fieldType === "output"; | ||
|
|
||
| if (!isInput && !isOutput) return false; | ||
|
|
||
| if (isInput && hasGoogleInputFormat(data)) return true; | ||
| if (isOutput && hasGoogleOutputFormat(data)) return true; | ||
|
|
||
| return false; | ||
| }; |
11 changes: 11 additions & 0 deletions
11
apps/opik-frontend/src/shared/PrettyLLMMessage/llmMessages/providers/google/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { LLMMessageFormatImplementation } from "../../types"; | ||
| import { detectGoogleFormat } from "./detector"; | ||
| import { mapGoogleMessages } from "./mapper"; | ||
|
|
||
| export const googleFormat: LLMMessageFormatImplementation = { | ||
| name: "google", | ||
| detector: detectGoogleFormat, | ||
| mapper: mapGoogleMessages, | ||
| }; | ||
|
|
||
| export { detectGoogleFormat, mapGoogleMessages }; |
210 changes: 210 additions & 0 deletions
210
apps/opik-frontend/src/shared/PrettyLLMMessage/llmMessages/providers/google/mapper.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| import PrettyLLMMessage from "@/shared/PrettyLLMMessage"; | ||
| import { | ||
| FormatMapper, | ||
| LLMMessageDescriptor, | ||
| LLMBlockDescriptor, | ||
| LLMMapperResult, | ||
| } from "../../types"; | ||
| import { MessageRole } from "@/shared/PrettyLLMMessage/types"; | ||
| import { isPlaceholder } from "../../utils"; | ||
|
|
||
| interface GoogleInlineData { | ||
| data?: string; | ||
| mime_type?: string; | ||
| } | ||
|
|
||
| interface GooglePart { | ||
| text?: string; | ||
| inline_data?: GoogleInlineData; | ||
| function_call?: { | ||
| name?: string; | ||
| args?: Record<string, unknown>; | ||
| }; | ||
| function_response?: { | ||
| name?: string; | ||
| response?: unknown; | ||
| }; | ||
| } | ||
|
|
||
| interface GoogleContent { | ||
| role?: string; | ||
| parts?: GooglePart[]; | ||
| } | ||
|
|
||
| interface GoogleCandidate { | ||
| content?: GoogleContent; | ||
| finish_reason?: string; | ||
| } | ||
|
|
||
| interface GoogleInputData { | ||
| contents: GoogleContent[]; | ||
| } | ||
|
|
||
| interface GoogleOutputData { | ||
| candidates: GoogleCandidate[]; | ||
| usage_metadata?: { | ||
| prompt_token_count?: number; | ||
| candidates_token_count?: number; | ||
| total_token_count?: number; | ||
| }; | ||
| } | ||
|
|
||
| const generateMessageId = (index: number, prefix: string): string => | ||
| `${prefix}-${index}`; | ||
|
|
||
| /** | ||
| * Normalizes Google role names to our internal MessageRole type. | ||
| */ | ||
| const normalizeRole = (role: string | undefined): MessageRole => { | ||
| if (role === "model") return "assistant"; | ||
| if (role === "function") return "tool"; | ||
| return (role as MessageRole) || "user"; | ||
| }; | ||
|
|
||
| /** | ||
| * Maps an array of Google parts to block descriptors. | ||
| */ | ||
| const mapParts = ( | ||
| parts: GooglePart[], | ||
| role: MessageRole, | ||
| ): LLMBlockDescriptor[] => { | ||
| const blocks: LLMBlockDescriptor[] = []; | ||
| const images: Array<{ url: string; name: string }> = []; | ||
|
|
||
| parts.forEach((part, index) => { | ||
| if (part.text !== undefined) { | ||
| // Flush pending images before text | ||
| if (images.length > 0) { | ||
| blocks.push({ | ||
| blockType: "image", | ||
| component: PrettyLLMMessage.ImageBlock, | ||
| props: { images: [...images] }, | ||
| }); | ||
| images.length = 0; | ||
| } | ||
| blocks.push({ | ||
| blockType: "text", | ||
| component: PrettyLLMMessage.TextBlock, | ||
| props: { | ||
| children: part.text, | ||
| role, | ||
| showMoreButton: true, | ||
| }, | ||
| }); | ||
| } else if (part.inline_data) { | ||
| const data = part.inline_data.data; | ||
| if (data && data.length > 0) { | ||
| images.push({ | ||
| url: data, | ||
| name: isPlaceholder(data) ? data : `Image ${index + 1}`, | ||
| }); | ||
| } | ||
| } else if (part.function_call) { | ||
| const name = part.function_call.name || "function_call"; | ||
| const args = part.function_call.args | ||
| ? JSON.stringify(part.function_call.args, null, 2) | ||
| : ""; | ||
| blocks.push({ | ||
| blockType: "code", | ||
| component: PrettyLLMMessage.CodeBlock, | ||
| props: { code: args, label: name }, | ||
| }); | ||
| } else if (part.function_response) { | ||
| const name = part.function_response.name || "function_response"; | ||
| const response = part.function_response.response | ||
| ? JSON.stringify(part.function_response.response, null, 2) | ||
| : ""; | ||
| blocks.push({ | ||
| blockType: "code", | ||
| component: PrettyLLMMessage.CodeBlock, | ||
| props: { code: response, label: name }, | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| // Flush any remaining images | ||
| if (images.length > 0) { | ||
| blocks.push({ | ||
| blockType: "image", | ||
| component: PrettyLLMMessage.ImageBlock, | ||
| props: { images }, | ||
| }); | ||
| } | ||
|
|
||
| return blocks; | ||
| }; | ||
|
|
||
| /** | ||
| * Maps a GoogleContent object to an LLMMessageDescriptor. | ||
| */ | ||
| const mapGoogleContent = ( | ||
| content: GoogleContent, | ||
| index: number, | ||
| prefix: string, | ||
| ): LLMMessageDescriptor => { | ||
| const role = normalizeRole(content.role); | ||
| const blocks = content.parts ? mapParts(content.parts, role) : []; | ||
|
|
||
| return { | ||
| id: generateMessageId(index, prefix), | ||
| role, | ||
| blocks, | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Maps Google GenAI input format to LLMMapperResult. | ||
| */ | ||
| const mapGoogleInput = (data: GoogleInputData): LLMMapperResult => { | ||
| const messages = data.contents.map((content, index) => | ||
| mapGoogleContent(content, index, "input"), | ||
| ); | ||
| return { messages }; | ||
| }; | ||
|
|
||
| /** | ||
| * Maps Google GenAI output format to LLMMapperResult. | ||
| */ | ||
| const mapGoogleOutput = (data: GoogleOutputData): LLMMapperResult => { | ||
| const messages: LLMMessageDescriptor[] = []; | ||
|
|
||
| data.candidates.forEach((candidate, index) => { | ||
| if (!candidate.content) return; | ||
|
|
||
| const message = mapGoogleContent(candidate.content, index, "output"); | ||
| if (candidate.finish_reason) { | ||
| message.finishReason = candidate.finish_reason; | ||
| } | ||
| messages.push(message); | ||
| }); | ||
|
|
||
| const usage = data.usage_metadata | ||
| ? { | ||
| prompt_tokens: data.usage_metadata.prompt_token_count, | ||
| completion_tokens: data.usage_metadata.candidates_token_count, | ||
| total_tokens: data.usage_metadata.total_token_count, | ||
| } | ||
| : undefined; | ||
|
|
||
| return { messages, usage }; | ||
| }; | ||
|
|
||
| /** | ||
| * Maps Google GenAI format data to normalized LLMMapperResult. | ||
| */ | ||
| export const mapGoogleMessages: FormatMapper = (data, prettifyConfig) => { | ||
| if (!data) return { messages: [] }; | ||
|
|
||
| const isInput = prettifyConfig?.fieldType === "input"; | ||
| const isOutput = prettifyConfig?.fieldType === "output"; | ||
|
|
||
| if (isInput && typeof data === "object" && "contents" in data) { | ||
| return mapGoogleInput(data as GoogleInputData); | ||
| } | ||
|
|
||
| if (isOutput && typeof data === "object" && "candidates" in data) { | ||
| return mapGoogleOutput(data as GoogleOutputData); | ||
| } | ||
|
|
||
| return { messages: [] }; | ||
| }; |
1 change: 1 addition & 0 deletions
1
apps/opik-frontend/src/shared/PrettyLLMMessage/llmMessages/providers/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export { openaiFormat, detectOpenAIFormat, mapOpenAIMessages } from "./openai"; | ||
| export { googleFormat, detectGoogleFormat, mapGoogleMessages } from "./google"; | ||
| export { getFormat, getAllFormats } from "./registry"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove these screenshots, these should be comitted.