aient 1.0.29__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/utils/scripts.py ADDED
@@ -0,0 +1,235 @@
1
+ import os
2
+ import json
3
+ import base64
4
+ import tiktoken
5
+ import requests
6
+ import urllib.parse
7
+
8
+ from ..core.utils import get_image_message
9
+
10
+ def get_encode_text(text, model_name):
11
+ tiktoken.get_encoding("cl100k_base")
12
+ model_name = "gpt-3.5-turbo"
13
+ encoding = tiktoken.encoding_for_model(model_name)
14
+ encode_text = encoding.encode(text, disallowed_special=())
15
+ return encoding, encode_text
16
+
17
+ def get_text_token_len(text, model_name):
18
+ encoding, encode_text = get_encode_text(text, model_name)
19
+ return len(encode_text)
20
+
21
+ def cut_message(message: str, max_tokens: int, model_name: str):
22
+ if type(message) != str:
23
+ message = str(message)
24
+ encoding, encode_text = get_encode_text(message, model_name)
25
+ if len(encode_text) > max_tokens:
26
+ encode_text = encode_text[:max_tokens]
27
+ message = encoding.decode(encode_text)
28
+ encode_text = encoding.encode(message, disallowed_special=())
29
+ return message, len(encode_text)
30
+
31
+ import imghdr
32
+ def encode_image(image_path):
33
+ with open(image_path, "rb") as image_file:
34
+ file_content = image_file.read()
35
+ file_type = imghdr.what(None, file_content)
36
+ base64_encoded = base64.b64encode(file_content).decode('utf-8')
37
+
38
+ if file_type == 'png':
39
+ return f"data:image/png;base64,{base64_encoded}"
40
+ elif file_type in ['jpeg', 'jpg']:
41
+ return f"data:image/jpeg;base64,{base64_encoded}"
42
+ else:
43
+ raise ValueError(f"不支持的图片格式: {file_type}")
44
+
45
+ def get_doc_from_url(url):
46
+ filename = urllib.parse.unquote(url.split("/")[-1])
47
+ response = requests.get(url, stream=True)
48
+ with open(filename, 'wb') as f:
49
+ for chunk in response.iter_content(chunk_size=1024):
50
+ f.write(chunk)
51
+ return filename
52
+
53
+ def get_encode_image(image_url):
54
+ filename = get_doc_from_url(image_url)
55
+ image_path = os.getcwd() + "/" + filename
56
+ base64_image = encode_image(image_path)
57
+ os.remove(image_path)
58
+ return base64_image
59
+
60
+ from io import BytesIO
61
+ def get_audio_message(file_bytes):
62
+ try:
63
+ # 创建一个字节流对象
64
+ audio_stream = BytesIO(file_bytes)
65
+
66
+ # 直接使用字节流对象进行转录
67
+ import config
68
+ transcript = config.whisperBot.generate(audio_stream)
69
+ # print("transcript", transcript)
70
+
71
+ return transcript
72
+
73
+ except Exception as e:
74
+ return f"处理音频文件时出错: {str(e)}"
75
+
76
+ async def Document_extract(docurl, docpath=None, engine_type = None):
77
+ filename = docpath
78
+ text = None
79
+ prompt = None
80
+ if docpath and docurl and "paper.pdf" != docpath:
81
+ filename = get_doc_from_url(docurl)
82
+ docpath = os.getcwd() + "/" + filename
83
+ if filename and filename[-3:] == "pdf":
84
+ from pdfminer.high_level import extract_text
85
+ text = extract_text(docpath)
86
+ if filename and (filename[-3:] == "txt" or filename[-3:] == ".md" or filename[-3:] == ".py" or filename[-3:] == "yml"):
87
+ with open(docpath, 'r') as f:
88
+ text = f.read()
89
+ if text:
90
+ prompt = (
91
+ "Here is the document, inside <document></document> XML tags:"
92
+ "<document>"
93
+ "{}"
94
+ "</document>"
95
+ ).format(text)
96
+ if filename and filename[-3:] == "jpg" or filename[-3:] == "png" or filename[-4:] == "jpeg":
97
+ prompt = await get_image_message(docurl, engine_type)
98
+ if filename and filename[-3:] == "wav" or filename[-3:] == "mp3":
99
+ with open(docpath, "rb") as file:
100
+ file_bytes = file.read()
101
+ prompt = get_audio_message(file_bytes)
102
+ prompt = (
103
+ "Here is the text content after voice-to-text conversion, inside <voice-to-text></voice-to-text> XML tags:"
104
+ "<voice-to-text>"
105
+ "{}"
106
+ "</voice-to-text>"
107
+ ).format(prompt)
108
+ if os.path.exists(docpath):
109
+ os.remove(docpath)
110
+ return prompt
111
+
112
+ def split_json_strings(input_string):
113
+ # 初始化结果列表和当前 JSON 字符串
114
+ json_strings = []
115
+ current_json = ""
116
+ brace_count = 0
117
+
118
+ # 遍历输入字符串的每个字符
119
+ for char in input_string:
120
+ current_json += char
121
+ if char == '{':
122
+ brace_count += 1
123
+ elif char == '}':
124
+ brace_count -= 1
125
+
126
+ # 如果花括号配对完成,我们找到了一个完整的 JSON 字符串
127
+ if brace_count == 0:
128
+ # 尝试解析当前 JSON 字符串
129
+ try:
130
+ json.loads(current_json)
131
+ json_strings.append(current_json)
132
+ current_json = ""
133
+ except json.JSONDecodeError:
134
+ # 如果解析失败,继续添加字符
135
+ pass
136
+ if json_strings == []:
137
+ json_strings.append(input_string)
138
+ return json_strings
139
+
140
+ def check_json(json_data):
141
+ while True:
142
+ try:
143
+ result = split_json_strings(json_data)
144
+ if len(result) > 0:
145
+ json_data = result[0]
146
+ json.loads(json_data)
147
+ break
148
+ except json.decoder.JSONDecodeError as e:
149
+ print("JSON error:", e)
150
+ print("JSON body", repr(json_data))
151
+ if "Invalid control character" in str(e):
152
+ json_data = json_data.replace("\n", "\\n")
153
+ elif "Unterminated string starting" in str(e):
154
+ json_data += '"}'
155
+ elif "Expecting ',' delimiter" in str(e):
156
+ json_data = {"prompt": json_data}
157
+ elif "Expecting ':' delimiter" in str(e):
158
+ json_data = '{"prompt": ' + json.dumps(json_data) + '}'
159
+ elif "Expecting value: line 1 column 1" in str(e):
160
+ if json_data.startswith("prompt: "):
161
+ json_data = json_data.replace("prompt: ", "")
162
+ json_data = '{"prompt": ' + json.dumps(json_data) + '}'
163
+ else:
164
+ json_data = '{"prompt": ' + json.dumps(json_data) + '}'
165
+ return json_data
166
+
167
+ def is_surrounded_by_chinese(text, index):
168
+ left_char = text[index - 1]
169
+ if 0 < index < len(text) - 1:
170
+ right_char = text[index + 1]
171
+ return '\u4e00' <= left_char <= '\u9fff' or '\u4e00' <= right_char <= '\u9fff'
172
+ if index == len(text) - 1:
173
+ return '\u4e00' <= left_char <= '\u9fff'
174
+ return False
175
+
176
+ def replace_char(string, index, new_char):
177
+ return string[:index] + new_char + string[index+1:]
178
+
179
+ def claude_replace(text):
180
+ Punctuation_mapping = {",": ",", ":": ":", "!": "!", "?": "?", ";": ";"}
181
+ key_list = list(Punctuation_mapping.keys())
182
+ for i in range(len(text)):
183
+ if is_surrounded_by_chinese(text, i) and (text[i] in key_list):
184
+ text = replace_char(text, i, Punctuation_mapping[text[i]])
185
+ return text
186
+
187
+ def safe_get(data, *keys, default=None):
188
+ for key in keys:
189
+ try:
190
+ data = data[key] if isinstance(data, (dict, list)) else data.get(key)
191
+ except (KeyError, IndexError, AttributeError, TypeError):
192
+ return default
193
+ return data
194
+
195
+ import asyncio
196
+ def async_generator_to_sync(async_gen):
197
+ """
198
+ 将异步生成器转换为同步生成器的工具函数
199
+
200
+ Args:
201
+ async_gen: 异步生成器函数
202
+
203
+ Yields:
204
+ 异步生成器产生的每个值
205
+ """
206
+ loop = asyncio.new_event_loop()
207
+ asyncio.set_event_loop(loop)
208
+
209
+ try:
210
+ async def collect_chunks():
211
+ chunks = []
212
+ async for chunk in async_gen:
213
+ chunks.append(chunk)
214
+ return chunks
215
+
216
+ chunks = loop.run_until_complete(collect_chunks())
217
+ for chunk in chunks:
218
+ yield chunk
219
+
220
+ except Exception as e:
221
+ print(f"Error during async execution: {e}")
222
+ raise
223
+ finally:
224
+ try:
225
+ # 清理所有待处理的任务
226
+ tasks = [t for t in asyncio.all_tasks(loop) if not t.done()]
227
+ if tasks:
228
+ loop.run_until_complete(asyncio.gather(*tasks, return_exceptions=True))
229
+ loop.run_until_complete(loop.shutdown_asyncgens())
230
+ loop.close()
231
+ except Exception as e:
232
+ print(f"Error during cleanup: {e}")
233
+
234
+ if __name__ == "__main__":
235
+ os.system("clear")
@@ -0,0 +1,119 @@
1
+ Metadata-Version: 2.4
2
+ Name: aient
3
+ Version: 1.0.29
4
+ Summary: Aient: The Awakening of Agent.
5
+ Description-Content-Type: text/markdown
6
+ License-File: LICENSE
7
+ Requires-Dist: pytz
8
+ Requires-Dist: httpx
9
+ Requires-Dist: pillow
10
+ Requires-Dist: msgspec
11
+ Requires-Dist: fastapi
12
+ Requires-Dist: PyExecJS
13
+ Requires-Dist: requests
14
+ Requires-Dist: html2text
15
+ Requires-Dist: fake-useragent
16
+ Requires-Dist: beautifulsoup4
17
+ Requires-Dist: tiktoken==0.6.0
18
+ Requires-Dist: lxml-html-clean
19
+ Requires-Dist: pdfminer.six==20240706
20
+ Requires-Dist: duckduckgo-search==5.3.1
21
+ Dynamic: description
22
+ Dynamic: description-content-type
23
+ Dynamic: license-file
24
+ Dynamic: requires-dist
25
+ Dynamic: summary
26
+
27
+ # aient
28
+
29
+ [English](./README.md) | [Chinese](./README_CN.md)
30
+
31
+ aient is a powerful library designed to simplify and unify the use of different large language models, including GPT-3.5/4/4 Turbo/4o, o1-preview/o1-mini, DALL-E 3, Claude2/3/3.5, Gemini1.5 Pro/Flash, Vertex AI (Claude, Gemini), DuckDuckGo, and Groq. The library supports GPT format function calls and has built-in Google search and URL summarization features, greatly enhancing the practicality and flexibility of the models.
32
+
33
+ ## ✨ Features
34
+
35
+ - **Multi-model support**: Integrate various latest large language models.
36
+ - **Real-time Interaction**: Supports real-time query streams, real-time model response retrieval.
37
+ - **Function Expansion**: With built-in function calling support, the model's functions can be easily expanded, currently supporting plugins such as DuckDuckGo and Google search, content summarization, Dalle-3 drawing, arXiv paper summaries, current time, code interpreter, and more.
38
+ - **Simple Interface**: Provides a concise and unified API interface, making it easy to call and manage the model.
39
+
40
+ ## Quick Start
41
+
42
+ The following is a guide on how to quickly integrate and use aient in your Python project.
43
+
44
+ ### Install
45
+
46
+ First, you need to install aient. It can be installed directly via pip:
47
+
48
+ ```bash
49
+ pip install aient
50
+ ```
51
+
52
+ ### Usage example
53
+
54
+ The following is a simple example demonstrating how to use aient to request the GPT-4 model and handle the returned streaming data:
55
+
56
+ ```python
57
+ from aient import chatgpt
58
+
59
+ # Initialize the model, set the API key and the selected model
60
+ bot = chatgpt(api_key="{YOUR_API_KEY}", engine="gpt-4o")
61
+
62
+ # Get response
63
+ result = bot.ask("python list use")
64
+
65
+ # Send request and get streaming response in real-time
66
+ for text in bot.ask_stream("python list use"):
67
+ print(text, end="")
68
+
69
+ # Disable all plugins
70
+ bot = chatgpt(api_key="{YOUR_API_KEY}", engine="gpt-4o", use_plugins=False)
71
+ ```
72
+
73
+ ## 🍃 Environment Variables
74
+
75
+ The following is a list of environment variables related to plugin settings:
76
+
77
+ | Variable Name | Description | Required? |
78
+ |---------------|-------------|-----------|
79
+ | get_search_results | Enable search plugin. Default value is `False`. | No |
80
+ | get_url_content | Enable URL summary plugin. The default value is `False`. | No |
81
+ | download_read_arxiv_pdf | Whether to enable the arXiv paper abstract plugin. The default value is `False`. | No |
82
+ | run_python_script | Whether to enable the code interpreter plugin. The default value is `False`. | No |
83
+ | generate_image | Whether to enable the image generation plugin. The default value is `False`. | No |
84
+ | get_date_time_weekday | Whether to enable the date plugin. The default value is `False`. | No |
85
+
86
+ ## Supported models
87
+
88
+ - GPT-3.5/4/4 Turbo/4o
89
+ - o1-preview/o1-mini
90
+ - DALL-E 3
91
+ - Claude2/3/3.5
92
+ - Gemini1.5 Pro/Flash
93
+ - Vertex AI (Claude, Gemini)
94
+ - Groq
95
+ - DuckDuckGo(gpt-4o-mini, claude-3-haiku, Meta-Llama-3.1-70B, Mixtral-8x7B)
96
+
97
+ ## 🧩 Plugin
98
+
99
+ This project supports multiple plugins, including: DuckDuckGo and Google search, URL summary, ArXiv paper summary, DALLE-3 drawing, and code interpreter, etc. You can enable or disable these plugins by setting environment variables.
100
+
101
+ - How to develop a plugin?
102
+
103
+ The plugin-related code is all in the aient git submodule of this repository. aient is an independent repository I developed for handling API requests, conversation history management, and other functionality. When you clone this repository with the `--recurse-submodules` parameter, aient will be automatically downloaded. All plugin code is located in the relative path `aient/src/aient/plugins` in this repository. You can add your own plugin code in this directory. The plugin development process is as follows:
104
+
105
+ 1. Create a new Python file in the `aient/src/aient/plugins` directory, for example, `myplugin.py`. Register the plugin by adding the `@register_tool()` decorator above the function. Import `register_tool` with `from .registry import register_tool`.
106
+
107
+ After completing the above steps, your plugin is ready to use. 🎉
108
+
109
+ ## License
110
+
111
+ This project is licensed under the MIT License.
112
+
113
+ ## Contribution
114
+
115
+ Welcome to contribute improvements by submitting issues or pull requests through GitHub.
116
+
117
+ ## Contact Information
118
+
119
+ If you have any questions or need assistance, please contact us at [yym68686@outlook.com](mailto:yym68686@outlook.com).
@@ -0,0 +1,36 @@
1
+ aient/__init__.py,sha256=SRfF7oDVlOOAi6nGKiJIUK6B_arqYLO9iSMp-2IZZps,21
2
+ aient/core/.git,sha256=lrAcW1SxzRBUcUiuKL5tS9ykDmmTXxyLP3YYU-Y-Q-I,45
3
+ aient/core/__init__.py,sha256=NxjebTlku35S4Dzr16rdSqSTWUvvwEeACe8KvHJnjPg,34
4
+ aient/core/log_config.py,sha256=kz2_yJv1p-o3lUQOwA3qh-LSc3wMHv13iCQclw44W9c,274
5
+ aient/core/models.py,sha256=8MsuiYHBHVR5UMQ_cNLkvntoxalS7NpVwaNwHA0iZmk,7379
6
+ aient/core/request.py,sha256=Tk8ylLBHPsrA4C_fb2XUEz_ZM7tR4691mlIxn7x8LUU,48249
7
+ aient/core/response.py,sha256=jE2d-X-ukP5Jy5FCG2VHw5VZgPzhH56_N0gQPqIVggA,26351
8
+ aient/core/utils.py,sha256=i9ZwyywBLIhRM0fNmFSD3jF3dBL5QqVMOtSlG_ddv-I,24101
9
+ aient/core/test/test_base_api.py,sha256=CjfFzMG26r8C4xCPoVkKb3Ac6pp9gy5NUCbZJHoSSsM,393
10
+ aient/core/test/test_image.py,sha256=_T4peNGdXKBHHxyQNx12u-NTyFE8TlYI6NvvagsG2LE,319
11
+ aient/core/test/test_payload.py,sha256=8jBiJY1uidm1jzL-EiK0s6UGmW9XkdsuuKFGrwFhFkw,2755
12
+ aient/models/__init__.py,sha256=ouNDNvoBBpIFrLsk09Q_sq23HR0GbLAKfGLIFmfEuXE,219
13
+ aient/models/audio.py,sha256=kRd-8-WXzv4vwvsTGwnstK-WR8--vr9CdfCZzu8y9LA,1934
14
+ aient/models/base.py,sha256=Loyt2F2WrDMBbK-sdmTtgkLVtdUXxK5tg4qoI6nc0Xo,7527
15
+ aient/models/chatgpt.py,sha256=-07w8jTw6S5eKMWRtyWxTAM-2QQsDI1qyUWvnsT5eLU,36921
16
+ aient/models/claude.py,sha256=thK9P8qkaaoUN3OOJ9Shw4KDs-pAGKPoX4FOPGFXva8,28597
17
+ aient/models/duckduckgo.py,sha256=1l7vYCs9SG5SWPCbcl7q6pCcB5AUF_r-a4l9frz3Ogo,8115
18
+ aient/models/gemini.py,sha256=chGLc-8G_DAOxr10HPoOhvVFW1RvMgHd6mt--VyAW98,14730
19
+ aient/models/groq.py,sha256=2JCB0QE1htOprJHI5fZ11R2RtOhsHlsTjbmFyzc8oSM,10084
20
+ aient/models/vertex.py,sha256=qVD5l1Q538xXUPulxG4nmDjXE1VoV4yuAkTCpIeJVw0,16795
21
+ aient/plugins/__init__.py,sha256=KrCM6kFD1NB96hfhwUZIG8vJcdZVnfpACMew5YOWxSo,956
22
+ aient/plugins/arXiv.py,sha256=yHjb6PS3GUWazpOYRMKMzghKJlxnZ5TX8z9F6UtUVow,1461
23
+ aient/plugins/config.py,sha256=S7UHlo5e-czjssSyDQXVbR9bUlB21Fj934OfOKW5Vug,8191
24
+ aient/plugins/image.py,sha256=ZElCIaZznE06TN9xW3DrSukS7U3A5_cjk1Jge4NzPxw,2072
25
+ aient/plugins/registry.py,sha256=YknzhieU_8nQ3oKlUSSWDB4X7t2Jx0JnqT2Jd9Xsvfk,3574
26
+ aient/plugins/run_python.py,sha256=dgcUwBunMuDkaSKR5bToudVzSdrXVewktDDFUz_iIOQ,4589
27
+ aient/plugins/today.py,sha256=btnXJNqWorJDKPvH9PBTdHaExpVI1YPuSAeRrq-fg9A,667
28
+ aient/plugins/websearch.py,sha256=RY0BaYgPC3OTTTDlezakXm6JvXixMEXUqHN5MXpXijw,15144
29
+ aient/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
30
+ aient/utils/prompt.py,sha256=UcSzKkFE4-h_1b6NofI6xgk3GoleqALRKY8VBaXLjmI,11311
31
+ aient/utils/scripts.py,sha256=O-0IXN3mezPauFs6fw83WDDgklpXTDvcbJBNTDrsIG0,8201
32
+ aient-1.0.29.dist-info/licenses/LICENSE,sha256=XNdbcWldt0yaNXXWB_Bakoqnxb3OVhUft4MgMA_71ds,1051
33
+ aient-1.0.29.dist-info/METADATA,sha256=BmMRouk-Oict-XOBDYh8toZrKz54jBi2r9XHtzceiOY,4986
34
+ aient-1.0.29.dist-info/WHEEL,sha256=1tXe9gY0PYatrMPMDd6jXqjfpz_B-Wqm32CPfRC58XU,91
35
+ aient-1.0.29.dist-info/top_level.txt,sha256=3oXzrP5sAVvyyqabpeq8A2_vfMtY554r4bVE-OHBrZk,6
36
+ aient-1.0.29.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (77.0.3)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2024 yym68686
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ aient