ILLMClient & OpenAIAdapter
At the core of any AI agent's intelligence is its interaction with a Large Language Model (LLM). AgentB abstracts these interactions through the ILLMClient interface, allowing for flexibility in choosing and potentially switching LLM providers.
The ILLMClient Interface
ILLMClient InterfaceThe ILLMClient interface defines the standard contract for how agents communicate with an LLM. Any class that implements ILLMClient can be used by AgentB.
Key Methods:
generateResponse(messages: LLMMessage[], options: { ... }): Promise<LLMMessage | AsyncGenerator<LLMMessageChunk, void, unknown>>Purpose: This is the primary method for getting a response from the LLM.
messages: LLMMessage[]: An array ofLLMMessageobjects representing the conversation history and current prompt. TheLLMMessagestructure includes:role: 'system' | 'user' | 'assistant' | 'tool'content: string | Array<{type:'text',...} | {type:'image_url',...}>name?: Optional name, used fortoolrole or someassistantresponses.tool_calls?: Forassistantmessages, if the LLM requests tool usage.tool_call_id?: Fortoolmessages, linking back to atool_callsrequest.
options: { ... }: An object containing various parameters for the LLM call:model: string(Mandatory in options passed togenerateResponse): The specific model ID to use (e.g., "gpt-4o-mini").tools?: any[]: An array of tool definitions formatted specifically for the target LLM provider (e.g., OpenAI's function/tool format). TheILLMClientimplementation is expected to know how to use this.tool_choice?: LLMToolChoice: Controls how the LLM decides to use tools (e.g.,'auto','none','required', or forcing a specific function).stream?: boolean: Iftrue, the method returns anAsyncGeneratoryieldingLLMMessageChunkobjects for real-time streaming. Iffalseor undefined, it returns aPromise<LLMMessage>with the complete response.temperature?: number: Controls response randomness.max_tokens?: number: Maximum tokens for the LLM's generated response.systemPrompt?: string: Some adapters might allow a direct override or augmentation of the system message here, though typically the system message is part of themessagesarray.[otherOptions: string]: any: Allows passing additional provider-specific parameters.
Returns:
If
stream: true:AsyncGenerator<LLMMessageChunk, void, unknown>If
stream: false(or undefined):Promise<LLMMessage>
countTokens(messages: LLMMessage[], model: string): Promise<number>Purpose: Estimates the number of tokens that a given list of messages would consume for a specific model.
Crucial for
ContextManager: Helps manage the LLM's context window by determining if history needs to be summarized or truncated.Implementation Note: Accurate token counting is provider-specific. For OpenAI, this typically involves using a library like
tiktoken.
formatToolsForProvider(toolDefinitions: IToolDefinition[]): any[](Optional but highly recommended for adapters)Purpose: Converts an array of AgentB's generic
IToolDefinitionobjects into the specific format required by the LLM provider's API for tool/function calling.For example,
OpenAIAdapteruses this to transformIToolDefinitions into OpenAI's chat completion tool format.If an
ILLMClientimplementation doesn't provide this, the component callinggenerateResponse(likeBaseAgent) would need to format tools itself or rely on a default formatting mechanism.
OpenAIAdapter: The Default Implementation
OpenAIAdapter: The Default ImplementationAgentB comes with OpenAIAdapter, a concrete implementation of ILLMClient for interacting with OpenAI's Chat Completions API (including models like GPT-3.5-turbo, GPT-4, GPT-4o, GPT-4o-mini etc.).
Initialization (new OpenAIAdapter(options)):
Key Behaviors of OpenAIAdapter:
API Calls: Uses the official
openaiNode.js SDK.Message Mapping: Maps AgentB's
LLMMessageto OpenAI'sChatCompletionMessageParamstructure and vice-versa.Tool Formatting: Implements
formatToolsForProviderusingadaptToolDefinitionsToOpenAIto convertIToolDefinitioninto the JSON schema format OpenAI expects for functions/tools.Streaming: Correctly handles OpenAI's streaming API, yielding
LLMMessageChunkobjects that includecontentdeltas,tool_callsdeltas, andfinish_reason.Error Handling: Catches errors from the OpenAI SDK and re-throws them as standardized
LLMErrorobjects with more context.Token Counting (Basic): Currently provides a rough string-length-based estimate for
countTokens. For production accuracy with OpenAI, integratingtiktokendirectly or enhancing the adapter would be necessary.
Using ILLMClient in IAgentContext
ILLMClient in IAgentContextWhen an agent runs, its IAgentContext contains an llmClient property, which is an instance of an ILLMClient.
Extending with Other LLM Providers
To use a different LLM provider (e.g., Anthropic Claude, Google Gemini):
Create a New Adapter Class:
Use it in Configuration: When initializing
AgentBorApiInteractionManager, provide an instance of your custom adapter:
The ILLMClient interface ensures that the rest of the AgentB framework (agents, context manager, etc.) can operate consistently, regardless of the underlying LLM provider, as long as the adapter correctly implements the contract.
Last updated