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.
This commit is contained in:
parent
0acc1456f9
commit
e6bc23e564
1 changed files with 19 additions and 0 deletions
|
@ -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",
|
||||
|
|
Loading…
Reference in a new issue