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
|
||||
|
||||
# Process response chunks
|
||||
batch = 3 # Batches of 10 to update
|
||||
for chunk in response:
|
||||
batch -= 1
|
||||
if 'choices' in chunk:
|
||||
choice = chunk['choices'][0]
|
||||
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)
|
||||
message_sent = True
|
||||
else:
|
||||
try:
|
||||
if new_content.strip() != "":
|
||||
await context.bot.edit_message_text(chat_id=update.effective_chat.id, message_id=sent_message.message_id, text=assistant_message)
|
||||
except BadRequest as e:
|
||||
if "Message is not modified" not in str(e):
|
||||
raise e
|
||||
except RetryAfter as e:
|
||||
await asyncio.sleep(e.retry_after)
|
||||
if batch == 0:
|
||||
try:
|
||||
if new_content.strip() != "":
|
||||
await context.bot.edit_message_text(chat_id=update.effective_chat.id, message_id=sent_message.message_id, text=assistant_message)
|
||||
except BadRequest as e:
|
||||
if "Message is not modified" not in str(e):
|
||||
raise e
|
||||
except RetryAfter as e:
|
||||
await asyncio.sleep(e.retry_after)
|
||||
batch = 3
|
||||
|
||||
# Update context
|
||||
user_context.append({"role": "assistant", "content": assistant_message})
|
||||
|
|
Ładowanie…
Reference in New Issue