pycoze 0.1.289__py3-none-any.whl → 0.1.290__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 +46 -24
- {pycoze-0.1.289.dist-info → pycoze-0.1.290.dist-info}/METADATA +1 -1
- {pycoze-0.1.289.dist-info → pycoze-0.1.290.dist-info}/RECORD +6 -6
- {pycoze-0.1.289.dist-info → pycoze-0.1.290.dist-info}/LICENSE +0 -0
- {pycoze-0.1.289.dist-info → pycoze-0.1.290.dist-info}/WHEEL +0 -0
- {pycoze-0.1.289.dist-info → pycoze-0.1.290.dist-info}/top_level.txt +0 -0
pycoze/ai/llm/chat.py
CHANGED
@@ -10,7 +10,12 @@ def never_retry_on_rate_limit_error(exception):
|
|
10
10
|
"""Return True if we should retry (in this case when it's NOT a RateLimitError), False otherwise"""
|
11
11
|
return not isinstance(exception, openai.RateLimitError)
|
12
12
|
|
13
|
-
|
13
|
+
|
14
|
+
@retry(
|
15
|
+
retry_on_exception=never_retry_on_rate_limit_error,
|
16
|
+
wait_exponential_multiplier=500,
|
17
|
+
stop_max_attempt_number=5,
|
18
|
+
)
|
14
19
|
def chat(messages, temperature=0.2, stop=None, **kwargs):
|
15
20
|
cfg = utils.read_json_file("llm.json")
|
16
21
|
|
@@ -22,16 +27,18 @@ def chat(messages, temperature=0.2, stop=None, **kwargs):
|
|
22
27
|
|
23
28
|
client = OpenAI(api_key=api_key, base_url=base_url)
|
24
29
|
|
25
|
-
response = client.chat.completions.create(
|
26
|
-
|
27
|
-
|
28
|
-
stop=stop)
|
30
|
+
response = client.chat.completions.create(
|
31
|
+
model=model, messages=messages, temperature=temperature, stop=stop
|
32
|
+
)
|
29
33
|
return response.choices[0].message.content
|
30
34
|
|
31
35
|
|
32
|
-
@retry(
|
33
|
-
|
34
|
-
|
36
|
+
@retry(
|
37
|
+
retry_on_exception=never_retry_on_rate_limit_error,
|
38
|
+
wait_exponential_multiplier=500,
|
39
|
+
stop_max_attempt_number=5,
|
40
|
+
)
|
41
|
+
def chat_stream(messages, temperature=0.2, stop=None, **kwargs):
|
35
42
|
cfg = utils.read_json_file("llm.json")
|
36
43
|
|
37
44
|
base_url = cfg["baseURL"]
|
@@ -42,17 +49,18 @@ def chat_stream(user_text, history, temperature=0.2, stop=None, **kwargs):
|
|
42
49
|
|
43
50
|
client = OpenAI(api_key=api_key, base_url=base_url)
|
44
51
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
stream=True,
|
49
|
-
temperature=temperature,
|
50
|
-
stop=stop)
|
52
|
+
stream = client.chat.completions.create(
|
53
|
+
model=model, messages=messages, stream=True, temperature=temperature, stop=stop
|
54
|
+
)
|
51
55
|
for chunk in stream:
|
52
56
|
yield chunk.choices[0].delta.content or ""
|
53
57
|
|
54
58
|
|
55
|
-
@retry(
|
59
|
+
@retry(
|
60
|
+
retry_on_exception=never_retry_on_rate_limit_error,
|
61
|
+
wait_exponential_multiplier=500,
|
62
|
+
stop_max_attempt_number=3,
|
63
|
+
)
|
56
64
|
def extract(response_data, text: str, temperature=0, **kwargs):
|
57
65
|
"""print(extract({"name": "lowercase"}, "hello XiaoMing"))"""
|
58
66
|
if isinstance(response_data, dict):
|
@@ -78,8 +86,10 @@ Request:
|
|
78
86
|
{text}
|
79
87
|
"""
|
80
88
|
# text放后面,当翻译等情况时,不会把"The output should"之类翻译了,导致错误
|
81
|
-
markdown = chat(
|
82
|
-
|
89
|
+
markdown = chat(
|
90
|
+
[{"role": "user", "content": chat_text}], temperature=temperature, **kwargs
|
91
|
+
)
|
92
|
+
pattern = r"```json(.*?)```"
|
83
93
|
matches = re.findall(pattern, markdown, re.DOTALL)
|
84
94
|
if matches:
|
85
95
|
json_str = matches[0].strip()
|
@@ -92,22 +102,34 @@ Request:
|
|
92
102
|
|
93
103
|
|
94
104
|
def yes_or_no(question, temperature=0, **kwargs):
|
95
|
-
result = extract(
|
96
|
-
|
105
|
+
result = extract(
|
106
|
+
[("Result", "Yes or No")], question, temperature=temperature, **kwargs
|
107
|
+
)["Result"]
|
97
108
|
if isinstance(result, bool):
|
98
109
|
return result
|
99
110
|
return result.upper() == "YES"
|
100
111
|
|
101
112
|
|
102
|
-
@retry(
|
103
|
-
|
113
|
+
@retry(
|
114
|
+
retry_on_exception=never_retry_on_rate_limit_error,
|
115
|
+
wait_exponential_multiplier=500,
|
116
|
+
stop_max_attempt_number=3,
|
117
|
+
)
|
118
|
+
def extract_code(
|
119
|
+
text: str, temperature=0, language="python", markdown_word="python", **kwargs
|
120
|
+
):
|
104
121
|
"""print(extract_code("sum 1~100"))"""
|
105
|
-
chat_text =
|
122
|
+
chat_text = (
|
123
|
+
text
|
124
|
+
+ f"""
|
106
125
|
The output should be a complete and usable {language} code snippet, including the leading and trailing "```{markdown_word}" and "```":
|
107
126
|
"""
|
108
|
-
|
127
|
+
)
|
128
|
+
markdown = chat(
|
129
|
+
[{"role": "user", "content": chat_text}], temperature=temperature, **kwargs
|
130
|
+
)
|
109
131
|
# 使用正则表达式匹配围绕在```{markdown_word} 和 ```之间的文本
|
110
|
-
pattern = rf
|
132
|
+
pattern = rf"```{markdown_word}(.*?)```"
|
111
133
|
matches = re.findall(pattern, markdown, re.DOTALL)
|
112
134
|
if matches:
|
113
135
|
# 去除可能的前后空白字符
|
@@ -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=edfHn714poJat9bElplh1DXg1YKwWjlTo4npmrwiT7I,4615
|
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.290.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
|
39
|
+
pycoze-0.1.290.dist-info/METADATA,sha256=80oHjVcuMNyuYMwVsQgU6yN4xG4gZw1QECKJmN3H13o,755
|
40
|
+
pycoze-0.1.290.dist-info/WHEEL,sha256=ewwEueio1C2XeHTvT17n8dZUJgOvyCWCt0WVNLClP9o,92
|
41
|
+
pycoze-0.1.290.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
|
42
|
+
pycoze-0.1.290.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|