diff --git a/chatgpt_telegram_inference_bot.py b/chatgpt_telegram_inference_bot.py index 5e86444..7bc6f93 100644 --- a/chatgpt_telegram_inference_bot.py +++ b/chatgpt_telegram_inference_bot.py @@ -18,6 +18,16 @@ client = OpenAI( api_key=os.environ.get("OPENAI_API_KEY"), ) +GPT_4O = "gpt-4" +GPT_4O_MINI = "gpt-4-1106-preview" + +model_max_tokens = { + GPT_4O: 4096, + GPT_4O_MINI: 16384 +} + +use_smart_model = False + # Set up logging to console and file logging.basicConfig(level=logging.WARNING, handlers=[ logging.StreamHandler(), @@ -100,39 +110,37 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> messages = conversation_history[user_id] response = get_chat_response(messages) - tool_calls = response.get('function_calls', []) - assistant_message = response.get('content', '') - messages.append({"role": "assistant", "content": assistant_message}) + assistant_message = response.choices[0].message + tool_calls = [] + if hasattr(assistant_message, 'function_call') and assistant_message.function_call is not None: + tool_calls.append(assistant_message.function_call) toolUseCount = 0 - previous_function_name = "" - while len(tool_calls) > 0 and toolUseCount < 50 and processing_status[user_id]["processing"]: tool_use_results = [] - for tool_call in tool_calls: - function_name = tool_call['name'] - - if previous_function_name != function_name: - await update_status_message(context, update.effective_chat.id, status_message.message_id, f"Using tool: {function_name}") - previous_function_name = function_name - + while len(tool_calls) > 0: + tool_call = tool_calls.pop(0) + function_name = tool_call.name + + # Update status message + await update_status_message(context, update.effective_chat.id, status_message.message_id, f"Using tool: {function_name}") + tool_response = call_tool(tool_call) - tool_use_results.append({"name": function_name, "content": json.dumps(tool_response)}) + tool_use_results.append({"role": "function", "name": function_name, "content": json.dumps(tool_response)}) - formatted_result = {"role": "function", "content": json.dumps(tool_use_results)} - - messages.append(formatted_result) + messages.extend(tool_use_results) response = get_chat_response(messages) - tool_calls = response.get('function_calls', []) - assistant_message = response.get('content', '') - messages.append({"role": "assistant", "content": assistant_message}) + assistant_message = response.choices[0].message + messages.append({"role": "assistant", "content": assistant_message.content}) + if hasattr(assistant_message, 'function_call') and assistant_message.function_call is not None: + tool_calls.append(assistant_message.function_call) toolUseCount += 1 if toolUseCount == 0: - conversation_history[user_id].append({"role": "assistant", "content": assistant_message}) + messages.append({"role": "assistant", "content": assistant_message.content}) if len(conversation_history[user_id]) > 20: conversation_history[user_id] = conversation_history[user_id][-20:] @@ -141,7 +149,7 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> await context.bot.delete_message(chat_id=update.effective_chat.id, message_id=status_message.message_id) del processing_status[user_id] try: - await update.message.reply_text(assistant_message) + await update.message.reply_text(messages[-1]["content"]) except TelegramErrors.BadRequest as e: logging.error(f"An error occurred when trying to send a message in telegram: {str(e)}") @@ -150,51 +158,33 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> await update.message.reply_text("Sorry, an error occurred while processing your request.") def call_tool(function_call): - function_name = function_call['name'] - function_args = json.loads(function_call['arguments']) + function_name = function_call.name + function_args = json.loads(function_call.arguments) for tool in tools: if function_name in [f["name"] for f in tool.get_functions()]: return tool.execute(function_name, **function_args) def get_chat_response(messages): - return get_openai_response(messages) + model = GPT_4O if use_smart_model else GPT_4O_MINI + response = client.chat.completions.create( + model=model, + messages = [{"role": "system", "content": system_prompt}] + messages, + functions=functions, + function_call="auto", + max_tokens=model_max_tokens[model] + ) + return response -def get_openai_response(messages): - try: - openai_functions = [ - { - "name": function['name'], - "description": function['description'], - "parameters": function['parameters'] if function['parameters'] not in [None, {}] else {"type": "object", "properties": {"param1": {"type": "string", "description": "Unnecessary"}}, "required": []} - } - for function in functions - ] - - response = client.chat.completions.create( - model="gpt-4", # Changed from "gpt-4o" to "gpt-4" - messages=[{"role": "system", "content": system_prompt}] + messages, - functions=openai_functions, - function_call="auto", - ) - - message = response.choices[0].message - content = message.content - function_call = message.function_call - - if function_call: - return { - 'content': content, - 'function_calls': [function_call] - } - else: - return {'content': content} - - except Exception as e: - logging.error(f"An error occurred: {str(e)}") - return None +async def switch(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + global use_smart_model + use_smart_model = not use_smart_model + model = GPT_4O if use_smart_model else GPT_4O_MINI + logging.info(f"Switched to model: {model}") + await update.message.reply_text(f"Switched to model: {model}") async def status(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - await update.message.reply_text("Currently using gpt-4") + model = GPT_4O if use_smart_model else GPT_4O_MINI + await update.message.reply_text(f"Currently using: {model}") async def abort_processing(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: query = update.callback_query @@ -219,6 +209,7 @@ def main() -> None: # Add handlers application.add_handler(CommandHandler("start", start)) application.add_handler(CommandHandler("clear", clear)) + application.add_handler(CommandHandler("switch", switch)) application.add_handler(CommandHandler("status", status)) application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) application.add_handler(CallbackQueryHandler(abort_processing, pattern='^abort$'))