@cloudbase/agent-adapter-llm
v0.0.24
Published
Direct LLM adapter for AG-Kit agents (OpenAI & Anthropic)
Readme
@cloudbase/agent-adapter-llm
Direct LLM adapter for AG-Kit agents with support for OpenAI and Anthropic models.
Features
- ✅ Direct OpenAI and Anthropic model integration
- ✅ Automatic streaming message conversion to AGUI events
- ✅ Support for tool calls (function calling)
- ✅ Type-safe with TypeScript
- ✅ Observable-based event streaming
- ✅ No additional abstraction layers
Installation
npm install @cloudbase/agent-adapter-llm openai @anthropic-ai/sdkQuick Start
With OpenAI
import OpenAI from 'openai';
import { LLMAgent } from '@cloudbase/agent-adapter-llm';
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
const agent = new LLMAgent({
agentId: 'my-agent',
model: openai,
modelName: 'gpt-4o-mini',
systemPrompt: 'You are a helpful assistant.',
temperature: 0.7,
maxTokens: 2000
});
const observable = agent.run({
messages: [
{ id: 'msg_1', role: 'user', content: 'Hello!' }
],
runId: 'run_123',
threadId: 'thread_456',
tools: []
});
observable.subscribe({
next: (event) => {
console.log('Event:', event);
}
});With Anthropic
import Anthropic from '@anthropic-ai/sdk';
import { LLMAgent } from '@cloudbase/agent-adapter-llm';
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY
});
const agent = new LLMAgent({
agentId: 'my-agent',
model: anthropic,
modelName: 'claude-3-5-sonnet-20241022',
systemPrompt: 'You are a helpful assistant.',
temperature: 0.7,
maxTokens: 2000
});AGUI Events
The agent emits the following AGUI protocol events:
RUN_STARTED- Run has startedTEXT_MESSAGE_START- Text message streaming startedTEXT_MESSAGE_CONTENT- Text content deltaTEXT_MESSAGE_END- Text message streaming endedTOOL_CALL_START- Tool call startedTOOL_CALL_ARGS- Tool call arguments deltaTOOL_CALL_END- Tool call endedRUN_FINISHED- Run completed successfullyRUN_ERROR- Run encountered an error
Configuration
LLMAgentConfig
interface LLMAgentConfig {
agentId: string; // Unique agent identifier
name?: string; // Agent name
description?: string; // Agent description
model: OpenAI | Anthropic; // Model provider instance
modelName: string; // Model name (e.g., 'gpt-4o-mini', 'claude-3-5-sonnet-20241022')
systemPrompt?: string; // System prompt
temperature?: number; // Temperature (0-2)
maxTokens?: number; // Max tokens to generate
threadId?: string; // Thread ID
}Streaming Example
import { EventType } from '@ag-ui/client';
observable.subscribe({
next: (event) => {
switch (event.type) {
case EventType.TEXT_MESSAGE_CONTENT:
process.stdout.write(event.delta);
break;
case EventType.TOOL_CALL_START:
console.log(`\nTool: ${event.toolCallName}`);
break;
case EventType.RUN_FINISHED:
console.log('\nDone!');
break;
}
}
});Tool Calling
const tools = [
{
name: 'get_weather',
description: 'Get the current weather',
parameters: JSON.stringify({
type: 'object',
properties: {
location: { type: 'string' }
},
required: ['location']
})
}
];
agent.run({ messages, runId, threadId, tools });License
MIT
