-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_api.py
57 lines (46 loc) · 1.25 KB
/
chat_api.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
52
53
54
55
56
57
import os
import logging
from typing import List
import openai
logging.basicConfig(
format="%(asctime)s %(name)s:%(levelname)s:%(message)s",
datefmt="%d-%M-%Y %H:%M:%S",
level=logging.WARN
)
openai.api_key = os.getenv("OPENAI_API_KEY")
Q_PREFIX = "人类: "
A_PREFIX = "AI助手: "
MAX_HISTORY_COUNT = 1500
def get_completion(prompt: str) -> str:
logging.info("get_completion(): Prompt: %s", prompt)
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
max_tokens=400,
temperature=0.9,
top_p=1,
n=1,
stop=[Q_PREFIX, A_PREFIX],
presence_penalty=0.6,
echo=False,
)
logging.debug(response)
return response.choices[0].text
def get_correction(text: str) -> str:
response = openai.Edit.create(
model="text-davinci-edit-001",
input=text,
instruction="Fix the spelling mistakes"
)
logging.debug(response)
return response.choices[0].text
def get_image(description: str, num: int) -> List[str]:
response = openai.Image.create(
prompt=description,
n=num,
size="512x512"
)
result = []
for data in response['data']:
result.append(data['url'])
return result