UNPKG

@genkit-ai/anthropic

Version:

Genkit AI framework plugin for Anthropic APIs.

73 lines 1.93 kB
import Anthropic from "@anthropic-ai/sdk"; import { genkitPluginV2 } from "genkit/plugin"; import { listActions } from "./list.mjs"; import { claudeModel, claudeModelReference, listKnownModels } from "./models.mjs"; import { __testClient } from "./types.mjs"; import { cacheControl } from "./utils.mjs"; function getAnthropicClient(options) { const internalOptions = options; if (internalOptions?.[__testClient]) { return internalOptions[__testClient]; } const apiKey = options?.apiKey || process.env.ANTHROPIC_API_KEY; if (!apiKey) { throw new Error( "Please pass in the API key or set the ANTHROPIC_API_KEY environment variable" ); } return new Anthropic({ apiKey }); } function anthropicPlugin(options) { const client = getAnthropicClient(options); const defaultApiVersion = options?.apiVersion; let listActionsCache = null; return genkitPluginV2({ name: "anthropic", init: async () => { const actions = []; actions.push(...listKnownModels(client, defaultApiVersion)); return actions; }, resolve: (actionType, name) => { if (actionType === "model") { const modelName = name.startsWith("anthropic/") ? name.slice(10) : name; return claudeModel({ name: modelName, client, defaultApiVersion }); } return void 0; }, list: async () => { if (listActionsCache) return listActionsCache; listActionsCache = await listActions(client); return listActionsCache; } }); } const anthropic = anthropicPlugin; anthropic.model = (name, config) => { return claudeModelReference(name, config); }; function anthropicDocument(options) { return { custom: { anthropicDocument: options } }; } var index_default = anthropic; export { anthropic, anthropicDocument, cacheControl, index_default as default }; //# sourceMappingURL=index.mjs.map