Update src/txagent/utils.py
Browse files- src/txagent/utils.py +13 -33
src/txagent/utils.py
CHANGED
|
@@ -3,7 +3,7 @@ import json
|
|
| 3 |
import hashlib
|
| 4 |
import torch
|
| 5 |
from typing import List
|
| 6 |
-
|
| 7 |
|
| 8 |
def get_md5(input_str):
|
| 9 |
# Create an MD5 hash object
|
|
@@ -13,35 +13,18 @@ def get_md5(input_str):
|
|
| 13 |
|
| 14 |
|
| 15 |
def tool_result_format(function_call_messages):
|
| 16 |
-
""
|
| 17 |
-
Format tool outputs as a list of ChatMessage objects with metadata
|
| 18 |
-
so the UI can display tool names and details cleanly.
|
| 19 |
-
"""
|
| 20 |
-
formatted_messages = []
|
| 21 |
-
|
| 22 |
for each_message in function_call_messages:
|
| 23 |
if each_message['role'] == 'tool':
|
| 24 |
try:
|
| 25 |
-
|
| 26 |
-
tool_name =
|
| 27 |
-
tool_output =
|
| 28 |
-
|
| 29 |
-
except Exception
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
log = {"error": "Malformed tool output", "raw": tool_output}
|
| 34 |
-
|
| 35 |
-
formatted_messages.append(ChatMessage(
|
| 36 |
-
role="assistant",
|
| 37 |
-
content=tool_output,
|
| 38 |
-
metadata={
|
| 39 |
-
"title": f"⚒️ {tool_name}",
|
| 40 |
-
"log": json.dumps(log, indent=2)
|
| 41 |
-
}
|
| 42 |
-
))
|
| 43 |
-
|
| 44 |
-
return formatted_messages
|
| 45 |
|
| 46 |
|
| 47 |
class NoRepeatSentenceProcessor:
|
|
@@ -80,15 +63,12 @@ class ReasoningTraceChecker:
|
|
| 80 |
each = self.conversation[i]
|
| 81 |
self.index = i
|
| 82 |
if each['role'] == 'assistant':
|
| 83 |
-
print(each)
|
| 84 |
thought = each['content']
|
| 85 |
actions = each['tool_calls']
|
| 86 |
-
|
| 87 |
good_status, current_info = self.check_repeat_thought(thought)
|
| 88 |
info += current_info
|
| 89 |
if not good_status:
|
| 90 |
return False, info
|
| 91 |
-
|
| 92 |
good_status, current_info = self.check_repeat_action(actions)
|
| 93 |
info += current_info
|
| 94 |
if not good_status:
|
|
@@ -107,8 +87,8 @@ class ReasoningTraceChecker:
|
|
| 107 |
for each_action in actions:
|
| 108 |
if 'call_id' in each_action:
|
| 109 |
del each_action['call_id']
|
| 110 |
-
|
| 111 |
-
if
|
| 112 |
return False, "repeat_action"
|
| 113 |
-
self.existing_actions.append(
|
| 114 |
return True, ''
|
|
|
|
| 3 |
import hashlib
|
| 4 |
import torch
|
| 5 |
from typing import List
|
| 6 |
+
|
| 7 |
|
| 8 |
def get_md5(input_str):
|
| 9 |
# Create an MD5 hash object
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
def tool_result_format(function_call_messages):
|
| 16 |
+
current_output = "\n\n<details>\n<summary> <strong>Verified Feedback from Tools</strong>, click to see details:</summary>\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
for each_message in function_call_messages:
|
| 18 |
if each_message['role'] == 'tool':
|
| 19 |
try:
|
| 20 |
+
parsed = json.loads(each_message['content'])
|
| 21 |
+
tool_name = parsed.get("tool_name", "Unknown Tool")
|
| 22 |
+
tool_output = parsed.get("content", each_message['content'])
|
| 23 |
+
current_output += f"**🔧 Tool: {tool_name}**\n\n{tool_output}\n\n"
|
| 24 |
+
except Exception:
|
| 25 |
+
current_output += f"{each_message['content']}\n\n"
|
| 26 |
+
current_output += "</details>\n\n\n"
|
| 27 |
+
return current_output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
class NoRepeatSentenceProcessor:
|
|
|
|
| 63 |
each = self.conversation[i]
|
| 64 |
self.index = i
|
| 65 |
if each['role'] == 'assistant':
|
|
|
|
| 66 |
thought = each['content']
|
| 67 |
actions = each['tool_calls']
|
|
|
|
| 68 |
good_status, current_info = self.check_repeat_thought(thought)
|
| 69 |
info += current_info
|
| 70 |
if not good_status:
|
| 71 |
return False, info
|
|
|
|
| 72 |
good_status, current_info = self.check_repeat_action(actions)
|
| 73 |
info += current_info
|
| 74 |
if not good_status:
|
|
|
|
| 87 |
for each_action in actions:
|
| 88 |
if 'call_id' in each_action:
|
| 89 |
del each_action['call_id']
|
| 90 |
+
each_action = json.dumps(each_action)
|
| 91 |
+
if each_action in self.existing_actions:
|
| 92 |
return False, "repeat_action"
|
| 93 |
+
self.existing_actions.append(each_action)
|
| 94 |
return True, ''
|