@ai-sdk/google
Advanced tools
Comparing version 0.0.26 to 0.0.27
@@ -33,3 +33,3 @@ "use strict"; | ||
// src/google-generative-ai-language-model.ts | ||
var import_provider = require("@ai-sdk/provider"); | ||
var import_provider2 = require("@ai-sdk/provider"); | ||
var import_provider_utils3 = require("@ai-sdk/provider-utils"); | ||
@@ -39,2 +39,3 @@ var import_zod2 = require("zod"); | ||
// src/convert-to-google-generative-ai-messages.ts | ||
var import_provider = require("@ai-sdk/provider"); | ||
var import_provider_utils = require("@ai-sdk/provider-utils"); | ||
@@ -45,11 +46,18 @@ async function convertToGoogleGenerativeAIMessages({ | ||
}) { | ||
const messages = []; | ||
const systemInstructionParts = []; | ||
const contents = []; | ||
let systemMessagesAllowed = true; | ||
for (const { role, content } of prompt) { | ||
switch (role) { | ||
case "system": { | ||
messages.push({ role: "user", parts: [{ text: content }] }); | ||
messages.push({ role: "model", parts: [{ text: "" }] }); | ||
if (!systemMessagesAllowed) { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "system messages are only supported at the beginning of the conversation" | ||
}); | ||
} | ||
systemInstructionParts.push({ text: content }); | ||
break; | ||
} | ||
case "user": { | ||
systemMessagesAllowed = false; | ||
const parts = []; | ||
@@ -85,7 +93,8 @@ for (const part of content) { | ||
} | ||
messages.push({ role: "user", parts }); | ||
contents.push({ role: "user", parts }); | ||
break; | ||
} | ||
case "assistant": { | ||
messages.push({ | ||
systemMessagesAllowed = false; | ||
contents.push({ | ||
role: "model", | ||
@@ -113,3 +122,4 @@ parts: content.map((part) => { | ||
case "tool": { | ||
messages.push({ | ||
systemMessagesAllowed = false; | ||
contents.push({ | ||
role: "user", | ||
@@ -131,3 +141,6 @@ parts: content.map((part) => ({ | ||
} | ||
return messages; | ||
return { | ||
systemInstruction: systemInstructionParts.length > 0 ? { parts: systemInstructionParts } : void 0, | ||
contents | ||
}; | ||
} | ||
@@ -174,3 +187,3 @@ | ||
this.specificationVersion = "v1"; | ||
this.defaultObjectGenerationMode = "json"; | ||
this.defaultObjectGenerationMode = "tool"; | ||
this.modelId = modelId; | ||
@@ -193,2 +206,3 @@ this.settings = settings; | ||
}) { | ||
var _a; | ||
const type = mode.type; | ||
@@ -222,3 +236,3 @@ const warnings = []; | ||
}; | ||
const contents = await convertToGoogleGenerativeAIMessages({ prompt }); | ||
const { contents, systemInstruction } = await convertToGoogleGenerativeAIMessages({ prompt }); | ||
switch (type) { | ||
@@ -230,4 +244,6 @@ case "regular": { | ||
contents, | ||
systemInstruction, | ||
safetySettings: this.settings.safetySettings, | ||
...prepareToolsAndToolConfig(mode) | ||
...prepareToolsAndToolConfig(mode), | ||
cachedContent: this.settings.cachedContent | ||
}, | ||
@@ -245,3 +261,5 @@ warnings | ||
contents, | ||
safetySettings: this.settings.safetySettings | ||
systemInstruction, | ||
safetySettings: this.settings.safetySettings, | ||
cachedContent: this.settings.cachedContent | ||
}, | ||
@@ -252,8 +270,24 @@ warnings | ||
case "object-tool": { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "object-tool mode" | ||
}); | ||
return { | ||
args: { | ||
generationConfig, | ||
contents, | ||
tools: { | ||
functionDeclarations: [ | ||
{ | ||
name: mode.tool.name, | ||
description: (_a = mode.tool.description) != null ? _a : "", | ||
parameters: prepareJsonSchema(mode.tool.parameters) | ||
} | ||
] | ||
}, | ||
toolConfig: { functionCallingConfig: { mode: "ANY" } }, | ||
safetySettings: this.settings.safetySettings, | ||
cachedContent: this.settings.cachedContent | ||
}, | ||
warnings | ||
}; | ||
} | ||
case "object-grammar": { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: "object-grammar mode" | ||
@@ -260,0 +294,0 @@ }); |
@@ -14,2 +14,8 @@ import { LanguageModelV1 } from '@ai-sdk/provider'; | ||
/** | ||
Optional. | ||
The name of the cached content used as context to serve the prediction. | ||
Format: cachedContents/{cachedContent} | ||
*/ | ||
cachedContent?: string; | ||
/** | ||
Optional. A list of unique safety settings for blocking unsafe content. | ||
@@ -32,3 +38,3 @@ */ | ||
readonly specificationVersion = "v1"; | ||
readonly defaultObjectGenerationMode = "json"; | ||
readonly defaultObjectGenerationMode = "tool"; | ||
readonly modelId: GoogleGenerativeAIModelId; | ||
@@ -35,0 +41,0 @@ readonly settings: GoogleGenerativeAISettings; |
@@ -33,3 +33,3 @@ "use strict"; | ||
// src/google-generative-ai-language-model.ts | ||
var import_provider = require("@ai-sdk/provider"); | ||
var import_provider2 = require("@ai-sdk/provider"); | ||
var import_provider_utils3 = require("@ai-sdk/provider-utils"); | ||
@@ -39,2 +39,3 @@ var import_zod2 = require("zod"); | ||
// src/convert-to-google-generative-ai-messages.ts | ||
var import_provider = require("@ai-sdk/provider"); | ||
var import_provider_utils = require("@ai-sdk/provider-utils"); | ||
@@ -45,11 +46,18 @@ async function convertToGoogleGenerativeAIMessages({ | ||
}) { | ||
const messages = []; | ||
const systemInstructionParts = []; | ||
const contents = []; | ||
let systemMessagesAllowed = true; | ||
for (const { role, content } of prompt) { | ||
switch (role) { | ||
case "system": { | ||
messages.push({ role: "user", parts: [{ text: content }] }); | ||
messages.push({ role: "model", parts: [{ text: "" }] }); | ||
if (!systemMessagesAllowed) { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "system messages are only supported at the beginning of the conversation" | ||
}); | ||
} | ||
systemInstructionParts.push({ text: content }); | ||
break; | ||
} | ||
case "user": { | ||
systemMessagesAllowed = false; | ||
const parts = []; | ||
@@ -85,7 +93,8 @@ for (const part of content) { | ||
} | ||
messages.push({ role: "user", parts }); | ||
contents.push({ role: "user", parts }); | ||
break; | ||
} | ||
case "assistant": { | ||
messages.push({ | ||
systemMessagesAllowed = false; | ||
contents.push({ | ||
role: "model", | ||
@@ -113,3 +122,4 @@ parts: content.map((part) => { | ||
case "tool": { | ||
messages.push({ | ||
systemMessagesAllowed = false; | ||
contents.push({ | ||
role: "user", | ||
@@ -131,3 +141,6 @@ parts: content.map((part) => ({ | ||
} | ||
return messages; | ||
return { | ||
systemInstruction: systemInstructionParts.length > 0 ? { parts: systemInstructionParts } : void 0, | ||
contents | ||
}; | ||
} | ||
@@ -174,3 +187,3 @@ | ||
this.specificationVersion = "v1"; | ||
this.defaultObjectGenerationMode = "json"; | ||
this.defaultObjectGenerationMode = "tool"; | ||
this.modelId = modelId; | ||
@@ -193,2 +206,3 @@ this.settings = settings; | ||
}) { | ||
var _a; | ||
const type = mode.type; | ||
@@ -222,3 +236,3 @@ const warnings = []; | ||
}; | ||
const contents = await convertToGoogleGenerativeAIMessages({ prompt }); | ||
const { contents, systemInstruction } = await convertToGoogleGenerativeAIMessages({ prompt }); | ||
switch (type) { | ||
@@ -230,4 +244,6 @@ case "regular": { | ||
contents, | ||
systemInstruction, | ||
safetySettings: this.settings.safetySettings, | ||
...prepareToolsAndToolConfig(mode) | ||
...prepareToolsAndToolConfig(mode), | ||
cachedContent: this.settings.cachedContent | ||
}, | ||
@@ -245,3 +261,5 @@ warnings | ||
contents, | ||
safetySettings: this.settings.safetySettings | ||
systemInstruction, | ||
safetySettings: this.settings.safetySettings, | ||
cachedContent: this.settings.cachedContent | ||
}, | ||
@@ -252,8 +270,24 @@ warnings | ||
case "object-tool": { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "object-tool mode" | ||
}); | ||
return { | ||
args: { | ||
generationConfig, | ||
contents, | ||
tools: { | ||
functionDeclarations: [ | ||
{ | ||
name: mode.tool.name, | ||
description: (_a = mode.tool.description) != null ? _a : "", | ||
parameters: prepareJsonSchema(mode.tool.parameters) | ||
} | ||
] | ||
}, | ||
toolConfig: { functionCallingConfig: { mode: "ANY" } }, | ||
safetySettings: this.settings.safetySettings, | ||
cachedContent: this.settings.cachedContent | ||
}, | ||
warnings | ||
}; | ||
} | ||
case "object-grammar": { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: "object-grammar mode" | ||
@@ -260,0 +294,0 @@ }); |
{ | ||
"name": "@ai-sdk/google", | ||
"version": "0.0.26", | ||
"version": "0.0.27", | ||
"license": "Apache-2.0", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
@@ -28,3 +28,3 @@ # Vercel AI SDK - Google Generative AI Provider | ||
const { text } = await generateText({ | ||
model: google('models/gemini-1.5-flash-latest'), | ||
model: google('models/gemini-1.5-pro-latest'), | ||
prompt: 'Write a vegetarian lasagna recipe for 4 people.', | ||
@@ -31,0 +31,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
141991
1950