-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathai.py
51 lines (42 loc) · 1.89 KB
/
ai.py
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
import openai
import logging
def translate(text, target_lang):
logging.info("translate. text: %s, target_lang: %s", text, target_lang)
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{
"role": "user",
"content": f"Please translate `{text}` to {target_lang}. Please return only translated content not include the origin text.",
}],
)
return completion["choices"][0].get("message").get("content").encode("utf8").decode()
def rephrase(text):
logging.info("rephrase. text: %s", text)
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{
"role": "user",
"content": f"Please improve the writing of the following text in its original language: `{text}`",
}],
)
return completion["choices"][0].get("message").get("content").encode("utf8").decode()
def write(language, title, content, other_req):
logging.info("write. language: %s, title: %s, content: %s, other_req: %s", language, title, content, other_req)
if language == "chinese (simplified)":
if other_req == "":
other_req = "无"
prompt = f"请帮我写一篇标题为《{title}》的文章。文章大意为:\"{content}\"。其他要求:{other_req}。"
elif language == "english":
if other_req == "":
other_req = "None"
prompt = f"Please write an article with the title of \"{title}\". The main idea of the article is: \"{content}\". Other requirements: {other_req}."
else:
raise Exception("Language not supported")
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{
"role": "user",
"content": prompt,
}],
)
return completion["choices"][0].get("message").get("content").encode("utf8").decode()