From e6bc23e564e51aa149432fc67ce381a9260ee5f5 Mon Sep 17 00:00:00 2001 From: Kumi Date: Fri, 29 Dec 2023 22:56:22 +0100 Subject: [PATCH] Implement recursion check in response generation Added a safety check to prevent infinite recursion within the response generation function. When `use_tools` is active, the code now inspects the call stack and terminates the process if a certain recursion depth is exceeded. This ensures that the code is robust against potential infinite loops that could block or crash the service. A default threshold is set with a TODO for revisiting the hard-coded limit, and the recursion detection logs the occurrence for easier debugging and maintenance. Note: Recursion limit handling may require future adjustments to the `allow_override` parameter based on real-world feedback or testing. --- src/gptbot/classes/openai.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/gptbot/classes/openai.py b/src/gptbot/classes/openai.py index 307ac7e..9f02bd3 100644 --- a/src/gptbot/classes/openai.py +++ b/src/gptbot/classes/openai.py @@ -5,6 +5,7 @@ import tiktoken import asyncio import json import base64 +import inspect from functools import partial from contextlib import closing @@ -140,6 +141,24 @@ class OpenAI: f"Generating response to {len(messages)} messages for user {user} in room {room}..." ) + # Check current recursion depth to prevent infinite loops + + if use_tools: + frames = inspect.stack() + current_function = inspect.getframeinfo(frames[0][0]).function + count = sum(1 for frame in frames if inspect.getframeinfo(frame[0]).function == current_function) + self.logger.log(f"{current_function} appears {count} times in the call stack") + + if count > 5: + self.logger.log(f"Recursion depth exceeded, aborting.") + return self.generate_chat_response( + messages, + user=user, + room=room, + allow_override=False, # TODO: Could this be a problem? + use_tools=False, + ) + tools = [ { "type": "function",