UNPKG

@genkit-ai/compat-oai

Version:

Genkit AI framework plugin for OpenAI APIs.

158 lines (153 loc) 7.86 kB
import { z, ModelReference, Genkit, GenerateRequest, StreamingCallback, GenerateResponseChunkData, GenerateResponseData, Role, Part, MessageData, ToolRequestPart } from 'genkit'; import { ModelInfo, ModelAction, ToolDefinition } from 'genkit/model'; import OpenAI from 'openai'; import { ChatCompletionCreateParams, ChatCompletionRole, ChatCompletionTool, ChatCompletionContentPart, ChatCompletionMessageParam, ChatCompletionMessageToolCall, ChatCompletionChunk, ChatCompletion } from 'openai/resources/index.mjs'; /** * Copyright 2024 The Fire Company * Copyright 2024 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ declare const VisualDetailLevelSchema: z.ZodOptional<z.ZodEnum<["auto", "low", "high"]>>; type VisualDetailLevel = z.infer<typeof VisualDetailLevelSchema>; type ModelRequestBuilder = (req: GenerateRequest, params: ChatCompletionCreateParams) => void; declare const ChatCompletionCommonConfigSchema: z.ZodObject<{ version: z.ZodOptional<z.ZodString>; maxOutputTokens: z.ZodOptional<z.ZodNumber>; topK: z.ZodOptional<z.ZodNumber>; topP: z.ZodOptional<z.ZodNumber>; stopSequences: z.ZodOptional<z.ZodArray<z.ZodString, "many">>; } & { temperature: z.ZodOptional<z.ZodNumber>; frequencyPenalty: z.ZodOptional<z.ZodNumber>; logProbs: z.ZodOptional<z.ZodBoolean>; presencePenalty: z.ZodOptional<z.ZodNumber>; topLogProbs: z.ZodOptional<z.ZodNumber>; }, "passthrough", z.ZodTypeAny, z.objectOutputType<{ version: z.ZodOptional<z.ZodString>; maxOutputTokens: z.ZodOptional<z.ZodNumber>; topK: z.ZodOptional<z.ZodNumber>; topP: z.ZodOptional<z.ZodNumber>; stopSequences: z.ZodOptional<z.ZodArray<z.ZodString, "many">>; } & { temperature: z.ZodOptional<z.ZodNumber>; frequencyPenalty: z.ZodOptional<z.ZodNumber>; logProbs: z.ZodOptional<z.ZodBoolean>; presencePenalty: z.ZodOptional<z.ZodNumber>; topLogProbs: z.ZodOptional<z.ZodNumber>; }, z.ZodTypeAny, "passthrough">, z.objectInputType<{ version: z.ZodOptional<z.ZodString>; maxOutputTokens: z.ZodOptional<z.ZodNumber>; topK: z.ZodOptional<z.ZodNumber>; topP: z.ZodOptional<z.ZodNumber>; stopSequences: z.ZodOptional<z.ZodArray<z.ZodString, "many">>; } & { temperature: z.ZodOptional<z.ZodNumber>; frequencyPenalty: z.ZodOptional<z.ZodNumber>; logProbs: z.ZodOptional<z.ZodBoolean>; presencePenalty: z.ZodOptional<z.ZodNumber>; topLogProbs: z.ZodOptional<z.ZodNumber>; }, z.ZodTypeAny, "passthrough">>; declare function toOpenAIRole(role: Role): ChatCompletionRole; /** * Converts a Genkit ToolDefinition to an OpenAI ChatCompletionTool object. * @param tool The Genkit ToolDefinition to convert. * @returns The converted OpenAI ChatCompletionTool object. */ declare function toOpenAITool(tool: ToolDefinition): ChatCompletionTool; /** * Converts a Genkit Part to the corresponding OpenAI ChatCompletionContentPart. * @param part The Genkit Part to convert. * @param visualDetailLevel The visual detail level to use for media parts. * @returns The corresponding OpenAI ChatCompletionContentPart. * @throws Error if the part contains unsupported fields for the current message role. */ declare function toOpenAITextAndMedia(part: Part, visualDetailLevel: VisualDetailLevel): ChatCompletionContentPart; /** * Converts a Genkit MessageData array to an OpenAI ChatCompletionMessageParam array. * @param messages The Genkit MessageData array to convert. * @param visualDetailLevel The visual detail level to use for media parts. * @returns The converted OpenAI ChatCompletionMessageParam array. */ declare function toOpenAIMessages(messages: MessageData[], visualDetailLevel?: VisualDetailLevel): ChatCompletionMessageParam[]; /** * Converts an OpenAI tool call to a Genkit ToolRequestPart. * @param toolCall The OpenAI tool call to convert. * @returns The converted Genkit ToolRequestPart. */ declare function fromOpenAIToolCall(toolCall: ChatCompletionMessageToolCall | ChatCompletionChunk.Choice.Delta.ToolCall, choice: ChatCompletion.Choice | ChatCompletionChunk.Choice): ToolRequestPart; /** * Converts an OpenAI message event to a Genkit GenerateResponseData object. * @param choice The OpenAI message event to convert. * @param jsonMode Whether the event is a JSON response. * @returns The converted Genkit GenerateResponseData object. */ declare function fromOpenAIChoice(choice: ChatCompletion.Choice, jsonMode?: boolean): GenerateResponseData; /** * Converts an OpenAI message stream event to a Genkit GenerateResponseData * object. * @param choice The OpenAI message stream event to convert. * @param jsonMode Whether the event is a JSON response. * @returns The converted Genkit GenerateResponseData object. */ declare function fromOpenAIChunkChoice(choice: ChatCompletionChunk.Choice, jsonMode?: boolean): GenerateResponseData; /** * Converts an OpenAI request to an OpenAI API request body. * @param modelName The name of the OpenAI model to use. * @param request The Genkit GenerateRequest to convert. * @returns The converted OpenAI API request body. * @throws An error if the specified model is not supported or if an unsupported output format is requested. */ declare function toOpenAIRequestBody(modelName: string, request: GenerateRequest, requestBuilder?: ModelRequestBuilder): OpenAI.Chat.Completions.ChatCompletionCreateParamsNonStreaming; /** * Creates the runner used by Genkit to interact with an OpenAI compatible * model. * @param name The name of the GPT model. * @param client The OpenAI client instance. * @returns The runner that Genkit will call when the model is invoked. */ declare function openAIModelRunner(name: string, client: OpenAI, requestBuilder?: ModelRequestBuilder): (request: GenerateRequest, options?: { streamingRequested?: boolean; sendChunk?: StreamingCallback<GenerateResponseChunkData>; abortSignal?: AbortSignal; }) => Promise<GenerateResponseData>; /** * Method to define a new Genkit Model that is compatible with Open AI * Chat Completions API. * * These models are to be used to chat with a large language model. * * @param params An object containing parameters for defining the OpenAI * Chat model. * @param params.ai The Genkit AI instance. * @param params.name The name of the model. * @param params.client The OpenAI client instance. * @param params.modelRef Optional reference to the model's configuration and * custom options. * @returns the created {@link ModelAction} */ declare function defineCompatOpenAIModel<CustomOptions extends z.ZodTypeAny = z.ZodTypeAny>(params: { ai: Genkit; name: string; client: OpenAI; modelRef?: ModelReference<CustomOptions>; requestBuilder?: ModelRequestBuilder; }): ModelAction; /** ModelRef helper, with reasonable defaults for OpenAI-compatible providers */ declare function compatOaiModelRef<CustomOptions extends z.ZodTypeAny = z.ZodTypeAny>(params: { name: string; info?: ModelInfo; configSchema?: CustomOptions; config?: any; }): ModelReference<CustomOptions>; export { ChatCompletionCommonConfigSchema, type ModelRequestBuilder, compatOaiModelRef, defineCompatOpenAIModel, fromOpenAIChoice, fromOpenAIChunkChoice, fromOpenAIToolCall, openAIModelRunner, toOpenAIMessages, toOpenAIRequestBody, toOpenAIRole, toOpenAITextAndMedia, toOpenAITool };