{"version":3,"sources":["../../src/deepseek/index.ts"],"sourcesContent":["/**\n * Copyright 2024 Google LLC\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 {\n  ActionMetadata,\n  Genkit,\n  GenkitError,\n  modelActionMetadata,\n  ModelReference,\n  z,\n} from 'genkit';\nimport { logger } from 'genkit/logging';\nimport { GenkitPlugin } from 'genkit/plugin';\nimport { ActionType } from 'genkit/registry';\nimport OpenAI from 'openai';\nimport { openAICompatible, PluginOptions } from '../index.js';\nimport { defineCompatOpenAIModel } from '../model.js';\nimport {\n  DeepSeekChatCompletionConfigSchema,\n  deepSeekModelRef,\n  deepSeekRequestBuilder,\n  SUPPORTED_DEEPSEEK_MODELS,\n} from './deepseek.js';\n\nexport type DeepSeekPluginOptions = Omit<PluginOptions, 'name' | 'baseURL'>;\n\nconst resolver = async (\n  ai: Genkit,\n  client: OpenAI,\n  actionType: ActionType,\n  actionName: string\n) => {\n  if (actionType === 'model') {\n    const modelRef = deepSeekModelRef({\n      name: `deepseek/${actionName}`,\n    });\n    defineCompatOpenAIModel({\n      ai,\n      name: modelRef.name,\n      client,\n      modelRef,\n      requestBuilder: deepSeekRequestBuilder,\n    });\n  } else {\n    logger.warn('Only model actions are supported by the DeepSeek plugin');\n  }\n};\n\nconst listActions = async (client: OpenAI): Promise<ActionMetadata[]> => {\n  return await client.models.list().then((response) =>\n    response.data\n      .filter((model) => model.object === 'model')\n      .map((model: OpenAI.Model) => {\n        const modelRef =\n          SUPPORTED_DEEPSEEK_MODELS[model.id] ??\n          deepSeekModelRef({\n            name: `deepseek/${model.id}`,\n          });\n        return modelActionMetadata({\n          name: modelRef.name,\n          info: modelRef.info,\n          configSchema: modelRef.configSchema,\n        });\n      })\n  );\n};\n\nexport function deepSeekPlugin(options?: DeepSeekPluginOptions): GenkitPlugin {\n  const apiKey = options?.apiKey ?? process.env.DEEPSEEK_API_KEY;\n  if (!apiKey) {\n    throw new GenkitError({\n      status: 'FAILED_PRECONDITION',\n      message:\n        'Please pass in the API key or set the DEEPSEEK_API_KEY environment variable.',\n    });\n  }\n  return openAICompatible({\n    name: 'deepseek',\n    baseURL: 'https://api.deepseek.com',\n    apiKey,\n    ...options,\n    initializer: async (ai, client) => {\n      Object.values(SUPPORTED_DEEPSEEK_MODELS).forEach((modelRef) =>\n        defineCompatOpenAIModel({\n          ai,\n          name: modelRef.name,\n          client,\n          modelRef,\n          requestBuilder: deepSeekRequestBuilder,\n        })\n      );\n    },\n    resolver,\n    listActions,\n  });\n}\n\nexport type DeepSeekPlugin = {\n  (params?: DeepSeekPluginOptions): GenkitPlugin;\n  model(\n    name: keyof typeof SUPPORTED_DEEPSEEK_MODELS,\n    config?: z.infer<typeof DeepSeekChatCompletionConfigSchema>\n  ): ModelReference<typeof DeepSeekChatCompletionConfigSchema>;\n  model(name: string, config?: any): ModelReference<z.ZodTypeAny>;\n};\n\nconst model = ((name: string, config?: any): ModelReference<z.ZodTypeAny> => {\n  return deepSeekModelRef({\n    name: `deepseek/${name}`,\n    config,\n  });\n}) as DeepSeekPlugin['model'];\n\n/**\n * This module provides an interface to the DeepSeek models through the Genkit\n * plugin system. It allows users to interact with various models by providing\n * an API key and optional configuration.\n *\n * The main export is the `deepseek` plugin, which can be configured with an API\n * key either directly or through environment variables. It initializes the\n * OpenAI client and makes available the models for use.\n *\n * Exports:\n * - deepSeek: The main plugin function to interact with DeepSeek, via OpenAI\n *   compatible API.\n *\n * Usage: To use the models, initialize the deepseek plugin inside\n * `configureGenkit` and pass the configuration options. If no API key is\n * provided in the options, the environment variable `OPENAI_API_KEY` must be\n * set.\n *\n * Example:\n * ```\n * import { deepSeek } from '@genkit-ai/compat-oai/deepseek';\n *\n * export default configureGenkit({\n *  plugins: [\n *    deepSeek()\n *    ... // other plugins\n *  ]\n * });\n * ```\n */\nexport const deepSeek: DeepSeekPlugin = Object.assign(deepSeekPlugin, {\n  model,\n});\n\nexport default deepSeek;\n"],"mappings":"AAgBA;AAAA,EAGE;AAAA,EACA;AAAA,OAGK;AACP,SAAS,cAAc;AAIvB,SAAS,wBAAuC;AAChD,SAAS,+BAA+B;AACxC;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAIP,MAAM,WAAW,OACf,IACA,QACA,YACA,eACG;AACH,MAAI,eAAe,SAAS;AAC1B,UAAM,WAAW,iBAAiB;AAAA,MAChC,MAAM,YAAY,UAAU;AAAA,IAC9B,CAAC;AACD,4BAAwB;AAAA,MACtB;AAAA,MACA,MAAM,SAAS;AAAA,MACf;AAAA,MACA;AAAA,MACA,gBAAgB;AAAA,IAClB,CAAC;AAAA,EACH,OAAO;AACL,WAAO,KAAK,yDAAyD;AAAA,EACvE;AACF;AAEA,MAAM,cAAc,OAAO,WAA8C;AACvE,SAAO,MAAM,OAAO,OAAO,KAAK,EAAE;AAAA,IAAK,CAAC,aACtC,SAAS,KACN,OAAO,CAACA,WAAUA,OAAM,WAAW,OAAO,EAC1C,IAAI,CAACA,WAAwB;AAC5B,YAAM,WACJ,0BAA0BA,OAAM,EAAE,KAClC,iBAAiB;AAAA,QACf,MAAM,YAAYA,OAAM,EAAE;AAAA,MAC5B,CAAC;AACH,aAAO,oBAAoB;AAAA,QACzB,MAAM,SAAS;AAAA,QACf,MAAM,SAAS;AAAA,QACf,cAAc,SAAS;AAAA,MACzB,CAAC;AAAA,IACH,CAAC;AAAA,EACL;AACF;AAEO,SAAS,eAAe,SAA+C;AAC5E,QAAM,SAAS,SAAS,UAAU,QAAQ,IAAI;AAC9C,MAAI,CAAC,QAAQ;AACX,UAAM,IAAI,YAAY;AAAA,MACpB,QAAQ;AAAA,MACR,SACE;AAAA,IACJ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