UNPKG

genkitx-anthropic

Version:

Firebase Genkit AI framework plugin for Anthropic APIs.

1 lines 4.07 kB
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Copyright 2024 The Fire Company\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 type { Genkit } from 'genkit';\nimport { genkitPlugin } from 'genkit/plugin';\nimport Anthropic from '@anthropic-ai/sdk';\n\nimport {\n claude37Sonnet,\n claude35Sonnet,\n claude3Opus,\n claude3Sonnet,\n claude3Haiku,\n claude35Haiku,\n claudeModel,\n SUPPORTED_CLAUDE_MODELS,\n} from './claude.js';\n\nexport {\n claude37Sonnet,\n claude35Sonnet,\n claude3Opus,\n claude3Sonnet,\n claude3Haiku,\n claude35Haiku,\n};\n\nexport interface PluginOptions {\n apiKey?: string;\n cacheSystemPrompt?: boolean;\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 genkitPlugin('anthropic', async (ai: Genkit) => {\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 for (const name of Object.keys(SUPPORTED_CLAUDE_MODELS)) {\n claudeModel(ai, name, client, options?.cacheSystemPrompt);\n }\n });\n\nexport default anthropic;\n"],"mappings":";;;AAiBA,SAAS,oBAAoB;AAC7B,OAAO,eAAe;AAEtB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AA8CA,MAAM,YAAY,CAAC,YACxB,aAAa,aAAa,CAAO,OAAe;AAC9C,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,aAAW,QAAQ,OAAO,KAAK,uBAAuB,GAAG;AACvD,gBAAY,IAAI,MAAM,QAAQ,mCAAS,iBAAiB;AAAA,EAC1D;AACF,EAAC;AAEH,IAAO,cAAQ;","names":[]}