@naktibalda/jorel
Version:
The easiest way to use LLMs, including streams, images, documents, tools and various agent scenarios.
301 lines (300 loc) • 13.4 kB
TypeScript
import { CreateLlmDocument, LlmDocument, LlmDocumentCollection } from "../documents";
import { LoggerOption, LogLevel, LogService } from "../logger";
import { ImageContent } from "../media";
import { AnthropicConfig, GoogleGenerativeAIConfig, GoogleVertexAiConfig, GroqConfig, JsonSpecification, LlmAssistantMessage, LlmAssistantMessageMeta, LlmAssistantMessageWithToolCalls, LlmCoreProvider, LlmJsonResponseWithMeta, LlmMessage, LlmStreamResponse, LlmStreamResponseChunk, LlmStreamResponseMessages, LlmStreamResponseWithToolCalls, LlmStreamToolCallCompleted, LlmStreamToolCallStarted, LlmTextResponseWithMeta, LlmToolChoice, MistralConfig, OllamaConfig, OpenAIConfig } from "../providers";
import { Nullable } from "../shared";
import { LlmTool, LlmToolConfiguration, LLmToolContextSegment, LlmToolKit } from "../tools";
import { JorElAgentManager } from "./jorel.team";
interface InitialConfig {
anthropic?: AnthropicConfig | true;
googleGenAi?: GoogleGenerativeAIConfig | true;
grok?: OpenAIConfig | true;
groq?: GroqConfig | true;
mistral?: MistralConfig | true;
ollama?: OllamaConfig | true;
openAI?: OpenAIConfig | true;
openRouter?: OpenAIConfig | true;
vertexAi?: GoogleVertexAiConfig | true;
systemMessage?: Nullable<string>;
documentSystemMessage?: string;
temperature?: Nullable<number>;
logger?: LoggerOption | LogService;
logLevel?: LogLevel;
}
export interface JorElCoreGenerationConfig {
temperature?: Nullable<number>;
maxTokens?: number;
}
export interface JorElTextGenerationConfigWithTools extends JorElCoreGenerationConfig {
model?: string;
systemMessage?: string;
documentSystemMessage?: string;
documents?: (LlmDocument | CreateLlmDocument)[] | LlmDocumentCollection;
tools?: LlmToolKit | (LlmTool | LlmToolConfiguration)[];
toolChoice?: LlmToolChoice;
/** @deprecated Use `maxToolCalls` instead */
maxAttempts?: number;
maxToolCalls?: number;
maxToolCallErrors?: number;
context?: LLmToolContextSegment;
secureContext?: LLmToolContextSegment;
messageHistory?: LlmMessage[];
}
export interface JorElJsonGenerationConfigWithTools extends JorElTextGenerationConfigWithTools {
jsonSchema?: JsonSpecification;
jsonSchemaDescription?: string;
}
export interface JorElGenerationConfigWithTools extends JorElCoreGenerationConfig {
model?: string;
tools?: LlmToolKit;
toolChoice?: LlmToolChoice;
/** @deprecated Use `maxToolCalls` instead */
maxAttempts?: number;
maxToolCalls?: number;
maxToolCallErrors?: number;
context?: LLmToolContextSegment;
secureContext?: LLmToolContextSegment;
json?: boolean | JsonSpecification;
jsonDescription?: string;
}
export type JorElTaskInput = string | (string | ImageContent)[];
export type JorElGenerationOutput = (LlmAssistantMessage | LlmAssistantMessageWithToolCalls) & {
meta: LlmAssistantMessageMeta;
};
/**
* Jor-El: Singular interface for managing multiple LLM providers and models
*/
export declare class JorEl {
/**
* System message use for all requests by default (unless specified per request)
*/
systemMessage: string;
/**
* Agent related functionality
*/
readonly team: JorElAgentManager;
/**
* Public methods for managing models
*/
readonly models: {
list: () => {
model: string;
provider: string;
}[];
register: (params: {
model: string;
provider: string;
setAsDefault?: boolean;
}) => void;
unregister: (model: string) => void;
getDefault: () => string;
setDefault: (model: string) => void;
embeddings: {
register: (params: {
model: string;
provider: string;
dimensions: number;
setAsDefault?: boolean;
}) => void;
unregister: (model: string) => void;
getDefault: () => string;
setDefault: (model: string) => void;
list: () => {
model: string;
provider: string;
}[];
};
};
/**
* Public methods for managing providers
*/
readonly providers: {
list: () => string[];
registerCustom: (provider: string, coreProvider: LlmCoreProvider) => void;
registerAnthropic: (config?: AnthropicConfig) => void;
registerGoogleGenAi: (config?: GoogleGenerativeAIConfig) => void;
registerGrok: (config?: OpenAIConfig) => void;
registerGroq: (config?: GroqConfig) => void;
registerMistral: (config?: MistralConfig) => void;
registerOllama: (config?: OllamaConfig) => void;
registerOpenAi: (config?: OpenAIConfig) => void;
registerOpenRouter: (config?: OpenAIConfig) => void;
registerGoogleVertexAi: (config?: GoogleVertexAiConfig) => void;
anthropic: {
addModel: (model: string) => void;
getClient: () => import("@anthropic-ai/sdk").default | import("@anthropic-ai/bedrock-sdk").default;
};
googleGenAi: {
addModel: (model: string) => void;
getClient: () => import("@google/generative-ai").GoogleGenerativeAI;
};
grok: {
addModel: (model: string) => void;
getClient: () => import("openai").OpenAI;
};
groq: {
addModel: (model: string) => void;
getClient: () => import("openai").OpenAI;
};
mistral: {
addModel: (model: string) => void;
getClient: () => import("@mistralai/mistralai").Mistral;
};
openAi: {
addModel: (model: string) => void;
getClient: () => import("openai").OpenAI;
};
openRouter: {
addModel: (model: string) => void;
getClient: () => import("openai").OpenAI;
};
vertexAi: {
addModel: (model: string) => void;
getClient: () => import("@google-cloud/vertexai").VertexAI;
};
};
/**
* Create a new Jor-El instance.
*
* @param config - The configuration for the Jor-El instance.
* @param config.anthropic - Anthropic configuration (optional).
* @param config.googleGenAi - Google Generative AI configuration (optional).
* @param config.grok - Grok configuration (optional).
* @param config.groq - Groq configuration (optional).
* @param config.vertexAi - Google Vertex AI configuration (optional).
* @param config.ollama - Ollama configuration (optional).
* @param config.openAI - OpenAI configuration (optional).
* @param config.openRouter - OpenRouter configuration (optional).
* @param config.systemMessage - System message to include in all requests (optional).
* @param config.documentSystemMessage - System message to include in all requests with documents (optional).
* @param config.temperature - Default temperature for all requests (optional).
*/
constructor(config?: InitialConfig);
/**
* Default document system message for all requests (only used when documents are included)
*/
get documentSystemMessage(): string;
/**
* Set the default document system message for all requests (only used when documents are included)
*/
set documentSystemMessage(documentSystemMessage: string);
/**
* Default temperature for all requests
*/
get temperature(): Nullable<number> | undefined;
/**
* Set the default temperature for all requests
*/
set temperature(temperature: Nullable<number>);
/**
* Logger instance
*/
get logger(): LogService;
/**
* Set the logger instance
*/
set logger(logger: LogService);
/**
* Log level
*/
get logLevel(): LogLevel;
/**
* Set the log level
*/
set logLevel(logLevel: LogLevel);
/**
* Generate a response for a given set of messages.
*
* @param messages - The messages to generate a response for.
* @param config - The configuration for the generation.
* @param config.model - Model to use for this generation (optional).
* @param config.systemMessage - System message to include in this request (optional).
* @param config.temperature - Temperature for this request (optional).
* @param config.tools - Tools to use for this request (optional).
*/
generate(messages: LlmMessage[], config?: JorElGenerationConfigWithTools): Promise<JorElGenerationOutput>;
/**
* Generate a response for a given task.
* @deprecated Use `text` instead.
* @param task - The task to generate a response for (either a string or an array of strings and ImageContent objects).
* @param config - Configuration for the specific generation.
* @param includeMeta - Whether to include the metadata and all previous messages in the response.
* @returns The text response, or an object with the response, metadata, and messages.
*/
ask(task: JorElTaskInput, config?: JorElTextGenerationConfigWithTools, includeMeta?: false): Promise<string>;
ask(task: JorElTaskInput, config?: JorElTextGenerationConfigWithTools, includeMeta?: true): Promise<{
response: string;
meta: LlmAssistantMessageMeta;
messages: LlmMessage[];
}>;
/**
* Generate a response for a given task.
*
* @param task - The task to generate a response for (either a string or an array of strings and ImageContent objects).
* @param config - Configuration for the specific generation.
* @param includeMeta - Whether to include the metadata and all previous messages in the response.
* @returns The text response, or an object with the response, metadata, and messages.
*/
text(task: JorElTaskInput, config?: JorElTextGenerationConfigWithTools, includeMeta?: false): Promise<string>;
text(task: JorElTaskInput, config?: JorElTextGenerationConfigWithTools, includeMeta?: true): Promise<LlmTextResponseWithMeta>;
/**
* Generate a JSON response for a given task.
*
* @param task - The task to generate a response for (either a string or an array of strings and ImageContent objects).
* @param config - Configuration for the specific generation.
* @param includeMeta - Whether to include the metadata and all previous messages in the response.
* @returns The JSON response, or an object with the response, metadata, and messages.
* @throws Error - If the response is not valid JSON.
*/
json(task: JorElTaskInput, config?: JorElJsonGenerationConfigWithTools, includeMeta?: false): Promise<object>;
json(task: JorElTaskInput, config?: JorElJsonGenerationConfigWithTools, includeMeta?: true): Promise<LlmJsonResponseWithMeta>;
/**
* Generate a stream of response chunks for a given set of messages.
*
* @param messages - The messages to generate a response for.
* @param config - The configuration for the generation.
*/
generateContentStream(messages: LlmMessage[], config?: JorElGenerationConfigWithTools): AsyncGenerator<LlmStreamResponseChunk | LlmStreamResponse | LlmStreamResponseWithToolCalls, void, unknown>;
/**
* Generate a stream of response chunks for a given task.
*
* @param task - The task to generate a response for (either a string or an array of strings and ImageContent objects).
* @param config - Configuration for the specific generation.
*/
stream(task: JorElTaskInput, config?: JorElTextGenerationConfigWithTools): AsyncGenerator<string, void, unknown>;
/**
* Generate a stream of response chunks for a given task with metadata.
*
* @param task - The task to generate a response for (either a string or an array of strings and ImageContent objects).
* @param config - Configuration for the specific generation.
*/
streamWithMeta(task: JorElTaskInput, config?: JorElTextGenerationConfigWithTools): AsyncGenerator<LlmStreamResponseChunk | LlmStreamResponse | LlmStreamResponseWithToolCalls | LlmStreamResponseMessages | LlmStreamToolCallStarted | LlmStreamToolCallCompleted, void, unknown>;
/**
* Create an embedding for a given text.
*
* @param text - The text to create an embedding for.
* @param config - The configuration for the embedding.
* @param config.model - The model to use for the embedding (optional).
*/
embed(text: string, config?: {
model?: string;
}): Promise<number[]>;
/**
* Generate a system message - optionally with a set of documents.
*
* @param systemMessage - The system message to use.
* @param documents - The documents to include in the system message (optional).
* @param documentSystemMessage - The system message to use for documents (optional).
*/
generateSystemMessage(systemMessage?: string, { documents, documentSystemMessage, }?: {
documents?: (LlmDocument | CreateLlmDocument)[] | LlmDocumentCollection;
documentSystemMessage?: string;
}): import("../providers").LlmSystemMessage;
/**
* Generate a user message.
*
* @param content - The content to include in the user message.
*/
generateUserMessage(content: JorElTaskInput): Promise<import("../providers").LlmUserMessage>;
}
export {};