LightAgent 0.4.2__tar.gz → 0.4.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {lightagent-0.4.2 → lightagent-0.4.3}/LightAgent/la_core.py +73 -63
- {lightagent-0.4.2 → lightagent-0.4.3}/PKG-INFO +1 -1
- {lightagent-0.4.2 → lightagent-0.4.3}/pyproject.toml +1 -1
- {lightagent-0.4.2 → lightagent-0.4.3}/LICENSE +0 -0
- {lightagent-0.4.2 → lightagent-0.4.3}/LightAgent/__init__.py +0 -0
- {lightagent-0.4.2 → lightagent-0.4.3}/README.de.md +0 -0
- {lightagent-0.4.2 → lightagent-0.4.3}/README.es.md +0 -0
- {lightagent-0.4.2 → lightagent-0.4.3}/README.fr.md +0 -0
- {lightagent-0.4.2 → lightagent-0.4.3}/README.ja.md +0 -0
- {lightagent-0.4.2 → lightagent-0.4.3}/README.ko.md +0 -0
- {lightagent-0.4.2 → lightagent-0.4.3}/README.md +0 -0
- {lightagent-0.4.2 → lightagent-0.4.3}/README.pt.md +0 -0
- {lightagent-0.4.2 → lightagent-0.4.3}/README.ru.md +0 -0
- {lightagent-0.4.2 → lightagent-0.4.3}/README.zh-CN.md +0 -0
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
"""
|
|
5
5
|
作者: [weego/WXAI-Team]
|
|
6
|
-
版本: 0.4.
|
|
6
|
+
版本: 0.4.3
|
|
7
7
|
最后更新: 2025-07-10
|
|
8
8
|
"""
|
|
9
9
|
|
|
@@ -30,7 +30,7 @@ from mcp.client.sse import sse_client
|
|
|
30
30
|
from mcp.client.stdio import stdio_client
|
|
31
31
|
from openai.types.chat import ChatCompletionChunk
|
|
32
32
|
|
|
33
|
-
__version__ = "0.4.
|
|
33
|
+
__version__ = "0.4.3" # 你可以根据需要设置版本号
|
|
34
34
|
|
|
35
35
|
|
|
36
36
|
# openai.langfuse_auth_check()
|
|
@@ -242,13 +242,14 @@ class LoggerManager:
|
|
|
242
242
|
|
|
243
243
|
trace_info = f"[TraceID: {self.traceid}] " if self.traceid else ""
|
|
244
244
|
log_message = f"{trace_info}{action}: {data}"
|
|
245
|
+
safe_msg = log_message.encode('utf-8', 'ignore').decode('utf-8')
|
|
245
246
|
|
|
246
247
|
if level == "DEBUG":
|
|
247
|
-
self.logger.debug(
|
|
248
|
+
self.logger.debug(safe_msg)
|
|
248
249
|
elif level == "INFO":
|
|
249
|
-
self.logger.info(
|
|
250
|
+
self.logger.info(safe_msg)
|
|
250
251
|
elif level == "ERROR":
|
|
251
|
-
self.logger.error(
|
|
252
|
+
self.logger.error(safe_msg)
|
|
252
253
|
|
|
253
254
|
def set_traceid(self, traceid: str):
|
|
254
255
|
"""设置当前跟踪ID"""
|
|
@@ -423,7 +424,7 @@ class MCPClientManager:
|
|
|
423
424
|
|
|
424
425
|
|
|
425
426
|
class LightAgent:
|
|
426
|
-
__version__ = "0.4.
|
|
427
|
+
__version__ = "0.4.3" # 将版本号放在类中
|
|
427
428
|
|
|
428
429
|
def __init__(
|
|
429
430
|
self,
|
|
@@ -605,12 +606,12 @@ class LightAgent:
|
|
|
605
606
|
try:
|
|
606
607
|
tool_func = self.tool_loader.load_tool(tool)
|
|
607
608
|
self.tool_registry.register_tool(tool_func)
|
|
608
|
-
self.
|
|
609
|
+
self.log("DEBUG", "load_tools", {"tool": tool, "status": "success"})
|
|
609
610
|
except Exception as e:
|
|
610
|
-
self.
|
|
611
|
+
self.log("ERROR", "load_tools", {"tool": tool, "error": str(e)})
|
|
611
612
|
elif callable(tool) and hasattr(tool, "tool_info"):
|
|
612
613
|
if self.tool_registry.register_tool(tool):
|
|
613
|
-
self.
|
|
614
|
+
self.log("DEBUG", "register_tool", {"tool": tool.__name__, "status": "success"})
|
|
614
615
|
|
|
615
616
|
async def setup_mcp(
|
|
616
617
|
self,
|
|
@@ -622,7 +623,15 @@ class LightAgent:
|
|
|
622
623
|
if self.mcp_setting and not self.mcp_client:
|
|
623
624
|
self.mcp_client = MCPClientManager(self.mcp_setting, self.tool_registry)
|
|
624
625
|
await self.mcp_client.register_mcp_tool()
|
|
625
|
-
self.
|
|
626
|
+
self.log("INFO", "setup_mcp", "MCP 模块初始化成功")
|
|
627
|
+
|
|
628
|
+
def log(self, level, action, data):
|
|
629
|
+
"""
|
|
630
|
+
日志打印入口
|
|
631
|
+
"""
|
|
632
|
+
if not self.debug:
|
|
633
|
+
return
|
|
634
|
+
self.logger.log(level, action, data)
|
|
626
635
|
|
|
627
636
|
def run(
|
|
628
637
|
self,
|
|
@@ -648,8 +657,9 @@ class LightAgent:
|
|
|
648
657
|
"""
|
|
649
658
|
# 设置跟踪ID
|
|
650
659
|
traceid = uuid4().hex
|
|
651
|
-
self.logger
|
|
652
|
-
|
|
660
|
+
if self.debug and hasattr(self, 'logger'): # 仅在 debug=True 且 logger 存在时记录日志
|
|
661
|
+
self.logger.set_traceid(traceid)
|
|
662
|
+
self.log("INFO", "run_start", {"query": query, "user_id": user_id, "stream": stream})
|
|
653
663
|
|
|
654
664
|
# 初始化历史记录
|
|
655
665
|
history = history or []
|
|
@@ -679,10 +689,10 @@ class LightAgent:
|
|
|
679
689
|
if self.tree_of_thought:
|
|
680
690
|
tot_response, active_tools = self.run_thought(query)
|
|
681
691
|
system_prompt += f"\n##以下是问题的补充说明\n{tot_response}"
|
|
682
|
-
self.
|
|
692
|
+
self.log("DEBUG", "tree_of_thought", {"response": tot_response, "active_tools": active_tools})
|
|
683
693
|
|
|
684
694
|
# 准备API参数
|
|
685
|
-
self.chat_params =
|
|
695
|
+
self.chat_params = {
|
|
686
696
|
"model": self.model,
|
|
687
697
|
"messages": [{"role": "system", "content": system_prompt}] + history + [{"role": "user", "content": query}],
|
|
688
698
|
"stream": stream
|
|
@@ -704,7 +714,7 @@ class LightAgent:
|
|
|
704
714
|
self.chat_params["session_id"] = traceid
|
|
705
715
|
|
|
706
716
|
# 调用模型
|
|
707
|
-
self.
|
|
717
|
+
self.log("DEBUG", "first_request_params", {"params": self.chat_params})
|
|
708
718
|
response = self.client.chat.completions.create(**self.chat_params)
|
|
709
719
|
return self._core_run_logic(response, stream, max_retry)
|
|
710
720
|
|
|
@@ -750,7 +760,7 @@ class LightAgent:
|
|
|
750
760
|
output = ""
|
|
751
761
|
function_call_name = ""
|
|
752
762
|
tool_calls = response.choices[0].message.tool_calls
|
|
753
|
-
self.
|
|
763
|
+
self.log("DEBUG", "non_stream tool_calls", {"tool_calls": tool_calls})
|
|
754
764
|
|
|
755
765
|
# 遍历所有工具调用
|
|
756
766
|
for tool_call in tool_calls:
|
|
@@ -758,7 +768,7 @@ class LightAgent:
|
|
|
758
768
|
|
|
759
769
|
# 尝试自动修复常见转义问题
|
|
760
770
|
fixed_args = function_call.arguments.replace('\\"', '"').replace('\\\\', '\\')
|
|
761
|
-
self.
|
|
771
|
+
self.log("DEBUG", "non_stream function_call", {"function_call": fixed_args})
|
|
762
772
|
|
|
763
773
|
# 解析函数参数
|
|
764
774
|
function_args = json.loads(fixed_args)
|
|
@@ -807,14 +817,14 @@ class LightAgent:
|
|
|
807
817
|
pass # 如果不是 JSON 字符串,保持原样
|
|
808
818
|
single_tool_response = combined_response # 处理单个工具的方法
|
|
809
819
|
|
|
810
|
-
self.
|
|
811
|
-
|
|
820
|
+
self.log("INFO", "non_stream single_tool_response",
|
|
821
|
+
{"single_tool_response": single_tool_response})
|
|
812
822
|
|
|
813
823
|
# 将单个工具的响应结果添加到列表中
|
|
814
824
|
tool_responses.append(single_tool_response)
|
|
815
825
|
|
|
816
826
|
# 将所有工具调用的结果合并为一个字符串
|
|
817
|
-
self.
|
|
827
|
+
self.log("DEBUG", "non_stream tool_responses", {"tool_responses": tool_responses})
|
|
818
828
|
|
|
819
829
|
combined_tool_response = "\n".join(tool_responses)
|
|
820
830
|
|
|
@@ -834,14 +844,14 @@ class LightAgent:
|
|
|
834
844
|
else:
|
|
835
845
|
# 返回最终回复
|
|
836
846
|
reply = response.choices[0].message.content
|
|
837
|
-
self.
|
|
847
|
+
self.log("INFO", "non_stream final_reply", {"reply": reply})
|
|
838
848
|
return reply
|
|
839
849
|
|
|
840
850
|
# 更新响应
|
|
841
851
|
if function_call_name == 'finish':
|
|
842
852
|
return # 如果最后调用了finish工具,则结束生成器
|
|
843
853
|
# print("params:",self.chat_params)
|
|
844
|
-
self.
|
|
854
|
+
self.log("DEBUG", "non_stream chat-completions params", {"params": self.chat_params})
|
|
845
855
|
|
|
846
856
|
try:
|
|
847
857
|
response = self.client.chat.completions.create(**self.chat_params)
|
|
@@ -849,7 +859,7 @@ class LightAgent:
|
|
|
849
859
|
print(f"An error occurred: {e}")
|
|
850
860
|
|
|
851
861
|
# 重试次数用尽
|
|
852
|
-
self.
|
|
862
|
+
self.log("ERROR", "max_retry_reached", {"message": "Failed to generate a valid response."})
|
|
853
863
|
return "Failed to generate a valid response."
|
|
854
864
|
|
|
855
865
|
def _run_stream_logic(self, response, max_retry) -> Generator[str, None, None]:
|
|
@@ -889,7 +899,7 @@ class LightAgent:
|
|
|
889
899
|
tool_calls[tool_call_index]["arguments"] += tool_call_delta.function.arguments
|
|
890
900
|
|
|
891
901
|
except (IndexError, AttributeError, KeyError) as e:
|
|
892
|
-
self.
|
|
902
|
+
self.log("ERROR", "tool_call_error", {
|
|
893
903
|
"error": str(e),
|
|
894
904
|
"traceback": traceback.format_exc()
|
|
895
905
|
})
|
|
@@ -897,13 +907,13 @@ class LightAgent:
|
|
|
897
907
|
# 如果流式输出结束
|
|
898
908
|
finish_reason = chunk.choices[0].finish_reason if chunk.choices else None
|
|
899
909
|
if finish_reason == "stop" and not any(tc["name"] for tc in tool_calls):
|
|
900
|
-
self.
|
|
910
|
+
self.log("INFO", "stream_response", {"output": output})
|
|
901
911
|
return # 结束生成器
|
|
902
912
|
|
|
903
913
|
# 如果工具调用结束
|
|
904
914
|
elif finish_reason in ("tool_calls", "stop") and any(tc["name"] for tc in tool_calls):
|
|
905
915
|
# 遍历所有工具调用
|
|
906
|
-
self.
|
|
916
|
+
self.log("DEBUG", "stream tool_calls", {"tool_calls": tool_calls})
|
|
907
917
|
for tool_call in tool_calls:
|
|
908
918
|
if tool_call["name"]: # 确保工具调用有名称
|
|
909
919
|
tool_name = tool_call["name"]
|
|
@@ -922,7 +932,7 @@ class LightAgent:
|
|
|
922
932
|
"title": tool_title,
|
|
923
933
|
"arguments": arguments,
|
|
924
934
|
}
|
|
925
|
-
self.
|
|
935
|
+
self.log("INFO", "stream function_call", {"tool_call_start": tool_call_info})
|
|
926
936
|
# 将工具的调用信息推送给开发者
|
|
927
937
|
yield tool_call_info
|
|
928
938
|
|
|
@@ -940,7 +950,7 @@ class LightAgent:
|
|
|
940
950
|
for json_obj in json_objects:
|
|
941
951
|
# 尝试自动修复常见转义问题
|
|
942
952
|
fixed_args = json_obj.replace('\\"', '"').replace('\\\\', '\\')
|
|
943
|
-
self.
|
|
953
|
+
self.log("DEBUG", "stream fixed_args", {"fixed_args": fixed_args})
|
|
944
954
|
|
|
945
955
|
# 解析参数
|
|
946
956
|
function_args = json.loads(fixed_args)
|
|
@@ -965,8 +975,8 @@ class LightAgent:
|
|
|
965
975
|
"title": tool_title,
|
|
966
976
|
"output": chunk,
|
|
967
977
|
}
|
|
968
|
-
self.
|
|
969
|
-
|
|
978
|
+
self.log("DEBUG", "stream tool_output",
|
|
979
|
+
{"tool_output": tool_output})
|
|
970
980
|
yield tool_output
|
|
971
981
|
# 将工具的调用信息推送给开发者
|
|
972
982
|
if tool_name == 'finish':
|
|
@@ -987,8 +997,8 @@ class LightAgent:
|
|
|
987
997
|
yield tool_output
|
|
988
998
|
|
|
989
999
|
# 记录工具响应
|
|
990
|
-
self.
|
|
991
|
-
|
|
1000
|
+
self.log("INFO", "stream single_tool_response",
|
|
1001
|
+
{"single_tool_response": single_tool_response})
|
|
992
1002
|
|
|
993
1003
|
# 将单个工具的响应结果保存到列表中
|
|
994
1004
|
tool_responses.append(single_tool_response)
|
|
@@ -996,18 +1006,18 @@ class LightAgent:
|
|
|
996
1006
|
# 检查是否调用了finish工具
|
|
997
1007
|
if tool_name == 'finish':
|
|
998
1008
|
finish_called = True
|
|
999
|
-
self.
|
|
1009
|
+
self.log("INFO", "finish_tool_called", {"response": combined_response})
|
|
1000
1010
|
|
|
1001
1011
|
except json.JSONDecodeError as e:
|
|
1002
1012
|
error_msg = f"JSON解析错误: {str(e)}\n参数: {arguments}"
|
|
1003
|
-
self.
|
|
1004
|
-
|
|
1013
|
+
self.log("ERROR", "json_decode_error",
|
|
1014
|
+
{"tool": tool_name, "title": tool_title, "error": error_msg})
|
|
1005
1015
|
tool_responses.append(error_msg)
|
|
1006
1016
|
yield {"name": tool_name, "title": tool_title, "error": error_msg}
|
|
1007
1017
|
|
|
1008
1018
|
except Exception as e:
|
|
1009
1019
|
error_msg = f"工具调用错误: {str(e)}\n{traceback.format_exc()}"
|
|
1010
|
-
self.
|
|
1020
|
+
self.log("ERROR", "tool_execution_error", {
|
|
1011
1021
|
"tool": tool_name,
|
|
1012
1022
|
"title": tool_title,
|
|
1013
1023
|
"error": error_msg
|
|
@@ -1040,12 +1050,12 @@ class LightAgent:
|
|
|
1040
1050
|
)
|
|
1041
1051
|
|
|
1042
1052
|
# 创建新的响应流
|
|
1043
|
-
self.
|
|
1053
|
+
self.log("DEBUG", "stream next_request_params", {"params": self.chat_params})
|
|
1044
1054
|
response = self.client.chat.completions.create(**self.chat_params)
|
|
1045
1055
|
break
|
|
1046
1056
|
|
|
1047
1057
|
# 重试次数用尽
|
|
1048
|
-
self.
|
|
1058
|
+
self.log("ERROR", "max_retry_reached", {"message": "Failed to generate a valid response."})
|
|
1049
1059
|
yield "Failed to generate a valid response."
|
|
1050
1060
|
|
|
1051
1061
|
def _handle_task_transfer(
|
|
@@ -1065,9 +1075,9 @@ class LightAgent:
|
|
|
1065
1075
|
intent = self._detect_intent(query, light_swarm)
|
|
1066
1076
|
if intent and intent.get("transfer_to"):
|
|
1067
1077
|
target_agent_name = intent["transfer_to"]
|
|
1068
|
-
self.
|
|
1078
|
+
self.log("INFO", "detect_intent", {"intent": intent})
|
|
1069
1079
|
if target_agent_name == self.name:
|
|
1070
|
-
self.
|
|
1080
|
+
self.log("INFO", "self_transfer_detected", {"target_agent": target_agent_name})
|
|
1071
1081
|
return None # 如果是自身,直接返回 None
|
|
1072
1082
|
if stream:
|
|
1073
1083
|
return self._handle_task_transfer_stream(light_swarm.agents[target_agent_name], query, light_swarm)
|
|
@@ -1089,18 +1099,18 @@ class LightAgent:
|
|
|
1089
1099
|
:param light_swarm: LightSwarm 实例。
|
|
1090
1100
|
:return: 生成器,用于流式输出。
|
|
1091
1101
|
"""
|
|
1092
|
-
self.
|
|
1102
|
+
self.log("INFO", "transfer_to_agent", {"from": self.name, "to": target_agent.name, "context": context})
|
|
1093
1103
|
|
|
1094
1104
|
# 检查目标代理是否有效
|
|
1095
1105
|
if not hasattr(target_agent, 'run'):
|
|
1096
|
-
self.
|
|
1106
|
+
self.log("ERROR", "invalid_target_agent", {"target_agent": target_agent})
|
|
1097
1107
|
yield "Failed to transfer task: invalid target agent"
|
|
1098
1108
|
return
|
|
1099
1109
|
|
|
1100
1110
|
try:
|
|
1101
1111
|
yield from target_agent.run(context, light_swarm=light_swarm, stream=True)
|
|
1102
1112
|
except Exception as e:
|
|
1103
|
-
self.
|
|
1113
|
+
self.log("ERROR", "run_failed", {"error": str(e)})
|
|
1104
1114
|
raise # 重新抛出异常以便调试
|
|
1105
1115
|
|
|
1106
1116
|
def _handle_task_transfer_non_stream(
|
|
@@ -1117,11 +1127,11 @@ class LightAgent:
|
|
|
1117
1127
|
:param light_swarm: LightSwarm 实例。
|
|
1118
1128
|
:return: 字符串,表示非流式输出结果。
|
|
1119
1129
|
"""
|
|
1120
|
-
self.
|
|
1130
|
+
self.log("INFO", "transfer_to_agent", {"from": self.name, "to": target_agent.name, "context": context})
|
|
1121
1131
|
|
|
1122
1132
|
# 检查目标代理是否有效
|
|
1123
1133
|
if not hasattr(target_agent, 'run'):
|
|
1124
|
-
self.
|
|
1134
|
+
self.log("ERROR", "invalid_target_agent", {"target_agent": target_agent})
|
|
1125
1135
|
return "Failed to transfer task: invalid target agent"
|
|
1126
1136
|
|
|
1127
1137
|
try:
|
|
@@ -1130,7 +1140,7 @@ class LightAgent:
|
|
|
1130
1140
|
return "".join(result) # 将生成器转换为字符串
|
|
1131
1141
|
return result
|
|
1132
1142
|
except Exception as e:
|
|
1133
|
-
self.
|
|
1143
|
+
self.log("ERROR", "run_failed", {"error": str(e)})
|
|
1134
1144
|
raise # 重新抛出异常以便调试
|
|
1135
1145
|
|
|
1136
1146
|
def _build_context(self, related_memories):
|
|
@@ -1147,7 +1157,7 @@ class LightAgent:
|
|
|
1147
1157
|
return ""
|
|
1148
1158
|
|
|
1149
1159
|
prompt = f"\n##用户偏好 \n用户之前提到了\n{memory_context}。"
|
|
1150
|
-
self.
|
|
1160
|
+
self.log("DEBUG", "related_memories", {"memory_context": memory_context})
|
|
1151
1161
|
return prompt
|
|
1152
1162
|
|
|
1153
1163
|
def _build_agent_memory(self, agent_memories):
|
|
@@ -1165,7 +1175,7 @@ class LightAgent:
|
|
|
1165
1175
|
return ""
|
|
1166
1176
|
|
|
1167
1177
|
prompt = f"\n##以下是解决该问题的相关补充信息:\n{memory_context}。"
|
|
1168
|
-
self.
|
|
1178
|
+
self.log("DEBUG", "agent_memories", {"memory_context": memory_context})
|
|
1169
1179
|
return prompt
|
|
1170
1180
|
|
|
1171
1181
|
def run_thought(self, query: str) -> tuple:
|
|
@@ -1178,7 +1188,7 @@ class LightAgent:
|
|
|
1178
1188
|
current_date = now.strftime("%Y-%m-%d")
|
|
1179
1189
|
current_time = now.strftime("%H:%M:%S")
|
|
1180
1190
|
system_prompt = f"""你是一个智能助手,请根据用户输入的问题,结合工具使用计划,生成一个思维树,并按照思维树依次调用工具步骤,最终生成一个最终回答。\n 今日的日期: {current_date} 当前时间: {current_time} \n 工具列表: {tools}"""
|
|
1181
|
-
self.
|
|
1191
|
+
self.log("DEBUG", "run_thought", {"system_prompt": system_prompt})
|
|
1182
1192
|
|
|
1183
1193
|
try:
|
|
1184
1194
|
# 1. 第一次请求,生成初始的工具使用计划
|
|
@@ -1187,7 +1197,7 @@ class LightAgent:
|
|
|
1187
1197
|
stream=False)
|
|
1188
1198
|
response = self.tot_client.chat.completions.create(**params)
|
|
1189
1199
|
thought_response = response.choices[0].message.content
|
|
1190
|
-
self.
|
|
1200
|
+
self.log("DEBUG", "thought_response", {"response": thought_response})
|
|
1191
1201
|
|
|
1192
1202
|
# 2. 第二次请求,请求大模型反思并生成新的工具使用规划
|
|
1193
1203
|
reflection_prompt = "请反思你的回答,请严格按照<工具列表>中的工具来规划,不可以创造其他新的工具。请输出新的任务规划,不要输出其他分析和回答。"
|
|
@@ -1196,10 +1206,10 @@ class LightAgent:
|
|
|
1196
1206
|
{"role": "assistant", "content": thought_response},
|
|
1197
1207
|
{"role": "user", "content": reflection_prompt}
|
|
1198
1208
|
], stream=False)
|
|
1199
|
-
self.
|
|
1209
|
+
self.log("DEBUG", "reflection_params", {"params": reflection_params})
|
|
1200
1210
|
reflection_response = self.tot_client.chat.completions.create(**reflection_params)
|
|
1201
1211
|
refined_content = reflection_response.choices[0].message.content
|
|
1202
|
-
self.
|
|
1212
|
+
self.log("DEBUG", "reflection_response", {"response": refined_content})
|
|
1203
1213
|
|
|
1204
1214
|
# 获取工具的使用集合
|
|
1205
1215
|
tool_reflection_prompt = """请严格按以下要求执行:
|
|
@@ -1221,21 +1231,21 @@ class LightAgent:
|
|
|
1221
1231
|
stream=False
|
|
1222
1232
|
)
|
|
1223
1233
|
|
|
1224
|
-
self.
|
|
1234
|
+
self.log("DEBUG", "tool_reflection_params", {"params": tool_reflection_params})
|
|
1225
1235
|
tool_reflection_response = self.tot_client.chat.completions.create(**tool_reflection_params)
|
|
1226
1236
|
tool_reflection_result = tool_reflection_response.choices[0].message.content
|
|
1227
|
-
self.
|
|
1237
|
+
self.log("DEBUG", "tool_reflection_result", {"result": tool_reflection_result})
|
|
1228
1238
|
|
|
1229
1239
|
# 3.执行自适应工具过滤
|
|
1230
1240
|
current_tools = []
|
|
1231
1241
|
if self.filter_tools:
|
|
1232
1242
|
current_tools = self.tool_registry.filter_tools(tool_reflection_result)
|
|
1233
|
-
self.
|
|
1243
|
+
self.log("DEBUG", "current_tools", {"get_tools": current_tools})
|
|
1234
1244
|
|
|
1235
1245
|
return refined_content, current_tools
|
|
1236
1246
|
|
|
1237
1247
|
except Exception as e:
|
|
1238
|
-
self.
|
|
1248
|
+
self.log("ERROR", "run_thought_failure", {"error": str(e)})
|
|
1239
1249
|
raise RuntimeError(f"思维链执行失败: {str(e)}") from e
|
|
1240
1250
|
|
|
1241
1251
|
def _detect_intent(self, query: str, light_swarm=None) -> Optional[Dict]:
|
|
@@ -1272,7 +1282,7 @@ class LightAgent:
|
|
|
1272
1282
|
messages=[{"role": "system", "content": prompt}]
|
|
1273
1283
|
)
|
|
1274
1284
|
intent = response.choices[0].message.content
|
|
1275
|
-
self.
|
|
1285
|
+
self.log("DEBUG", "detect_intent", {"intent": intent})
|
|
1276
1286
|
|
|
1277
1287
|
# # 使用正则表达式解析意图
|
|
1278
1288
|
# match = re.search(r"transfer to (\w+)", intent, re.IGNORECASE)
|
|
@@ -1304,11 +1314,11 @@ class LightAgent:
|
|
|
1304
1314
|
:param stream: 是否启用流式输出。
|
|
1305
1315
|
:return: 如果 stream=True,返回生成器;否则返回完整结果字符串。
|
|
1306
1316
|
"""
|
|
1307
|
-
self.
|
|
1317
|
+
self.log("INFO", "transfer_to_agent", {"from": self.name, "to": target_agent.name, "context": context})
|
|
1308
1318
|
|
|
1309
1319
|
# 检查目标代理是否有效
|
|
1310
1320
|
if not hasattr(target_agent, 'run'):
|
|
1311
|
-
self.
|
|
1321
|
+
self.log("ERROR", "invalid_target_agent", {"target_agent": target_agent})
|
|
1312
1322
|
return "Failed to transfer task: invalid target agent"
|
|
1313
1323
|
#
|
|
1314
1324
|
# # 调用目标代理的 run 方法
|
|
@@ -1328,7 +1338,7 @@ class LightAgent:
|
|
|
1328
1338
|
return "".join(result) # 将生成器转换为字符串
|
|
1329
1339
|
return result
|
|
1330
1340
|
except Exception as e:
|
|
1331
|
-
self.
|
|
1341
|
+
self.log("ERROR", "run_failed", {"error": str(e)})
|
|
1332
1342
|
raise # 重新抛出异常以便调试
|
|
1333
1343
|
|
|
1334
1344
|
def create_tool(self, user_input: str, tools_directory: str = "tools"):
|
|
@@ -1405,19 +1415,19 @@ get_weather.tool_info = {
|
|
|
1405
1415
|
tool_code = tool_data.get("tool_code")
|
|
1406
1416
|
|
|
1407
1417
|
if not tool_name or not tool_code:
|
|
1408
|
-
self.
|
|
1418
|
+
self.log("ERROR", "invalid_tool_data", {"tool_data": tool_data})
|
|
1409
1419
|
continue
|
|
1410
1420
|
|
|
1411
1421
|
# 保存生成的代码到 tools 目录
|
|
1412
1422
|
tool_path = os.path.join(tools_directory, f"{tool_name}.py")
|
|
1413
1423
|
with open(tool_path, "w", encoding="utf-8") as f:
|
|
1414
1424
|
f.write(tool_code)
|
|
1415
|
-
self.
|
|
1425
|
+
self.log("INFO", "tool_created", {"tool_name": tool_name, "tool_path": tool_path})
|
|
1416
1426
|
|
|
1417
1427
|
# 自动加载新创建的工具
|
|
1418
1428
|
self.load_tools([tool_name], tools_directory)
|
|
1419
1429
|
except Exception as e:
|
|
1420
|
-
self.
|
|
1430
|
+
self.log("ERROR", "tool_creation_failed", {"error": str(e)})
|
|
1421
1431
|
|
|
1422
1432
|
|
|
1423
1433
|
class LightSwarm:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: LightAgent
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.3
|
|
4
4
|
Summary: LightAgent: Lightweight AI agent framework with memory, tools & tree-of-thought. Supports multi-agent collaboration, self-learning, and major LLMs (OpenAI/DeepSeek/Qwen). Open-source with MCP/SSE protocol integration.
|
|
5
5
|
Home-page: https://github.com/wxai-space/LightAgent
|
|
6
6
|
License: Apache 2.0
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "LightAgent"
|
|
3
|
-
version = "0.4.
|
|
3
|
+
version = "0.4.3"
|
|
4
4
|
description = "LightAgent: Lightweight AI agent framework with memory, tools & tree-of-thought. Supports multi-agent collaboration, self-learning, and major LLMs (OpenAI/DeepSeek/Qwen). Open-source with MCP/SSE protocol integration."
|
|
5
5
|
authors = ["caiweige <caiweige@qq.com>"]
|
|
6
6
|
license = "Apache 2.0"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|