aient 1.1.61__py3-none-any.whl → 1.1.62__py3-none-any.whl

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.
aient/models/base.py CHANGED
@@ -53,20 +53,10 @@ class BaseLLM:
53
53
  "https": proxy,
54
54
  },
55
55
  )
56
- if proxy := (
57
- proxy or os.environ.get("all_proxy") or os.environ.get("ALL_PROXY") or None
58
- ):
59
- if "socks5h" not in proxy:
60
- self.aclient = httpx.AsyncClient(
61
- follow_redirects=True,
62
- proxies=proxy,
63
- timeout=timeout,
64
- )
65
- else:
66
- self.aclient = httpx.AsyncClient(
67
- follow_redirects=True,
68
- timeout=timeout,
69
- )
56
+ self._aclient = None
57
+ self._proxy = proxy
58
+ self._timeout = timeout
59
+ self._loop = None
70
60
 
71
61
  self.conversation: dict[str, list[dict]] = {
72
62
  "default": [
@@ -83,6 +73,33 @@ class BaseLLM:
83
73
  self.use_plugins = use_plugins
84
74
  self.print_log: bool = print_log
85
75
 
76
+ def _get_aclient(self):
77
+ """
78
+ Lazily initialize and return the httpx.AsyncClient.
79
+ This method ensures the client is always bound to a running event loop.
80
+ """
81
+ import asyncio
82
+ try:
83
+ loop = asyncio.get_running_loop()
84
+ except RuntimeError:
85
+ loop = asyncio.new_event_loop()
86
+ asyncio.set_event_loop(loop)
87
+
88
+ if self._aclient is None or self._aclient.is_closed or self._loop is not loop:
89
+ self._loop = loop
90
+ proxy = self._proxy or os.environ.get("all_proxy") or os.environ.get("ALL_PROXY") or None
91
+ proxies = proxy if proxy and "socks5h" not in proxy else None
92
+ self._aclient = httpx.AsyncClient(
93
+ follow_redirects=True,
94
+ proxy=proxies,
95
+ timeout=self._timeout,
96
+ )
97
+ return self._aclient
98
+
99
+ @property
100
+ def aclient(self):
101
+ return self._get_aclient()
102
+
86
103
  def add_to_conversation(
87
104
  self,
88
105
  message: list,
@@ -196,7 +213,6 @@ class BaseLLM:
196
213
  **kwargs,
197
214
  ):
198
215
  response += chunk
199
- # full_response: str = "".join([r async for r in response])
200
216
  full_response: str = "".join(response)
201
217
  return full_response
202
218
 
aient/models/chatgpt.py CHANGED
@@ -711,11 +711,14 @@ class chatgpt(BaseLLM):
711
711
  # 替换原来的获取请求体的代码
712
712
  # json_post = next(async_generator_to_sync(get_post_body_async()))
713
713
  try:
714
- url, headers, json_post, engine_type = asyncio.run(get_post_body_async())
715
- except RuntimeError:
716
- # 如果已经在事件循环中,则使用不同的方法
717
714
  loop = asyncio.get_event_loop()
718
- url, headers, json_post, engine_type = loop.run_until_complete(get_post_body_async())
715
+ if loop.is_closed():
716
+ loop = asyncio.new_event_loop()
717
+ asyncio.set_event_loop(loop)
718
+ except RuntimeError:
719
+ loop = asyncio.new_event_loop()
720
+ asyncio.set_event_loop(loop)
721
+ url, headers, json_post, engine_type = loop.run_until_complete(get_post_body_async())
719
722
 
720
723
  self.truncate_conversation(convo_id=convo_id)
721
724
 
aient/utils/scripts.py CHANGED
@@ -212,6 +212,8 @@ def async_generator_to_sync(async_gen):
212
212
  # 清理所有待处理的任务
213
213
  tasks = [t for t in asyncio.all_tasks(loop) if not t.done()]
214
214
  if tasks:
215
+ for task in tasks:
216
+ task.cancel()
215
217
  loop.run_until_complete(asyncio.gather(*tasks, return_exceptions=True))
216
218
  loop.run_until_complete(loop.shutdown_asyncgens())
217
219
  loop.close()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aient
3
- Version: 1.1.61
3
+ Version: 1.1.62
4
4
  Summary: Aient: The Awakening of Agent.
5
5
  Requires-Python: >=3.11
6
6
  Description-Content-Type: text/markdown
@@ -11,8 +11,8 @@ aient/core/test/test_image.py,sha256=_T4peNGdXKBHHxyQNx12u-NTyFE8TlYI6NvvagsG2LE
11
11
  aient/core/test/test_payload.py,sha256=8jBiJY1uidm1jzL-EiK0s6UGmW9XkdsuuKFGrwFhFkw,2755
12
12
  aient/models/__init__.py,sha256=ZTiZgbfBPTjIPSKURE7t6hlFBVLRS9lluGbmqc1WjxQ,43
13
13
  aient/models/audio.py,sha256=kRd-8-WXzv4vwvsTGwnstK-WR8--vr9CdfCZzu8y9LA,1934
14
- aient/models/base.py,sha256=z-Z0pJfTN2x0cuwfvu0BdMRY9O-RmLwHEnBIJN1x4Fg,6719
15
- aient/models/chatgpt.py,sha256=pk34Oggn2z3Rnj65PRKufYknBgTK8yngAaMOxk3Nur8,54423
14
+ aient/models/base.py,sha256=-nnihYnx-vHZMqeVO9ljjt3k4FcD3n-iMk4tT-10nRQ,7232
15
+ aient/models/chatgpt.py,sha256=vpzjALLjxZTjUac_VZulxU1oKzI44LL1H-n3i3YMt5M,54472
16
16
  aient/plugins/__init__.py,sha256=p3KO6Aa3Lupos4i2SjzLQw1hzQTigOAfEHngsldrsyk,986
17
17
  aient/plugins/arXiv.py,sha256=yHjb6PS3GUWazpOYRMKMzghKJlxnZ5TX8z9F6UtUVow,1461
18
18
  aient/plugins/config.py,sha256=2DXH-LP9KGl_P4467chJu3q4AAbX5nSn4DIkdI0aYH8,7105
@@ -29,9 +29,9 @@ aient/plugins/websearch.py,sha256=aPsBjUQ3zQ4gzNrbVq7BMh28ENj9h_fSAeJFF2h9TNk,15
29
29
  aient/plugins/write_file.py,sha256=Jt8fOEwqhYiSWpCbwfAr1xoi_BmFnx3076GMhuL06uI,3949
30
30
  aient/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
31
31
  aient/utils/prompt.py,sha256=UcSzKkFE4-h_1b6NofI6xgk3GoleqALRKY8VBaXLjmI,11311
32
- aient/utils/scripts.py,sha256=CjB4qq6MoltfT0AnoekFqhw3p5sH7iWz65RuImTipVo,40905
33
- aient-1.1.61.dist-info/licenses/LICENSE,sha256=XNdbcWldt0yaNXXWB_Bakoqnxb3OVhUft4MgMA_71ds,1051
34
- aient-1.1.61.dist-info/METADATA,sha256=pp6yzSaglBHddABnIYJI5V7pnye9vkaOdXwa8LTiMM8,4994
35
- aient-1.1.61.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
36
- aient-1.1.61.dist-info/top_level.txt,sha256=3oXzrP5sAVvyyqabpeq8A2_vfMtY554r4bVE-OHBrZk,6
37
- aient-1.1.61.dist-info/RECORD,,
32
+ aient/utils/scripts.py,sha256=VqtK4RFEx7KxkmcqG3lFDS1DxoNlFFGErEjopVcc8IE,40974
33
+ aient-1.1.62.dist-info/licenses/LICENSE,sha256=XNdbcWldt0yaNXXWB_Bakoqnxb3OVhUft4MgMA_71ds,1051
34
+ aient-1.1.62.dist-info/METADATA,sha256=wROTpkVN1qjMAro9hZU50FrJec6X6pIAOEIpA_oXR1s,4994
35
+ aient-1.1.62.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
36
+ aient-1.1.62.dist-info/top_level.txt,sha256=3oXzrP5sAVvyyqabpeq8A2_vfMtY554r4bVE-OHBrZk,6
37
+ aient-1.1.62.dist-info/RECORD,,
File without changes