Files
AstrBot/cores/openai/core.py
Soulter 3e26ea282f fix: 修复了prompts超出界限的bug
feat:添加历史记录功能
perf:优化prompt缓存模式
2022-12-10 02:42:14 +08:00

38 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import openai
import yaml
from util.errors.errors import PromptExceededError
inst = None
class ChatGPT:
def __init__(self, chatGPT_configs):
with open("./configs/config.yaml", 'r', encoding='utf-8') as ymlfile:
cfg = yaml.safe_load(ymlfile)
if cfg['openai']['key'] != '':
print("读取ChatGPT Key成功")
openai.api_key = cfg['openai']['key']
else:
print("请先去完善ChatGPT的Key。详情请前往https://beta.openai.com/account/api-keys")
self.chatGPT_configs = chatGPT_configs
global inst
inst = self
async def chat(self, prompt):
print("[ChatGPT] 接收到prompt:\n")
try:
response = openai.Completion.create(
prompt=prompt,
**self.chatGPT_configs
)
except(openai.error.InvalidRequestError) as e:
raise PromptExceededError("OpenAI遇到错误输入了一个不合法的请求。\n"+str(e))
return response["choices"][0]["text"]
def newSession(self):
return openai.Session()
def getInst() -> ChatGPT:
global inst
return inst