Merge pull request #70 from bucolucas/update-chatgpt-bot

Update chatgpt_telegram_inference_bot.py with OpenAI implementation
This commit is contained in:
2024-08-19 10:55:47 -05:00
committed by GitHub
+49 -58
View File
@@ -18,6 +18,16 @@ client = OpenAI(
api_key=os.environ.get("OPENAI_API_KEY"), 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 # Set up logging to console and file
logging.basicConfig(level=logging.WARNING, handlers=[ logging.basicConfig(level=logging.WARNING, handlers=[
logging.StreamHandler(), logging.StreamHandler(),
@@ -100,39 +110,37 @@ 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('function_calls', []) assistant_message = response.choices[0].message
assistant_message = response.get('content', '') tool_calls = []
messages.append({"role": "assistant", "content": assistant_message}) if hasattr(assistant_message, 'function_call') and assistant_message.function_call is not None:
tool_calls.append(assistant_message.function_call)
toolUseCount = 0 toolUseCount = 0
previous_function_name = ""
while len(tool_calls) > 0 and toolUseCount < 50 and processing_status[user_id]["processing"]: while len(tool_calls) > 0 and toolUseCount < 50 and processing_status[user_id]["processing"]:
tool_use_results = [] tool_use_results = []
for tool_call in tool_calls: while len(tool_calls) > 0:
function_name = tool_call['name'] tool_call = tool_calls.pop(0)
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}") # Update status message
previous_function_name = function_name 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_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.extend(tool_use_results)
messages.append(formatted_result)
response = get_chat_response(messages) response = get_chat_response(messages)
tool_calls = response.get('function_calls', []) assistant_message = response.choices[0].message
assistant_message = response.get('content', '') messages.append({"role": "assistant", "content": assistant_message.content})
messages.append({"role": "assistant", "content": assistant_message}) if hasattr(assistant_message, 'function_call') and assistant_message.function_call is not None:
tool_calls.append(assistant_message.function_call)
toolUseCount += 1 toolUseCount += 1
if toolUseCount == 0: 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: if len(conversation_history[user_id]) > 20:
conversation_history[user_id] = 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) await context.bot.delete_message(chat_id=update.effective_chat.id, message_id=status_message.message_id)
del processing_status[user_id] del processing_status[user_id]
try: try:
await update.message.reply_text(assistant_message) await update.message.reply_text(messages[-1]["content"])
except TelegramErrors.BadRequest as e: except TelegramErrors.BadRequest as e:
logging.error(f"An error occurred when trying to send a message in telegram: {str(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.") await update.message.reply_text("Sorry, an error occurred while processing your request.")
def call_tool(function_call): def call_tool(function_call):
function_name = function_call['name'] function_name = function_call.name
function_args = json.loads(function_call['arguments']) function_args = json.loads(function_call.arguments)
for tool in tools: for tool in tools:
if function_name in [f["name"] for f in tool.get_functions()]: if function_name in [f["name"] for f in tool.get_functions()]:
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) 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): async def switch(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
try: global use_smart_model
openai_functions = [ use_smart_model = not use_smart_model
{ model = GPT_4O if use_smart_model else GPT_4O_MINI
"name": function['name'], logging.info(f"Switched to model: {model}")
"description": function['description'], await update.message.reply_text(f"Switched to model: {model}")
"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 status(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 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: async def abort_processing(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
query = update.callback_query query = update.callback_query
@@ -219,6 +209,7 @@ def main() -> None:
# Add handlers # Add handlers
application.add_handler(CommandHandler("start", start)) application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("clear", clear)) application.add_handler(CommandHandler("clear", clear))
application.add_handler(CommandHandler("switch", switch))
application.add_handler(CommandHandler("status", status)) application.add_handler(CommandHandler("status", status))
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
application.add_handler(CallbackQueryHandler(abort_processing, pattern='^abort$')) application.add_handler(CallbackQueryHandler(abort_processing, pattern='^abort$'))