mirror of
https://github.com/Mintplex-Labs/langchain-python.git
synced 2026-08-27 09:21:30 -04:00
aad0a498ac
Co-authored-by: yummydum <sumita@nowcast.co.jp>
27 lines
920 B
Python
27 lines
920 B
Python
import json
|
|
from typing import Union
|
|
|
|
from langchain.agents.agent import AgentOutputParser
|
|
from langchain.agents.chat.prompt import FORMAT_INSTRUCTIONS
|
|
from langchain.schema import AgentAction, AgentFinish, OutputParserException
|
|
|
|
FINAL_ANSWER_ACTION = "Final Answer:"
|
|
|
|
|
|
class ChatOutputParser(AgentOutputParser):
|
|
def get_format_instructions(self) -> str:
|
|
return FORMAT_INSTRUCTIONS
|
|
|
|
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
|
|
if FINAL_ANSWER_ACTION in text:
|
|
return AgentFinish(
|
|
{"output": text.split(FINAL_ANSWER_ACTION)[-1].strip()}, text
|
|
)
|
|
try:
|
|
action = text.split("```")[1]
|
|
response = json.loads(action.strip())
|
|
return AgentAction(response["action"], response["action_input"], text)
|
|
|
|
except Exception:
|
|
raise OutputParserException(f"Could not parse LLM output: {text}")
|