-
Notifications
You must be signed in to change notification settings - Fork 586
feat(js/plugins): add anthropic plugin #3785
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
Merged
Merged
Changes from all commits
Commits
Show all changes
52 commits
Select commit
Hold shift + click to select a range
6a574ec
feat(js/plugins/anthropic): add community plugin as starter
cabljac 3cf225f
refactor(js/plugins/anthropic): make compatible with monorepo
cabljac e83d82b
feat(js/plugins/anthropic): convert tests and add test setup
cabljac f66c101
chore(js/plugins/anthropic): remove retired claude-3-sonnet
HassanBahati 48bc949
chore(js/plugins/anthropic): remove retired claude-35-sonnet
HassanBahati 9403e05
chore(js/plugins/anthropic): format
HassanBahati 81edfdc
chore(js/plugins/anthropic): add supported anthropic models
HassanBahati 1a6ec39
refactor: code improvements
CorieW 1e4246f
fix(anthropic): remove todo comment as unnecessary and replace deprec…
CorieW 885d52e
refactor: allow hidden test client injection
cabljac 381c745
feat: add pdf support and complete some test todos
cabljac fbb2256
refactor: some small code improvements
cabljac b87e6b7
feat(js/plugins/anthropic): add .model() shim for convenient model re…
cabljac ff40bb8
refactor(anthropic): a lot of refactoring and add beta api
CorieW 0ecd9c8
refactor(anthropic): pass in message generics, keep two runners separate
cabljac 85e79c1
refactor(anthropic): clean up classes and types a bit
cabljac dd9c8b8
refactor(anthropic): tighten up API and tests
cabljac 759fcdf
refactor(anthropic): get tool types from genkit messages instead
cabljac 031dc25
refactor(anthropic): provide beta flag in options and at request conf…
cabljac e4e4860
refactor(anthropic): extract out types.ts for runner
cabljac 723783c
refactor(anthropic): hoist runners
cabljac 2fd1878
refactor(anthropic): rename classes and add more tests
cabljac 5135664
docs: refresh anthropic plugin branding and attribution
cabljac 7812c8a
refactor(anthropic): add basic testapp and remove nested ternary
cabljac 7e8aaf6
refactor(anthropic): remove redundant factory layer
cabljac 04a3c9e
refactor(anthropic): clean up ABC pattern and generic name
cabljac d6ed66b
refactor(anthropic): collect up runner param types
cabljac 94771a5
docs(anthropic): document shared helper
cabljac 5f0d0c2
refactor(anthropic): handle stable server tool responses
cabljac e2840a5
refactor(anthropic): add some code comments and extract web search ma…
cabljac 4086727
feat(anthropic): handle remote image URLs
cabljac e7d3731
fix(anthropic): some system prompt and image handling issues
cabljac aaf9314
docs(anthropic): have runner interfaces extend the base interface
cabljac c3a4658
refactor(anthropic): extract out list function
cabljac 88089d9
refactor(anthropic): clean up list code
cabljac aadc150
docs(anthropic): update README
cabljac 455899e
fix(anthropic): correct model ids/refs/names
cabljac a57c03a
chore(anthropic): remove old link in README
cabljac dfae670
refactor(anthropic): extract magic string
cabljac 509128c
fix(js/plugins/anthropic): pnpm install no frozen lockfile
cabljac 2325311
feat(js/plugins/anthropic): handle extended thinking requests in configs
cabljac 2990cdd
chore(js/plugins/anthropic): update license headers
cabljac 4158750
fix(js/plugins/anthropic): handle webp and text/plain
cabljac edd7134
test(js/testapps/anthropic): add extra flows for manual tests
cabljac df96b08
docs(js/plugins/anthropic): update code comments and test app
cabljac 66771ff
fix(js/plugins/anthropic): use genkit logger and update docs
cabljac 74f8539
refactor(js/plugins/anthropic): refactor model references approach to…
cabljac 00cd665
refactor(js/testapps/anthropic): fix commonRef and listActions
cabljac 4c86cda
Update js/plugins/anthropic/package.json
cabljac 0d3f695
refactor(js/plugins/anthropic): remove dedicated workflow
cabljac 8b90519
Merge remote-tracking branch 'upstream/main' into anthropic-plugin
cabljac 00182da
chore(js): regen lockfile to fix builds
cabljac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| lib/ | ||
| node_modules/ | ||
| coverage/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # typescript source files | ||
| src/ | ||
| tests/ | ||
| tsconfig.json | ||
| tsup.common.ts | ||
| tsup.config.ts | ||
|
|
||
| # GitHub files | ||
| .github/ | ||
| .gitignore | ||
| .npmignore | ||
| CODE_OF_CONDUCT.md | ||
| CONTRIBUTING.md | ||
|
|
||
| # Developer related files | ||
| .devcontainer/ | ||
| .vscode/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| This project includes code derived from the Firebase Genkit Anthropic community plugin | ||
| (https://github.com/BloomLabsInc/genkit-plugins/tree/main/plugins/anthropic). | ||
|
|
||
| Copyright 2024 Bloom Labs Inc. | ||
| Copyright 2025 Google LLC. | ||
|
|
||
| Licensed under the Apache License, Version 2.0. | ||
| See the LICENSE file distributed with this project for the full license text. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| # Firebase Genkit + Anthropic AI | ||
|
|
||
| <h1 align="center">Firebase Genkit <> Anthropic AI Plugin</h1> | ||
|
|
||
| <h4 align="center">Anthropic AI plugin for Google Firebase Genkit</h4> | ||
|
|
||
| `@genkit-ai/anthropic` is the official Anthropic plugin for [Firebase Genkit](https://github.com/firebase/genkit). It supersedes the earlier community package `genkitx-anthropic` and is now maintained by Google. | ||
|
|
||
| ## Supported models | ||
|
|
||
| The plugin supports the most recent Anthropic models: **Claude Sonnet 4.5**, **Claude Opus 4.1**, **Claude Haiku 4.5**, **Claude Sonnet 4**, **Claude Opus 4**, **Claude 3.5 Haiku**, and **Claude 3 Haiku**. | ||
|
|
||
| ## Installation | ||
|
|
||
| Install the plugin in your project with your favorite package manager: | ||
|
|
||
| - `npm install @genkit-ai/anthropic` | ||
| - `yarn add @genkit-ai/anthropic` | ||
| - `pnpm add @genkit-ai/anthropic` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Initialize | ||
|
|
||
| ```typescript | ||
| import { genkit } from 'genkit'; | ||
| import { anthropic } from '@genkit-ai/anthropic'; | ||
|
|
||
| const ai = genkit({ | ||
| plugins: [anthropic({ apiKey: process.env.ANTHROPIC_API_KEY })], | ||
| // specify a default model for generate here if you wish: | ||
| model: anthropic.model('claude-sonnet-4-5'), | ||
| }); | ||
| ``` | ||
|
|
||
| ### Basic examples | ||
|
|
||
| The simplest way to generate text is by using the `generate` method: | ||
|
|
||
| ```typescript | ||
| const response = await ai.generate({ | ||
| model: anthropic.model('claude-3-haiku'), | ||
| prompt: 'Tell me a joke.', | ||
| }); | ||
|
|
||
| console.log(response.text); | ||
| ``` | ||
|
|
||
| ### Multi-modal prompt | ||
|
|
||
| ```typescript | ||
| // ...initialize Genkit instance (as shown above)... | ||
|
|
||
| const response = await ai.generate({ | ||
| prompt: [ | ||
| { text: 'What animal is in the photo?' }, | ||
| { media: { url: imageUrl } }, | ||
| ], | ||
| config: { | ||
| // control of the level of visual detail when processing image embeddings | ||
| // Low detail level also decreases the token usage | ||
| visualDetailLevel: 'low', | ||
| }, | ||
| }); | ||
| console.log(response.text); | ||
| ``` | ||
|
|
||
| ### Extended thinking | ||
|
|
||
| Claude 4 models can expose their internal reasoning. Enable it per-request with the Anthropic thinking config and read the reasoning from the response: | ||
|
|
||
| ```typescript | ||
| const response = await ai.generate({ | ||
| prompt: 'Walk me through your reasoning for Fermat’s little theorem.', | ||
| config: { | ||
| thinking: { | ||
| enabled: true, | ||
| budgetTokens: 4096, // Must be >= 1024 and less than max_tokens | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| console.log(response.text); // Final assistant answer | ||
| console.log(response.reasoning); // Summarized thinking steps | ||
| ``` | ||
|
|
||
| When thinking is enabled, request bodies sent through the plugin include the `thinking` payload (`{ type: 'enabled', budget_tokens: … }`) that Anthropic's API expects, and streamed responses deliver `reasoning` parts as they arrive so you can render the chain-of-thought incrementally. | ||
|
|
||
| ### Beta API Limitations | ||
|
|
||
| The beta API surface provides access to experimental features, but some server-managed tool blocks are not yet supported by this plugin. The following beta API features will cause an error if encountered: | ||
|
|
||
| - `web_fetch_tool_result` | ||
| - `code_execution_tool_result` | ||
| - `bash_code_execution_tool_result` | ||
| - `text_editor_code_execution_tool_result` | ||
| - `mcp_tool_result` | ||
| - `mcp_tool_use` | ||
| - `container_upload` | ||
|
|
||
| Note that `server_tool_use` and `web_search_tool_result` ARE supported and work with both stable and beta APIs. | ||
|
|
||
| ### Within a flow | ||
|
|
||
| ```typescript | ||
| import { z } from 'genkit'; | ||
|
|
||
| // ...initialize Genkit instance (as shown above)... | ||
|
|
||
| export const jokeFlow = ai.defineFlow( | ||
| { | ||
| name: 'jokeFlow', | ||
| inputSchema: z.string(), | ||
| outputSchema: z.string(), | ||
| }, | ||
| async (subject) => { | ||
| const llmResponse = await ai.generate({ | ||
| prompt: `tell me a joke about ${subject}`, | ||
| }); | ||
| return llmResponse.text; | ||
| } | ||
| ); | ||
| ``` | ||
|
|
||
| ### Direct model usage (without Genkit instance) | ||
|
|
||
| The plugin supports Genkit Plugin API v2, which allows you to use models directly without initializing the full Genkit framework: | ||
|
|
||
| ```typescript | ||
| import { anthropic } from '@genkit-ai/anthropic'; | ||
|
|
||
| // Create a model reference directly | ||
| const claude = anthropic.model('claude-sonnet-4-5'); | ||
|
|
||
| // Use the model directly | ||
| const response = await claude({ | ||
| messages: [ | ||
| { | ||
| role: 'user', | ||
| content: [{ text: 'Tell me a joke.' }], | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| console.log(response); | ||
| ``` | ||
|
|
||
| You can also create model references using the plugin's `model()` method: | ||
|
|
||
| ```typescript | ||
| import { anthropic } from '@genkit-ai/anthropic'; | ||
|
|
||
| // Create model references | ||
| const claudeSonnet45 = anthropic.model('claude-sonnet-4-5'); | ||
| const claudeOpus41 = anthropic.model('claude-opus-4-1'); | ||
| const claude35Haiku = anthropic.model('claude-3-5-haiku'); | ||
|
|
||
| // Use the model reference directly | ||
| const response = await claudeSonnet45({ | ||
| messages: [ | ||
| { | ||
| role: 'user', | ||
| content: [{ text: 'Hello!' }], | ||
| }, | ||
| ], | ||
| }); | ||
| ``` | ||
|
|
||
| This approach is useful for: | ||
|
|
||
| - Framework developers who need raw model access | ||
| - Testing models in isolation | ||
| - Using Genkit models in non-Genkit applications | ||
|
|
||
| ## Acknowledgements | ||
|
|
||
| This plugin builds on the community work published as [`genkitx-anthropic`](https://github.com/BloomLabsInc/genkit-plugins/blob/main/plugins/anthropic/README.md) by Bloom Labs Inc. Their Apache 2.0–licensed implementation provided the foundation for this maintained package. | ||
|
|
||
| ## Contributing | ||
|
|
||
| Want to contribute to the project? That's awesome! Head over to our [Contribution Guidelines](CONTRIBUTING.md). | ||
|
|
||
| ## Need support? | ||
|
|
||
| > [!NOTE] | ||
| > This repository depends on Google's Firebase Genkit. For issues and questions related to Genkit, please refer to instructions available in [Genkit's repository](https://github.com/firebase/genkit). | ||
|
|
||
|
|
||
| ## Credits | ||
|
|
||
| This plugin is maintained by Google with acknowledgement to the community contributions from [Bloom Labs Inc](https://github.com/BloomLabsInc). | ||
|
|
||
| ## License | ||
|
|
||
| This project is licensed under the [Apache 2.0 License](https://github.com/BloomLabsInc/genkit-plugins/blob/main/LICENSE). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| { | ||
| "name": "@genkit-ai/anthropic", | ||
| "description": "Genkit AI framework plugin for Anthropic APIs.", | ||
| "keywords": [ | ||
| "genkit", | ||
| "genkit-plugin", | ||
| "genkit-model", | ||
| "anthropic", | ||
| "anthropic-ai", | ||
| "claude-4", | ||
| "haiku-4", | ||
| "opus", | ||
| "haiku", | ||
| "sonnet", | ||
| "ai", | ||
| "genai", | ||
| "generative-ai" | ||
| ], | ||
| "version": "1.23.0", | ||
| "type": "commonjs", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/firebase/genkit.git", | ||
| "directory": "js/plugins/anthropic" | ||
| }, | ||
| "author": "genkit", | ||
| "license": "Apache-2.0", | ||
| "peerDependencies": { | ||
| "genkit": "workspace:^" | ||
| }, | ||
| "dependencies": { | ||
| "@anthropic-ai/sdk": "^0.68.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "^20.11.16", | ||
| "check-node-version": "^4.2.1", | ||
| "genkit": "workspace:*", | ||
| "npm-run-all": "^4.1.5", | ||
| "rimraf": "^6.0.1", | ||
| "tsup": "^8.3.5", | ||
| "tsx": "^4.19.2", | ||
| "typescript": "^4.9.0" | ||
| }, | ||
| "types": "./lib/index.d.ts", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./lib/index.d.ts", | ||
| "require": "./lib/index.js", | ||
| "import": "./lib/index.mjs", | ||
| "default": "./lib/index.js" | ||
| } | ||
| }, | ||
| "files": [ | ||
| "lib" | ||
| ], | ||
| "publishConfig": { | ||
| "provenance": true, | ||
| "access": "public" | ||
| }, | ||
| "scripts": { | ||
| "check": "tsc", | ||
| "compile": "tsup-node", | ||
| "build:clean": "rimraf ./lib", | ||
| "build": "npm-run-all build:clean check compile", | ||
| "build:watch": "tsup-node --watch", | ||
| "test": "tsx --test tests/*_test.ts", | ||
| "test:file": "tsx --test", | ||
| "test:coverage": "check-node-version --node '>=22' && tsx --test --experimental-test-coverage --test-coverage-include='src/**/*.ts' ./tests/**/*_test.ts" | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.