1 Star 0 Fork 0

与衡/ChatDB

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
chatgpt.py 1.31 KB
一键复制 编辑 原始数据 按行查看 历史
import time
import openai
from colorama import Fore
from config import cfg
def create_chat_completion(messages, model=cfg.fast_llm_model, temperature=cfg.temperature, max_tokens=None) -> str:
"""Create a chat completion using the OpenAI API"""
response = None
num_retries = 5
for attempt in range(num_retries):
try:
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature,
max_tokens=max_tokens
)
break
except openai.error.RateLimitError:
if cfg.debug_mode:
print(Fore.RED + "Error: ", "API Rate Limit Reached. Waiting 20 seconds..." + Fore.RESET)
time.sleep(20)
except openai.error.APIError as e:
if e.http_status == 502:
if cfg.debug_mode:
print(Fore.RED + "Error: ", "API Bad gateway. Waiting 20 seconds..." + Fore.RESET)
time.sleep(20)
else:
raise
if attempt == num_retries - 1:
raise
except openai.error.InvalidRequestError:
raise
if response is None:
raise RuntimeError("Failed to get response after 5 retries")
return response.choices[0].message["content"]
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/JackieChan007/ChatDB.git
git@gitee.com:JackieChan007/ChatDB.git
JackieChan007
ChatDB
ChatDB
main

搜索帮助

0d507c66 1850385 C8b1a773 1850385