-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parser.py
More file actions
137 lines (114 loc) · 4.83 KB
/
test_parser.py
File metadata and controls
137 lines (114 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import re
from typing import Union
# 定义必要的类和异常
class AgentAction:
def __init__(self, action, action_input, text):
self.action = action
self.action_input = action_input
self.text = text
class AgentFinish:
def __init__(self, output, text):
self.output = output
self.text = text
class OutputParserException(Exception):
def __init__(self, message, observation=None, llm_output=None, send_to_llm=False):
super().__init__(message)
self.observation = observation
self.llm_output = llm_output
self.send_to_llm = send_to_llm
# 定义常量
FINAL_ANSWER_ACTION = "Final Answer:"
FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGE = "Final answer and parsable action found."
MISSING_ACTION_AFTER_THOUGHT_ERROR_MESSAGE = "Missing action after thought."
MISSING_ACTION_INPUT_AFTER_ACTION_ERROR_MESSAGE = "Missing action input after action."
class Parser:
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
# includes_answer = FINAL_ANSWER_ACTION in text
pattern = r'^Final Answer: .*?\.$'
match = re.search(pattern, text, flags=re.MULTILINE)
regex = r"Action\s*\d*\s*:\s*(.*?)\s*Action\s*\d*\s*Input\s*\d*\s*:([^$\n]*)"
action_match = re.search(regex, text, re.DOTALL)
if action_match:
new_text = text
if match:
new_text = re.sub(pattern, '', text, flags=re.MULTILINE)
# raise OutputParserException(
# f"{FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGE}: {text}"
# )
# return AgentFinish(
# {"output": text.split(FINAL_ANSWER_ACTION)[-1].strip()}, text
# )
action = action_match.group(1).strip()
action_input = action_match.group(2).strip()
action_input = action_input.strip('"')
corrected_input = input(f"是否执行:{action}, {action_input}").strip()
bool_map = {"是": True, "否": False}
bool_value = bool_map.get(corrected_input, False)
# action_input = corrected_input
# print(new_text)
# return AgentAction(action, action_input, text)
if bool_value:
return AgentAction(action, action_input, new_text)
elif match:
return AgentFinish(
{"output": text.split(FINAL_ANSWER_ACTION)[-1].strip()}, text
)
if not re.search(r"Action\s*\d*\s*:\s*(.*?)\s*(.*?)$", text, re.MULTILINE):
raise OutputParserException(
f"Check your output and make sure it conforms, use the Action/Action Input syntax",
observation=MISSING_ACTION_AFTER_THOUGHT_ERROR_MESSAGE,
llm_output=text,
send_to_llm=True,
)
elif not re.search(
r"\s*Action\s*\d*\s*Input\s*\d*\s*:\s*(.*?)\s*(.*?)$", text, re.MULTILINE
):
raise OutputParserException(
f"Check your output and make sure it conforms, use the Action/Action Input syntax",
observation=MISSING_ACTION_INPUT_AFTER_ACTION_ERROR_MESSAGE,
llm_output=text,
send_to_llm=True,
)
else:
raise OutputParserException(f"Could not parse LLM output: `{text}`")
# 创建 Parser 实例
parser = Parser()
# 示例 1: 解析动作和输入
text_with_action = """
The oscilloscope channel has been set up. Now, I need to observe the waveform and calculate the rise and fall times to determine the slew rate.
Action:1231231
Action Input: 12312
示波器上的波形已显示给用户
I have successfully observed the waveform on the oscilloscope. Now, I need to calculate the rise and fall times to determine the slew rate.
I have successfully calculated the slew rate. Now, I can proceed to the final answer.
Final Answer: The slew rate of the waveform is 0.0000 V/us.
"""
try:
result = parser.parse(text_with_action)
if isinstance(result, AgentAction):
print(f"Action111: {result.action}===")
print(f"Action Input111: {result.action_input}===")
# print(f"=========================Original Text: {result.text}")
except OutputParserException as e:
print(f"Exception: {e}")
# # 示例 2: 解析最终答案
# text_with_final_answer = """
# Action:1.abc
# 2.abc
# 3.abc
# 4.abc
# Action Input: 12312
# """
# try:
# result = parser.parse(text_with_final_answer)
# if isinstance(result, AgentFinish):
# print(f"Output: {result.output}")
# print(f"Original Text: {result.text}")
# except OutputParserException as e:
# print(f"Exception: {e}")
# # 示例 3: 无效格式的文本
# invalid_text = "This is an invalid format"
# try:
# result = parser.parse(invalid_text)
# except OutputParserException as e:
# print(f"Exception: {e}")