kopia lustrzana https://codeberg.org/pluja/openai-telegram-bot
Use batches of 3
rodzic
2c0677b974
commit
8d46391bcc
20
main.py
20
main.py
|
@ -179,7 +179,9 @@ async def chat(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
sent_message = None
|
sent_message = None
|
||||||
|
|
||||||
# Process response chunks
|
# Process response chunks
|
||||||
|
batch = 3 # Batches of 10 to update
|
||||||
for chunk in response:
|
for chunk in response:
|
||||||
|
batch -= 1
|
||||||
if 'choices' in chunk:
|
if 'choices' in chunk:
|
||||||
choice = chunk['choices'][0]
|
choice = chunk['choices'][0]
|
||||||
if 'delta' in choice and 'content' in choice['delta']:
|
if 'delta' in choice and 'content' in choice['delta']:
|
||||||
|
@ -191,14 +193,16 @@ async def chat(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
sent_message = await context.bot.send_message(chat_id=update.effective_chat.id, text=assistant_message)
|
sent_message = await context.bot.send_message(chat_id=update.effective_chat.id, text=assistant_message)
|
||||||
message_sent = True
|
message_sent = True
|
||||||
else:
|
else:
|
||||||
try:
|
if batch == 0:
|
||||||
if new_content.strip() != "":
|
try:
|
||||||
await context.bot.edit_message_text(chat_id=update.effective_chat.id, message_id=sent_message.message_id, text=assistant_message)
|
if new_content.strip() != "":
|
||||||
except BadRequest as e:
|
await context.bot.edit_message_text(chat_id=update.effective_chat.id, message_id=sent_message.message_id, text=assistant_message)
|
||||||
if "Message is not modified" not in str(e):
|
except BadRequest as e:
|
||||||
raise e
|
if "Message is not modified" not in str(e):
|
||||||
except RetryAfter as e:
|
raise e
|
||||||
await asyncio.sleep(e.retry_after)
|
except RetryAfter as e:
|
||||||
|
await asyncio.sleep(e.retry_after)
|
||||||
|
batch = 3
|
||||||
|
|
||||||
# Update context
|
# Update context
|
||||||
user_context.append({"role": "assistant", "content": assistant_message})
|
user_context.append({"role": "assistant", "content": assistant_message})
|
||||||
|
|
Ładowanie…
Reference in New Issue