LightAgent 0.4.0__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.0
7
- 最后更新: 2025-06-12
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.0" # 你可以根据需要设置版本号
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.0" # 将版本号放在类中
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,25 +682,31 @@ 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
681
689
  }
682
690
 
691
+ # 添加参数
692
+ if metadata:
693
+ for key, value in metadata.items():
694
+ self.chat_params[key] = value
695
+
683
696
  # 添加工具
684
697
  tools = active_tools or self.tool_registry.get_tools()
685
698
  if tools:
686
- params["tools"] = tools
687
- params["tool_choice"] = "auto"
699
+ self.chat_params["tools"] = tools
700
+ self.chat_params["tool_choice"] = "auto"
688
701
 
689
702
  # 添加跟踪会话
690
703
  if hasattr(self, 'tracetools') and self.tracetools:
691
- params["session_id"] = traceid
704
+ self.chat_params["session_id"] = traceid
692
705
 
693
706
  # 调用模型
694
- response = self.client.chat.completions.create(**params)
695
- 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)
696
710
 
697
711
  def _add_memory_context(self, query: str, user_id: str) -> str:
698
712
  """添加记忆上下文"""
@@ -717,14 +731,14 @@ class LightAgent:
717
731
 
718
732
  return f"{context}\n##用户提问:\n{query}" if context else query
719
733
 
720
- 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]:
721
735
  """核心运行逻辑"""
722
736
  if stream:
723
- return self._run_stream_logic(response, params, max_retry)
737
+ return self._run_stream_logic(response, max_retry)
724
738
  else:
725
- return self._run_non_stream_logic(response, params, max_retry)
739
+ return self._run_non_stream_logic(response, max_retry)
726
740
 
727
- 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]:
728
742
  """
729
743
  非流式处理逻辑。
730
744
  """
@@ -793,7 +807,8 @@ class LightAgent:
793
807
  pass # 如果不是 JSON 字符串,保持原样
794
808
  single_tool_response = combined_response # 处理单个工具的方法
795
809
 
796
- 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})
797
812
 
798
813
  # 将单个工具的响应结果添加到列表中
799
814
  tool_responses.append(single_tool_response)
@@ -804,13 +819,13 @@ class LightAgent:
804
819
  combined_tool_response = "\n".join(tool_responses)
805
820
 
806
821
  # 将工具调用和响应添加到消息列表中
807
- params["messages"].append(
822
+ self.chat_params["messages"].append(
808
823
  {
809
824
  "role": "assistant",
810
825
  "content": f"使用工具: \n {json.dumps([tool_call.function.model_dump() for tool_call in tool_calls], ensure_ascii=False)}\n",
811
826
  }
812
827
  )
813
- params["messages"].append(
828
+ self.chat_params["messages"].append(
814
829
  {
815
830
  "role": "user",
816
831
  "content": f"工具响应内容:\n {combined_tool_response} \n 请给出下一步输出",
@@ -825,11 +840,11 @@ class LightAgent:
825
840
  # 更新响应
826
841
  if function_call_name == 'finish':
827
842
  return # 如果最后调用了finish工具,则结束生成器
828
- # print("params:",params)
829
- 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})
830
845
 
831
846
  try:
832
- response = self.client.chat.completions.create(**params)
847
+ response = self.client.chat.completions.create(**self.chat_params)
833
848
  except Exception as e:
834
849
  print(f"An error occurred: {e}")
835
850
 
@@ -837,7 +852,7 @@ class LightAgent:
837
852
  self.logger.log("ERROR", "max_retry_reached", {"message": "Failed to generate a valid response."})
838
853
  return "Failed to generate a valid response."
839
854
 
840
- 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]:
841
856
  """流式处理逻辑"""
842
857
  for _ in range(max_retry):
843
858
  # 初始化变量
@@ -950,7 +965,8 @@ class LightAgent:
950
965
  "title": tool_title,
951
966
  "output": chunk,
952
967
  }
953
- self.logger.log("DEBUG", "stream tool_output", {"tool_output": tool_output})
968
+ self.logger.log("DEBUG", "stream tool_output",
969
+ {"tool_output": tool_output})
954
970
  yield tool_output
955
971
  # 将工具的调用信息推送给开发者
956
972
  if tool_name == 'finish':
@@ -972,7 +988,7 @@ class LightAgent:
972
988
 
973
989
  # 记录工具响应
974
990
  self.logger.log("INFO", "stream single_tool_response",
975
- {"single_tool_response": single_tool_response})
991
+ {"single_tool_response": single_tool_response})
976
992
 
977
993
  # 将单个工具的响应结果保存到列表中
978
994
  tool_responses.append(single_tool_response)
@@ -984,7 +1000,8 @@ class LightAgent:
984
1000
 
985
1001
  except json.JSONDecodeError as e:
986
1002
  error_msg = f"JSON解析错误: {str(e)}\n参数: {arguments}"
987
- 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})
988
1005
  tool_responses.append(error_msg)
989
1006
  yield {"name": tool_name, "title": tool_title, "error": error_msg}
990
1007
 
@@ -1009,13 +1026,13 @@ class LightAgent:
1009
1026
  ensure_ascii=False)
1010
1027
 
1011
1028
  # 添加工具调用和响应到消息历史
1012
- params["messages"].append(
1029
+ self.chat_params["messages"].append(
1013
1030
  {
1014
1031
  "role": "assistant",
1015
1032
  "content": f"使用工具: \n {tool_str}\n"
1016
1033
  }
1017
1034
  )
1018
- params["messages"].append(
1035
+ self.chat_params["messages"].append(
1019
1036
  {
1020
1037
  "role": "user",
1021
1038
  "content": combined_tool_response,
@@ -1023,15 +1040,14 @@ class LightAgent:
1023
1040
  )
1024
1041
 
1025
1042
  # 创建新的响应流
1026
- self.logger.log("DEBUG", "stream next_request_params", {"params": params})
1027
- 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)
1028
1045
  break
1029
1046
 
1030
1047
  # 重试次数用尽
1031
1048
  self.logger.log("ERROR", "max_retry_reached", {"message": "Failed to generate a valid response."})
1032
1049
  yield "Failed to generate a valid response."
1033
1050
 
1034
-
1035
1051
  def _handle_task_transfer(
1036
1052
  self,
1037
1053
  query: str,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: LightAgent
3
- Version: 0.4.0
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.0"
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