Skip to content
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

feat (provider/google): add promptFeedback outputs #4265

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/poor-clouds-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/google': patch
---

feat (provider/google): add promptFeedback outputs
6 changes: 6 additions & 0 deletions examples/ai-core/src/generate-text/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ async function main() {
prompt: 'Invent a new holiday and describe its traditions.',
});

const googleMetadata = result.experimental_providerMetadata?.google;

console.log(result.text);
console.log();
console.log('Token usage:', result.usage);
console.log('Safety info:', {
promptFeedback: googleMetadata?.promptFeedback,
safetyRatings: googleMetadata?.safetyRatings,
});
console.log('Finish reason:', result.finishReason);
}

Expand Down
6 changes: 6 additions & 0 deletions examples/ai-core/src/stream-text/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ async function main() {
process.stdout.write(textPart);
}

const googleMetadata = (await result.experimental_providerMetadata)?.google;

console.log();
console.log('Token usage:', await result.usage);
console.log('Safety info:', {
promptFeedback: googleMetadata?.promptFeedback,
safetyRatings: googleMetadata?.safetyRatings,
});
console.log('Finish reason:', await result.finishReason);
}

Expand Down
75 changes: 75 additions & 0 deletions packages/google/src/google-generative-ai-language-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,42 @@ describe('doGenerate', () => {
),
);

it(
'should expose PromptFeedback in provider metadata',
withTestServer(
{
url: 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent',
type: 'json-value',
content: {
candidates: [
{
content: { parts: [{ text: 'No' }], role: 'model' },
finishReason: 'SAFETY',
index: 0,
safetyRatings: SAFETY_RATINGS,
},
],
promptFeedback: {
blockReason: 'SAFETY',
safetyRatings: SAFETY_RATINGS,
},
},
},
async () => {
const { providerMetadata } = await model.doGenerate({
inputFormat: 'prompt',
mode: { type: 'regular' },
prompt: TEST_PROMPT,
});

expect(providerMetadata?.google.promptFeedback).toStrictEqual({
blockReason: 'SAFETY',
safetyRatings: SAFETY_RATINGS,
});
},
),
);

it(
'should expose grounding metadata in provider metadata',
withTestServer(
Expand Down Expand Up @@ -1086,6 +1122,7 @@ describe('doStream', () => {
providerMetadata: {
google: {
groundingMetadata: null,
promptFeedback: null,
safetyRatings: [
{
category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
Expand Down Expand Up @@ -1260,6 +1297,7 @@ describe('doStream', () => {
providerMetadata: {
google: {
groundingMetadata: null,
promptFeedback: null,
safetyRatings: [
{
category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
Expand Down Expand Up @@ -1326,6 +1364,43 @@ describe('doStream', () => {
},
),
);

it(
'should expose PromptFeedback in provider metadata on finish',
withTestServer(
{
url: 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:streamGenerateContent',
type: 'stream-values',
content: [
`data: {"candidates": [{"content": {"parts": [{"text": "No"}],"role": "model"},` +
`"finishReason": "PROHIBITED_CONTENT","index": 0}],` +
`"promptFeedback": {"blockReason": "PROHIBITED_CONTENT","safetyRatings": [` +
`{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT","probability": "NEGLIGIBLE"},` +
`{"category": "HARM_CATEGORY_HATE_SPEECH","probability": "NEGLIGIBLE"},` +
`{"category": "HARM_CATEGORY_HARASSMENT","probability": "NEGLIGIBLE"},` +
`{"category": "HARM_CATEGORY_DANGEROUS_CONTENT","probability": "NEGLIGIBLE"}]}}\n\n`,
],
},
async () => {
const { stream } = await model.doStream({
inputFormat: 'prompt',
mode: { type: 'regular' },
prompt: TEST_PROMPT,
});

const events = await convertReadableStreamToArray(stream);
const finishEvent = events.find(event => event.type === 'finish');

expect(
finishEvent?.type === 'finish' &&
finishEvent.providerMetadata?.google.promptFeedback,
).toStrictEqual({
blockReason: 'PROHIBITED_CONTENT',
safetyRatings: SAFETY_RATINGS,
});
},
),
);
describe('search tool selection', () => {
const provider = createGoogleGenerativeAI({
apiKey: 'test-api-key',
Expand Down
14 changes: 14 additions & 0 deletions packages/google/src/google-generative-ai-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ export class GoogleGenerativeAILanguageModel implements LanguageModelV1 {
warnings,
providerMetadata: {
google: {
promptFeedback: response.promptFeedback ?? null,
groundingMetadata: candidate.groundingMetadata ?? null,
safetyRatings: candidate.safetyRatings ?? null,
},
Expand Down Expand Up @@ -330,6 +331,7 @@ export class GoogleGenerativeAILanguageModel implements LanguageModelV1 {

providerMetadata = {
google: {
promptFeedback: value.promptFeedback ?? null,
groundingMetadata: candidate.groundingMetadata ?? null,
safetyRatings: candidate.safetyRatings ?? null,
},
Expand Down Expand Up @@ -521,6 +523,12 @@ const responseSchema = z.object({
groundingMetadata: groundingMetadataSchema.nullish(),
}),
),
promptFeedback: z
.object({
blockReason: z.string().nullish(),
safetyRatings: z.array(safetyRatingSchema).nullish(),
})
.nullish(),
usageMetadata: z
.object({
promptTokenCount: z.number().nullish(),
Expand All @@ -543,6 +551,12 @@ const chunkSchema = z.object({
}),
)
.nullish(),
promptFeedback: z
.object({
blockReason: z.string().nullish(),
safetyRatings: z.array(safetyRatingSchema).nullish(),
})
.nullish(),
usageMetadata: z
.object({
promptTokenCount: z.number().nullish(),
Expand Down
Loading