fixed upenai
This commit is contained in:
@@ -48,6 +48,9 @@ class BaseTelegramInferenceBot(ABC):
|
|||||||
if user_id in self.conversation_history:
|
if user_id in self.conversation_history:
|
||||||
del self.conversation_history[user_id]
|
del self.conversation_history[user_id]
|
||||||
|
|
||||||
|
for tool in self.tools:
|
||||||
|
tool.clear()
|
||||||
|
|
||||||
def call_tool(self, function_call_name, function_call_arguments):
|
def call_tool(self, function_call_name, function_call_arguments):
|
||||||
function_name = function_call_name
|
function_name = function_call_name
|
||||||
function_args = json.loads(function_call_arguments if function_call_arguments is not None else "{}")
|
function_args = json.loads(function_call_arguments if function_call_arguments is not None else "{}")
|
||||||
|
|||||||
@@ -16,8 +16,7 @@ class ChatGPTTelegramInferenceBot(BaseTelegramInferenceBot):
|
|||||||
response = self.client.chat.completions.create(
|
response = self.client.chat.completions.create(
|
||||||
model=self.model,
|
model=self.model,
|
||||||
messages=[{"role": "system", "content": self.system_prompt}] + messages,
|
messages=[{"role": "system", "content": self.system_prompt}] + messages,
|
||||||
functions=self.functions,
|
tools=self.functions,
|
||||||
function_call="auto",
|
|
||||||
max_tokens=self.max_tokens
|
max_tokens=self.max_tokens
|
||||||
)
|
)
|
||||||
return response
|
return response
|
||||||
@@ -30,26 +29,35 @@ class ChatGPTTelegramInferenceBot(BaseTelegramInferenceBot):
|
|||||||
messages = self.conversation_history[user_id]
|
messages = self.conversation_history[user_id]
|
||||||
|
|
||||||
response = self.get_chat_response(messages)
|
response = self.get_chat_response(messages)
|
||||||
assistant_message = response.choices[0].message
|
|
||||||
tool_calls = []
|
tool_calls = []
|
||||||
if hasattr(assistant_message, 'function_call') and assistant_message.function_call is not None:
|
assistant_message = {}
|
||||||
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
|
||||||
|
|
||||||
tool_use_count = 0
|
tool_use_count = 0
|
||||||
while len(tool_calls) > 0 and tool_use_count < 50:
|
while len(tool_calls) > 0 and tool_use_count < 50:
|
||||||
tool_use_results = []
|
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)})
|
tool_use_results.append({"role": "function", "name": tool_call.name, "content": json.dumps(tool_response)})
|
||||||
|
|
||||||
messages.extend(tool_use_results)
|
messages.extend(tool_use_results)
|
||||||
|
|
||||||
response = self.get_chat_response(messages)
|
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
|
assistant_message = response.choices[0].message
|
||||||
messages.append({"role": "assistant", "content": assistant_message.content})
|
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)
|
|
||||||
|
|
||||||
tool_use_count += 1
|
tool_use_count += 1
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user