langchain/templates/solo-performance-prompting-agent/solo_performance_prompting_agent/parser.py
Sayandip 8dbbcf0b6c
Adding a template for Solo Performance Prompting Agent (#12627)
**Description:** This template creates an agent that transforms a single
LLM into a cognitive synergist by engaging in multi-turn
self-collaboration with multiple personas.
**Tag maintainer:** @hwchase17

---------

Co-authored-by: Sayandip Sarkar <sayandip.sarkar@skypointcloud.com>
Co-authored-by: Erick Friis <erick@langchain.dev>
2023-11-01 08:10:07 -07:00

19 lines
813 B
Python

from langchain.schema import AgentAction, AgentFinish
def parse_output(message: str):
FINAL_ANSWER_ACTION = "<final_answer>"
includes_answer = FINAL_ANSWER_ACTION in message
if includes_answer:
answer = message.split(FINAL_ANSWER_ACTION)[1].strip()
if "</final_answer>" in answer:
answer = answer.split("</final_answer>")[0].strip()
return AgentFinish(return_values={"output": answer}, log=message)
elif "</tool>" in message:
tool, tool_input = message.split("</tool>")
_tool = tool.split("<tool>")[1]
_tool_input = tool_input.split("<tool_input>")[1]
if "</tool_input>" in _tool_input:
_tool_input = _tool_input.split("</tool_input>")[0]
return AgentAction(tool=_tool, tool_input=_tool_input, log=message)