UNPKG

@genkit-ai/compat-oai

Version:

Genkit AI framework plugin for OpenAI APIs.

348 lines 9.33 kB
import { GenerationCommonConfigSchema, Message, modelRef, z } from "genkit"; const VisualDetailLevelSchema = z.enum(["auto", "low", "high"]).optional(); const ChatCompletionCommonConfigSchema = GenerationCommonConfigSchema.extend({ temperature: z.number().min(0).max(2).optional(), frequencyPenalty: z.number().min(-2).max(2).optional(), logProbs: z.boolean().optional(), presencePenalty: z.number().min(-2).max(2).optional(), topLogProbs: z.number().int().min(0).max(20).optional() }); function toOpenAIRole(role) { switch (role) { case "user": return "user"; case "model": return "assistant"; case "system": return "system"; case "tool": return "tool"; default: throw new Error(`role ${role} doesn't map to an OpenAI role.`); } } function toOpenAITool(tool) { return { type: "function", function: { name: tool.name, parameters: tool.inputSchema !== null ? tool.inputSchema : void 0 } }; } function toOpenAITextAndMedia(part, visualDetailLevel) { if (part.text) { return { type: "text", text: part.text }; } else if (part.media) { return { type: "image_url", image_url: { url: part.media.url, detail: visualDetailLevel } }; } throw Error( `Unsupported genkit part fields encountered for current message role: ${JSON.stringify(part)}.` ); } function toOpenAIMessages(messages, visualDetailLevel = "auto") { const apiMessages = []; for (const message of messages) { const msg = new Message(message); const role = toOpenAIRole(message.role); switch (role) { case "user": const content = msg.content.map( (part) => toOpenAITextAndMedia(part, visualDetailLevel) ); const onlyTextContent = content.some((item) => item.type !== "text"); if (!onlyTextContent) { content.forEach((item) => { if (item.type === "text") { apiMessages.push({ role, content: item.text }); } }); } else { apiMessages.push({ role, content }); } break; case "system": apiMessages.push({ role, content: msg.text }); break; case "assistant": { const toolCalls = msg.content.filter( (part) => Boolean(part.toolRequest) ).map((part) => ({ id: part.toolRequest.ref ?? "", type: "function", function: { name: part.toolRequest.name, arguments: JSON.stringify(part.toolRequest.input) } })); if (toolCalls.length > 0) { apiMessages.push({ role, tool_calls: toolCalls }); } else { apiMessages.push({ role, content: msg.text }); } break; } case "tool": { const toolResponseParts = msg.toolResponseParts(); toolResponseParts.map((part) => { apiMessages.push({ role, tool_call_id: part.toolResponse.ref ?? "", content: typeof part.toolResponse.output === "string" ? part.toolResponse.output : JSON.stringify(part.toolResponse.output) }); }); break; } } } return apiMessages; } const finishReasonMap = { length: "length", stop: "stop", tool_calls: "stop", content_filter: "blocked" }; function fromOpenAIToolCall(toolCall, choice) { if (!toolCall.function) { throw Error( `Unexpected openAI chunk choice. tool_calls was provided but one or more tool_calls is missing.` ); } const f = toolCall.function; if (choice.finish_reason === "tool_calls") { return { toolRequest: { name: f.name, ref: toolCall.id, input: f.arguments ? JSON.parse(f.arguments) : f.arguments } }; } else { return { toolRequest: { name: f.name, ref: toolCall.id, input: "" } }; } } function fromOpenAIChoice(choice, jsonMode = false) { const toolRequestParts = choice.message.tool_calls?.map( (toolCall) => fromOpenAIToolCall(toolCall, choice) ); return { finishReason: finishReasonMap[choice.finish_reason] || "other", message: { role: "model", content: toolRequestParts ? ( // Note: Not sure why I have to cast here exactly. // Otherwise it thinks toolRequest must be 'undefined' if provided toolRequestParts ) : [ jsonMode ? { data: JSON.parse(choice.message.content) } : { text: choice.message.content } ] } }; } function fromOpenAIChunkChoice(choice, jsonMode = false) { const toolRequestParts = choice.delta.tool_calls?.map( (toolCall) => fromOpenAIToolCall(toolCall, choice) ); return { finishReason: choice.finish_reason ? finishReasonMap[choice.finish_reason] || "other" : "unknown", message: { role: "model", content: toolRequestParts ? ( // Note: Not sure why I have to cast here exactly. // Otherwise it thinks toolRequest must be 'undefined' if provided toolRequestParts ) : [ jsonMode ? { data: JSON.parse(choice.delta.content) } : { text: choice.delta.content } ] } }; } function toOpenAIRequestBody(modelName, request, requestBuilder) { const messages = toOpenAIMessages( request.messages, request.config?.visualDetailLevel ); const { temperature, maxOutputTokens, // unused topK, // unused topP: top_p, frequencyPenalty: frequency_penalty, logProbs: logprobs, presencePenalty: presence_penalty, topLogProbs: top_logprobs, stopSequences: stop, version: modelVersion, tools: toolsFromConfig, ...restOfConfig } = request.config ?? {}; const tools = request.tools?.map(toOpenAITool) ?? []; if (toolsFromConfig) { tools.push(...toolsFromConfig); } let body = { model: modelVersion ?? modelName, messages, tools: tools.length > 0 ? tools : void 0, temperature, top_p, stop, frequency_penalty, presence_penalty, top_logprobs, logprobs }; if (requestBuilder) { requestBuilder(request, body); } else { body = { ...body, ...restOfConfig }; } const response_format = request.output?.format; if (response_format === "json") { body.response_format = { type: "json_object" }; } else if (response_format === "text") { body.response_format = { type: "text" }; } for (const key in body) { if (!body[key] || Array.isArray(body[key]) && !body[key].length) delete body[key]; } return body; } function openAIModelRunner(name, client, requestBuilder) { return async (request, options) => { let response; const body = toOpenAIRequestBody(name, request, requestBuilder); if (options?.streamingRequested) { const stream = client.beta.chat.completions.stream( { ...body, stream: true, stream_options: { include_usage: true } }, { signal: options?.abortSignal } ); for await (const chunk of stream) { chunk.choices?.forEach((chunk2) => { const c = fromOpenAIChunkChoice(chunk2); options?.sendChunk({ index: chunk2.index, content: c.message?.content ?? [] }); }); } response = await stream.finalChatCompletion(); } else { response = await client.chat.completions.create(body, { signal: options?.abortSignal }); } const standardResponse = { usage: { inputTokens: response.usage?.prompt_tokens, outputTokens: response.usage?.completion_tokens, totalTokens: response.usage?.total_tokens }, raw: response }; if (response.choices.length === 0) { return standardResponse; } else { const choice = response.choices[0]; return { ...fromOpenAIChoice(choice, request.output?.format === "json"), ...standardResponse }; } }; } function defineCompatOpenAIModel(params) { const { ai, name, client, modelRef: modelRef2, requestBuilder } = params; const modelName = name.substring(name.indexOf("/") + 1); return ai.defineModel( { name, apiVersion: "v2", ...modelRef2?.info, configSchema: modelRef2?.configSchema }, openAIModelRunner(modelName, client, requestBuilder) ); } const GENERIC_MODEL_INFO = { supports: { multiturn: true, media: true, tools: true, toolChoice: true, systemRole: true } }; function compatOaiModelRef(params) { const { name, info = GENERIC_MODEL_INFO, configSchema, config = void 0 } = params; return modelRef({ name, configSchema: configSchema || ChatCompletionCommonConfigSchema, info, config }); } export { ChatCompletionCommonConfigSchema, compatOaiModelRef, defineCompatOpenAIModel, fromOpenAIChoice, fromOpenAIChunkChoice, fromOpenAIToolCall, openAIModelRunner, toOpenAIMessages, toOpenAIRequestBody, toOpenAIRole, toOpenAITextAndMedia, toOpenAITool }; //# sourceMappingURL=model.mjs.map