UNPKG

genkitx-anthropic

Version:

Firebase Genkit AI framework plugin for Anthropic APIs.

1 lines 6.41 kB
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Copyright 2024 Bloom Labs Inc\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { genkitPluginV2, model, modelActionMetadata } from 'genkit/plugin';\nimport Anthropic from '@anthropic-ai/sdk';\n\nimport {\n claude4Sonnet,\n claude4Opus,\n claude37Sonnet,\n claude35Sonnet,\n claude3Opus,\n claude3Sonnet,\n claude3Haiku,\n claude35Haiku,\n claude45Sonnet,\n claude45Haiku,\n claude45Opus,\n claudeModel,\n SUPPORTED_CLAUDE_MODELS,\n} from './claude.js';\nimport { ModelAction } from 'genkit/model';\nimport { ActionType } from 'genkit/registry';\nimport { ActionMetadata } from 'genkit';\n\nexport {\n claude4Sonnet,\n claude4Opus,\n claude37Sonnet,\n claude35Sonnet,\n claude3Opus,\n claude3Sonnet,\n claude3Haiku,\n claude35Haiku,\n claude45Sonnet,\n claude45Haiku,\n claude45Opus,\n};\n\nexport interface PluginOptions {\n apiKey?: string;\n cacheSystemPrompt?: boolean;\n}\n\nasync function list(client: Anthropic): Promise<ActionMetadata[]> {\n const clientModels = (await client.models.list()).data;\n\n return clientModels\n .map((modelInfo) => {\n // Remove the date suffix from the model id\n const normalizedId = modelInfo.id.replace(/-\\d{8}$/, '');\n // Get the model reference from the supported models\n const ref = SUPPORTED_CLAUDE_MODELS[normalizedId];\n // Return the model action metadata if the model is supported\n return ref ? modelActionMetadata({\n name: ref.name,\n info: ref.info,\n configSchema: ref.configSchema,\n }) : undefined;\n })\n // Filter out undefined values\n .filter((metadata) => metadata !== undefined);\n}\n\n/**\n * This module provides an interface to the Anthropic AI models through the Genkit plugin system.\n * It allows users to interact with various Claude models by providing an API key and optional configuration.\n *\n * The main export is the `anthropic` plugin, which can be configured with an API key either directly or through\n * environment variables. It initializes the Anthropic client and makes available the Claude models for use.\n *\n * Exports:\n * - claude35Sonnet: Reference to the Claude 3.5 Sonnet model.\n * - claude3Haiku: Reference to the Claude 3 Haiku model.\n * - claude3Sonnet: Reference to the Claude 3 Sonnet model.\n * - claude3Opus: Reference to the Claude 3 Opus model.\n * - anthropic: The main plugin function to interact with the Anthropic AI.\n *\n * Usage:\n * To use the Claude models, initialize the anthropic plugin inside `configureGenkit` and pass the configuration options. If no API key is provided in the options, the environment variable `ANTHROPIC_API_KEY` must be set. If you want to cache the system prompt, set `cacheSystemPrompt` to `true`. **Note:** Prompt caching is in beta and may change. To learn more, see https://docs.anthropic.com/en/docs/prompt-caching.\n *\n * Example:\n * ```\n * import anthropic from 'genkitx-anthropic';\n *\n * export default configureGenkit({\n * plugins: [\n * anthropic({ apiKey: 'your-api-key', cacheSystemPrompt: false })\n * ... // other plugins\n * ]\n * });\n * ```\n */\n// TODO: add support for voyage embeddings and tool use (both not documented well in docs.anthropic.com)\nexport const anthropic = (options?: PluginOptions) => {\n let apiKey = options?.apiKey || process.env.ANTHROPIC_API_KEY;\n if (!apiKey) {\n throw new Error(\n 'Please pass in the API key or set the ANTHROPIC_API_KEY environment variable'\n );\n }\n let defaultHeaders = {};\n if (options?.cacheSystemPrompt == true) {\n defaultHeaders['anthropic-beta'] = 'prompt-caching-2024-07-31';\n }\n const client = new Anthropic({ apiKey, defaultHeaders });\n\n let listActionsCache: ActionMetadata[] | null = null;\n\n return genkitPluginV2({\n name: 'anthropic',\n init: async () => {\n const actions: ModelAction[] = [];\n for (const name of Object.keys(SUPPORTED_CLAUDE_MODELS)) {\n const action = claudeModel(name, client, options?.cacheSystemPrompt);\n actions.push(action);\n }\n return actions;\n },\n resolve: (actionType: ActionType, name: string) => {\n if (actionType === 'model') {\n return claudeModel(\n name,\n client\n );\n }\n return undefined;\n },\n list: async () => {\n if (listActionsCache) return listActionsCache;\n listActionsCache = await list(client);\n return listActionsCache;\n }\n });\n};\n\nexport default anthropic;\n"],"mappings":";;;AAgBA,SAAS,gBAAuB,2BAA2B;AAC3D,OAAO,eAAe;AAEtB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAwBP,SAAe,KAAK,QAA8C;AAAA;AAChE,UAAM,gBAAgB,MAAM,OAAO,OAAO,KAAK,GAAG;AAElD,WAAO,aACJ,IAAI,CAAC,cAAc;AAElB,YAAM,eAAe,UAAU,GAAG,QAAQ,WAAW,EAAE;AAEvD,YAAM,MAAM,wBAAwB,YAAY;AAEhD,aAAO,MAAM,oBAAoB;AAAA,QAC/B,MAAM,IAAI;AAAA,QACV,MAAM,IAAI;AAAA,QACV,cAAc,IAAI;AAAA,MACpB,CAAC,IAAI;AAAA,IACP,CAAC,EAEA,OAAO,CAAC,aAAa,aAAa,MAAS;AAAA,EAChD;AAAA;AAgCO,MAAM,YAAY,CAAC,YAA4B;AACpD,MAAI,UAAS,mCAAS,WAAU,QAAQ,IAAI;AAC5C,MAAI,CAAC,QAAQ;AACX,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,MAAI,iBAAiB,CAAC;AACtB,OAAI,mCAAS,sBAAqB,MAAM;AACtC,mBAAe,gBAAgB,IAAI;AAAA,EACrC;AACA,QAAM,SAAS,IAAI,UAAU,EAAE,QAAQ,eAAe,CAAC;AAEvD,MAAI,mBAA4C;AAEhD,SAAO,eAAe;AAAA,IACpB,MAAM;AAAA,IACN,MAAM,MAAY;AAChB,YAAM,UAAyB,CAAC;AAChC,iBAAW,QAAQ,OAAO,KAAK,uBAAuB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