UNPKG

@genkit-ai/compat-oai

Version:

Genkit AI framework plugin for OpenAI APIs.

75 lines (72 loc) 3.64 kB
import * as genkit_plugin from 'genkit/plugin'; import { Genkit, ActionMetadata } from 'genkit'; import { ActionType } from 'genkit/registry'; import { ClientOptions, OpenAI } from 'openai'; export { SpeechConfigSchema, SpeechRequestBuilder, TranscriptionConfigSchema, TranscriptionRequestBuilder, compatOaiSpeechModelRef, compatOaiTranscriptionModelRef, defineCompatOpenAISpeechModel, defineCompatOpenAITranscriptionModel } from './audio.mjs'; export { defineCompatOpenAIEmbedder } from './embedder.mjs'; export { ImageGenerationCommonConfigSchema, ImageRequestBuilder, compatOaiImageModelRef, defineCompatOpenAIImageModel } from './image.mjs'; export { ChatCompletionCommonConfigSchema, ModelRequestBuilder, compatOaiModelRef, defineCompatOpenAIModel, openAIModelRunner } from './model.mjs'; import 'genkit/model'; import 'openai/resources/audio/index.mjs'; import 'openai/resources/images.mjs'; import 'openai/resources/index.mjs'; interface PluginOptions extends Partial<ClientOptions> { name: string; initializer?: (ai: Genkit, client: OpenAI) => Promise<void>; resolver?: (ai: Genkit, client: OpenAI, actionType: ActionType, actionName: string) => Promise<void>; listActions?: (client: OpenAI) => Promise<ActionMetadata[]>; } /** * This module provides the `openAICompatible` plugin factory for Genkit. It * enables interaction with OpenAI-compatible API endpoints, allowing users to * leverage various AI models by configuring API keys and other client options. * * The core export is `openAICompatible`, a function that accepts * `PluginOptions` and returns a Genkit plugin. * * Key `PluginOptions` include: * - `name`: A string to uniquely identify this plugin instance * (e.g., 'deepSeek', 'customOpenAI'). * - `apiKey`: The API key for the service. If not provided directly, the * plugin will attempt to use the `OPENAI_API_KEY` environment variable. * - `initializer`: An optional asynchronous function for custom setup after * the OpenAI client is initialized. It receives the Genkit instance and the * OpenAI client. * - Additional properties from OpenAI's `ClientOptions` (like `baseURL`, * `timeout`, etc.) can be passed to customize the OpenAI client. * * The returned plugin initializes an OpenAI client tailored to the provided * options, making configured models available for use within Genkit flows. * * @param {PluginOptions} options - Configuration options for the plugin. * @returns A Genkit plugin configured for an OpenAI-compatible service. * * Usage: Import `openAICompatible` (or your chosen import name for the default * export) from this package (e.g., `genkitx-openai`). Then, invoke it within * the `plugins` array of `configureGenkit`, providing the necessary * `PluginOptions`. * * Example: * ```typescript * import myOpenAICompatiblePlugin from 'genkitx-openai'; // Default import * * export default configureGenkit({ * plugins: [ * myOpenAICompatiblePlugin({ * name: 'gpt4o', // Name for this specific plugin configuration * apiKey: process.env.OPENAI_API_KEY, * // For a non-OpenAI compatible endpoint: * // baseURL: 'https://api.custom-llm-provider.com/v1', * }), * myOpenAICompatiblePlugin({ * name: 'localLlama', * apiKey: 'ollama', // Or specific key if required by local server * baseURL: 'http://localhost:11434/v1', // Example for Ollama * }), * // ... other plugins * ], * }); * ``` */ declare const openAICompatible: (options: PluginOptions) => genkit_plugin.GenkitPlugin; export { type PluginOptions, openAICompatible as default, openAICompatible };