Spaces:
Runtime error
Runtime error
File size: 1,601 Bytes
8bda2ce | 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 | import asyncio
import textwrap
import openai
from halo import Halo
gpt_costs_per_thousand_out = {
"gpt-3.5-turbo-16k": 0.004,
"gpt-4-32k": 0.12,
}
gpt_costs_per_thousand_in = {
"gpt-3.5-turbo-16k": 0.003,
"gpt-4-32k": 0.06,
}
def estimate_costs(prompt_tokens, model: str):
costs = (prompt_tokens / 1000) * gpt_costs_per_thousand_in[model]
return costs
async def chatbot(conversation, model, temperature=0):
max_retry = 7
retry = 0
while True:
try:
response = await openai.ChatCompletion.acreate(
model=model, messages=conversation, temperature=temperature
)
text = response["choices"][0]["message"]["content"]
return text, response["usage"]
except Exception as oops:
print(f'\n\nError communicating with OpenAI: "{oops}"')
if "maximum context length" in str(oops):
a = conversation.pop(0)
print("\n\n DEBUG: Trimming oldest message")
continue
retry += 1
if retry >= max_retry:
print(f"\n\nExiting due to excessive errors in API: {oops}")
exit(1)
print(f"\n\nRetrying in {2 ** (retry - 1) * 5} seconds...")
await asyncio.sleep(2 ** (retry - 1) * 5)
def chat_print(text):
formatted_lines = [
textwrap.fill(line, width=120, initial_indent=" ", subsequent_indent=" ")
for line in text.split("\n")
]
formatted_text = "\n".join(formatted_lines)
print("\n\n\nCHATBOT:\n\n%s" % formatted_text)
|