Skip to content

Commit a1ecae1

Browse files
committed
fix gemini image api usage
1 parent 24a803b commit a1ecae1

File tree

3 files changed

+45
-25
lines changed

3 files changed

+45
-25
lines changed

packages/prai/src/history.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ export type HistoryState = {
118118
usedSchemas: Set<Schema>
119119
}
120120

121+
export const base64UrlPrefix = 'data:image/jpeg;base64,'
122+
121123
export class History {
122124
public readonly id = crypto.randomUUID()
123125
private readonly subscriptionMap = new Map<string, Set<(data: any) => void>>()
@@ -326,7 +328,7 @@ export class History {
326328
content.push({ type: 'input_audio', input_audio: { data: toBase64(value), format: fileType } })
327329
break
328330
case 'image':
329-
content.push({ type: 'image_url', image_url: { url: `data:image/jpeg;base64,${toBase64(value)}` } })
331+
content.push({ type: 'image_url', image_url: { url: `${base64UrlPrefix}${toBase64(value)}` } })
330332
break
331333
case 'data':
332334
content.push({ type: 'text', text: JSON.stringify(value) })

packages/prai/src/provider/gemini.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Provider } from '../model.js'
33
import { buildJsonSchema } from '../schema/json.js'
44
import { Schema, ZodString } from 'zod'
55
import { Message } from '../step.js'
6+
import { base64UrlPrefix } from '../history.js'
67

78
function buildAdditionalConfig(schema: Schema): GenerateContentConfig {
89
if (schema instanceof ZodString) {
@@ -79,7 +80,12 @@ export function gemini(
7980
function messageContentToPartUnion(content: Message['content'][number]): Part {
8081
switch (content.type) {
8182
case 'image_url':
82-
return { fileData: { fileUri: content.image_url.url, mimeType: 'image/jpeg' } }
83+
return {
84+
inlineData: {
85+
data: content.image_url.url.slice(base64UrlPrefix.length),
86+
mimeType: `image/jpeg`,
87+
},
88+
}
8389
case 'input_audio':
8490
return {
8591
inlineData: {

pnpm-lock.yaml

Lines changed: 35 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)