From bd36bc989d819f5da003a0a34fc9f570e100583c Mon Sep 17 00:00:00 2001 From: martin-purplefish Date: Sat, 28 Dec 2024 22:27:13 -0500 Subject: [PATCH] Do not pass function context if at max depth (#1306) --- .changeset/khaki-candles-rest.md | 5 +++++ .../livekit/agents/pipeline/pipeline_agent.py | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 .changeset/khaki-candles-rest.md diff --git a/.changeset/khaki-candles-rest.md b/.changeset/khaki-candles-rest.md new file mode 100644 index 000000000..91afec21c --- /dev/null +++ b/.changeset/khaki-candles-rest.md @@ -0,0 +1,5 @@ +--- +"livekit-agents": patch +--- + +Do not pass function context if at max depth diff --git a/livekit-agents/livekit/agents/pipeline/pipeline_agent.py b/livekit-agents/livekit/agents/pipeline/pipeline_agent.py index 2c7dc1363..872bd5d4b 100644 --- a/livekit-agents/livekit/agents/pipeline/pipeline_agent.py +++ b/livekit-agents/livekit/agents/pipeline/pipeline_agent.py @@ -960,7 +960,24 @@ async def _execute_function_calls() -> None: chat_ctx = call_ctx.chat_ctx.copy() chat_ctx.messages.extend(extra_tools_messages) chat_ctx.messages.extend(call_ctx.extra_chat_messages) - answer_llm_stream = self._llm.chat(chat_ctx=chat_ctx, fnc_ctx=self.fnc_ctx) + fnc_ctx = self.fnc_ctx + if ( + fnc_ctx + and new_speech_handle.fnc_nested_depth + >= self._opts.max_nested_fnc_calls + ): + logger.warning( + "max function calls nested depth reached, not propagating fnc ctx", + extra={ + "speech_id": speech_handle.id, + "fnc_nested_depth": speech_handle.fnc_nested_depth, + }, + ) + fnc_ctx = None + answer_llm_stream = self._llm.chat( + chat_ctx=chat_ctx, + fnc_ctx=fnc_ctx, + ) synthesis_handle = self._synthesize_agent_speech( new_speech_handle.id, answer_llm_stream