koishi-plugin-chatluna 1.0.0-beta.21 → 1.0.0-beta.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/config.js +1 -1
- package/lib/index.js +12 -12
- package/lib/llm-core/agent/index.d.ts +69 -0
- package/lib/llm-core/agent/index.js +159 -0
- package/lib/llm-core/agent/openai/index.d.ts +69 -0
- package/lib/llm-core/agent/openai/index.js +171 -0
- package/lib/llm-core/agent/openai/output_parser.d.ts +59 -0
- package/lib/llm-core/agent/openai/output_parser.js +139 -0
- package/lib/llm-core/agent/openai/prompt.d.ts +2 -0
- package/lib/llm-core/agent/openai/prompt.js +5 -0
- package/lib/llm-core/agent/output_parser.d.ts +59 -0
- package/lib/llm-core/agent/output_parser.js +135 -0
- package/lib/llm-core/agent/prompt.d.ts +2 -0
- package/lib/llm-core/agent/prompt.js +5 -0
- package/lib/llm-core/agent/types.d.ts +135 -0
- package/lib/llm-core/agent/types.js +2 -0
- package/lib/llm-core/chain/browsing_chain.d.ts +35 -0
- package/lib/llm-core/chain/browsing_chain.js +195 -0
- package/lib/llm-core/chain/plugin_chat_chain.js +19 -22
- package/lib/llm-core/chain/prompt.d.ts +3 -31
- package/lib/llm-core/chain/prompt.js +7 -183
- package/lib/llm-core/chat/app.js +8 -1
- package/lib/llm-core/chat/default.js +9 -17
- package/lib/llm-core/memory/message/database_memory.d.ts +2 -2
- package/lib/llm-core/memory/message/database_memory.js +12 -8
- package/lib/llm-core/platform/client.d.ts +1 -0
- package/lib/llm-core/platform/config.d.ts +1 -1
- package/lib/llm-core/platform/config.js +6 -3
- package/lib/llm-core/platform/model.d.ts +6 -1
- package/lib/llm-core/platform/model.js +59 -7
- package/lib/llm-core/platform/service.d.ts +5 -3
- package/lib/llm-core/platform/service.js +17 -11
- package/lib/llm-core/platform/types.d.ts +4 -3
- package/lib/llm-core/utils/count_tokens.js +5 -13
- package/lib/renders/image.js +1 -1
- package/lib/renders/mixed-image.js +1 -1
- package/lib/utils/sse.d.ts +1 -1
- package/lib/utils/sse.js +9 -2
- package/package.json +97 -97
- package/resources/out.html +18 -3
- package/resources/template.html +1 -1
package/lib/config.js
CHANGED
|
@@ -45,7 +45,7 @@ exports.Config = koishi_1.Schema.intersect([
|
|
|
45
45
|
.description('分割消息发送(看起来更像普通水友(并且会不支持引用消息,不支持原始模式和图片模式。开启流式响应后启用该项会进行更细化的分割消息))')
|
|
46
46
|
.default(false),
|
|
47
47
|
censor: koishi_1.Schema.boolean()
|
|
48
|
-
.description('文本审核服务(需要安装 censor
|
|
48
|
+
.description('文本审核服务(需要安装 censor 服务)')
|
|
49
49
|
.default(false),
|
|
50
50
|
sendThinkingMessage: koishi_1.Schema.boolean()
|
|
51
51
|
.description('发送等待消息,在请求时会发送这条消息')
|
package/lib/index.js
CHANGED
|
@@ -41,17 +41,17 @@ exports.inject = {
|
|
|
41
41
|
required: ['cache', 'database'],
|
|
42
42
|
optional: ['censor', 'vits', 'puppeteer']
|
|
43
43
|
};
|
|
44
|
-
exports.usage = `
|
|
45
|
-
## chatluna v1.0 alpha
|
|
46
|
-
|
|
47
|
-
### 目前插件还在 alpha 阶段,可能会有很多 bug,可以去插件主页那边提 issue 或加群反馈。
|
|
48
|
-
|
|
49
|
-
ChatLuna 插件交流群:282381753 (有问题不知道怎么弄先加群问)
|
|
50
|
-
|
|
51
|
-
群里目前可能有搭载了该插件的 bot,加群的话最好是来询问问题或者提出意见的
|
|
52
|
-
|
|
53
|
-
[文档](https://chatluna.dingyi222666.top/) 也在缓慢制作中,有问题可以在群里提出
|
|
54
|
-
|
|
44
|
+
exports.usage = `
|
|
45
|
+
## chatluna v1.0 alpha
|
|
46
|
+
|
|
47
|
+
### 目前插件还在 alpha 阶段,可能会有很多 bug,可以去插件主页那边提 issue 或加群反馈。
|
|
48
|
+
|
|
49
|
+
ChatLuna 插件交流群:282381753 (有问题不知道怎么弄先加群问)
|
|
50
|
+
|
|
51
|
+
群里目前可能有搭载了该插件的 bot,加群的话最好是来询问问题或者提出意见的
|
|
52
|
+
|
|
53
|
+
[文档](https://chatluna.dingyi222666.top/) 也在缓慢制作中,有问题可以在群里提出
|
|
54
|
+
|
|
55
55
|
`;
|
|
56
56
|
function apply(ctx, config) {
|
|
57
57
|
exports.logger = (0, logger_1.createLogger)(ctx);
|
|
@@ -70,9 +70,9 @@ function apply(ctx, config) {
|
|
|
70
70
|
disposables.push(ctx.plugin({
|
|
71
71
|
apply: (ctx, config) => {
|
|
72
72
|
ctx.on('ready', async () => {
|
|
73
|
+
await (0, default_1.defaultFactory)(ctx, ctx.chatluna.platform);
|
|
73
74
|
await (0, middleware_1.middleware)(ctx, config);
|
|
74
75
|
await (0, command_1.command)(ctx, config);
|
|
75
|
-
await (0, default_1.defaultFactory)(ctx, ctx.chatluna.platform);
|
|
76
76
|
await ctx.chatluna.preset.loadAllPreset();
|
|
77
77
|
});
|
|
78
78
|
ctx.middleware(async (session, next) => {
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { AgentAction, AgentFinish, AgentStep, BaseMessage, ChainValues, SystemMessage } from 'langchain/schema';
|
|
2
|
+
import { OpenAIFunctionsAgentOutputParser, OpenAIToolsAgentOutputParser } from './output_parser';
|
|
3
|
+
import { Agent, AgentArgs, AgentInput } from 'langchain/agents';
|
|
4
|
+
import { CallbackManager } from 'langchain/callbacks';
|
|
5
|
+
import { BaseLanguageModel } from 'langchain/base_language';
|
|
6
|
+
import { BasePromptTemplate } from 'langchain/prompts';
|
|
7
|
+
import { StructuredTool } from 'langchain/tools';
|
|
8
|
+
export declare function _formatIntermediateSteps(intermediateSteps: AgentStep[]): BaseMessage[];
|
|
9
|
+
/**
|
|
10
|
+
* Interface for the input data required to create an OpenAIAgent.
|
|
11
|
+
*/
|
|
12
|
+
export interface OpenAIAgentInput extends AgentInput {
|
|
13
|
+
tools: StructuredTool[];
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Interface for the arguments required to create a prompt for an
|
|
17
|
+
* OpenAIAgent.
|
|
18
|
+
*/
|
|
19
|
+
export interface OpenAIAgentCreatePromptArgs {
|
|
20
|
+
prefix?: string;
|
|
21
|
+
systemMessage?: SystemMessage;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Class representing an agent for the OpenAI chat model in LangChain. It
|
|
25
|
+
* extends the Agent class and provides additional functionality specific
|
|
26
|
+
* to the OpenAIAgent type.
|
|
27
|
+
*/
|
|
28
|
+
export declare class OpenAIAgent extends Agent {
|
|
29
|
+
static lc_name(): string;
|
|
30
|
+
lc_namespace: string[];
|
|
31
|
+
_agentType(): "openai-functions";
|
|
32
|
+
observationPrefix(): string;
|
|
33
|
+
llmPrefix(): string;
|
|
34
|
+
_stop(): string[];
|
|
35
|
+
tools: StructuredTool[];
|
|
36
|
+
outputParser: OpenAIToolsAgentOutputParser | OpenAIFunctionsAgentOutputParser | any;
|
|
37
|
+
constructor(input: Omit<OpenAIAgentInput, 'outputParser'>);
|
|
38
|
+
/**
|
|
39
|
+
* Creates a prompt for the OpenAIAgent using the provided tools and
|
|
40
|
+
* fields.
|
|
41
|
+
* @param _tools The tools to be used in the prompt.
|
|
42
|
+
* @param fields Optional fields for creating the prompt.
|
|
43
|
+
* @returns A BasePromptTemplate object representing the created prompt.
|
|
44
|
+
*/
|
|
45
|
+
static createPrompt(_tools: StructuredTool[], fields?: OpenAIAgentCreatePromptArgs): BasePromptTemplate;
|
|
46
|
+
/**
|
|
47
|
+
* Creates an OpenAIAgent from a BaseLanguageModel and a list of tools.
|
|
48
|
+
* @param llm The BaseLanguageModel to use.
|
|
49
|
+
* @param tools The tools to be used by the agent.
|
|
50
|
+
* @param args Optional arguments for creating the agent.
|
|
51
|
+
* @returns An instance of OpenAIAgent.
|
|
52
|
+
*/
|
|
53
|
+
static fromLLMAndTools(llm: BaseLanguageModel, tools: StructuredTool[], args?: OpenAIAgentCreatePromptArgs & Pick<AgentArgs, 'callbacks'>): OpenAIAgent;
|
|
54
|
+
/**
|
|
55
|
+
* Constructs a scratch pad from a list of agent steps.
|
|
56
|
+
* @param steps The steps to include in the scratch pad.
|
|
57
|
+
* @returns A string or a list of BaseMessages representing the constructed scratch pad.
|
|
58
|
+
*/
|
|
59
|
+
constructScratchPad(steps: AgentStep[]): Promise<string | BaseMessage[]>;
|
|
60
|
+
/**
|
|
61
|
+
* Plans the next action or finish state of the agent based on the
|
|
62
|
+
* provided steps, inputs, and optional callback manager.
|
|
63
|
+
* @param steps The steps to consider in planning.
|
|
64
|
+
* @param inputs The inputs to consider in planning.
|
|
65
|
+
* @param callbackManager Optional CallbackManager to use in planning.
|
|
66
|
+
* @returns A Promise that resolves to an AgentAction or AgentFinish object representing the planned action or finish state.
|
|
67
|
+
*/
|
|
68
|
+
plan(steps: AgentStep[], inputs: ChainValues, callbackManager?: CallbackManager): Promise<AgentAction | AgentFinish>;
|
|
69
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OpenAIAgent = exports._formatIntermediateSteps = void 0;
|
|
4
|
+
const schema_1 = require("langchain/schema");
|
|
5
|
+
const output_parser_1 = require("./output_parser");
|
|
6
|
+
const agents_1 = require("langchain/agents");
|
|
7
|
+
const chains_1 = require("langchain/chains");
|
|
8
|
+
const prompts_1 = require("langchain/prompts");
|
|
9
|
+
const prompt_1 = require("./prompt");
|
|
10
|
+
/**
|
|
11
|
+
* Checks if the given action is a FunctionsAgentAction.
|
|
12
|
+
* @param action The action to check.
|
|
13
|
+
* @returns True if the action is a FunctionsAgentAction, false otherwise.
|
|
14
|
+
*/
|
|
15
|
+
function isFunctionsAgentAction(action) {
|
|
16
|
+
return action.messageLog !== undefined;
|
|
17
|
+
}
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
19
|
+
function _convertAgentStepToMessages(action, observation) {
|
|
20
|
+
if (isFunctionsAgentAction(action) && action.messageLog !== undefined) {
|
|
21
|
+
return action.messageLog?.concat(new schema_1.FunctionMessage(observation, action.tool));
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
return [new schema_1.AIMessage(action.log)];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
28
|
+
function _formatIntermediateSteps(intermediateSteps) {
|
|
29
|
+
return intermediateSteps.flatMap(({ action, observation }) => _convertAgentStepToMessages(action, observation));
|
|
30
|
+
}
|
|
31
|
+
exports._formatIntermediateSteps = _formatIntermediateSteps;
|
|
32
|
+
/**
|
|
33
|
+
* Class representing an agent for the OpenAI chat model in LangChain. It
|
|
34
|
+
* extends the Agent class and provides additional functionality specific
|
|
35
|
+
* to the OpenAIAgent type.
|
|
36
|
+
*/
|
|
37
|
+
class OpenAIAgent extends agents_1.Agent {
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
39
|
+
static lc_name() {
|
|
40
|
+
return 'OpenAIAgent';
|
|
41
|
+
}
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
43
|
+
lc_namespace = ['langchain', 'agents', 'openai'];
|
|
44
|
+
_agentType() {
|
|
45
|
+
return 'openai-functions';
|
|
46
|
+
}
|
|
47
|
+
observationPrefix() {
|
|
48
|
+
return 'Observation: ';
|
|
49
|
+
}
|
|
50
|
+
llmPrefix() {
|
|
51
|
+
return 'Thought:';
|
|
52
|
+
}
|
|
53
|
+
_stop() {
|
|
54
|
+
return ['Observation:'];
|
|
55
|
+
}
|
|
56
|
+
tools;
|
|
57
|
+
outputParser = new output_parser_1.OpenAIFunctionsAgentOutputParser();
|
|
58
|
+
constructor(input) {
|
|
59
|
+
super({ ...input, outputParser: undefined });
|
|
60
|
+
this.tools = input.tools;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Creates a prompt for the OpenAIAgent using the provided tools and
|
|
64
|
+
* fields.
|
|
65
|
+
* @param _tools The tools to be used in the prompt.
|
|
66
|
+
* @param fields Optional fields for creating the prompt.
|
|
67
|
+
* @returns A BasePromptTemplate object representing the created prompt.
|
|
68
|
+
*/
|
|
69
|
+
static createPrompt(_tools, fields) {
|
|
70
|
+
const { prefix = prompt_1.PREFIX } = fields || {};
|
|
71
|
+
return prompts_1.ChatPromptTemplate.fromMessages([
|
|
72
|
+
prompts_1.SystemMessagePromptTemplate.fromTemplate(prefix),
|
|
73
|
+
new prompts_1.MessagesPlaceholder('chat_history'),
|
|
74
|
+
prompts_1.HumanMessagePromptTemplate.fromTemplate('{input}'),
|
|
75
|
+
new prompts_1.MessagesPlaceholder('agent_scratchpad')
|
|
76
|
+
]);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Creates an OpenAIAgent from a BaseLanguageModel and a list of tools.
|
|
80
|
+
* @param llm The BaseLanguageModel to use.
|
|
81
|
+
* @param tools The tools to be used by the agent.
|
|
82
|
+
* @param args Optional arguments for creating the agent.
|
|
83
|
+
* @returns An instance of OpenAIAgent.
|
|
84
|
+
*/
|
|
85
|
+
static fromLLMAndTools(llm, tools, args) {
|
|
86
|
+
OpenAIAgent.validateTools(tools);
|
|
87
|
+
/* if (
|
|
88
|
+
llm._modelType() !== 'base_chat_model' ||
|
|
89
|
+
llm._llmType() !== 'openai'
|
|
90
|
+
) {
|
|
91
|
+
throw new Error('OpenAIAgent requires an OpenAI chat model')
|
|
92
|
+
} */
|
|
93
|
+
const prompt = OpenAIAgent.createPrompt(tools, args);
|
|
94
|
+
const chain = new chains_1.LLMChain({
|
|
95
|
+
prompt,
|
|
96
|
+
llm,
|
|
97
|
+
callbacks: args?.callbacks
|
|
98
|
+
});
|
|
99
|
+
return new OpenAIAgent({
|
|
100
|
+
llmChain: chain,
|
|
101
|
+
allowedTools: tools.map((t) => t.name),
|
|
102
|
+
tools
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Constructs a scratch pad from a list of agent steps.
|
|
107
|
+
* @param steps The steps to include in the scratch pad.
|
|
108
|
+
* @returns A string or a list of BaseMessages representing the constructed scratch pad.
|
|
109
|
+
*/
|
|
110
|
+
async constructScratchPad(steps) {
|
|
111
|
+
return _formatIntermediateSteps(steps);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Plans the next action or finish state of the agent based on the
|
|
115
|
+
* provided steps, inputs, and optional callback manager.
|
|
116
|
+
* @param steps The steps to consider in planning.
|
|
117
|
+
* @param inputs The inputs to consider in planning.
|
|
118
|
+
* @param callbackManager Optional CallbackManager to use in planning.
|
|
119
|
+
* @returns A Promise that resolves to an AgentAction or AgentFinish object representing the planned action or finish state.
|
|
120
|
+
*/
|
|
121
|
+
async plan(steps, inputs, callbackManager) {
|
|
122
|
+
// Add scratchpad and stop to inputs
|
|
123
|
+
const thoughts = await this.constructScratchPad(steps);
|
|
124
|
+
const newInputs = {
|
|
125
|
+
...inputs,
|
|
126
|
+
agent_scratchpad: thoughts
|
|
127
|
+
};
|
|
128
|
+
if (this._stop().length !== 0) {
|
|
129
|
+
newInputs.stop = this._stop();
|
|
130
|
+
}
|
|
131
|
+
// Split inputs between prompt and llm
|
|
132
|
+
const llm = this.llmChain.llm;
|
|
133
|
+
const valuesForPrompt = { ...newInputs };
|
|
134
|
+
const valuesForLLM = {
|
|
135
|
+
tools: this.tools
|
|
136
|
+
};
|
|
137
|
+
const callKeys = 'callKeys' in this.llmChain.llm ? this.llmChain.llm.callKeys : [];
|
|
138
|
+
for (const key of callKeys) {
|
|
139
|
+
if (key in inputs) {
|
|
140
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
141
|
+
valuesForLLM[key] =
|
|
142
|
+
inputs[key];
|
|
143
|
+
delete valuesForPrompt[key];
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const promptValue = await this.llmChain.prompt.formatPromptValue(valuesForPrompt);
|
|
147
|
+
const message = await llm.predictMessages(promptValue.toChatMessages(), valuesForLLM, callbackManager);
|
|
148
|
+
if (message.additional_kwargs.tool_calls &&
|
|
149
|
+
this.outputParser instanceof output_parser_1.OpenAIFunctionsAgentOutputParser) {
|
|
150
|
+
this.outputParser = new output_parser_1.OpenAIToolsAgentOutputParser();
|
|
151
|
+
}
|
|
152
|
+
else if (message.additional_kwargs.function_call &&
|
|
153
|
+
this.outputParser instanceof output_parser_1.OpenAIToolsAgentOutputParser) {
|
|
154
|
+
this.outputParser = new output_parser_1.OpenAIFunctionsAgentOutputParser();
|
|
155
|
+
}
|
|
156
|
+
return this.outputParser.parseAIMessage(message);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
exports.OpenAIAgent = OpenAIAgent;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { AgentAction, AgentFinish, AgentStep, BaseMessage, ChainValues, SystemMessage } from 'langchain/schema';
|
|
2
|
+
import { OpenAIFunctionsAgentOutputParser, OpenAIToolsAgentOutputParser } from './output_parser';
|
|
3
|
+
import { Agent, AgentArgs, AgentInput } from 'langchain/agents';
|
|
4
|
+
import { CallbackManager } from 'langchain/callbacks';
|
|
5
|
+
import { BaseLanguageModel } from 'langchain/base_language';
|
|
6
|
+
import { BasePromptTemplate } from 'langchain/prompts';
|
|
7
|
+
import { StructuredTool } from 'langchain/tools';
|
|
8
|
+
export declare function _formatIntermediateSteps(intermediateSteps: AgentStep[]): BaseMessage[];
|
|
9
|
+
/**
|
|
10
|
+
* Interface for the input data required to create an OpenAIAgent.
|
|
11
|
+
*/
|
|
12
|
+
export interface OpenAIAgentInput extends AgentInput {
|
|
13
|
+
tools: StructuredTool[];
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Interface for the arguments required to create a prompt for an
|
|
17
|
+
* OpenAIAgent.
|
|
18
|
+
*/
|
|
19
|
+
export interface OpenAIAgentCreatePromptArgs {
|
|
20
|
+
prefix?: string;
|
|
21
|
+
systemMessage?: SystemMessage;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Class representing an agent for the OpenAI chat model in LangChain. It
|
|
25
|
+
* extends the Agent class and provides additional functionality specific
|
|
26
|
+
* to the OpenAIAgent type.
|
|
27
|
+
*/
|
|
28
|
+
export declare class OpenAIAgent extends Agent {
|
|
29
|
+
static lc_name(): string;
|
|
30
|
+
lc_namespace: string[];
|
|
31
|
+
_agentType(): "openai-functions";
|
|
32
|
+
observationPrefix(): string;
|
|
33
|
+
llmPrefix(): string;
|
|
34
|
+
_stop(): string[];
|
|
35
|
+
tools: StructuredTool[];
|
|
36
|
+
outputParser: OpenAIToolsAgentOutputParser | OpenAIFunctionsAgentOutputParser | any;
|
|
37
|
+
constructor(input: Omit<OpenAIAgentInput, 'outputParser'>);
|
|
38
|
+
/**
|
|
39
|
+
* Creates a prompt for the OpenAIAgent using the provided tools and
|
|
40
|
+
* fields.
|
|
41
|
+
* @param _tools The tools to be used in the prompt.
|
|
42
|
+
* @param fields Optional fields for creating the prompt.
|
|
43
|
+
* @returns A BasePromptTemplate object representing the created prompt.
|
|
44
|
+
*/
|
|
45
|
+
static createPrompt(_tools: StructuredTool[], fields?: OpenAIAgentCreatePromptArgs): BasePromptTemplate;
|
|
46
|
+
/**
|
|
47
|
+
* Creates an OpenAIAgent from a BaseLanguageModel and a list of tools.
|
|
48
|
+
* @param llm The BaseLanguageModel to use.
|
|
49
|
+
* @param tools The tools to be used by the agent.
|
|
50
|
+
* @param args Optional arguments for creating the agent.
|
|
51
|
+
* @returns An instance of OpenAIAgent.
|
|
52
|
+
*/
|
|
53
|
+
static fromLLMAndTools(llm: BaseLanguageModel, tools: StructuredTool[], args?: OpenAIAgentCreatePromptArgs & Pick<AgentArgs, 'callbacks'>): OpenAIAgent;
|
|
54
|
+
/**
|
|
55
|
+
* Constructs a scratch pad from a list of agent steps.
|
|
56
|
+
* @param steps The steps to include in the scratch pad.
|
|
57
|
+
* @returns A string or a list of BaseMessages representing the constructed scratch pad.
|
|
58
|
+
*/
|
|
59
|
+
constructScratchPad(steps: AgentStep[]): Promise<string | BaseMessage[]>;
|
|
60
|
+
/**
|
|
61
|
+
* Plans the next action or finish state of the agent based on the
|
|
62
|
+
* provided steps, inputs, and optional callback manager.
|
|
63
|
+
* @param steps The steps to consider in planning.
|
|
64
|
+
* @param inputs The inputs to consider in planning.
|
|
65
|
+
* @param callbackManager Optional CallbackManager to use in planning.
|
|
66
|
+
* @returns A Promise that resolves to an AgentAction or AgentFinish object representing the planned action or finish state.
|
|
67
|
+
*/
|
|
68
|
+
plan(steps: AgentStep[], inputs: ChainValues, callbackManager?: CallbackManager): Promise<AgentAction | AgentFinish>;
|
|
69
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OpenAIAgent = exports._formatIntermediateSteps = void 0;
|
|
4
|
+
const schema_1 = require("langchain/schema");
|
|
5
|
+
const output_parser_1 = require("./output_parser");
|
|
6
|
+
const agents_1 = require("langchain/agents");
|
|
7
|
+
const chains_1 = require("langchain/chains");
|
|
8
|
+
const prompts_1 = require("langchain/prompts");
|
|
9
|
+
const prompt_1 = require("./prompt");
|
|
10
|
+
/**
|
|
11
|
+
* Checks if the given action is a FunctionsAgentAction.
|
|
12
|
+
* @param action The action to check.
|
|
13
|
+
* @returns True if the action is a FunctionsAgentAction, false otherwise.
|
|
14
|
+
*/
|
|
15
|
+
function isFunctionsAgentAction(action) {
|
|
16
|
+
return action.messageLog !== undefined;
|
|
17
|
+
}
|
|
18
|
+
function isToolsAgentAction(action) {
|
|
19
|
+
return action.toolCallId !== undefined;
|
|
20
|
+
}
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
22
|
+
function _convertAgentStepToMessages(action, observation) {
|
|
23
|
+
if (isToolsAgentAction(action) && action.toolCallId !== undefined) {
|
|
24
|
+
const log = action.messageLog;
|
|
25
|
+
return log.concat(new schema_1.ToolMessage({
|
|
26
|
+
content: observation,
|
|
27
|
+
name: action.tool,
|
|
28
|
+
tool_call_id: action.toolCallId
|
|
29
|
+
}));
|
|
30
|
+
}
|
|
31
|
+
else if (isFunctionsAgentAction(action) &&
|
|
32
|
+
action.messageLog !== undefined) {
|
|
33
|
+
return action.messageLog?.concat(new schema_1.FunctionMessage(observation, action.tool));
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
return [new schema_1.AIMessage(action.log)];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
40
|
+
function _formatIntermediateSteps(intermediateSteps) {
|
|
41
|
+
return intermediateSteps.flatMap(({ action, observation }) => _convertAgentStepToMessages(action, observation));
|
|
42
|
+
}
|
|
43
|
+
exports._formatIntermediateSteps = _formatIntermediateSteps;
|
|
44
|
+
/**
|
|
45
|
+
* Class representing an agent for the OpenAI chat model in LangChain. It
|
|
46
|
+
* extends the Agent class and provides additional functionality specific
|
|
47
|
+
* to the OpenAIAgent type.
|
|
48
|
+
*/
|
|
49
|
+
class OpenAIAgent extends agents_1.Agent {
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
51
|
+
static lc_name() {
|
|
52
|
+
return 'OpenAIAgent';
|
|
53
|
+
}
|
|
54
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
55
|
+
lc_namespace = ['langchain', 'agents', 'openai'];
|
|
56
|
+
_agentType() {
|
|
57
|
+
return 'openai-functions';
|
|
58
|
+
}
|
|
59
|
+
observationPrefix() {
|
|
60
|
+
return 'Observation: ';
|
|
61
|
+
}
|
|
62
|
+
llmPrefix() {
|
|
63
|
+
return 'Thought:';
|
|
64
|
+
}
|
|
65
|
+
_stop() {
|
|
66
|
+
return ['Observation:'];
|
|
67
|
+
}
|
|
68
|
+
tools;
|
|
69
|
+
outputParser = new output_parser_1.OpenAIFunctionsAgentOutputParser();
|
|
70
|
+
constructor(input) {
|
|
71
|
+
super({ ...input, outputParser: undefined });
|
|
72
|
+
this.tools = input.tools;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Creates a prompt for the OpenAIAgent using the provided tools and
|
|
76
|
+
* fields.
|
|
77
|
+
* @param _tools The tools to be used in the prompt.
|
|
78
|
+
* @param fields Optional fields for creating the prompt.
|
|
79
|
+
* @returns A BasePromptTemplate object representing the created prompt.
|
|
80
|
+
*/
|
|
81
|
+
static createPrompt(_tools, fields) {
|
|
82
|
+
const { prefix = prompt_1.PREFIX } = fields || {};
|
|
83
|
+
return prompts_1.ChatPromptTemplate.fromMessages([
|
|
84
|
+
prompts_1.SystemMessagePromptTemplate.fromTemplate(prefix),
|
|
85
|
+
new prompts_1.MessagesPlaceholder('chat_history'),
|
|
86
|
+
prompts_1.HumanMessagePromptTemplate.fromTemplate('{input}'),
|
|
87
|
+
new prompts_1.MessagesPlaceholder('agent_scratchpad')
|
|
88
|
+
]);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Creates an OpenAIAgent from a BaseLanguageModel and a list of tools.
|
|
92
|
+
* @param llm The BaseLanguageModel to use.
|
|
93
|
+
* @param tools The tools to be used by the agent.
|
|
94
|
+
* @param args Optional arguments for creating the agent.
|
|
95
|
+
* @returns An instance of OpenAIAgent.
|
|
96
|
+
*/
|
|
97
|
+
static fromLLMAndTools(llm, tools, args) {
|
|
98
|
+
OpenAIAgent.validateTools(tools);
|
|
99
|
+
/* if (
|
|
100
|
+
llm._modelType() !== 'base_chat_model' ||
|
|
101
|
+
llm._llmType() !== 'openai'
|
|
102
|
+
) {
|
|
103
|
+
throw new Error('OpenAIAgent requires an OpenAI chat model')
|
|
104
|
+
} */
|
|
105
|
+
const prompt = OpenAIAgent.createPrompt(tools, args);
|
|
106
|
+
const chain = new chains_1.LLMChain({
|
|
107
|
+
prompt,
|
|
108
|
+
llm,
|
|
109
|
+
callbacks: args?.callbacks
|
|
110
|
+
});
|
|
111
|
+
return new OpenAIAgent({
|
|
112
|
+
llmChain: chain,
|
|
113
|
+
allowedTools: tools.map((t) => t.name),
|
|
114
|
+
tools
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Constructs a scratch pad from a list of agent steps.
|
|
119
|
+
* @param steps The steps to include in the scratch pad.
|
|
120
|
+
* @returns A string or a list of BaseMessages representing the constructed scratch pad.
|
|
121
|
+
*/
|
|
122
|
+
async constructScratchPad(steps) {
|
|
123
|
+
return _formatIntermediateSteps(steps);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Plans the next action or finish state of the agent based on the
|
|
127
|
+
* provided steps, inputs, and optional callback manager.
|
|
128
|
+
* @param steps The steps to consider in planning.
|
|
129
|
+
* @param inputs The inputs to consider in planning.
|
|
130
|
+
* @param callbackManager Optional CallbackManager to use in planning.
|
|
131
|
+
* @returns A Promise that resolves to an AgentAction or AgentFinish object representing the planned action or finish state.
|
|
132
|
+
*/
|
|
133
|
+
async plan(steps, inputs, callbackManager) {
|
|
134
|
+
// Add scratchpad and stop to inputs
|
|
135
|
+
const thoughts = await this.constructScratchPad(steps);
|
|
136
|
+
const newInputs = {
|
|
137
|
+
...inputs,
|
|
138
|
+
agent_scratchpad: thoughts
|
|
139
|
+
};
|
|
140
|
+
if (this._stop().length !== 0) {
|
|
141
|
+
newInputs.stop = this._stop();
|
|
142
|
+
}
|
|
143
|
+
// Split inputs between prompt and llm
|
|
144
|
+
const llm = this.llmChain.llm;
|
|
145
|
+
const valuesForPrompt = { ...newInputs };
|
|
146
|
+
const valuesForLLM = {
|
|
147
|
+
tools: this.tools
|
|
148
|
+
};
|
|
149
|
+
const callKeys = 'callKeys' in this.llmChain.llm ? this.llmChain.llm.callKeys : [];
|
|
150
|
+
for (const key of callKeys) {
|
|
151
|
+
if (key in inputs) {
|
|
152
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
153
|
+
valuesForLLM[key] =
|
|
154
|
+
inputs[key];
|
|
155
|
+
delete valuesForPrompt[key];
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const promptValue = await this.llmChain.prompt.formatPromptValue(valuesForPrompt);
|
|
159
|
+
const message = await llm.predictMessages(promptValue.toChatMessages(), valuesForLLM, callbackManager);
|
|
160
|
+
if (message.additional_kwargs.tool_calls &&
|
|
161
|
+
this.outputParser instanceof output_parser_1.OpenAIFunctionsAgentOutputParser) {
|
|
162
|
+
this.outputParser = new output_parser_1.OpenAIToolsAgentOutputParser();
|
|
163
|
+
}
|
|
164
|
+
else if (message.additional_kwargs.function_call &&
|
|
165
|
+
this.outputParser instanceof output_parser_1.OpenAIToolsAgentOutputParser) {
|
|
166
|
+
this.outputParser = new output_parser_1.OpenAIFunctionsAgentOutputParser();
|
|
167
|
+
}
|
|
168
|
+
return this.outputParser.parseAIMessage(message);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
exports.OpenAIAgent = OpenAIAgent;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { AgentAction, AgentFinish, AgentStep, BaseMessage, ChatGeneration } from 'langchain/schema';
|
|
2
|
+
import { BaseOutputParser } from 'langchain/schema/output_parser';
|
|
3
|
+
/**
|
|
4
|
+
* Type that represents an agent action with an optional message log.
|
|
5
|
+
*/
|
|
6
|
+
export type FunctionsAgentAction = AgentAction & {
|
|
7
|
+
messageLog?: BaseMessage[];
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Abstract class representing an output parser specifically for agent
|
|
11
|
+
* actions and finishes in LangChain. It extends the `BaseOutputParser`
|
|
12
|
+
* class.
|
|
13
|
+
*/
|
|
14
|
+
export declare abstract class AgentActionOutputParser extends BaseOutputParser<AgentAction | AgentFinish> {
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Abstract class representing an output parser specifically for agents
|
|
18
|
+
* that return multiple actions.
|
|
19
|
+
*/
|
|
20
|
+
export declare abstract class AgentMultiActionOutputParser extends BaseOutputParser<AgentAction[] | AgentFinish> {
|
|
21
|
+
}
|
|
22
|
+
export declare class OpenAIFunctionsAgentOutputParser extends AgentActionOutputParser {
|
|
23
|
+
lc_namespace: string[];
|
|
24
|
+
static lc_name(): string;
|
|
25
|
+
parse(text: string): Promise<AgentAction | AgentFinish>;
|
|
26
|
+
parseResult(generations: ChatGeneration[]): Promise<FunctionsAgentAction | AgentFinish>;
|
|
27
|
+
/**
|
|
28
|
+
* Parses the output message into a FunctionsAgentAction or AgentFinish
|
|
29
|
+
* object.
|
|
30
|
+
* @param message The BaseMessage to parse.
|
|
31
|
+
* @returns A FunctionsAgentAction or AgentFinish object.
|
|
32
|
+
*/
|
|
33
|
+
parseAIMessage(message: BaseMessage): FunctionsAgentAction | AgentFinish;
|
|
34
|
+
getFormatInstructions(): string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Type that represents an agent action with an optional message log.
|
|
38
|
+
*/
|
|
39
|
+
export type ToolsAgentAction = AgentAction & {
|
|
40
|
+
toolCallId: string;
|
|
41
|
+
messageLog?: BaseMessage[];
|
|
42
|
+
};
|
|
43
|
+
export type ToolsAgentStep = AgentStep & {
|
|
44
|
+
action: ToolsAgentAction;
|
|
45
|
+
};
|
|
46
|
+
export declare class OpenAIToolsAgentOutputParser extends AgentMultiActionOutputParser {
|
|
47
|
+
lc_namespace: string[];
|
|
48
|
+
static lc_name(): string;
|
|
49
|
+
parse(text: string): Promise<AgentAction[] | AgentFinish>;
|
|
50
|
+
parseResult(generations: ChatGeneration[]): Promise<AgentFinish | ToolsAgentAction[]>;
|
|
51
|
+
/**
|
|
52
|
+
* Parses the output message into a ToolsAgentAction[] or AgentFinish
|
|
53
|
+
* object.
|
|
54
|
+
* @param message The BaseMessage to parse.
|
|
55
|
+
* @returns A ToolsAgentAction[] or AgentFinish object.
|
|
56
|
+
*/
|
|
57
|
+
parseAIMessage(message: BaseMessage): ToolsAgentAction[] | AgentFinish;
|
|
58
|
+
getFormatInstructions(): string;
|
|
59
|
+
}
|