koishi-plugin-chatluna 1.0.0-beta.21 → 1.0.0-beta.22

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.
Files changed (40) hide show
  1. package/lib/config.js +1 -1
  2. package/lib/llm-core/agent/index.d.ts +69 -0
  3. package/lib/llm-core/agent/index.js +159 -0
  4. package/lib/llm-core/agent/openai/index.d.ts +69 -0
  5. package/lib/llm-core/agent/openai/index.js +171 -0
  6. package/lib/llm-core/agent/openai/output_parser.d.ts +59 -0
  7. package/lib/llm-core/agent/openai/output_parser.js +139 -0
  8. package/lib/llm-core/agent/openai/prompt.d.ts +2 -0
  9. package/lib/llm-core/agent/openai/prompt.js +5 -0
  10. package/lib/llm-core/agent/output_parser.d.ts +59 -0
  11. package/lib/llm-core/agent/output_parser.js +135 -0
  12. package/lib/llm-core/agent/prompt.d.ts +2 -0
  13. package/lib/llm-core/agent/prompt.js +5 -0
  14. package/lib/llm-core/agent/types.d.ts +135 -0
  15. package/lib/llm-core/agent/types.js +2 -0
  16. package/lib/llm-core/chain/browsing_chain.d.ts +35 -0
  17. package/lib/llm-core/chain/browsing_chain.js +187 -0
  18. package/lib/llm-core/chain/plugin_chat_chain.js +19 -22
  19. package/lib/llm-core/chain/prompt.d.ts +3 -31
  20. package/lib/llm-core/chain/prompt.js +7 -183
  21. package/lib/llm-core/chat/app.js +8 -1
  22. package/lib/llm-core/chat/default.js +9 -17
  23. package/lib/llm-core/memory/message/database_memory.d.ts +2 -2
  24. package/lib/llm-core/memory/message/database_memory.js +12 -8
  25. package/lib/llm-core/platform/client.d.ts +1 -0
  26. package/lib/llm-core/platform/config.d.ts +1 -1
  27. package/lib/llm-core/platform/config.js +6 -3
  28. package/lib/llm-core/platform/model.d.ts +6 -1
  29. package/lib/llm-core/platform/model.js +56 -7
  30. package/lib/llm-core/platform/service.d.ts +5 -3
  31. package/lib/llm-core/platform/service.js +17 -11
  32. package/lib/llm-core/platform/types.d.ts +4 -3
  33. package/lib/llm-core/utils/count_tokens.js +5 -13
  34. package/lib/renders/image.js +1 -1
  35. package/lib/renders/mixed-image.js +1 -1
  36. package/lib/utils/sse.d.ts +1 -1
  37. package/lib/utils/sse.js +9 -2
  38. package/package.json +102 -102
  39. package/resources/out.html +83 -64
  40. 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('发送等待消息,在请求时会发送这条消息')
@@ -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
+ }
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OpenAIToolsAgentOutputParser = exports.OpenAIFunctionsAgentOutputParser = exports.AgentMultiActionOutputParser = exports.AgentActionOutputParser = void 0;
4
+ const schema_1 = require("langchain/schema");
5
+ const output_parser_1 = require("langchain/schema/output_parser");
6
+ // F** langchain
7
+ /**
8
+ * Abstract class representing an output parser specifically for agent
9
+ * actions and finishes in LangChain. It extends the `BaseOutputParser`
10
+ * class.
11
+ */
12
+ class AgentActionOutputParser extends output_parser_1.BaseOutputParser {
13
+ }
14
+ exports.AgentActionOutputParser = AgentActionOutputParser;
15
+ /**
16
+ * Abstract class representing an output parser specifically for agents
17
+ * that return multiple actions.
18
+ */
19
+ class AgentMultiActionOutputParser extends output_parser_1.BaseOutputParser {
20
+ }
21
+ exports.AgentMultiActionOutputParser = AgentMultiActionOutputParser;
22
+ class OpenAIFunctionsAgentOutputParser extends AgentActionOutputParser {
23
+ // eslint-disable-next-line @typescript-eslint/naming-convention
24
+ lc_namespace = ['langchain', 'agents', 'openai'];
25
+ // eslint-disable-next-line @typescript-eslint/naming-convention
26
+ static lc_name() {
27
+ return 'OpenAIFunctionsAgentOutputParser';
28
+ }
29
+ async parse(text) {
30
+ throw new Error(`OpenAIFunctionsAgentOutputParser can only parse messages.\nPassed input: ${text}`);
31
+ }
32
+ async parseResult(generations) {
33
+ if ('message' in generations[0] &&
34
+ (0, schema_1.isBaseMessage)(generations[0].message)) {
35
+ return this.parseAIMessage(generations[0].message);
36
+ }
37
+ throw new Error('parseResult on OpenAIFunctionsAgentOutputParser only works on ChatGeneration output');
38
+ }
39
+ /**
40
+ * Parses the output message into a FunctionsAgentAction or AgentFinish
41
+ * object.
42
+ * @param message The BaseMessage to parse.
43
+ * @returns A FunctionsAgentAction or AgentFinish object.
44
+ */
45
+ parseAIMessage(message) {
46
+ if (message.content && typeof message.content !== 'string') {
47
+ throw new Error('This agent cannot parse non-string model responses.');
48
+ }
49
+ if (message.additional_kwargs.function_call) {
50
+ // eslint-disable-next-line prefer-destructuring, @typescript-eslint/naming-convention
51
+ const function_call = message.additional_kwargs.function_call;
52
+ try {
53
+ const toolInput = function_call.arguments
54
+ ? JSON.parse(function_call.arguments)
55
+ : {};
56
+ return {
57
+ tool: function_call.name,
58
+ toolInput,
59
+ log: `Invoking "${function_call.name}" with ${function_call.arguments ?? '{}'}\n${message.content}`,
60
+ messageLog: [message]
61
+ };
62
+ }
63
+ catch (error) {
64
+ throw new output_parser_1.OutputParserException(`Failed to parse function arguments from chat model response. Text: "${function_call.arguments}". ${error}`);
65
+ }
66
+ }
67
+ else {
68
+ return {
69
+ returnValues: { output: message.content },
70
+ log: message.content
71
+ };
72
+ }
73
+ }
74
+ getFormatInstructions() {
75
+ throw new Error('getFormatInstructions not implemented inside OpenAIFunctionsAgentOutputParser.');
76
+ }
77
+ }
78
+ exports.OpenAIFunctionsAgentOutputParser = OpenAIFunctionsAgentOutputParser;
79
+ class OpenAIToolsAgentOutputParser extends AgentMultiActionOutputParser {
80
+ // eslint-disable-next-line @typescript-eslint/naming-convention
81
+ lc_namespace = ['langchain', 'agents', 'openai'];
82
+ // eslint-disable-next-line @typescript-eslint/naming-convention
83
+ static lc_name() {
84
+ return 'OpenAIToolsAgentOutputParser';
85
+ }
86
+ async parse(text) {
87
+ throw new Error(`OpenAIFunctionsAgentOutputParser can only parse messages.\nPassed input: ${text}`);
88
+ }
89
+ async parseResult(generations) {
90
+ if ('message' in generations[0] &&
91
+ (0, schema_1.isBaseMessage)(generations[0].message)) {
92
+ return this.parseAIMessage(generations[0].message);
93
+ }
94
+ throw new Error('parseResult on OpenAIFunctionsAgentOutputParser only works on ChatGeneration output');
95
+ }
96
+ /**
97
+ * Parses the output message into a ToolsAgentAction[] or AgentFinish
98
+ * object.
99
+ * @param message The BaseMessage to parse.
100
+ * @returns A ToolsAgentAction[] or AgentFinish object.
101
+ */
102
+ parseAIMessage(message) {
103
+ if (message.content && typeof message.content !== 'string') {
104
+ throw new Error('This agent cannot parse non-string model responses.');
105
+ }
106
+ if (message.additional_kwargs.tool_calls) {
107
+ const toolCalls = message
108
+ .additional_kwargs.tool_calls;
109
+ try {
110
+ return toolCalls.map((toolCall, i) => {
111
+ const toolInput = toolCall.function.arguments
112
+ ? JSON.parse(toolCall.function.arguments)
113
+ : {};
114
+ const messageLog = i === 0 ? [message] : [];
115
+ return {
116
+ tool: toolCall.function.name,
117
+ toolInput,
118
+ toolCallId: toolCall.id,
119
+ log: `Invoking "${toolCall.function.name}" with ${toolCall.function.arguments ?? '{}'}\n${message.content}`,
120
+ messageLog
121
+ };
122
+ });
123
+ }
124
+ catch (error) {
125
+ throw new output_parser_1.OutputParserException(`Failed to parse tool arguments from chat model response. Text: "${JSON.stringify(toolCalls)}". ${error}`);
126
+ }
127
+ }
128
+ else {
129
+ return {
130
+ returnValues: { output: message.content },
131
+ log: message.content
132
+ };
133
+ }
134
+ }
135
+ getFormatInstructions() {
136
+ throw new Error('getFormatInstructions not implemented inside OpenAIToolsAgentOutputParser.');
137
+ }
138
+ }
139
+ exports.OpenAIToolsAgentOutputParser = OpenAIToolsAgentOutputParser;
@@ -0,0 +1,2 @@
1
+ export declare const PREFIX = "You are a helpful AI assistant.";
2
+ export declare const SUFFIX = "";