openai-agents
Version:
A TypeScript library extending the OpenAI Node.js SDK for building highly customizable agents and simplifying 'function calling'. Easily create and manage tools to extend LLM capabilities.
134 lines • 7.58 kB
TypeScript
import OpenAI, { ClientOptions } from 'openai';
import { RedisClientType } from 'redis';
import { AgentOptions, CompletionResult, CreateChatCompletionOptions, HistoryOptions } from './types';
import { AgentStorage } from './storage';
import { ChatCompletionMessageParam, ChatModel, ChatCompletionAudioParam, ChatCompletionModality, ChatCompletionPredictionContent, ResponseFormatText, ResponseFormatJSONObject, ResponseFormatJSONSchema, ChatCompletionToolChoiceOption } from 'openai/resources';
/**
* @class OpenAIAgent
* @description Extends the OpenAI API client to manage chat completions, tool interactions, and persistent storage of conversation history. Provides methods for creating chat completions, managing tools, and interacting with a Redis storage.
*/
export declare class OpenAIAgent extends OpenAI {
private static readonly REQUIRED_ENV_VARS;
private completionParams;
private defaultHistoryMessages;
storage: AgentStorage | null;
system_instruction: string | null;
/**
* @constructor
* @param {AgentOptions} agentOptions - Options for configuring the agent, including the model, system instruction, initial messages template, etc.
* @param {ClientOptions} [options] - Optional OpenAI client options.
* @throws {ValidationError} If the model is not specified in the agent options.
*/
constructor(agentOptions: AgentOptions, options?: ClientOptions);
get model(): (string & {}) | ChatModel;
set model(value: (string & {}) | ChatModel);
get temperature(): number | null | undefined;
set temperature(value: number | null | undefined);
get top_p(): number | null | undefined;
set top_p(value: number | null | undefined);
get max_completion_tokens(): number | null | undefined;
set max_completion_tokens(value: number | null | undefined);
get max_tokens(): number | null | undefined;
set max_tokens(value: number | null | undefined);
get n(): number | null | undefined;
set n(value: number | null | undefined);
get frequency_penalty(): number | null | undefined;
set frequency_penalty(value: number | null | undefined);
get presence_penalty(): number | null | undefined;
set presence_penalty(value: number | null | undefined);
get tool_choice(): ChatCompletionToolChoiceOption | undefined;
set tool_choice(value: ChatCompletionToolChoiceOption | undefined);
get parallel_tool_calls(): boolean | undefined;
set parallel_tool_calls(value: boolean | undefined);
get audioParams(): ChatCompletionAudioParam | null | undefined;
set audioParams(value: ChatCompletionAudioParam | null | undefined);
get response_format(): ResponseFormatText | ResponseFormatJSONObject | ResponseFormatJSONSchema | undefined;
set response_format(value: ResponseFormatText | ResponseFormatJSONObject | ResponseFormatJSONSchema | undefined);
get logit_bias(): Record<string, number> | null | undefined;
set logit_bias(value: Record<string, number> | null | undefined);
get logprobs(): boolean | null | undefined;
set logprobs(value: boolean | null | undefined);
get top_logprobs(): number | null | undefined;
set top_logprobs(value: number | null | undefined);
get metadata(): Record<string, string> | null | undefined;
set metadata(value: Record<string, string> | null | undefined);
get stop(): string | null | string[] | undefined;
set stop(value: string | null | string[] | undefined);
get modalities(): ChatCompletionModality[] | null | undefined;
set modalities(value: ChatCompletionModality[] | null | undefined);
get prediction(): ChatCompletionPredictionContent | null | undefined;
set prediction(value: ChatCompletionPredictionContent | null | undefined);
get seed(): number | null | undefined;
set seed(value: number | null | undefined);
get service_tier(): 'auto' | 'default' | null | undefined;
set service_tier(value: 'auto' | 'default' | null | undefined);
get store(): boolean | null | undefined;
set store(value: boolean | null | undefined);
/**
* Validates that required environment variables are set.
*/
private static validateEnvironment;
/**
* Determines the system instruction message to use based on default and custom instructions.
*/
private handleSystemInstructionMessage;
/**
* Retrieves context messages from the default history and/or from persistent storage.
*/
private handleContextMessages;
/**
* Executes the functions called by the model and returns their responses.
*/
private callChosenFunctions;
/**
* Handles the process of calling tools based on the model's response
* and making a subsequent API call with the tool responses.
*/
private handleToolCompletion;
/**
* Creates a chat completion, handles tool calls (if any), and manages conversation history.
*
* @param {string} message - The user's message.
* @param {CreateChatCompletionOptions} [completionOptions] - Options for the chat completion, including custom parameters, tool choices, and history management.
* @returns {Promise<CompletionResult>} A promise that resolves to the chat completion result.
* @throws {ChatCompletionError | StorageError | RedisConnectionError | RedisKeyValidationError | MessageValidationError | DirectoryAccessError | FileReadError | FileImportError | InvalidToolError | ToolNotFoundError | ToolCompletionError | FunctionCallError | ValidationError} If an error occurs during the completion process.
*/
createChatCompletion(message: string, completionOptions?: CreateChatCompletionOptions): Promise<CompletionResult>;
/**
* Loads tool functions from the specified directory.
*
* @param {string} toolsDirPath - The path to the directory containing the tool functions.
* @returns {Promise<boolean>} A promise that resolves to true if the tools are loaded successfully.
* @throws {ValidationError} If the tools directory path is not provided or invalid.
*/
loadToolFuctions(toolsDirPath: string): Promise<boolean>;
/**
* Sets up the storage using a Redis client.
*
* @param {RedisClientType} client - The Redis client instance.
* @param {{ history: HistoryOptions }} [options] - Options for configuring history storage.
* @returns {Promise<boolean>} A promise that resolves to true if the storage is set up successfully.
* @throws {RedisConnectionError} If the Redis client is not provided.
*/
setStorage(client: RedisClientType, options?: {
history: HistoryOptions;
}): Promise<boolean>;
/**
* Deletes the chat history for a given user.
*
* @param {string} userId - The ID of the user whose history should be deleted.
* @returns {Promise<boolean>} A promise that resolves to true if the history is deleted successfully.
* @throws {RedisConnectionError} If the storage is not initialized.
*/
deleteChatHistory(userId: string): Promise<boolean>;
/**
* Retrieves the chat history for a given user.
*
* @param {string} userId - The ID of the user whose history should be retrieved.
* @param {HistoryOptions} [options] - Options for retrieving history.
* @returns {Promise<ChatCompletionMessageParam[]>} A promise that resolves to an array of chat messages.
* @throws {RedisConnectionError} If the storage is not initialized.
*/
getChatHistory(userId: string): Promise<ChatCompletionMessageParam[]>;
}
//# sourceMappingURL=agent.d.ts.map