Add JSON serialization helper and update handle_message function
This commit is contained in:
@@ -51,6 +51,18 @@ functions = []
|
|||||||
for tool in tools:
|
for tool in tools:
|
||||||
functions.extend(tool.get_functions())
|
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:
|
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
logging.info("Bot started")
|
logging.info("Bot started")
|
||||||
await update.message.reply_text(
|
await update.message.reply_text(
|
||||||
@@ -98,7 +110,7 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
|||||||
tool_calls = []
|
tool_calls = []
|
||||||
fullMessage = []
|
fullMessage = []
|
||||||
for message_part in response.content:
|
for message_part in response.content:
|
||||||
fullMessage.append(message_part)
|
fullMessage.append(json_serializable(message_part))
|
||||||
if message_part.type == "tool_use":
|
if message_part.type == "tool_use":
|
||||||
tool_calls.append(message_part)
|
tool_calls.append(message_part)
|
||||||
messages.append({"role": "assistant", "content": fullMessage})
|
messages.append({"role": "assistant", "content": fullMessage})
|
||||||
@@ -118,9 +130,7 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
|||||||
previous_function_name = function_name
|
previous_function_name = function_name
|
||||||
|
|
||||||
tool_response = call_tool(tool_call)
|
tool_response = call_tool(tool_call)
|
||||||
tool_use_results.append({"type": "tool_result", "tool_use_id": tool_call.id, "content": json.dumps(tool_response)})
|
tool_use_results.append({"type": "tool_result", "tool_use_id": tool_call.id, "content": json.dumps(json_serializable(tool_response))})
|
||||||
|
|
||||||
formatted_result = {}
|
|
||||||
|
|
||||||
formatted_result = {"role": "user", "content": tool_use_results}
|
formatted_result = {"role": "user", "content": tool_use_results}
|
||||||
|
|
||||||
@@ -129,22 +139,22 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
|||||||
response = get_chat_response(messages)
|
response = get_chat_response(messages)
|
||||||
fullMessage = []
|
fullMessage = []
|
||||||
for message_part in response.content:
|
for message_part in response.content:
|
||||||
fullMessage.append(message_part)
|
fullMessage.append(json_serializable(message_part))
|
||||||
if message_part.type == "tool_use":
|
if message_part.type == "tool_use":
|
||||||
tool_calls.append(message_part)
|
tool_calls.append(message_part)
|
||||||
messages.append({"role": "assistant", "content": fullMessage})
|
messages.append({"role": "assistant", "content": fullMessage})
|
||||||
|
|
||||||
toolUseCount += 1
|
toolUseCount += 1
|
||||||
|
|
||||||
if (toolUseCount == 0):
|
if toolUseCount == 0:
|
||||||
assistant_reply = response.content
|
assistant_reply = json_serializable(response.content)
|
||||||
messages.append({"role": "assistant", "content": assistant_reply})
|
messages.append({"role": "assistant", "content": assistant_reply})
|
||||||
|
|
||||||
if len(messages) > 20:
|
if len(messages) > 20:
|
||||||
messages = messages[-20:]
|
messages = messages[-20:]
|
||||||
|
|
||||||
# Save the updated conversation history
|
# Save the updated conversation history
|
||||||
db.save_conversation(user_id, messages)
|
db.save_conversation(user_id, json_serializable(messages))
|
||||||
|
|
||||||
# Remove the status message
|
# Remove the status message
|
||||||
await context.bot.delete_message(chat_id=update.effective_chat.id, message_id=status_message.message_id)
|
await context.bot.delete_message(chat_id=update.effective_chat.id, message_id=status_message.message_id)
|
||||||
|
|||||||
Reference in New Issue
Block a user