updated names of bots

This commit is contained in:
2024-08-19 10:27:20 -05:00
parent acba08bbc2
commit 847d5ccd10
2 changed files with 22 additions and 19 deletions
@@ -9,13 +9,15 @@ from telegram.ext import Application, CommandHandler, MessageHandler, filters, C
from dotenv import load_dotenv from dotenv import load_dotenv
from tools.base_tool import BaseTool from tools.base_tool import BaseTool
from tools.metrics_tool import MetricsTool from tools.metrics_tool import MetricsTool
import openai from openai import OpenAI
# Load environment variables # Load environment variables
load_dotenv() load_dotenv()
openai.api_key = os.environ.get("OPENAI_API_KEY") openai_client = OpenAI(
openai.api_base = os.environ.get("OPENAI_API_BASE", "https://api.openai.com/v1") 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 # Set up logging to console and file
logging.basicConfig(level=logging.WARNING, handlers=[ logging.basicConfig(level=logging.WARNING, handlers=[
@@ -55,7 +57,7 @@ for tool in tools:
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(
"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: 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: for tool in tools:
tool.clear() tool.clear()
logging.info(f"Cleared conversation history and image for user {user_id}") logging.info(f"Cleared conversation history for user {user_id}")
await update.message.reply_text("Conversation history and image cleared. Let's start fresh!") 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): async def update_status_message(context: ContextTypes.DEFAULT_TYPE, chat_id: int, message_id: int, status: str):
keyboard = [ keyboard = [
@@ -99,9 +101,13 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
messages = conversation_history[user_id] messages = conversation_history[user_id]
response = get_chat_response(messages) response = get_chat_response(messages)
tool_calls = response.get('tool_calls', []) tool_calls = []
assistant_message = response.get('content', '') for choice in response.choices:
messages.append({"role": "assistant", "content": assistant_message}) 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 toolUseCount = 0
previous_function_name = "" previous_function_name = ""
@@ -153,22 +159,19 @@ def call_tool(function_call):
return tool.execute(function_name, **function_args) return tool.execute(function_name, **function_args)
def get_chat_response(messages): def get_chat_response(messages):
return get_openai_response(messages)
def get_openai_response(messages):
try: try:
response = openai.ChatCompletion.create( response = openai_client.chat.completions.create(
model="gpt-3.5-turbo", # You can change this to your desired model 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, messages=[{"role": "system", "content": system_prompt}] + messages,
max_tokens=1000, max_tokens=4096,
temperature=0.7, temperature=0.6,
top_p=1, top_p=1,
frequency_penalty=0, frequency_penalty=0,
presence_penalty=0, presence_penalty=0,
functions=functions, tools=functions
function_call="auto" #tool_choice={ }
) )
return response['choices'][0]['message'] return response
except Exception as e: except Exception as e:
logging.error(f"An error occurred: {str(e)}") logging.error(f"An error occurred: {str(e)}")
return None return None