Add logging functionality for console and file outputs.
This commit is contained in:
@@ -4,6 +4,7 @@ import importlib
|
|||||||
import inspect
|
import inspect
|
||||||
import tempfile
|
import tempfile
|
||||||
import base64
|
import base64
|
||||||
|
import logging
|
||||||
from telegram import Update
|
from telegram import Update
|
||||||
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
|
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
@@ -18,7 +19,20 @@ client = OpenAI()
|
|||||||
GPT_4O = "gpt-4o"
|
GPT_4O = "gpt-4o"
|
||||||
GPT_4O_MINI = "gpt-4o-mini"
|
GPT_4O_MINI = "gpt-4o-mini"
|
||||||
|
|
||||||
# TODO: ensure log output goes to both console and logs/output.log
|
# Set up logging
|
||||||
|
log_file = 'logs/output.log'
|
||||||
|
os.makedirs(os.path.dirname(log_file), exist_ok=True)
|
||||||
|
|
||||||
|
logging.basicConfig(
|
||||||
|
level=logging.INFO,
|
||||||
|
format='%(asctime)s - %(levelname)s - %(message)s',
|
||||||
|
handlers=[
|
||||||
|
logging.FileHandler(log_file, mode='a'),
|
||||||
|
logging.StreamHandler()
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
logging.info("Logging has been set up.")
|
||||||
|
|
||||||
# Set up Telegram bot
|
# Set up Telegram bot
|
||||||
TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
|
TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
|
||||||
@@ -154,9 +168,10 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
|||||||
await update.message.reply_text(assistant_reply)
|
await update.message.reply_text(assistant_reply)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"An error occurred: {str(e)}")
|
logging.error(f"An error occurred: {str(e)}")
|
||||||
await update.message.reply_text("Sorry, an error occurred while processing your request.")
|
await update.message.reply_text("Sorry, an error occurred while processing your request.")
|
||||||
|
|
||||||
|
|
||||||
def call_tool(function_call, messages):
|
def call_tool(function_call, messages):
|
||||||
# Execute the function
|
# Execute the function
|
||||||
function_name = function_call.name
|
function_name = function_call.name
|
||||||
@@ -165,6 +180,7 @@ def call_tool(function_call, messages):
|
|||||||
if function_name in [f["name"] for f in tool.get_functions()]:
|
if function_name in [f["name"] for f in tool.get_functions()]:
|
||||||
return tool.execute(function_name, **eval(function_args))
|
return tool.execute(function_name, **eval(function_args))
|
||||||
|
|
||||||
|
|
||||||
def get_chat_response(client, messages, max_tokens, model):
|
def get_chat_response(client, messages, max_tokens, model):
|
||||||
response = client.chat.completions.create(
|
response = client.chat.completions.create(
|
||||||
model=model,
|
model=model,
|
||||||
@@ -175,6 +191,7 @@ def get_chat_response(client, messages, max_tokens, model):
|
|||||||
)
|
)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
# Create the Application and pass it your bot's token
|
# Create the Application and pass it your bot's token
|
||||||
application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
|
application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
|
||||||
@@ -186,7 +203,7 @@ def main() -> None:
|
|||||||
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
||||||
|
|
||||||
# Start the Bot
|
# Start the Bot
|
||||||
print("Bot is running...")
|
logging.info("Bot is running...")
|
||||||
application.run_polling()
|
application.run_polling()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user