From 847d5ccd1058c672a2cb2637420d708d5d51f68c Mon Sep 17 00:00:00 2001 From: Jonathan Lucas Date: Mon, 19 Aug 2024 10:27:20 -0500 Subject: [PATCH] updated names of bots --- ....py => anthropic_telegram_inference_bot.py | 0 ...> openai_generic_telegram_inference_bot.py | 41 ++++++++++--------- 2 files changed, 22 insertions(+), 19 deletions(-) rename telegram_inference_bot.py => anthropic_telegram_inference_bot.py (100%) rename openai_telegram_inference_bot.py => openai_generic_telegram_inference_bot.py (88%) diff --git a/telegram_inference_bot.py b/anthropic_telegram_inference_bot.py similarity index 100% rename from telegram_inference_bot.py rename to anthropic_telegram_inference_bot.py diff --git a/openai_telegram_inference_bot.py b/openai_generic_telegram_inference_bot.py similarity index 88% rename from openai_telegram_inference_bot.py rename to openai_generic_telegram_inference_bot.py index dd3ffc0..e7e6f71 100644 --- a/openai_telegram_inference_bot.py +++ b/openai_generic_telegram_inference_bot.py @@ -9,13 +9,15 @@ from telegram.ext import Application, CommandHandler, MessageHandler, filters, C from dotenv import load_dotenv from tools.base_tool import BaseTool from tools.metrics_tool import MetricsTool -import openai +from openai import OpenAI # Load environment variables load_dotenv() -openai.api_key = os.environ.get("OPENAI_API_KEY") -openai.api_base = os.environ.get("OPENAI_API_BASE", "https://api.openai.com/v1") +openai_client = OpenAI( + api_key=os.environ.get("GLHF_CHAT_API_KEY"), + base_url=os.environ.get("GLHF_CHAT_API_BASE", "https://api.openai.com/v1") +) # Set up logging to console and file logging.basicConfig(level=logging.WARNING, handlers=[ @@ -55,7 +57,7 @@ for tool in tools: 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." + "Hello! I'm your AI assistant. How can I help you today?" ) async def clear(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: @@ -65,8 +67,8 @@ async def clear(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: for tool in tools: tool.clear() - 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!") + logging.info(f"Cleared conversation history for user {user_id}") + await update.message.reply_text("Conversation history cleared. Let's start fresh!") async def update_status_message(context: ContextTypes.DEFAULT_TYPE, chat_id: int, message_id: int, status: str): keyboard = [ @@ -99,9 +101,13 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> messages = conversation_history[user_id] response = get_chat_response(messages) - tool_calls = response.get('tool_calls', []) - assistant_message = response.get('content', '') - messages.append({"role": "assistant", "content": assistant_message}) + tool_calls = [] + for choice in response.choices: + if choice.finish_reason == "tool_calls": + tool_calls.append(choice) + else: + assistant_message = choice.message.content + messages.append({"role": "assistant", "content": assistant_message}) toolUseCount = 0 previous_function_name = "" @@ -153,22 +159,19 @@ def call_tool(function_call): return tool.execute(function_name, **function_args) def get_chat_response(messages): - return get_openai_response(messages) - -def get_openai_response(messages): try: - response = openai.ChatCompletion.create( - model="gpt-3.5-turbo", # You can change this to your desired model + response = openai_client.chat.completions.create( + model="hf:meta-llama/Meta-Llama-3.1-405B-Instruct", # You can change this to your desired model messages=[{"role": "system", "content": system_prompt}] + messages, - max_tokens=1000, - temperature=0.7, + max_tokens=4096, + temperature=0.6, top_p=1, frequency_penalty=0, presence_penalty=0, - functions=functions, - function_call="auto" + tools=functions + #tool_choice={ } ) - return response['choices'][0]['message'] + return response except Exception as e: logging.error(f"An error occurred: {str(e)}") return None