fixed upenai

This commit is contained in:
2024-08-19 13:38:39 -05:00
parent fe6c4e5684
commit f920da8537
2 changed files with 24 additions and 13 deletions
+3
View File
@@ -48,6 +48,9 @@ class BaseTelegramInferenceBot(ABC):
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
function_args = json.loads(function_call_arguments if function_call_arguments is not None else "{}")
+20 -12
View File
@@ -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)
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