kopia lustrzana https://codeberg.org/pluja/openai-telegram-bot
small optimizations
rodzic
edf2131a2e
commit
9767c18110
14
main.py
14
main.py
|
@ -166,12 +166,13 @@ def restricted(func):
|
|||
|
||||
|
||||
async def messageGPT(text: str, chat_id: str, user_name="User", user_data={}):
|
||||
await bot.send_chat_action(chat_id, action=types.ChatActions.TYPING)
|
||||
user_data["context"].append({"role": "user", "content": text})
|
||||
if len(user_data["context"]) > user_data["options"]["max-context"]:
|
||||
user_data["context"].pop(0)
|
||||
|
||||
try:
|
||||
await bot.send_chat_action(chat_id, action=types.ChatActions.TYPING)
|
||||
|
||||
response = openai.ChatCompletion.create(
|
||||
model=MODEL,
|
||||
messages=[
|
||||
|
@ -183,6 +184,10 @@ async def messageGPT(text: str, chat_id: str, user_name="User", user_data={}):
|
|||
+ user_data["context"],
|
||||
temperature=user_data["options"]["temperature"],
|
||||
)
|
||||
|
||||
user_data["usage"]["chatgpt"] += int(
|
||||
response.get("usage", {"total_tokens": 0})["total_tokens"]
|
||||
)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return f"There was a problem with OpenAI, so I can't answer you: \n\n{e}"
|
||||
|
@ -200,10 +205,6 @@ async def messageGPT(text: str, chat_id: str, user_name="User", user_data={}):
|
|||
if len(user_data["context"]) > user_data["options"]["max-context"]:
|
||||
user_data["context"].pop(0)
|
||||
|
||||
user_data["usage"]["chatgpt"] += int(
|
||||
response.get("usage", {"total_tokens": 0})["total_tokens"]
|
||||
)
|
||||
|
||||
database.update_user(chat_id, user_data)
|
||||
return assistant_message, user_data
|
||||
|
||||
|
@ -423,7 +424,6 @@ async def chat(message: types.Message):
|
|||
user_data = await getUserData(chat_id)
|
||||
|
||||
user_prompt = message.text
|
||||
await bot.send_chat_action(chat_id, action=types.ChatActions.TYPING)
|
||||
assistant_message, user_data = await messageGPT(
|
||||
user_prompt, chat_id, message.from_user.full_name, user_data
|
||||
)
|
||||
|
@ -431,7 +431,7 @@ async def chat(message: types.Message):
|
|||
await message.reply(assistant_message, parse_mode=ParseMode.MARKDOWN)
|
||||
|
||||
if user_data["options"]["assistant_voice_chat"]:
|
||||
asyncio.create_task(send_voice_message(chat_id, assistant_message))
|
||||
asyncio.ensure_future(send_voice_message(chat_id, assistant_message))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Ładowanie…
Reference in New Issue