@genkit-ai/compat-oai
Version:
Genkit AI framework plugin for OpenAI APIs.
1 lines • 7.74 kB
Source Map (JSON)
{"version":3,"sources":["../../src/xai/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 {\n defineCompatOpenAIImageModel,\n ImageGenerationCommonConfigSchema,\n} from '../image.js';\nimport { openAICompatible, PluginOptions } from '../index.js';\nimport { defineCompatOpenAIModel } from '../model.js';\nimport { SUPPORTED_IMAGE_MODELS, xaiImageModelRef } from './grok-image.js';\nimport {\n grokRequestBuilder,\n SUPPORTED_LANGUAGE_MODELS,\n XaiChatCompletionConfigSchema,\n xaiModelRef,\n} from './grok.js';\n\nexport type XAIPluginOptions = 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 = xaiModelRef({ name: `xai/${actionName}` });\n defineCompatOpenAIModel({\n ai,\n name: modelRef.name,\n client,\n modelRef,\n requestBuilder: grokRequestBuilder,\n });\n } else {\n logger.warn('Only model actions are supported by the XAI 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 if (model.id.includes('image')) {\n const modelRef =\n SUPPORTED_IMAGE_MODELS[model.id] ??\n xaiImageModelRef({ name: `xai/${model.id}` });\n return modelActionMetadata({\n name: modelRef.name,\n info: modelRef.info,\n configSchema: modelRef.configSchema,\n });\n } else {\n const modelRef =\n SUPPORTED_LANGUAGE_MODELS[model.id] ??\n xaiModelRef({ name: `xai/${model.id}` });\n return modelActionMetadata({\n name: modelRef.name,\n info: modelRef.info,\n configSchema: modelRef.configSchema,\n });\n }\n })\n );\n};\n\nexport function xAIPlugin(options?: XAIPluginOptions): GenkitPlugin {\n const apiKey = options?.apiKey ?? process.env.XAI_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 XAI_API_KEY environment variable.',\n });\n }\n return openAICompatible({\n name: 'xai',\n baseURL: 'https://api.x.ai/v1',\n apiKey,\n ...options,\n initializer: async (ai, client) => {\n Object.values(SUPPORTED_LANGUAGE_MODELS).forEach((modelRef) =>\n defineCompatOpenAIModel({\n ai,\n name: modelRef.name,\n client,\n modelRef,\n requestBuilder: grokRequestBuilder,\n })\n );\n Object.values(SUPPORTED_IMAGE_MODELS).forEach((modelRef) =>\n defineCompatOpenAIImageModel({\n ai,\n name: modelRef.name,\n client,\n modelRef,\n })\n );\n },\n resolver,\n listActions,\n });\n}\n\nexport type XAIPlugin = {\n (params?: XAIPluginOptions): GenkitPlugin;\n model(\n name: keyof typeof SUPPORTED_LANGUAGE_MODELS,\n config?: z.infer<typeof XaiChatCompletionConfigSchema>\n ): ModelReference<typeof XaiChatCompletionConfigSchema>;\n model(\n name: keyof typeof SUPPORTED_IMAGE_MODELS,\n config?: z.infer<typeof ImageGenerationCommonConfigSchema>\n ): ModelReference<typeof ImageGenerationCommonConfigSchema>;\n model(name: string, config?: any): ModelReference<z.ZodTypeAny>;\n};\n\nconst model = ((name: string, config?: any): ModelReference<z.ZodTypeAny> => {\n if (name.includes('image')) {\n return xaiImageModelRef({\n name: `xai/${name}`,\n config,\n });\n }\n return xaiModelRef({\n name: `xai/${name}`,\n config,\n });\n}) as XAIPlugin['model'];\n\n/**\n * This module provides an interface to the XAI 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 `xai` 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 * - xAI: The main plugin function to interact with XAI, via OpenAI\n * compatible API.\n *\n * Usage: To use the models, initialize the xAI 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 { xAI } from '@genkit-ai/compat-oai/xai';\n *\n * export default configureGenkit({\n * plugins: [\n * xAI()\n * ... // other plugins\n * ]\n * });\n * ```\n */\nexport const xAI: XAIPlugin = Object.assign(xAIPlugin, {\n model,\n});\n\nexport default xAI;\n"],"mappings":"AAgBA;AAAA,EAGE;AAAA,EACA;AAAA,OAGK;AACP,SAAS,cAAc;AAIvB;AAAA,EACE;AAAA,OAEK;AACP,SAAS,wBAAuC;AAChD,SAAS,+BAA+B;AACxC,SAAS,wBAAwB,wBAAwB;AACzD;AAAA,EACE;AAAA,EACA;AAAA,EAEA;AAAA,OACK;AAIP,MAAM,WAAW,OACf,IACA,QACA,YACA,eACG;AACH,MAAI,eAAe,SAAS;AAC1B,UAAM,WAAW,YAAY,EAAE,MAAM,OAAO,UAAU,GAAG,CAAC;AAC1D,4BAAwB;AAAA,MACtB;AAAA,MACA,MAAM,SAAS;AAAA,MACf;AAAA,MACA;AAAA,MACA,gBAAgB;AAAA,IAClB,CAAC;AAAA,EACH,OAAO;AACL,WAAO,KAAK,oDAAoD;AAAA,EAClE;AACF;AAEA,MAAM,cAAc,OAAO,WAA8C;AACvE,SAAO,MAAM,OAAO,OAAO,KAAK,EAAE;AAAA,IAAK,CAAC,aACtC,SAAS,KACN,OAAO,CAACA,WAAUA,OAAM,WAAW,OAA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