-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkimiDB.py
More file actions
159 lines (140 loc) · 5 KB
/
Copy pathkimiDB.py
File metadata and controls
159 lines (140 loc) · 5 KB
1
2
3
4
5
6
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import re
import json
import tomllib
from openai import OpenAI
Pooh = {}
def ask(user_content=None, system_content=None, temp=0.618):
if not system_content:
system_content = (
"你是 Kimi。由 Moonshot AI 提供的人工智能助手,你更擅长种草文案的撰写,你不可以写广告极限词,不可以写任何功效有关的文案。"
"你不使用中文符号,如果需要使用符号都是用英文符号,请输出json格式,key='data'"
"你可以正确处理汉字和英文的长度,一个汉字算两个字符,其他算一个字符"
"不可以出现下面这几个词语:0添加,无添加,首选,肠胃"
)
client = OpenAI(
api_key=Pooh["MOONSHOT_API_KEY"],
base_url="https://api.moonshot.cn/v1",
)
# print(system_content)
print(user_content)
completion = client.chat.completions.create(
model="moonshot-v1-8k",
messages=[
{"role": "system", "content": system_content},
{"role": "user", "content": user_content},
],
# temperature = 0.3,
temperature=temp, # 0.618
# temperature = 0.7,
response_format={"type": "json_object"},
)
content = completion.choices[0].message.content
if "data" in content:
print(content)
return json.loads(content)
else:
raise
# drop pause
# print(content)
# payload_dict = kimi_parse(content)
# if "data" in payload_dict:
# return payload_dict
# else:
# print("Output error")
# print(payload_dict.keys())
# return
def fetch(user_content=None, system_content=None, temp=0.618):
if not system_content:
system_content = """
小小酥是人名,程Yoooo也是人名,你擅长写B站文案,热别擅长吸引用户弹幕或者评论,支持喜欢的人。必须包含Powered by 。请输出json格式,key='data'
"""
client = OpenAI(
api_key=Pooh["MOONSHOT_API_KEY"],
base_url="https://api.moonshot.cn/v1",
)
# print(system_content)
# print(user_content)
completion = client.chat.completions.create(
model="moonshot-v1-8k",
messages=[
{"role": "system", "content": system_content},
{"role": "user", "content": user_content},
],
# temperature = 0.3,
temperature=temp, # 0.618
# temperature = 0.7,
response_format={"type": "json_object"},
)
content = completion.choices[0].message.content
if "data" in content:
# print(json.loads(content)["data"])
return json.loads(content)["data"]
else:
return ""
def launch():
client = OpenAI(
api_key=Pooh["MOONSHOT_API_KEY"],
base_url="https://api.moonshot.cn/v1",
)
completion = client.chat.completions.create(
model="moonshot-v1-8k",
messages=[
{
"role": "system",
"content": "你是 Kimi,是一个擅长写母婴类产品种草文案的母亲,你会为用户提供安全,有帮助,准确的回答。",
},
{
"role": "user",
"content": "请给我30条关于格力空调的种草标题,要求字数在50-58汉字,请输出标准的json格式,key='data'",
},
],
# temperature = 0.3,
temperature=0.618,
# temperature = 0.7,
)
content = completion.choices[0].message.content
print(content)
payload_dict = kimi_parse(content)
if "datax" in payload_dict:
return payload_dict
else:
print("Output error")
print(payload_dict.keys())
return
def kimi_parse(kimi_text):
# 定义正则表达式模式以匹配 JSON 字符串
json_pattern = r"```json\s*(.*?)```"
# 使用正则表达式搜索文本中的 JSON 字符串
json_match = re.search(json_pattern, kimi_text, re.DOTALL)
if json_match:
# 提取并清理 JSON 字符串
json_str = json_match.group(1).strip()
try:
# 尝试将字符串解析为 JSON 对象
content_dict = json.loads(json_str)
print("JSON 数据解析成功:")
print(content_dict)
return content_dict
except json.JSONDecodeError as e:
# 如果解析失败,打印错误信息
print(f"JSON 解析失败:{e}")
return
else:
try:
# 尝试将字符串解析为 JSON 对象
content_dict = json.loads(kimi_text)
print("JSON 数据解析成功:")
print(content_dict)
return content_dict
except json.JSONDecodeError as e:
# 如果解析失败,打印错误信息
print(f"JSON 解析失败:{e}")
return
def boot(api_key=None):
global Pooh
if not api_key:
print("MOONSHOT_API_KEY is essential!!")
Pooh["MOONSHOT_API_KEY"] = api_key
if __name__ == "__main__":
boot()
launch()