Implemented logging output to both console and logs/output.log in telegram_inference_bot.py.
This commit is contained in:
+11
-16
@@ -19,20 +19,13 @@ client = OpenAI()
|
|||||||
GPT_4O = "gpt-4o"
|
GPT_4O = "gpt-4o"
|
||||||
GPT_4O_MINI = "gpt-4o-mini"
|
GPT_4O_MINI = "gpt-4o-mini"
|
||||||
|
|
||||||
# Set up logging
|
# Set up logging to console and file
|
||||||
log_file = 'logs/output.log'
|
logging.basicConfig(level=logging.INFO, handlers=[
|
||||||
os.makedirs(os.path.dirname(log_file), exist_ok=True)
|
logging.StreamHandler(),
|
||||||
|
logging.FileHandler('logs/output.log', mode='a')
|
||||||
|
])
|
||||||
|
|
||||||
logging.basicConfig(
|
# TODO: ensure log output goes to both console and logs/output.log
|
||||||
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')
|
||||||
@@ -64,6 +57,7 @@ for tool in tools:
|
|||||||
functions.extend(tool.get_functions())
|
functions.extend(tool.get_functions())
|
||||||
|
|
||||||
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
|
logging.info("Bot started")
|
||||||
await update.message.reply_text("Hello! I'm your AI assistant. How can I help you today? You can send me images and then ask questions about them.")
|
await update.message.reply_text("Hello! I'm your AI assistant. How can I help you today? You can send me images and then ask questions about them.")
|
||||||
|
|
||||||
async def clear(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
async def clear(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
@@ -73,6 +67,7 @@ async def clear(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
|||||||
if user_id in user_images:
|
if user_id in user_images:
|
||||||
os.remove(user_images[user_id])
|
os.remove(user_images[user_id])
|
||||||
del user_images[user_id]
|
del user_images[user_id]
|
||||||
|
logging.info(f"Cleared conversation history and image for user {user_id}")
|
||||||
await update.message.reply_text("Conversation history and image cleared. Let's start fresh!")
|
await update.message.reply_text("Conversation history and image cleared. Let's start fresh!")
|
||||||
|
|
||||||
async def handle_image(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
async def handle_image(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
@@ -89,6 +84,7 @@ async def handle_image(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
|
|||||||
await photo_file.download_to_drive(custom_path=temp_file.name)
|
await photo_file.download_to_drive(custom_path=temp_file.name)
|
||||||
user_images[user_id] = temp_file.name
|
user_images[user_id] = temp_file.name
|
||||||
|
|
||||||
|
logging.info(f"Received image from user {user_id}")
|
||||||
await update.message.reply_text("I've received your image. What would you like to know about it?")
|
await update.message.reply_text("I've received your image. What would you like to know about it?")
|
||||||
|
|
||||||
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
@@ -96,6 +92,8 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
|||||||
user_id = update.effective_user.id
|
user_id = update.effective_user.id
|
||||||
user_message = update.message.text
|
user_message = update.message.text
|
||||||
|
|
||||||
|
logging.info(f"Message from user {user_id}: {user_message}")
|
||||||
|
|
||||||
# Initialize conversation history for new users
|
# Initialize conversation history for new users
|
||||||
if user_id not in conversation_history:
|
if user_id not in conversation_history:
|
||||||
conversation_history[user_id] = []
|
conversation_history[user_id] = []
|
||||||
@@ -171,7 +169,6 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
|||||||
logging.error(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
|
||||||
@@ -180,7 +177,6 @@ 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,
|
||||||
@@ -191,7 +187,6 @@ 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()
|
||||||
|
|||||||
Reference in New Issue
Block a user