pycoze 0.1.286__py3-none-any.whl → 0.1.288__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.
- pycoze/ai/llm/chat.py +124 -131
- {pycoze-0.1.286.dist-info → pycoze-0.1.288.dist-info}/METADATA +7 -9
- {pycoze-0.1.286.dist-info → pycoze-0.1.288.dist-info}/RECORD +6 -6
- {pycoze-0.1.286.dist-info → pycoze-0.1.288.dist-info}/WHEEL +1 -1
- {pycoze-0.1.286.dist-info → pycoze-0.1.288.dist-info}/LICENSE +0 -0
- {pycoze-0.1.286.dist-info → pycoze-0.1.288.dist-info}/top_level.txt +0 -0
pycoze/ai/llm/chat.py
CHANGED
@@ -1,131 +1,124 @@
|
|
1
|
-
from pycoze import utils
|
2
|
-
import json5
|
3
|
-
import re
|
4
|
-
from retrying import retry
|
5
|
-
from openai import OpenAI
|
6
|
-
import openai
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
}
|
82
|
-
```
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
"""
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
if __name__ == "__main__":
|
127
|
-
print(chat("你好", []))
|
128
|
-
for chunk in chat_stream("你好", []):
|
129
|
-
print(chunk)
|
130
|
-
print(extract({"name": "lowercase"}, "hello XiaoMing"))
|
131
|
-
print(extract_code("sum 1~100"))
|
1
|
+
from pycoze import utils
|
2
|
+
import json5
|
3
|
+
import re
|
4
|
+
from retrying import retry
|
5
|
+
from openai import OpenAI
|
6
|
+
import openai
|
7
|
+
|
8
|
+
|
9
|
+
def never_retry_on_rate_limit_error(exception):
|
10
|
+
"""Return True if we should retry (in this case when it's NOT a RateLimitError), False otherwise"""
|
11
|
+
return not isinstance(exception, openai.RateLimitError)
|
12
|
+
|
13
|
+
@retry(retry_on_exception=never_retry_on_rate_limit_error, wait_exponential_multiplier=500, stop_max_attempt_number=5)
|
14
|
+
def chat(messages, temperature=0.2, stop=None, **kwargs):
|
15
|
+
cfg = utils.read_json_file("llm.json")
|
16
|
+
|
17
|
+
base_url = cfg["baseURL"]
|
18
|
+
api_key = cfg["apiKey"]
|
19
|
+
model = cfg["model"]
|
20
|
+
|
21
|
+
base_url = base_url.replace("/chat/completions", "")
|
22
|
+
|
23
|
+
client = OpenAI(api_key=api_key, base_url=base_url)
|
24
|
+
|
25
|
+
response = client.chat.completions.create(model=model,
|
26
|
+
messages=messages,
|
27
|
+
temperature=temperature,
|
28
|
+
stop=stop)
|
29
|
+
return response.choices[0].message.content
|
30
|
+
|
31
|
+
|
32
|
+
@retry(retry_on_exception=never_retry_on_rate_limit_error, wait_exponential_multiplier=500, stop_max_attempt_number=5)
|
33
|
+
def chat_stream(user_text, history, temperature=0.2, stop=None, **kwargs):
|
34
|
+
user_msg = {"role": "user", "content": user_text}
|
35
|
+
cfg = utils.read_json_file("llm.json")
|
36
|
+
|
37
|
+
base_url = cfg["baseURL"]
|
38
|
+
api_key = cfg["apiKey"]
|
39
|
+
model = cfg["model"]
|
40
|
+
|
41
|
+
base_url = base_url.replace("/chat/completions", "")
|
42
|
+
|
43
|
+
client = OpenAI(api_key=api_key, base_url=base_url)
|
44
|
+
|
45
|
+
|
46
|
+
stream = client.chat.completions.create(model=model,
|
47
|
+
messages=simple_history(history) + [user_msg],
|
48
|
+
stream=True,
|
49
|
+
temperature=temperature,
|
50
|
+
stop=stop)
|
51
|
+
for chunk in stream:
|
52
|
+
yield chunk.choices[0].delta.content or ""
|
53
|
+
|
54
|
+
|
55
|
+
@retry(retry_on_exception=never_retry_on_rate_limit_error, wait_exponential_multiplier=500, stop_max_attempt_number=3)
|
56
|
+
def extract(response_data, text: str, temperature=0, **kwargs):
|
57
|
+
"""print(extract({"name": "lowercase"}, "hello XiaoMing"))"""
|
58
|
+
if isinstance(response_data, dict):
|
59
|
+
response_items = [[res, response_data[res]] for res in response_data]
|
60
|
+
else:
|
61
|
+
response_items = response_data
|
62
|
+
|
63
|
+
json_text = ""
|
64
|
+
for i, res in enumerate(response_items):
|
65
|
+
comma = "," if i != len(response_items) - 1 else ""
|
66
|
+
json_text += f' "{res[0]}": {res[1]}{comma}\n'
|
67
|
+
|
68
|
+
# Combine the provided text with the formatted JSON schema
|
69
|
+
chat_text = f"""
|
70
|
+
The output should be a markdown code snippet formatted in the following schema, including the leading and trailing "```json" and "```" tags:
|
71
|
+
```json
|
72
|
+
{{
|
73
|
+
{json_text}
|
74
|
+
}}
|
75
|
+
```
|
76
|
+
|
77
|
+
Request:
|
78
|
+
{text}
|
79
|
+
"""
|
80
|
+
# text放后面,当翻译等情况时,不会把"The output should"之类翻译了,导致错误
|
81
|
+
markdown = chat([{"role": "user", "content": chat_text}], temperature=temperature, **kwargs)
|
82
|
+
pattern = r'```json(.*?)```'
|
83
|
+
matches = re.findall(pattern, markdown, re.DOTALL)
|
84
|
+
if matches:
|
85
|
+
json_str = matches[0].strip()
|
86
|
+
# lines = [line.split("//")[0] for line in json_str.split("\n")]//这样当json中有//时会出错,例如https://
|
87
|
+
json_dict = json5.loads(json_str)
|
88
|
+
for item in response_items:
|
89
|
+
if item[0] not in json_dict:
|
90
|
+
raise "item:" + item + " not exists"
|
91
|
+
return json_dict
|
92
|
+
|
93
|
+
|
94
|
+
def yes_or_no(question, temperature=0, **kwargs):
|
95
|
+
result = extract([("Result", "Yes or No")], question,
|
96
|
+
temperature=temperature, **kwargs)["Result"]
|
97
|
+
if isinstance(result, bool):
|
98
|
+
return result
|
99
|
+
return result.upper() == "YES"
|
100
|
+
|
101
|
+
|
102
|
+
@retry(retry_on_exception=never_retry_on_rate_limit_error, wait_exponential_multiplier=500, stop_max_attempt_number=3)
|
103
|
+
def extract_code(text: str, temperature=0, language="python", markdown_word='python', **kwargs):
|
104
|
+
"""print(extract_code("sum 1~100"))"""
|
105
|
+
chat_text = text + f"""
|
106
|
+
The output should be a complete and usable {language} code snippet, including the leading and trailing "```{markdown_word}" and "```":
|
107
|
+
"""
|
108
|
+
markdown = chat([{"role": "user", "content": chat_text}], temperature=temperature, **kwargs)
|
109
|
+
# 使用正则表达式匹配围绕在```{markdown_word} 和 ```之间的文本
|
110
|
+
pattern = rf'```{markdown_word}(.*?)```'
|
111
|
+
matches = re.findall(pattern, markdown, re.DOTALL)
|
112
|
+
if matches:
|
113
|
+
# 去除可能的前后空白字符
|
114
|
+
return matches[0].strip()
|
115
|
+
else:
|
116
|
+
raise Exception("The string is not a valid python code.")
|
117
|
+
|
118
|
+
|
119
|
+
if __name__ == "__main__":
|
120
|
+
print(chat([{"role": "user", "content": "你好"}]))
|
121
|
+
for chunk in chat_stream([{"role": "user", "content": "你好"}]):
|
122
|
+
print(chunk)
|
123
|
+
print(extract({"name": "lowercase"}, "hello XiaoMing"))
|
124
|
+
print(extract_code("sum 1~100"))
|
@@ -1,22 +1,18 @@
|
|
1
|
-
Metadata-Version: 2.
|
1
|
+
Metadata-Version: 2.1
|
2
2
|
Name: pycoze
|
3
|
-
Version: 0.1.
|
3
|
+
Version: 0.1.288
|
4
4
|
Summary: Package for pycoze only!
|
5
|
+
Home-page: UNKNOWN
|
5
6
|
Author: Yuan Jie Xiong
|
6
7
|
Author-email: aiqqqqqqq@qq.com
|
8
|
+
License: UNKNOWN
|
9
|
+
Platform: UNKNOWN
|
7
10
|
Classifier: Programming Language :: Python :: 3
|
8
11
|
Classifier: License :: OSI Approved :: MIT License
|
9
12
|
Classifier: Operating System :: OS Independent
|
10
13
|
Requires-Python: >=3.6
|
11
14
|
Description-Content-Type: text/markdown
|
12
15
|
License-File: LICENSE
|
13
|
-
Dynamic: author
|
14
|
-
Dynamic: author-email
|
15
|
-
Dynamic: classifier
|
16
|
-
Dynamic: description
|
17
|
-
Dynamic: description-content-type
|
18
|
-
Dynamic: requires-python
|
19
|
-
Dynamic: summary
|
20
16
|
|
21
17
|
# PYCOZE
|
22
18
|
|
@@ -32,3 +28,5 @@ Package for pycoze only!
|
|
32
28
|
<!-- python setup.py sdist bdist_wheel -->
|
33
29
|
<!-- twine upload dist/* -->
|
34
30
|
<!-- 修改src\renderer\layout\init\const.ts中的库版本要求 -->
|
31
|
+
|
32
|
+
|
@@ -2,7 +2,7 @@ pycoze/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
2
|
pycoze/ai/__init__.py,sha256=amfVImcuiRRnGbmOjIqjXKiuH1H3zZPHOrJue2dEB8U,355
|
3
3
|
pycoze/ai/vram_reserve.py,sha256=DRxKzqf89fLAts0DzU8e19z9kecIF8OdMkQnJlCKZV0,4244
|
4
4
|
pycoze/ai/llm/__init__.py,sha256=5_AnOrzXI2V6ZZsLUWW1v5ATRWmJy53JLN9jfSZQXCg,249
|
5
|
-
pycoze/ai/llm/chat.py,sha256=
|
5
|
+
pycoze/ai/llm/chat.py,sha256=_aJo0KzqA2m4lLql-9AqTh9f6mJmwi80K_tam7F2ehU,4882
|
6
6
|
pycoze/ai/llm/text_to_image_prompt.py,sha256=0bx2C_YRvjAo7iphHGp1-pmGKsKqwur7dM0t3SiA8kA,3398
|
7
7
|
pycoze/ai/llm/think.py,sha256=sUgTBdGzcZtL3r-Wx8M3lDuVUmDVz8g3qC0VU8uiKAI,5143
|
8
8
|
pycoze/api/__init__.py,sha256=GGRRRPop0ZxdXe5JRhg2XvHITGIWfNcHA25opJZ0f1w,313
|
@@ -35,8 +35,8 @@ pycoze/utils/arg.py,sha256=jop1tBfe5hYkHW1NSpCeaZBEznkgguBscj_7M2dWfrs,503
|
|
35
35
|
pycoze/utils/env.py,sha256=5pWlXfM1F5ZU9hhv1rHlDEanjEW5wf0nbyez9bNRqqA,559
|
36
36
|
pycoze/utils/socket.py,sha256=bZbFFRH4mfThzRqt55BAAGQ6eICx_ja4x8UGGrUdAm8,2428
|
37
37
|
pycoze/utils/text_or_file.py,sha256=gpxZVWt2DW6YiEg_MnMuwg36VNf3TX383QD_1oZNB0Y,551
|
38
|
-
pycoze-0.1.
|
39
|
-
pycoze-0.1.
|
40
|
-
pycoze-0.1.
|
41
|
-
pycoze-0.1.
|
42
|
-
pycoze-0.1.
|
38
|
+
pycoze-0.1.288.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
|
39
|
+
pycoze-0.1.288.dist-info/METADATA,sha256=72pjOMZ7pPYKaJ7PLzFV-fa2zfNZkfCHuulwkvb2HOs,755
|
40
|
+
pycoze-0.1.288.dist-info/WHEEL,sha256=ewwEueio1C2XeHTvT17n8dZUJgOvyCWCt0WVNLClP9o,92
|
41
|
+
pycoze-0.1.288.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
|
42
|
+
pycoze-0.1.288.dist-info/RECORD,,
|
File without changes
|
File without changes
|