{"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  GenkitError,\n  modelActionMetadata,\n  ModelReference,\n  z,\n} from 'genkit';\nimport { logger } from 'genkit/logging';\nimport { type GenkitPluginV2 } from 'genkit/plugin';\nimport { ActionType } from 'genkit/registry';\nimport OpenAI from 'openai';\nimport { openAICompatible, PluginOptions } from '../index.mjs';\nimport { defineCompatOpenAIModel } from '../model.mjs';\nimport {\n  DeepSeekChatCompletionConfigSchema,\n  deepSeekModelRef,\n  deepSeekRequestBuilder,\n  SUPPORTED_DEEPSEEK_MODELS,\n} from './deepseek.mjs';\n\nexport type DeepSeekPluginOptions = Omit<PluginOptions, 'name' | 'baseURL'>;\n\nfunction createResolver(pluginOptions: PluginOptions) {\n  return async (client: OpenAI, actionType: ActionType, actionName: string) => {\n    if (actionType === 'model') {\n      const modelRef = deepSeekModelRef({\n        name: actionName,\n      });\n      return defineCompatOpenAIModel({\n        name: modelRef.name,\n        client,\n        pluginOptions,\n        modelRef,\n        requestBuilder: deepSeekRequestBuilder,\n      });\n    } else {\n      logger.warn('Only model actions are supported by the DeepSeek plugin');\n      return undefined;\n    }\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: 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(\n  options?: DeepSeekPluginOptions\n): GenkitPluginV2 {\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  const pluginOptions = { name: 'deepseek', ...options };\n  return openAICompatible({\n    name: 'deepseek',\n    baseURL: 'https://api.deepseek.com',\n    apiKey,\n    ...options,\n    initializer: async (client) => {\n      return Object.values(SUPPORTED_DEEPSEEK_MODELS).map((modelRef) =>\n        defineCompatOpenAIModel({\n          name: modelRef.name,\n          client,\n          pluginOptions,\n          modelRef,\n          requestBuilder: deepSeekRequestBuilder,\n        })\n      );\n    },\n    resolver: createResolver(pluginOptions),\n    listActions,\n  });\n}\n\nexport type DeepSeekPlugin = {\n  (params?: DeepSeekPluginOptions): GenkitPluginV2;\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,\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 `DEEPSEEK_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,EAEE;AAAA,EACA;AAAA,OAGK;AACP,SAAS,cAAc;AAIvB,SAAS,wBAAuC;AAChD,SAAS,+BAA+B;AACxC;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAIP,SAAS,eAAe,eAA8B;AACpD,SAAO,OAAO,QAAgB,YAAwB,eAAuB;AAC3E,QAAI,eAAe,SAAS;AAC1B,YAAM,WAAW,iBAAiB;AAAA,QAChC,MAAM;AAAA,MACR,CAAC;AACD,aAAO,wBAAwB;AAAA,QAC7B,MAAM,SAAS;AAAA,QACf;AAAA,QACA;AAAA,QACA;AAAA,QACA,gBAAgB;AAAA,MAClB,CAAC;AAAA,IACH,OAAO;AACL,aAAO,KAAK,yDAAyD;AACrE,aAAO;AAAA,IACT;AAAA,EACF;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,MAAMA,OAAM;AAAA,MACd,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,eACd,SACgB;AAChB,QAAM,SAAS,SAAS,UAAU,QAAQ,IAAI;AAC9C,MAAI,CAAC,QAAQ;AACX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