Skip to content
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
15 changes: 15 additions & 0 deletions js/plugins/anthropic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ The beta API surface provides access to experimental features, but some server-m

Note that `server_tool_use` and `web_search_tool_result` ARE supported and work with both stable and beta APIs.

### Web Search Tool

You can use Anthropic's web search tool by defining the tool in the prompt.

```typescript
const response = await ai.generate({
prompt: 'What is the weather in Tokyo?',
config: {
tools: [{ type: 'web_search_20250305', name: 'web_search' }],
},
});
```

By default, the tool will only be used if it matches the user's prompt. You can override this behavior by modifying the `tool_choice` config option.

### Within a flow

```typescript
Expand Down
1 change: 0 additions & 1 deletion js/plugins/anthropic/src/runner/converters/beta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export function betaServerToolUseBlockToPart(block: {
? `${block.server_name}/${baseName}`
: baseName;
return {
text: `[Anthropic server tool ${serverToolName}] input: ${JSON.stringify(block.input)}`,
metadata: {
anthropicServerToolUse: {
id: block.id,
Expand Down
1 change: 0 additions & 1 deletion js/plugins/anthropic/src/runner/converters/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export function webSearchToolResultBlockToPart(block: {
content: unknown;
}): Part {
return {
text: `[Anthropic server tool result ${block.tool_use_id}] ${JSON.stringify(block.content)}`,
metadata: {
anthropicServerToolResult: {
type: 'web_search_tool_result',
Expand Down
1 change: 0 additions & 1 deletion js/plugins/anthropic/src/runner/converters/stable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export function serverToolUseBlockToPart(block: {
input: unknown;
}): Part {
return {
text: `[Anthropic server tool ${block.name}] input: ${JSON.stringify(block.input)}`,
metadata: {
anthropicServerToolUse: {
id: block.id,
Expand Down
2 changes: 0 additions & 2 deletions js/plugins/anthropic/tests/beta_runner_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ describe('BetaRunner', () => {
} as any;
const toolPart = exposed.toGenkitPart(serverToolEvent);
assert.deepStrictEqual(toolPart, {
text: '[Anthropic server tool srv/myTool] input: {"foo":"bar"}',
metadata: {
anthropicServerToolUse: {
id: 'toolu_test',
Expand Down Expand Up @@ -765,7 +764,6 @@ describe('BetaRunner', () => {
server_name: 'srv',
});
assert.deepStrictEqual(serverToolPart, {
text: '[Anthropic server tool srv/serverTool] input: {"arg":"value"}',
metadata: {
anthropicServerToolUse: {
id: 'srv_tool_1',
Expand Down
5 changes: 5 additions & 0 deletions js/testapps/anthropic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ src/
- `pnpm run dev:stable:webp` – Start Dev UI for WEBP image handling demo.
- `pnpm run dev:stable:pdf` – Start Dev UI for PDF document processing demo.
- `pnpm run dev:stable:vision` – Start Dev UI for image/vision analysis demo.
- `pnpm run dev:stable:web-search-tool` – Start Dev UI for web search tool demo.

## Flows

Expand Down Expand Up @@ -72,4 +73,8 @@ Each source file defines flows that can be invoked from the Dev UI or the Genkit
- `stable-vision-base64` – Analyze an image from a local file (base64 encoded)
- `stable-vision-conversation` – Multi-turn conversation about an image

### Web Search Tool
- `stable-web-search-tool` – Use the web search tool to find the weather in Tokyo
- `stable-web-search-tool-stream` – Streaming response with web search tool

Example: `genkit flow:run anthropic-stable-hello`
1 change: 1 addition & 0 deletions js/testapps/anthropic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dev:stable:pdf": "genkit start -- npx tsx --watch src/stable/pdf.ts",
"dev:stable:tools": "genkit start -- npx tsx --watch src/stable/tools.ts",
"dev:stable:vision": "genkit start -- npx tsx --watch src/stable/vision.ts",
"dev:stable:web-search-tool": "genkit start -- npx tsx --watch src/stable/web-search-tool.ts",
"genkit:dev": "cross-env GENKIT_ENV=dev npm run dev:stable",
"genkit:start": "cross-env GENKIT_ENV=dev genkit start -- tsx --watch src/stable/basic.ts",
"dev": "export GENKIT_RUNTIME_ID=$(openssl rand -hex 8) && node lib/stable/basic.js 2>&1"
Expand Down
70 changes: 70 additions & 0 deletions js/testapps/anthropic/src/stable/web-search-tool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { anthropic } from '@genkit-ai/anthropic';
import { genkit } from 'genkit';

const ai = genkit({
plugins: [
// Configure the plugin with environment-driven API key
anthropic(),
],
});

ai.defineFlow('anthropic-stable-web-search-tool', async () => {
const { text } = await ai.generate({
model: anthropic.model('claude-sonnet-4-5'),
prompt: 'What is the weather in Tokyo?',
config: {
tools: [
{
type: 'web_search_20250305',
name: 'web_search',
},
],
},
});

return text;
});

ai.defineFlow(
'anthropic-stable-web-search-tool-stream',
async (_, { sendChunk }) => {
const { stream } = ai.generateStream({
model: anthropic.model('claude-sonnet-4-5'),
prompt: 'What is the weather in Tokyo?',
config: {
tools: [
{
type: 'web_search_20250305',
name: 'web_search',
},
],
},
});

let response = '';
for await (const chunk of stream) {
if (chunk.text) {
response += chunk.text;
sendChunk(chunk.text);
}
}

return response;
}
);