代码拉取完成,页面将自动刷新
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"]
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。