2023-09-29 16:04:44 +08:00
|
|
|
from __future__ import annotations
|
2023-09-28 20:19:26 +08:00
|
|
|
from langchain.agents import Tool, AgentOutputParser
|
2023-09-17 11:19:16 +08:00
|
|
|
from langchain.prompts import StringPromptTemplate
|
|
|
|
|
from typing import List, Union
|
2023-09-28 20:19:26 +08:00
|
|
|
from langchain.schema import AgentAction, AgentFinish
|
2023-09-17 11:19:16 +08:00
|
|
|
import re
|
2023-09-28 20:19:26 +08:00
|
|
|
|
2023-09-17 11:19:16 +08:00
|
|
|
class CustomPromptTemplate(StringPromptTemplate):
|
|
|
|
|
# The template to use
|
|
|
|
|
template: str
|
|
|
|
|
# The list of tools available
|
|
|
|
|
tools: List[Tool]
|
|
|
|
|
|
|
|
|
|
def format(self, **kwargs) -> str:
|
|
|
|
|
# Get the intermediate steps (AgentAction, Observation tuples)
|
|
|
|
|
# Format them in a particular way
|
|
|
|
|
intermediate_steps = kwargs.pop("intermediate_steps")
|
|
|
|
|
thoughts = ""
|
|
|
|
|
for action, observation in intermediate_steps:
|
|
|
|
|
thoughts += action.log
|
|
|
|
|
thoughts += f"\nObservation: {observation}\nThought: "
|
|
|
|
|
# Set the agent_scratchpad variable to that value
|
|
|
|
|
kwargs["agent_scratchpad"] = thoughts
|
|
|
|
|
# Create a tools variable from the list of tools provided
|
|
|
|
|
kwargs["tools"] = "\n".join([f"{tool.name}: {tool.description}" for tool in self.tools])
|
|
|
|
|
# Create a list of tool names for the tools provided
|
|
|
|
|
kwargs["tool_names"] = ", ".join([tool.name for tool in self.tools])
|
|
|
|
|
return self.template.format(**kwargs)
|
|
|
|
|
class CustomOutputParser(AgentOutputParser):
|
|
|
|
|
|
2023-09-28 20:19:26 +08:00
|
|
|
def parse(self, llm_output: str) -> AgentFinish | AgentAction | str:
|
2023-09-17 11:19:16 +08:00
|
|
|
# Check if agent should finish
|
|
|
|
|
if "Final Answer:" in llm_output:
|
|
|
|
|
return AgentFinish(
|
|
|
|
|
# Return values is generally always a dictionary with a single `output` key
|
|
|
|
|
# It is not recommended to try anything else at the moment :)
|
2023-09-27 19:19:25 +08:00
|
|
|
return_values={"output": llm_output.replace("Final Answer:", "").strip()},
|
2023-09-17 11:19:16 +08:00
|
|
|
log=llm_output,
|
|
|
|
|
)
|
|
|
|
|
# Parse out the action and action input
|
|
|
|
|
regex = r"Action\s*\d*\s*:(.*?)\nAction\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)"
|
|
|
|
|
match = re.search(regex, llm_output, re.DOTALL)
|
|
|
|
|
if not match:
|
|
|
|
|
return AgentFinish(
|
|
|
|
|
return_values={"output": f"调用agent失败: `{llm_output}`"},
|
|
|
|
|
log=llm_output,
|
|
|
|
|
)
|
|
|
|
|
action = match.group(1).strip()
|
|
|
|
|
action_input = match.group(2)
|
|
|
|
|
# Return the action and action input
|
2023-09-28 20:19:26 +08:00
|
|
|
try:
|
|
|
|
|
ans = AgentAction(
|
2023-09-27 19:19:25 +08:00
|
|
|
tool=action,
|
|
|
|
|
tool_input=action_input.strip(" ").strip('"'),
|
|
|
|
|
log=llm_output
|
2023-09-28 20:19:26 +08:00
|
|
|
)
|
|
|
|
|
return ans
|
|
|
|
|
except:
|
|
|
|
|
return AgentFinish(
|
|
|
|
|
return_values={"output": f"调用agent失败: `{llm_output}`"},
|
|
|
|
|
log=llm_output,
|
|
|
|
|
)
|
|
|
|
|
|
2023-09-17 11:19:16 +08:00
|
|
|
|
|
|
|
|
|