-
Notifications
You must be signed in to change notification settings - Fork 2k
/
json.ts
77 lines (74 loc) 路 2.5 KB
/
json.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
import type { ToolInterface } from "@langchain/core/tools";
import { Toolkit } from "@langchain/community/agents/toolkits/base";
import {
JsonGetValueTool,
JsonListKeysTool,
JsonSpec,
} from "../../../tools/json.js";
import { JSON_PREFIX, JSON_SUFFIX } from "./prompt.js";
import { LLMChain } from "../../../chains/llm_chain.js";
import { ZeroShotCreatePromptArgs, ZeroShotAgent } from "../../mrkl/index.js";
import { AgentExecutor } from "../../executor.js";
/**
* Represents a toolkit for working with JSON data. It initializes the
* JSON tools based on the provided JSON specification.
* @example
* ```typescript
* const toolkit = new JsonToolkit(new JsonSpec());
* const executor = createJsonAgent(model, toolkit);
* const result = await executor.invoke({
* input: 'What are the required parameters in the request body to the /completions endpoint?'
* });
* ```
*/
export class JsonToolkit extends Toolkit {
tools: ToolInterface[];
constructor(public jsonSpec: JsonSpec) {
super();
this.tools = [
new JsonListKeysTool(jsonSpec),
new JsonGetValueTool(jsonSpec),
];
}
}
/**
* @deprecated Create a specific agent with a custom tool instead.
*
* Creates a JSON agent using a language model, a JSON toolkit, and
* optional prompt arguments. It creates a prompt for the agent using the
* JSON tools and the provided prefix and suffix. It then creates a
* ZeroShotAgent with the prompt and the JSON tools, and returns an
* AgentExecutor for executing the agent with the tools.
* @param llm The language model used to create the JSON agent.
* @param toolkit The JSON toolkit used to create the JSON agent.
* @param args Optional prompt arguments used to create the JSON agent.
* @returns An AgentExecutor for executing the created JSON agent with the tools.
*/
export function createJsonAgent(
llm: BaseLanguageModelInterface,
toolkit: JsonToolkit,
args?: ZeroShotCreatePromptArgs
) {
const {
prefix = JSON_PREFIX,
suffix = JSON_SUFFIX,
inputVariables = ["input", "agent_scratchpad"],
} = args ?? {};
const { tools } = toolkit;
const prompt = ZeroShotAgent.createPrompt(tools, {
prefix,
suffix,
inputVariables,
});
const chain = new LLMChain({ prompt, llm });
const agent = new ZeroShotAgent({
llmChain: chain,
allowedTools: tools.map((t) => t.name),
});
return AgentExecutor.fromAgentAndTools({
agent,
tools,
returnIntermediateSteps: true,
});
}