forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add base agent * cr * cr * cr * cr * cr * cr * cr * Fix allowedTools * Rename existing example --------- Co-authored-by: Nuno Campos <[email protected]>
- Loading branch information
Showing
13 changed files
with
473 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
hide_table_of_contents: true | ||
sidebar_position: 1 | ||
--- | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import Example from "@examples/agents/custom_llm_agent.ts"; | ||
|
||
# Custom LLM Agent | ||
|
||
This example covers how to create a custom Agent powered by an LLM. | ||
|
||
<CodeBlock language="typescript">{Example}</CodeBlock> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
hide_table_of_contents: true | ||
sidebar_position: 1 | ||
--- | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import Example from "@examples/agents/custom_llm_agent_chat.ts"; | ||
|
||
# Custom LLM Agent (with Chat Model) | ||
|
||
This example covers how to create a custom Agent powered by a Chat Model. | ||
|
||
<CodeBlock language="typescript">{Example}</CodeBlock> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 0 additions & 87 deletions
87
docs/docs/modules/agents/agents/examples/custom_agent_llm.md
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { | ||
LLMSingleActionAgent, | ||
AgentActionOutputParser, | ||
AgentExecutor, | ||
} from "langchain/agents"; | ||
import { LLMChain } from "langchain/chains"; | ||
import { OpenAI } from "langchain/llms"; | ||
import { | ||
BasePromptTemplate, | ||
BaseStringPromptTemplate, | ||
SerializedBasePromptTemplate, | ||
renderTemplate, | ||
} from "langchain/prompts"; | ||
import { | ||
InputValues, | ||
PartialValues, | ||
AgentStep, | ||
AgentAction, | ||
AgentFinish, | ||
} from "langchain/schema"; | ||
import { SerpAPI, Calculator, Tool } from "langchain/tools"; | ||
|
||
const PREFIX = `Answer the following questions as best you can. You have access to the following tools:`; | ||
const formatInstructions = (toolNames: string) => `Use the following format: | ||
Question: the input question you must answer | ||
Thought: you should always think about what to do | ||
Action: the action to take, should be one of [${toolNames}] | ||
Action Input: the input to the action | ||
Observation: the result of the action | ||
... (this Thought/Action/Action Input/Observation can repeat N times) | ||
Thought: I now know the final answer | ||
Final Answer: the final answer to the original input question`; | ||
const SUFFIX = `Begin! | ||
Question: {input} | ||
Thought:{agent_scratchpad}`; | ||
|
||
class CustomPromptTemplate extends BaseStringPromptTemplate { | ||
tools: Tool[]; | ||
|
||
constructor(args: { tools: Tool[]; inputVariables: string[] }) { | ||
super({ inputVariables: args.inputVariables }); | ||
this.tools = args.tools; | ||
} | ||
|
||
_getPromptType(): string { | ||
throw new Error("Not implemented"); | ||
} | ||
|
||
format(input: InputValues): Promise<string> { | ||
/** Construct the final template */ | ||
const toolStrings = this.tools | ||
.map((tool) => `${tool.name}: ${tool.description}`) | ||
.join("\n"); | ||
const toolNames = this.tools.map((tool) => tool.name).join("\n"); | ||
const instructions = formatInstructions(toolNames); | ||
const template = [PREFIX, toolStrings, instructions, SUFFIX].join("\n\n"); | ||
/** Construct the agent_scratchpad */ | ||
const intermediateSteps = input.intermediate_steps as AgentStep[]; | ||
const agentScratchpad = intermediateSteps.reduce( | ||
(thoughts, { action, observation }) => | ||
thoughts + | ||
[action.log, `\nObservation: ${observation}`, "Thought:"].join("\n"), | ||
"" | ||
); | ||
const newInput = { agent_scratchpad: agentScratchpad, ...input }; | ||
/** Format the template. */ | ||
return Promise.resolve(renderTemplate(template, "f-string", newInput)); | ||
} | ||
|
||
partial(_values: PartialValues): Promise<BasePromptTemplate> { | ||
throw new Error("Not implemented"); | ||
} | ||
|
||
serialize(): SerializedBasePromptTemplate { | ||
throw new Error("Not implemented"); | ||
} | ||
} | ||
|
||
class CustomOutputParser extends AgentActionOutputParser { | ||
async parse(text: string): Promise<AgentAction | AgentFinish> { | ||
if (text.includes("Final Answer:")) { | ||
const parts = text.split("Final Answer:"); | ||
const input = parts[parts.length - 1].trim(); | ||
const finalAnswers = { output: input }; | ||
return { log: text, returnValues: finalAnswers }; | ||
} | ||
|
||
const match = /Action: (.*)\nAction Input: (.*)/s.exec(text); | ||
if (!match) { | ||
throw new Error(`Could not parse LLM output: ${text}`); | ||
} | ||
|
||
return { | ||
tool: match[1].trim(), | ||
toolInput: match[2].trim().replace(/^"+|"+$/g, ""), | ||
log: text, | ||
}; | ||
} | ||
|
||
getFormatInstructions(): string { | ||
throw new Error("Not implemented"); | ||
} | ||
} | ||
|
||
export const run = async () => { | ||
const model = new OpenAI({ temperature: 0 }); | ||
const tools = [new SerpAPI(), new Calculator()]; | ||
|
||
const llmChain = new LLMChain({ | ||
prompt: new CustomPromptTemplate({ | ||
tools, | ||
inputVariables: ["input", "agent_scratchpad"], | ||
}), | ||
llm: model, | ||
}); | ||
|
||
const agent = new LLMSingleActionAgent({ | ||
llmChain, | ||
outputParser: new CustomOutputParser(), | ||
stop: ["\nObservation"], | ||
}); | ||
const executor = new AgentExecutor({ | ||
agent, | ||
tools, | ||
}); | ||
console.log("Loaded agent."); | ||
|
||
const input = `Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?`; | ||
|
||
console.log(`Executing with input "${input}"...`); | ||
|
||
const result = await executor.call({ input }); | ||
|
||
console.log(`Got output ${result.output}`); | ||
}; |
Oops, something went wrong.