Skip to content

Commit

Permalink
feat: limit tool call depth in llm agent (#977)
Browse files Browse the repository at this point in the history
  • Loading branch information
elisalimli committed May 1, 2024
1 parent 3ae1001 commit f5efbbf
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions libs/superagent/app/agents/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,14 @@ def __init__(
LLMProvider.COHERE_CHAT,
]

intermediate_steps = []
@property
def max_tool_call_depth(self):
return 10

async def _execute_tools(
self,
tool_calls: list[ChatCompletionMessageToolCall | ChatCompletionDeltaToolCall],
depth: int,
**kwargs,
):
messages: list = kwargs.get("messages")
Expand Down Expand Up @@ -213,7 +216,7 @@ async def _execute_tools(

self.messages = messages
kwargs["messages"] = self.messages
return await self._acompletion(**kwargs)
return await self._acompletion(depth=depth + 1, **kwargs)

def _cleanup_output(self, output: str) -> str:
# anthropic returns a XML formatted response
Expand Down Expand Up @@ -295,7 +298,7 @@ async def _process_model_response(self, res: ModelResponse):

return (tool_calls, new_messages, new_message.content)

async def _acompletion(self, **kwargs) -> Any:
async def _acompletion(self, depth: int = 0, **kwargs) -> Any:
logger.info(f"Calling LLM with kwargs: {kwargs}")

if kwargs.get("stream"):
Expand All @@ -321,7 +324,16 @@ async def _acompletion(self, **kwargs) -> Any:
self.messages = new_messages

if tool_calls:
return await self._execute_tools(tool_calls, **kwargs)
if depth < self.max_tool_call_depth:
return await self._execute_tools(tool_calls, depth=depth, **kwargs)
else:
logger.error(
f"Exceeded max tool call depth of {self.max_tool_call_depth}"
)
if not output:
output = (
f"Exceeded max tool call depth of {self.max_tool_call_depth}"
)

output = self._cleanup_output(output)

Expand Down

0 comments on commit f5efbbf

Please sign in to comment.