LightAgent 0.2.0__tar.gz → 0.2.1__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.
@@ -10,13 +10,14 @@ from openai import OpenAI
10
10
  import logging
11
11
  import os
12
12
  import httpx
13
-
13
+ import importlib
14
14
  # 全局工具注册表
15
15
  _FUNCTION_MAPPINGS = {} # 工具名称 -> 工具函数
16
16
  _OPENAI_FUNCTION_SCHEMAS = [] # OpenAI 格式的工具描述
17
17
  _PROMPT_FUNCTION_SCHEMAS = [] # prompt 格式的工具描述
18
18
 
19
- import importlib
19
+ __version__ = "0.2.1" # 你可以根据需要设置版本号
20
+
20
21
 
21
22
 
22
23
  def register_tool_manually(tools: List[Union[str, Callable]]) -> bool:
@@ -146,7 +147,9 @@ def get_tools_str() -> str:
146
147
  tools_str = json.dumps(_OPENAI_FUNCTION_SCHEMAS, indent=4, ensure_ascii=False)
147
148
  return tools_str
148
149
 
150
+
149
151
  class LightAgent:
152
+ __version__ = "0.2.1" # 将版本号放在类中
150
153
  def __init__(
151
154
  self,
152
155
  *,
@@ -431,6 +434,9 @@ class LightAgent:
431
434
  if response.choices[0].message.tool_calls:
432
435
  # 初始化一个列表,用于存储所有工具调用的结果
433
436
  tool_responses = []
437
+ # 初始化变量
438
+ output = ""
439
+ function_call_name = ""
434
440
  tool_calls = response.choices[0].message.tool_calls
435
441
  self.log("DEBUG", "tool_calls", {"tool_calls": tool_calls})
436
442
 
@@ -444,6 +450,7 @@ class LightAgent:
444
450
 
445
451
  # 调用工具并获取响应
446
452
  tool_response = dispatch_tool(function_call.name, function_args)
453
+ function_call_name = function_call.name
447
454
  combined_response = ""
448
455
 
449
456
  # 如果工具返回的是生成器(流式输出),则将所有 chunk 叠加
@@ -489,6 +496,8 @@ class LightAgent:
489
496
  return reply
490
497
 
491
498
  # 更新响应
499
+ if function_call_name == 'finish':
500
+ return # 如果最后调用了finish工具,则结束生成器
492
501
  response = self.client.chat.completions.create(**params)
493
502
 
494
503
  # 重试次数用尽
@@ -569,13 +578,14 @@ class LightAgent:
569
578
  for json_obj in json_objects:
570
579
  function_args = json.loads(json_obj)
571
580
  tool_response = dispatch_tool(function_call["name"], function_args)
581
+ function_call_name = function_call["name"]
572
582
 
573
583
  # 如果工具返回的是生成器(流式输出),则将所有 chunk 叠加
574
584
  if isinstance(tool_response, Generator):
575
585
  # print(f"Streaming response from tool: {function_call['name']}")
576
586
  combined_response = ""
577
587
  for chunk in tool_response:
578
- # yield chunk
588
+ yield chunk
579
589
  combined_response += chunk # 将每个 chunk 叠加
580
590
  tool_responses.append(combined_response) # 将叠加后的完整响应添加到列表
581
591
  else:
@@ -610,6 +620,8 @@ class LightAgent:
610
620
  break
611
621
 
612
622
  # 更新响应
623
+ if function_call_name == 'finish':
624
+ return # 如果最后调用了finish工具,则结束生成器
613
625
  response = self.client.chat.completions.create(**params)
614
626
 
615
627
  # 重试次数用尽