Skip to content
Closed
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/gorgeous-months-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/google': patch
---

feat (provider/google): Added URL context Tool
43 changes: 43 additions & 0 deletions content/providers/01-ai-sdk-providers/15-google-generative-ai.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,49 @@ The `dynamicRetrievalConfig` describes the options to customize dynamic retrieva
supported with 8B variants.
</Note>

### URL Context

The URL context tool allows the you to provide specific URLs that you want the model to analyze directly in your prompt.

```ts highlight="6"
import { google } from '@ai-sdk/google';
import { generateText } from 'ai';

const { text } = await generateText({
model: google('gemini-2.0-flash-001', {
useUrlContext: true,
}),
prompt: `Based on the document: https://ai.google.dev/gemini-api/docs/url-context.
Answer this question: How many links we can consume in one request?`,
});
```

<Note>You can add up to 20 URLs per request.</Note>

<Note>
The URL context tool is only supported for Gemini 2.0 Flash models and above.
Check the [supported models for URL context
tool](https://ai.google.dev/gemini-api/docs/url-context#supported-models).
</Note>

#### Combine URL Context with Search Grounding

You can combine the URL context tool with search grounding to provide the model with the latest information from the web.

```ts
import { google } from '@ai-sdk/google';
import { generateText } from 'ai';

const { text, sources } = await generateText({
model: google('gemini-2.0-flash-001', {
useUrlContext: true,
useSearchGrounding: true,
}),
prompt: `Based on this context: https://ai-sdk.dev/providers/ai-sdk-providers/google-generative-ai, tell me how to use Gemini with AI SDK.
Also, provide the latest news about AI SDK V5.`,
});
```

### Sources

When you use [Search Grounding](#search-grounding), the model will include sources in the response.
Expand Down
33 changes: 33 additions & 0 deletions examples/ai-core/src/generate-text/google-url-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { google } from '@ai-sdk/google';
import { generateText } from 'ai';
import 'dotenv/config';

async function main() {
const result = await generateText({
model: google('gemini-2.0-flash-001', {
useUrlContext: true,
}),
messages: [
{
role: 'user',
content: [
{
type: 'text',
text: `Based on the document: https://ai.google.dev/gemini-api/docs/url-context#limitations.
Answer this question: How many links we can consume in one request?`,
},
],
},
],
});

console.log(result.text);
console.log();
console.log('SOURCES');
console.log(result.sources);
console.log();
console.log('PROVIDER METADATA');
console.log(result.providerMetadata?.google);
}

main().catch(console.error);
31 changes: 31 additions & 0 deletions examples/ai-core/src/stream-text/google-url-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { google } from '@ai-sdk/google';
import { streamText } from 'ai';
import 'dotenv/config';

async function main() {
const result = streamText({
model: google('gemini-2.0-flash-001', { useUrlContext: true }),
prompt: `Based on the document: https://ai.google.dev/gemini-api/docs/url-context#limitations.
Answer this question: How many links we can consume in one request?`,
});

for await (const part of result.fullStream) {
if (part.type === 'text-delta') {
process.stdout.write(part.textDelta);
}

if (part.type === 'source' && part.source.sourceType === 'url') {
console.log('\x1b[36m%s\x1b[0m', 'Source');
console.log('ID:', part.source.id);
console.log('Title:', part.source.title);
console.log('URL:', part.source.url);
console.log();
}
}

console.log();
console.log('Token usage:', await result.usage);
console.log('Finish reason:', await result.finishReason);
}

main().catch(console.error);
19 changes: 19 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 @@ -1181,6 +1181,25 @@ describe('doGenerate', () => {
},
});
});

it('should use useUrlContext for gemini-2.0-pro', async () => {
prepareJsonResponse({
url: TEST_URL_GEMINI_2_0_PRO,
});

const gemini2Pro = provider.languageModel('gemini-2.0-pro', {
useUrlContext: true,
});
await gemini2Pro.doGenerate({
inputFormat: 'prompt',
mode: { type: 'regular' },
prompt: TEST_PROMPT,
});

expect(await server.calls[0].requestBody).toMatchObject({
tools: { urlContext: {} },
});
});
});

it('should extract image file outputs', async () => {
Expand Down
1 change: 1 addition & 0 deletions packages/google/src/google-generative-ai-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export class GoogleGenerativeAILanguageModel implements LanguageModelV1 {
mode,
this.settings.useSearchGrounding ?? false,
this.settings.dynamicRetrievalConfig,
this.settings.useUrlContext ?? false,
this.modelId,
);

Expand Down
7 changes: 7 additions & 0 deletions packages/google/src/google-generative-ai-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type GoogleGenerativeAIModelId =
| 'gemini-2.5-pro-exp-03-25'
| 'gemini-2.5-pro-preview-05-06'
| 'gemini-2.5-flash-preview-04-17'
| 'gemini-2.5-flash-preview-05-20'
| 'gemini-exp-1206'
| 'gemma-3-27b-it'
| 'learnlm-1.5-pro-experimental'
Expand Down Expand Up @@ -103,6 +104,12 @@ Optional. Specifies the dynamic retrieval configuration.
@see https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/ground-with-google-search#dynamic-retrieval
*/
dynamicRetrievalConfig?: DynamicRetrievalConfig;

/**
* Optional. Specifies the URL context tool configuration.
* @see https://ai.google.dev/gemini-api/docs/url-context
*/
useUrlContext?: boolean;
}

export interface InternalGoogleGenerativeAISettings
Expand Down
12 changes: 11 additions & 1 deletion packages/google/src/google-prepare-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function prepareTools(
},
useSearchGrounding: boolean,
dynamicRetrievalConfig: DynamicRetrievalConfig | undefined,
useUrlContext: boolean,
modelId: GoogleGenerativeAIModelId,
): {
tools:
Expand All @@ -31,7 +32,8 @@ export function prepareTools(
| Record<string, never>
| { dynamicRetrievalConfig: DynamicRetrievalConfig };
}
| { googleSearch: Record<string, never> };
| { googleSearch: Record<string, never> }
| { urlContext: Record<string, never> };
toolConfig:
| undefined
| {
Expand Down Expand Up @@ -64,6 +66,14 @@ export function prepareTools(
};
}

if (useUrlContext) {
return {
tools: isGemini2 ? { urlContext: {} } : undefined,
toolConfig: undefined,
toolWarnings,
};
}

if (tools == null) {
return { tools: undefined, toolConfig: undefined, toolWarnings };
}
Expand Down
Loading