Fixed database issues by removing database
This commit is contained in:
+20
-28
@@ -9,7 +9,6 @@ from telegram.ext import Application, CommandHandler, MessageHandler, filters, C
|
||||
from dotenv import load_dotenv
|
||||
from tools.base_tool import BaseTool
|
||||
from anthropic import Anthropic
|
||||
from database import db # Import the database handler
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
@@ -32,6 +31,9 @@ TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
|
||||
with open("prompts/developer_prompt.txt", "r") as file:
|
||||
system_prompt = file.read().strip()
|
||||
|
||||
# Dictionary to store conversation history for each user
|
||||
conversation_history = {}
|
||||
|
||||
# Dictionary to store processing status for each user
|
||||
processing_status = {}
|
||||
|
||||
@@ -51,18 +53,6 @@ functions = []
|
||||
for tool in tools:
|
||||
functions.extend(tool.get_functions())
|
||||
|
||||
def json_serializable(obj):
|
||||
if isinstance(obj, (str, int, float, bool, type(None))):
|
||||
return obj
|
||||
elif isinstance(obj, (list, tuple)):
|
||||
return [json_serializable(item) for item in obj]
|
||||
elif isinstance(obj, dict):
|
||||
return {key: json_serializable(value) for key, value in obj.items()}
|
||||
elif hasattr(obj, '__dict__'):
|
||||
return json_serializable(obj.__dict__)
|
||||
else:
|
||||
return str(obj)
|
||||
|
||||
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
logging.info("Bot started")
|
||||
await update.message.reply_text(
|
||||
@@ -71,7 +61,8 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
|
||||
async def clear(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
user_id = update.effective_user.id
|
||||
db.clear_conversation(user_id)
|
||||
if user_id in conversation_history:
|
||||
del conversation_history[user_id]
|
||||
for tool in tools:
|
||||
tool.clear()
|
||||
|
||||
@@ -97,20 +88,22 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
||||
|
||||
logging.info(f"Message from user {user_id}: {user_message}")
|
||||
|
||||
conversation_history = db.get_conversation(user_id)
|
||||
conversation_history.append({"role": "user", "content": user_message})
|
||||
if user_id not in conversation_history:
|
||||
conversation_history[user_id] = []
|
||||
|
||||
conversation_history[user_id].append({"role": "user", "content": user_message})
|
||||
|
||||
# Send initial status message
|
||||
status_message = await update.message.reply_text("Processing your request...", reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("Abort", callback_data='abort')]]))
|
||||
processing_status[user_id] = {"processing": True, "message_id": status_message.message_id}
|
||||
|
||||
messages = conversation_history
|
||||
messages = conversation_history[user_id]
|
||||
|
||||
response = get_chat_response(messages)
|
||||
tool_calls = []
|
||||
fullMessage = []
|
||||
for message_part in response.content:
|
||||
fullMessage.append(json_serializable(message_part))
|
||||
fullMessage.append(message_part)
|
||||
if message_part.type == "tool_use":
|
||||
tool_calls.append(message_part)
|
||||
messages.append({"role": "assistant", "content": fullMessage})
|
||||
@@ -130,7 +123,9 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
||||
previous_function_name = function_name
|
||||
|
||||
tool_response = call_tool(tool_call)
|
||||
tool_use_results.append({"type": "tool_result", "tool_use_id": tool_call.id, "content": json.dumps(json_serializable(tool_response))})
|
||||
tool_use_results.append({"type": "tool_result", "tool_use_id": tool_call.id, "content": json.dumps(tool_response)})
|
||||
|
||||
formatted_result = {}
|
||||
|
||||
formatted_result = {"role": "user", "content":tool_use_results}
|
||||
|
||||
@@ -139,22 +134,19 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
||||
response = get_chat_response(messages)
|
||||
fullMessage = []
|
||||
for message_part in response.content:
|
||||
fullMessage.append(json_serializable(message_part))
|
||||
fullMessage.append(message_part)
|
||||
if message_part.type == "tool_use":
|
||||
tool_calls.append(message_part)
|
||||
messages.append({"role": "assistant", "content": fullMessage})
|
||||
|
||||
toolUseCount += 1
|
||||
|
||||
if toolUseCount == 0:
|
||||
assistant_reply = json_serializable(response.content)
|
||||
messages.append({"role": "assistant", "content": assistant_reply})
|
||||
if (toolUseCount == 0):
|
||||
assistant_reply = response.content
|
||||
conversation_history[user_id].append({"role": "assistant", "content": assistant_reply})
|
||||
|
||||
if len(messages) > 20:
|
||||
messages = messages[-20:]
|
||||
|
||||
# Save the updated conversation history
|
||||
db.save_conversation(user_id, json_serializable(messages))
|
||||
if len(conversation_history[user_id]) > 20:
|
||||
conversation_history[user_id] = conversation_history[user_id][-20:]
|
||||
|
||||
# Remove the status message
|
||||
await context.bot.delete_message(chat_id=update.effective_chat.id, message_id=status_message.message_id)
|
||||
|
||||
Reference in New Issue
Block a user