diff --git a/telegram_inference_bot.py b/telegram_inference_bot.py index 3d37c6b..e12e192 100644 --- a/telegram_inference_bot.py +++ b/telegram_inference_bot.py @@ -113,7 +113,7 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> conversation_history[user_id].append({"role": "user", "content": user_message}) # Prepare messages for OpenAI API - messages = [{"role": "system", "content": system_prompt}] + conversation_history[user_id] + messages = conversation_history[user_id] response = get_chat_response(messages) @@ -163,17 +163,13 @@ def call_tool(function_call): return tool.execute(function_name, **eval(function_args)) def get_chat_response(messages): - if use_anthropic: - response = get_openai_response(messages) - else: - response = get_claude_response(messages) - return response + return get_claude_response(messages) if use_anthropic else get_openai_response(messages) def get_openai_response(messages): model = GPT_4O if use_smart_model else GPT_4O_MINI response = openai_client.chat.completions.create( model=model, - messages=messages, + messages = [{"role": "system", "content": system_prompt}] + messages, functions=functions, function_call="auto", max_tokens=model_max_tokens[model] @@ -181,25 +177,35 @@ def get_openai_response(messages): return response def get_claude_response(messages): + anthropic_tools = [ + { + "name": function['name'], + "description": function['description'], + "input_schema": function['parameters'] if function['parameters'] not in [None, {}] else {"type": "object", "properties": {"param1": {"type": "string", "description": "Unnecessary"}}, "required": []} + } + for function in functions + ] response = anthropic_client.messages.create( + system=system_prompt, messages=messages, + tools=anthropic_tools, max_tokens=4096, model="claude-3-5-sonnet-20240620" ) return response -def switch(update: Update, context: ContextTypes.DEFAULT_TYPE) -> 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}") - update.message.reply_text(f"Switched to model: {model}") + await update.message.reply_text(f"Switched to model: {model}") -def switch_anthropic(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: +async def switch_anthropic(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: global use_anthropic use_anthropic = not use_anthropic logging.info("Using Anthropic" if use_anthropic else "Using OpenAI") - update.message.reply_text("Using Anthropic" if use_anthropic else "Using OpenAI") + await update.message.reply_text("Using Anthropic" if use_anthropic else "Using OpenAI") async def status(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: if use_anthropic: diff --git a/tools/persona_tool.py b/tools/persona_tool.prepy similarity index 100% rename from tools/persona_tool.py rename to tools/persona_tool.prepy diff --git a/tools/telegram_keyboard_tool.py b/tools/telegram_keyboard_tool.prepy similarity index 100% rename from tools/telegram_keyboard_tool.py rename to tools/telegram_keyboard_tool.prepy