pycoze 0.1.286__py3-none-any.whl → 0.1.289__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 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
- import requests
8
- import os
9
-
10
-
11
- def never_retry_on_rate_limit_error(exception):
12
- """Return True if we should retry (in this case when it's NOT a RateLimitError), False otherwise"""
13
- return not isinstance(exception, openai.RateLimitError)
14
-
15
- @retry(retry_on_exception=never_retry_on_rate_limit_error, wait_exponential_multiplier=500, stop_max_attempt_number=5)
16
- def chat(user_text, history, temperature=0.2, stop=None, **kwargs):
17
- user_msg = {"role": "user", "content": user_text}
18
- cfg = utils.read_json_file("llm.json")
19
-
20
- base_url = cfg["baseURL"]
21
- api_key = cfg["apiKey"]
22
- model = cfg["model"]
23
-
24
- base_url = base_url.replace("/chat/completions", "")
25
-
26
- client = OpenAI(api_key=api_key, base_url=base_url)
27
-
28
- response = client.chat.completions.create(model=model,
29
- messages=simple_history(history) + [user_msg],
30
- temperature=temperature,
31
- stop=stop)
32
- return response.choices[0].message.content
33
-
34
-
35
- @retry(retry_on_exception=never_retry_on_rate_limit_error, wait_exponential_multiplier=500, stop_max_attempt_number=5)
36
- def chat_stream(user_text, history, temperature=0.2, stop=None, **kwargs):
37
- user_msg = {"role": "user", "content": user_text}
38
- cfg = utils.read_json_file("llm.json")
39
-
40
- base_url = cfg["baseURL"]
41
- api_key = cfg["apiKey"]
42
- model = cfg["model"]
43
-
44
- base_url = base_url.replace("/chat/completions", "")
45
-
46
- client = OpenAI(api_key=api_key, base_url=base_url)
47
-
48
-
49
- stream = client.chat.completions.create(model=model,
50
- messages=simple_history(history) + [user_msg],
51
- stream=True,
52
- temperature=temperature,
53
- stop=stop)
54
- for chunk in stream:
55
- yield chunk.choices[0].delta.content or ""
56
-
57
-
58
- def simple_history(history):
59
- return [{"role": h["role"], "content": h["content"]} for h in history]
60
-
61
-
62
- @retry(retry_on_exception=never_retry_on_rate_limit_error, wait_exponential_multiplier=500, stop_max_attempt_number=3)
63
- def extract(response_data, text: str, temperature=0, **kwargs):
64
- """print(extract({"name": "lowercase"}, "hello XiaoMing"))"""
65
- if isinstance(response_data, dict):
66
- response_items = [[res, response_data[res]] for res in response_data]
67
- else:
68
- response_items = response_data
69
-
70
- json_text = ""
71
- for i, res in enumerate(response_items):
72
- comma = "," if i != len(response_items) - 1 else ""
73
- json_text += f' "{res[0]}": {res[1]}{comma}\n'
74
-
75
- # Combine the provided text with the formatted JSON schema
76
- chat_text = f"""
77
- The output should be a markdown code snippet formatted in the following schema, including the leading and trailing "```json" and "```" tags:
78
- ```json
79
- {{
80
- {json_text}
81
- }}
82
- ```
83
-
84
- Request:
85
- {text}
86
- """
87
- # text放后面,当翻译等情况时,不会把"The output should"之类翻译了,导致错误
88
- markdown = chat(chat_text, [], temperature=temperature, **kwargs)
89
- pattern = r'```json(.*?)```'
90
- matches = re.findall(pattern, markdown, re.DOTALL)
91
- if matches:
92
- json_str = matches[0].strip()
93
- # lines = [line.split("//")[0] for line in json_str.split("\n")]//这样当json中有//时会出错,例如https://
94
- json_dict = json5.loads(json_str)
95
- for item in response_items:
96
- if item[0] not in json_dict:
97
- raise "item:" + item + " not exists"
98
- return json_dict
99
-
100
-
101
- def yes_or_no(question, temperature=0, **kwargs):
102
- result = extract([("Result", "Yes or No")], question,
103
- temperature=temperature, **kwargs)["Result"]
104
- if isinstance(result, bool):
105
- return result
106
- return result.upper() == "YES"
107
-
108
-
109
- @retry(retry_on_exception=never_retry_on_rate_limit_error, wait_exponential_multiplier=500, stop_max_attempt_number=3)
110
- def extract_code(text: str, temperature=0, language="python", markdown_word='python', **kwargs):
111
- """print(extract_code("sum 1~100"))"""
112
- chat_text = text + f"""
113
- The output should be a complete and usable {language} code snippet, including the leading and trailing "```{markdown_word}" and "```":
114
- """
115
- markdown = chat(chat_text, [], temperature=temperature, **kwargs)
116
- # 使用正则表达式匹配围绕在```{markdown_word} ```之间的文本
117
- pattern = rf'```{markdown_word}(.*?)```'
118
- matches = re.findall(pattern, markdown, re.DOTALL)
119
- if matches:
120
- # 去除可能的前后空白字符
121
- return matches[0].strip()
122
- else:
123
- raise Exception("The string is not a valid python code.")
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.2
1
+ Metadata-Version: 2.1
2
2
  Name: pycoze
3
- Version: 0.1.286
3
+ Version: 0.1.289
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=izriC7nCp5qeJRqcUVQBVqTHiH6MJS77ROzGBJufdNI,5133
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.286.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
39
- pycoze-0.1.286.dist-info/METADATA,sha256=CE7H9CpBdsQrmZo7FaGjGSQNBcOAO0rkp7BsVQ3ZCYU,854
40
- pycoze-0.1.286.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
41
- pycoze-0.1.286.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
42
- pycoze-0.1.286.dist-info/RECORD,,
38
+ pycoze-0.1.289.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
39
+ pycoze-0.1.289.dist-info/METADATA,sha256=vInn6iqsGa2I4_Bzbq8t00mFNDxiSMzl6FxhW1TRBBE,755
40
+ pycoze-0.1.289.dist-info/WHEEL,sha256=ewwEueio1C2XeHTvT17n8dZUJgOvyCWCt0WVNLClP9o,92
41
+ pycoze-0.1.289.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
42
+ pycoze-0.1.289.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.8.0)
2
+ Generator: bdist_wheel (0.37.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5