LightAgent 0.4.1__tar.gz → 0.4.2__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.
@@ -3,8 +3,8 @@
3
3
 
4
4
  """
5
5
  作者: [weego/WXAI-Team]
6
- 版本: 0.4.1
7
- 最后更新: 2025-07-07
6
+ 版本: 0.4.2
7
+ 最后更新: 2025-07-10
8
8
  """
9
9
 
10
10
  import asyncio
@@ -30,8 +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
-
34
- __version__ = "0.4.1" # 你可以根据需要设置版本号
33
+ __version__ = "0.4.2" # 你可以根据需要设置版本号
35
34
 
36
35
 
37
36
  # openai.langfuse_auth_check()
@@ -44,6 +43,7 @@ class MemoryProtocol(Protocol):
44
43
  def retrieve(self, query: str, user_id: str) -> List[Any]:
45
44
  ...
46
45
 
46
+
47
47
  class ToolRegistry:
48
48
  """集中管理工具注册表,避免全局变量"""
49
49
 
@@ -421,8 +421,9 @@ class MCPClientManager:
421
421
  if field not in arguments:
422
422
  raise ValueError(f"缺少必要参数: {field}")
423
423
 
424
+
424
425
  class LightAgent:
425
- __version__ = "0.4.1" # 将版本号放在类中
426
+ __version__ = "0.4.2" # 将版本号放在类中
426
427
 
427
428
  def __init__(
428
429
  self,
@@ -545,6 +546,7 @@ class LightAgent:
545
546
 
546
547
  # 初始化客户端
547
548
  self._initialize_clients(tracetools, tot_api_key, tot_base_url, tot_model)
549
+ self.chat_params = {} # history 存储器
548
550
 
549
551
  def _initialize_clients(self, tracetools, tot_api_key, tot_base_url, tot_model):
550
552
  """初始化 OpenAI 客户端"""
@@ -574,6 +576,12 @@ class LightAgent:
574
576
  api_key=tot_api_key or self.api_key
575
577
  )
576
578
 
579
+ def get_history(self) -> List[Dict[str, Any]]:
580
+ """
581
+ 获取对话的history的描述(OpenAI 格式)
582
+ """
583
+ return deepcopy(self.chat_params['messages'])
584
+
577
585
  def get_tools(self) -> List[Dict[str, Any]]:
578
586
  """
579
587
  获取所有工具的描述(OpenAI 格式)
@@ -674,7 +682,7 @@ class LightAgent:
674
682
  self.logger.log("DEBUG", "tree_of_thought", {"response": tot_response, "active_tools": active_tools})
675
683
 
676
684
  # 准备API参数
677
- params = {
685
+ self.chat_params = {
678
686
  "model": self.model,
679
687
  "messages": [{"role": "system", "content": system_prompt}] + history + [{"role": "user", "content": query}],
680
688
  "stream": stream
@@ -683,22 +691,22 @@ class LightAgent:
683
691
  # 添加参数
684
692
  if metadata:
685
693
  for key, value in metadata.items():
686
- params[key] = value
694
+ self.chat_params[key] = value
687
695
 
688
696
  # 添加工具
689
697
  tools = active_tools or self.tool_registry.get_tools()
690
698
  if tools:
691
- params["tools"] = tools
692
- params["tool_choice"] = "auto"
699
+ self.chat_params["tools"] = tools
700
+ self.chat_params["tool_choice"] = "auto"
693
701
 
694
702
  # 添加跟踪会话
695
703
  if hasattr(self, 'tracetools') and self.tracetools:
696
- params["session_id"] = traceid
704
+ self.chat_params["session_id"] = traceid
697
705
 
698
706
  # 调用模型
699
- self.logger.log("DEBUG", "first_request_params", {"params": params})
700
- response = self.client.chat.completions.create(**params)
701
- return self._core_run_logic(response, params, stream, max_retry)
707
+ self.logger.log("DEBUG", "first_request_params", {"params": self.chat_params})
708
+ response = self.client.chat.completions.create(**self.chat_params)
709
+ return self._core_run_logic(response, stream, max_retry)
702
710
 
703
711
  def _add_memory_context(self, query: str, user_id: str) -> str:
704
712
  """添加记忆上下文"""
@@ -723,14 +731,14 @@ class LightAgent:
723
731
 
724
732
  return f"{context}\n##用户提问:\n{query}" if context else query
725
733
 
726
- def _core_run_logic(self, response, params, stream, max_retry) -> Union[Generator[str, None, None], str]:
734
+ def _core_run_logic(self, response, stream, max_retry) -> Union[Generator[str, None, None], str]:
727
735
  """核心运行逻辑"""
728
736
  if stream:
729
- return self._run_stream_logic(response, params, max_retry)
737
+ return self._run_stream_logic(response, max_retry)
730
738
  else:
731
- return self._run_non_stream_logic(response, params, max_retry)
739
+ return self._run_non_stream_logic(response, max_retry)
732
740
 
733
- def _run_non_stream_logic(self, response, params, max_retry) -> Union[str, None]:
741
+ def _run_non_stream_logic(self, response, max_retry) -> Union[str, None]:
734
742
  """
735
743
  非流式处理逻辑。
736
744
  """
@@ -799,7 +807,8 @@ class LightAgent:
799
807
  pass # 如果不是 JSON 字符串,保持原样
800
808
  single_tool_response = combined_response # 处理单个工具的方法
801
809
 
802
- self.logger.log("INFO", "non_stream single_tool_response", {"single_tool_response": single_tool_response})
810
+ self.logger.log("INFO", "non_stream single_tool_response",
811
+ {"single_tool_response": single_tool_response})
803
812
 
804
813
  # 将单个工具的响应结果添加到列表中
805
814
  tool_responses.append(single_tool_response)
@@ -810,13 +819,13 @@ class LightAgent:
810
819
  combined_tool_response = "\n".join(tool_responses)
811
820
 
812
821
  # 将工具调用和响应添加到消息列表中
813
- params["messages"].append(
822
+ self.chat_params["messages"].append(
814
823
  {
815
824
  "role": "assistant",
816
825
  "content": f"使用工具: \n {json.dumps([tool_call.function.model_dump() for tool_call in tool_calls], ensure_ascii=False)}\n",
817
826
  }
818
827
  )
819
- params["messages"].append(
828
+ self.chat_params["messages"].append(
820
829
  {
821
830
  "role": "user",
822
831
  "content": f"工具响应内容:\n {combined_tool_response} \n 请给出下一步输出",
@@ -831,11 +840,11 @@ class LightAgent:
831
840
  # 更新响应
832
841
  if function_call_name == 'finish':
833
842
  return # 如果最后调用了finish工具,则结束生成器
834
- # print("params:",params)
835
- self.logger.log("DEBUG", "non_stream chat-completions params", {"params": params})
843
+ # print("params:",self.chat_params)
844
+ self.logger.log("DEBUG", "non_stream chat-completions params", {"params": self.chat_params})
836
845
 
837
846
  try:
838
- response = self.client.chat.completions.create(**params)
847
+ response = self.client.chat.completions.create(**self.chat_params)
839
848
  except Exception as e:
840
849
  print(f"An error occurred: {e}")
841
850
 
@@ -843,7 +852,7 @@ class LightAgent:
843
852
  self.logger.log("ERROR", "max_retry_reached", {"message": "Failed to generate a valid response."})
844
853
  return "Failed to generate a valid response."
845
854
 
846
- def _run_stream_logic(self, response, params, max_retry) -> Generator[str, None, None]:
855
+ def _run_stream_logic(self, response, max_retry) -> Generator[str, None, None]:
847
856
  """流式处理逻辑"""
848
857
  for _ in range(max_retry):
849
858
  # 初始化变量
@@ -956,7 +965,8 @@ class LightAgent:
956
965
  "title": tool_title,
957
966
  "output": chunk,
958
967
  }
959
- self.logger.log("DEBUG", "stream tool_output", {"tool_output": tool_output})
968
+ self.logger.log("DEBUG", "stream tool_output",
969
+ {"tool_output": tool_output})
960
970
  yield tool_output
961
971
  # 将工具的调用信息推送给开发者
962
972
  if tool_name == 'finish':
@@ -978,7 +988,7 @@ class LightAgent:
978
988
 
979
989
  # 记录工具响应
980
990
  self.logger.log("INFO", "stream single_tool_response",
981
- {"single_tool_response": single_tool_response})
991
+ {"single_tool_response": single_tool_response})
982
992
 
983
993
  # 将单个工具的响应结果保存到列表中
984
994
  tool_responses.append(single_tool_response)
@@ -990,7 +1000,8 @@ class LightAgent:
990
1000
 
991
1001
  except json.JSONDecodeError as e:
992
1002
  error_msg = f"JSON解析错误: {str(e)}\n参数: {arguments}"
993
- self.logger.log("ERROR", "json_decode_error", {"tool": tool_name, "title": tool_title, "error": error_msg})
1003
+ self.logger.log("ERROR", "json_decode_error",
1004
+ {"tool": tool_name, "title": tool_title, "error": error_msg})
994
1005
  tool_responses.append(error_msg)
995
1006
  yield {"name": tool_name, "title": tool_title, "error": error_msg}
996
1007
 
@@ -1015,13 +1026,13 @@ class LightAgent:
1015
1026
  ensure_ascii=False)
1016
1027
 
1017
1028
  # 添加工具调用和响应到消息历史
1018
- params["messages"].append(
1029
+ self.chat_params["messages"].append(
1019
1030
  {
1020
1031
  "role": "assistant",
1021
1032
  "content": f"使用工具: \n {tool_str}\n"
1022
1033
  }
1023
1034
  )
1024
- params["messages"].append(
1035
+ self.chat_params["messages"].append(
1025
1036
  {
1026
1037
  "role": "user",
1027
1038
  "content": combined_tool_response,
@@ -1029,15 +1040,14 @@ class LightAgent:
1029
1040
  )
1030
1041
 
1031
1042
  # 创建新的响应流
1032
- self.logger.log("DEBUG", "stream next_request_params", {"params": params})
1033
- response = self.client.chat.completions.create(**params)
1043
+ self.logger.log("DEBUG", "stream next_request_params", {"params": self.chat_params})
1044
+ response = self.client.chat.completions.create(**self.chat_params)
1034
1045
  break
1035
1046
 
1036
1047
  # 重试次数用尽
1037
1048
  self.logger.log("ERROR", "max_retry_reached", {"message": "Failed to generate a valid response."})
1038
1049
  yield "Failed to generate a valid response."
1039
1050
 
1040
-
1041
1051
  def _handle_task_transfer(
1042
1052
  self,
1043
1053
  query: str,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: LightAgent
3
- Version: 0.4.1
3
+ Version: 0.4.2
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.1"
3
+ version = "0.4.2"
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