From f920da853710b6678ac4945157987831508ef46e Mon Sep 17 00:00:00 2001 From: Jonathan Lucas Date: Mon, 19 Aug 2024 13:38:39 -0500 Subject: [PATCH] fixed upenai --- base_telegram_inference_bot.py | 3 +++ chatgpt_telegram_inference_bot.py | 34 +++++++++++++++++++------------ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/base_telegram_inference_bot.py b/base_telegram_inference_bot.py index cbc8279..95f3d00 100644 --- a/base_telegram_inference_bot.py +++ b/base_telegram_inference_bot.py @@ -47,6 +47,9 @@ class BaseTelegramInferenceBot(ABC): def clear_conversation(self, user_id): if user_id in self.conversation_history: del self.conversation_history[user_id] + + for tool in self.tools: + tool.clear() def call_tool(self, function_call_name, function_call_arguments): function_name = function_call_name diff --git a/chatgpt_telegram_inference_bot.py b/chatgpt_telegram_inference_bot.py index 4bf6f91..9bd86d2 100644 --- a/chatgpt_telegram_inference_bot.py +++ b/chatgpt_telegram_inference_bot.py @@ -16,8 +16,7 @@ class ChatGPTTelegramInferenceBot(BaseTelegramInferenceBot): response = self.client.chat.completions.create( model=self.model, messages=[{"role": "system", "content": self.system_prompt}] + messages, - functions=self.functions, - function_call="auto", + tools=self.functions, max_tokens=self.max_tokens ) return response @@ -30,26 +29,35 @@ class ChatGPTTelegramInferenceBot(BaseTelegramInferenceBot): messages = self.conversation_history[user_id] response = self.get_chat_response(messages) - 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) + assistant_message = {} + + for message_part in response.choices: + if message_part.finish_reason == "function_call": + tool_calls.append(message_part.message.function_call) + else: + assistant_message = response.choices[0].message tool_use_count = 0 while len(tool_calls) > 0 and tool_use_count < 50: tool_use_results = [] - for tool_call in tool_calls: - tool_response = self.call_tool(tool_call) + + while len(tool_calls) > 0: + tool_call = tool_calls.pop(0) + tool_response = self.call_tool(tool_call.name, tool_call.arguments) tool_use_results.append({"role": "function", "name": tool_call.name, "content": json.dumps(tool_response)}) messages.extend(tool_use_results) - response = self.get_chat_response(messages) - assistant_message = response.choices[0].message - messages.append({"role": "assistant", "content": assistant_message.content}) - tool_calls = [] - if hasattr(assistant_message, 'function_call') and assistant_message.function_call is not None: - tool_calls.append(assistant_message.function_call) + response = self.get_chat_response(messages) + + for message_part in response.choices: + if message_part.finish_reason == "function_call": + tool_calls.append(message_part.message.function_call) + else: + assistant_message = response.choices[0].message + messages.append({"role": "assistant", "content": assistant_message.content}) tool_use_count += 1