Source code for langchain.agents.output_parsers.self_ask
from typing import Sequence, Union
from langchain_core.agents import AgentAction, AgentFinish
from langchain_core.exceptions import OutputParserException
from langchain.agents.agent import AgentOutputParser
[docs]class SelfAskOutputParser(AgentOutputParser):
"""解析自问自答风格的LLM调用。
期望输出为以下两种格式之一。
如果输出信号表明应采取行动,
应采用以下格式。这将导致返回AgentAction。
```
思考内容...
后续问题:旧金山的温度是多少?
```
如果输出信号表明应给出最终答案,
应采用以下格式。这将导致返回AgentFinish。
```
思考内容...
所以最终答案是:温度为100度
```"""
followups: Sequence[str] = ("Follow up:", "Followup:")
finish_string: str = "So the final answer is: "
[docs] def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
last_line = text.split("\n")[-1]
if not any([follow in last_line for follow in self.followups]):
if self.finish_string not in last_line:
raise OutputParserException(f"Could not parse output: {text}")
return AgentFinish({"output": last_line[len(self.finish_string) :]}, text)
after_colon = text.split(":")[-1].strip()
return AgentAction("Intermediate Answer", after_colon, text)
@property
def _type(self) -> str:
return "self_ask"