add user and assistant agents

This commit is contained in:
Wlad Paiva
2023-10-11 19:28:26 -03:00
parent ec05fd4ad4
commit dfb07e56ee
7 changed files with 82 additions and 8 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
import {Message} from './types'
import {Message} from '../types'
/**
* (In preview) An abstract class for AI agent.
+43
View File
@@ -0,0 +1,43 @@
import {AIProvider} from '../providers/ai-provider'
import {
ConversableAgent,
type ConversableAgentConfig,
} from './conversable-agent'
type AssistantAgentConfig<T extends AIProvider<unknown>> = Omit<
ConversableAgentConfig<T>,
'humanInputMode' | 'codeExecutionConfig'
>
/**
* Assistant agent, designed to solve a task with LLM.
*
* AssistantAgent is a subclass of ConversableAgent configured with a default system message.
* The default system message is designed to solve a task with LLM,
* including suggesting python code blocks and debugging.
* `human_input_mode` is default to "NEVER"
* and `code_execution_config` is default to False.
* This agent doesn't execute code by default, and expects the user to execute the code.
*/
export class AssistantAgent<
T extends AIProvider<unknown>,
> extends ConversableAgent<T> {
/**
* Default system message for the AssistantAgent.
*/
static DEFAULT_SYSTEM_MESSAGE = `
You are a helpful AI assistant...
(rest of the message)
...Reply "TERMINATE" in the end when everything is done.
`
constructor(config: AssistantAgentConfig<T>) {
const {systemMessage = AssistantAgent.DEFAULT_SYSTEM_MESSAGE, ...rest} =
config
super({
systemMessage,
...rest,
})
}
}
@@ -1,7 +1,7 @@
import {beforeEach, expect, mock, test} from 'bun:test'
import OpenAI from 'openai'
import {AIProvider} from './ai'
import {AIProvider} from '../providers/ai-provider'
import {ConversableAgent} from './conversable-agent'
// HACK: Mock the AI provider.
@@ -1,9 +1,9 @@
import {OpenAIStream, StreamingTextResponse} from 'ai'
import OpenAI, {ClientOptions} from 'openai'
import {AIProvider} from '../providers/ai-provider'
import type {Callable, LlmConfig, Message, ReplyFunc, Role} from '../types'
import {Agent} from './agent'
import {AIProvider} from './ai'
import type {Callable, LlmConfig, Message, ReplyFunc, Role} from './types'
/**
* The model to use for the OpenAI API.
@@ -66,7 +66,7 @@ export class OpenAIProvider extends AIProvider<OpenAI> {
}
}
type ConversableAgentConfig<T extends AIProvider<unknown>> = {
export type ConversableAgentConfig<T extends AIProvider<unknown>> = {
/**
* The name of the agent.
*/
@@ -150,7 +150,7 @@ export class ConversableAgent<T extends AIProvider<unknown>> extends Agent {
name,
provider,
onMessageReceived,
systemMessage = 'You are a helpful AI Assistant.',
systemMessage = '',
defaultAutoReply = '',
} = config
+31
View File
@@ -0,0 +1,31 @@
import {AIProvider} from '../providers/ai-provider'
import {
ConversableAgent,
type ConversableAgentConfig,
} from './conversable-agent'
/**
* A proxy agent for the user, that can execute code and provide feedback to the other agents.
*
* UserProxyAgent is a subclass of ConversableAgent configured with `humanInputMode` to ALWAYS
* and `llm_config` to False. By default, the agent will prompt for human input every time a message is received.
* Code execution is enabled by default. LLM-based auto reply is disabled by default.
* To modify auto reply, register a method with `registerReply`.
* To modify the way to get human input, override `getHumanInput` method.
* To modify the way to execute code blocks, single code block, or function call, override `executeCodeBlocks`,
* `runCode`, and `executeFunction` methods respectively.
*/
export class UserProxyAgent<
T extends AIProvider<unknown>,
> extends ConversableAgent<T> {
constructor(config: ConversableAgentConfig<T>) {
const {
// humanInputMode = 'ALWAYS',
...rest
} = config
super({
...rest,
})
}
}
+1 -1
View File
@@ -1,4 +1,4 @@
import {Message} from './types'
import {Message} from '../types'
export abstract class AIProvider<T> {
private _client: T
+1 -1
View File
@@ -1,6 +1,6 @@
import type OpenAI from 'openai'
import type {Agent} from './agent'
import type {Agent} from './agents/agent'
/**
* OpenAI Chat API message.