mirror of
https://github.com/Mintplex-Labs/langchain-python.git
synced 2026-08-27 01:15:22 -04:00
aad0a498ac
Co-authored-by: yummydum <sumita@nowcast.co.jp>
27 lines
978 B
Python
27 lines
978 B
Python
import re
|
|
from typing import Union
|
|
|
|
from langchain.agents.agent import AgentOutputParser
|
|
from langchain.agents.conversational.prompt import FORMAT_INSTRUCTIONS
|
|
from langchain.schema import AgentAction, AgentFinish, OutputParserException
|
|
|
|
|
|
class ConvoOutputParser(AgentOutputParser):
|
|
ai_prefix: str = "AI"
|
|
|
|
def get_format_instructions(self) -> str:
|
|
return FORMAT_INSTRUCTIONS
|
|
|
|
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
|
|
if f"{self.ai_prefix}:" in text:
|
|
return AgentFinish(
|
|
{"output": text.split(f"{self.ai_prefix}:")[-1].strip()}, text
|
|
)
|
|
regex = r"Action: (.*?)[\n]*Action Input: (.*)"
|
|
match = re.search(regex, text)
|
|
if not match:
|
|
raise OutputParserException(f"Could not parse LLM output: `{text}`")
|
|
action = match.group(1)
|
|
action_input = match.group(2)
|
|
return AgentAction(action.strip(), action_input.strip(" ").strip('"'), text)
|