Skip to content

Commit

Permalink
Updated dependencies, fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Biunovich committed Apr 6, 2024
1 parent aba0e9c commit 74fea6d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,5 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

.DS_Store
.vscode
11 changes: 4 additions & 7 deletions Bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import openai
from dotenv import load_dotenv
from openai import error
from telegram import Update
from telegram.ext import (Application, CommandHandler, ContextTypes,
MessageHandler, filters)
Expand All @@ -31,7 +30,7 @@
logger.error("No environment variables were found")
sys.exit()

openai.api_key = openai_api_key
openai_client = openai.AsyncOpenAI(api_key=openai_api_key, timeout=30)


def append_message(messages, role, message):
Expand All @@ -48,12 +47,10 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
append_message(messages, "system", "You are a helpful assistant")
append_message(messages, "user", message)
try:
response = await openai.ChatCompletion.acreate(
response = await openai_client.chat.completions.create(
model="gpt-4-turbo-preview",
messages=messages,
temperature=0.5,
user=user_id,
request_timeout=30
user=user_id
)
reply = response.choices[0].message.content
await update.message.reply_text(reply)
Expand All @@ -62,7 +59,7 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
name, user_id, list(
map(lambda x: f"{x['role']}:{x['content'][:100]}", messages[-3:]))
)
except error.APIConnectionError as e:
except openai.APIConnectionError as e:
logger.error(repr(e))
if (e.code == "context_length_exceeded"):
context.user_data["messages"] = []
Expand Down
21 changes: 17 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
python-telegram-bot
openai
python-dotenv
aiohttp
annotated-types==0.6.0
anyio==4.3.0
certifi==2024.2.2
distro==1.9.0
exceptiongroup==1.2.0
h11==0.14.0
httpcore==1.0.5
httpx==0.27.0
idna==3.6
openai==1.16.2
pydantic==2.6.4
pydantic_core==2.16.3
python-dotenv==1.0.1
python-telegram-bot==21.0.1
sniffio==1.3.1
tqdm==4.66.2
typing_extensions==4.11.0

0 comments on commit 74fea6d

Please sign in to comment.