UNPKG

llm-polyglot

Version:

A universal LLM client - provides adapters for various LLM providers to adhere to a universal interface - the openai sdk - allows you to use providers like anthropic using the same openai interface and transforms the responses in the same way - this allow

153 lines (149 loc) 5.87 kB
import Anthropic from '@anthropic-ai/sdk'; import { GoogleGenerativeAI, CachedContent, EnhancedGenerateContentResponse, SafetySetting, GenerationConfig, Content, FunctionDeclaration, GroundingMetadata, FunctionCall } from '@google/generative-ai'; import OpenAI, { ClientOptions } from 'openai'; type Providers = "openai" | "anthropic" | "google"; type SupportedChatCompletionMessageParam = Omit<OpenAI.ChatCompletionCreateParams["messages"][number], "content"> & { content: string | (Anthropic.Messages.TextBlockParam | Anthropic.Messages.ImageBlockParam | Anthropic.Messages.ToolUseBlockParam | Anthropic.Messages.ToolResultBlockParam)[]; }; type ExtendedCompletionAnthropic = Partial<OpenAI.ChatCompletion> & { originResponse: Anthropic.Messages.Message | Anthropic.Messages.ToolResultBlockParam; }; type ExtendedCompletionChunkAnthropic = Partial<OpenAI.ChatCompletionChunk> & { originResponse: Anthropic.Messages.Message | Anthropic.Messages.ToolResultBlockParam; }; type AnthropicChatCompletionParamsStream = Omit<Partial<OpenAI.ChatCompletionCreateParams>, "model" | "messages"> & { model: Anthropic.CompletionCreateParams["model"]; messages: SupportedChatCompletionMessageParam[]; stream: true; max_tokens: number; }; type AnthropicChatCompletionParamsNonStream = Omit<Partial<OpenAI.ChatCompletionCreateParams>, "model" | "messages"> & { model: Anthropic.CompletionCreateParams["model"]; messages: SupportedChatCompletionMessageParam[]; stream?: false | undefined; max_tokens: number; }; type AnthropicChatCompletionParams = AnthropicChatCompletionParamsStream | AnthropicChatCompletionParamsNonStream; /** Google types */ type GoogleChatCompletionParamsBase = Omit<Partial<OpenAI.ChatCompletionCreateParams>, "messages" | "model"> & { messages: SupportedChatCompletionMessageParam[]; max_tokens: number; additionalProperties?: { cacheName?: string; sessionId?: string; safetySettings?: SafetySetting[]; modelGenerationConfig?: GenerationConfig; }; groundingThreshold?: number; systemInstruction?: Content | string | undefined; tools?: Array<{ type: "function"; function: Omit<FunctionDeclaration, "parameters"> & { parameters?: Record<string, unknown>; }; }>; tool_choice?: { type: "function"; function: { name: string; }; }; }; type GoogleChatCompletionParamsStream = GoogleChatCompletionParamsBase & { stream: true; model: GeminiGenerativeModels | string; }; type GoogleChatCompletionParamsNonStream = GoogleChatCompletionParamsBase & { stream?: false | undefined; model: GeminiGenerativeModels; }; type GoogleChatCompletionParams = GoogleChatCompletionParamsStream | GoogleChatCompletionParamsNonStream; interface GoogleGroundingMetadata extends GroundingMetadata { search_queries: string[]; sources: Array<{ url: string; title: string; }>; search_suggestion_html?: string; supports?: Array<{ text: string; sources: Array<{ url: string; title: string; }>; confidence: number[]; }>; } interface GoogleToolCall { index: number; id: string; function: FunctionCall; type: string; } interface GoogleMessage { role: string; content: string; tool_calls?: GoogleToolCall[]; } interface GoogleChoice { index: number; message: GoogleMessage; finish_reason: string | null; logprobs?: null; grounding_metadata?: GoogleGroundingMetadata; } interface GoogleDeltaChoice extends Omit<GoogleChoice, "message"> { delta: GoogleMessage; } type ExtendedCompletionGoogle = Partial<OpenAI.ChatCompletion> & { originResponse: EnhancedGenerateContentResponse; choices: GoogleChoice[]; }; type ExtendedCompletionChunkGoogle = Partial<OpenAI.ChatCompletionChunk> & { originResponse: EnhancedGenerateContentResponse; choices: GoogleDeltaChoice[]; }; type GoogleCacheCreateParams = Omit<GoogleChatCompletionParams, "stream"> & { ttlSeconds: number; }; type GeminiGenerativeModels = "gemini-1.5-pro" | "gemini-1.5-pro-latest" | "gemini-1.5-flash-8b" | "gemini-1.5-flash-8b-latest" | "gemini-1.5-flash" | "gemini-1.5-flash-latest" | (string & {}); /** General type for providers */ type OpenAILikeClient<P> = P extends "openai" | "azure" ? OpenAI : P extends "google" ? GoogleGenerativeAI & { chat: { completions: { create: <P extends GoogleChatCompletionParams>(params: P) => P extends { stream: true; } ? Promise<AsyncIterable<ExtendedCompletionChunkGoogle>> : Promise<ExtendedCompletionGoogle>; }; }; cacheManager: { create: (params: GoogleCacheCreateParams) => Promise<CachedContent>; get: (cacheName: string) => Promise<CachedContent>; list: () => Promise<{ cachedContents: CachedContent[]; }>; delete: (cacheName: string) => Promise<void>; update: (cacheName: string, params: GoogleCacheCreateParams) => Promise<CachedContent>; }; } : P extends "anthropic" ? Anthropic & { [key: string]: unknown; chat: { completions: { create: <P extends AnthropicChatCompletionParams>(params: P) => P extends { stream: true; } ? Promise<AsyncIterable<ExtendedCompletionChunkAnthropic>> : Promise<ExtendedCompletionAnthropic>; }; }; } : never; declare class LLMClient<P extends Providers> { private providerInstance; constructor(opts: ClientOptions & { provider: P; }); getProviderInstance(): OpenAILikeClient<P>; } declare function createLLMClient<P extends Providers>(opts?: ClientOptions & { provider: P; logLevel?: string; }): OpenAILikeClient<P>; export { LLMClient, createLLMClient };