@@ -21,12 +21,14 @@ class ToolInput(BaseModel):
21
21
tool_name : str = Field (
22
22
...,
23
23
description = "The name of a tool that can be used to answer the current question"
24
- " or solve the current task." ,
24
+ " or solve the current task. "
25
+ "If no suitable tool is selected, leave this blank." ,
25
26
)
26
27
args : dict = Field (
27
28
default = {"arg name1" : "" , "arg name2" : "" },
28
29
description = "The tool selected for the current target, the parameter "
29
- "information required for execution" ,
30
+ "information required for execution, "
31
+ "If no suitable tool is selected, leave this blank." ,
30
32
)
31
33
thought : str = Field (..., description = "Summary of thoughts to the user" )
32
34
@@ -68,9 +70,9 @@ def ai_out_schema(self) -> Optional[str]:
68
70
}
69
71
70
72
return f"""Please response in the following json format:
71
- { json .dumps (out_put_schema , indent = 2 , ensure_ascii = False )}
72
- Make sure the response is correct json and can be parsed by Python json.loads.
73
- """
73
+ { json .dumps (out_put_schema , indent = 2 , ensure_ascii = False )}
74
+ Make sure the response is correct json and can be parsed by Python json.loads.
75
+ and do not write the comment in json,only write the json content. """
74
76
75
77
async def run (
76
78
self ,
@@ -91,6 +93,15 @@ async def run(
91
93
need_vis_render (bool, optional): Whether need visualization rendering.
92
94
Defaults to True.
93
95
"""
96
+ success , error = parse_json_safe (ai_message )
97
+ if not success :
98
+ return ActionOutput (
99
+ is_exe_success = False ,
100
+ content = f"Tool Action execute failed! llm reply { ai_message } "
101
+ f"is not a valid json format, json error: { error } . "
102
+ f"You need to strictly return the raw JSON format. " ,
103
+ )
104
+
94
105
try :
95
106
param : ToolInput = self ._input_convert (ai_message , ToolInput )
96
107
except Exception as e :
@@ -100,6 +111,16 @@ async def run(
100
111
content = "The requested correctly structured answer could not be found." ,
101
112
)
102
113
114
+ if param .tool_name is None or param .tool_name == "" :
115
+ # can not choice tools, it must be some reason
116
+ return ActionOutput (
117
+ is_exe_success = False ,
118
+ # content= param.thought,
119
+ content = f"There are no suitable tools available "
120
+ f"to achieve the user's goal: '{ param .thought } '" ,
121
+ have_retry = False ,
122
+ )
123
+
103
124
try :
104
125
tool_packs = ToolPack .from_resource (self .resource )
105
126
if not tool_packs :
@@ -137,10 +158,25 @@ async def run(
137
158
is_exe_success = response_success ,
138
159
content = str (tool_result ),
139
160
view = view ,
161
+ thoughts = param .thought ,
162
+ action = str ({"tool_name" : param .tool_name , "args" : param .args }),
140
163
observations = str (tool_result ),
141
164
)
142
165
except Exception as e :
143
166
logger .exception ("Tool Action Run Failed!" )
144
167
return ActionOutput (
145
- is_exe_success = False , content = f"Tool action run failed!{ str (e )} "
168
+ is_exe_success = False ,
169
+ content = f"Tool action run failed!{ str (e )} " ,
170
+ action = str ({"tool_name" : param .tool_name , "args" : param .args }),
146
171
)
172
+
173
+
174
+ def parse_json_safe (json_str ):
175
+ """Try to parse json."""
176
+ try :
177
+ # try to parse json
178
+ data = json .loads (json_str )
179
+ return True , data
180
+ except json .JSONDecodeError as e :
181
+ # 捕捉JSON解析错误并返回详细信息
182
+ return False , e .msg
0 commit comments