Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions livekit-agents/livekit/agents/llm/async_toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import asyncio
import copy
from collections.abc import Callable
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Literal, get_origin, get_type_hints

Expand Down Expand Up @@ -177,6 +178,7 @@ def __init__(
id: str,
tools: list[Tool] | None = None,
on_duplicate_call: DuplicateMode = "confirm",
reply_instructions: str | Callable[[list[ChatItem]], str] = REPLY_INSTRUCTIONS,
) -> None:
super().__init__(id=id, tools=tools)

Expand All @@ -193,6 +195,9 @@ def __init__(
self._pending_updates: list[_PendingUpdate] = []
self._reply_task: asyncio.Task[None] | None = None

# instructions to use when delivering a reply from tool updates
self._reply_instructions = reply_instructions

async def cancel(self, call_id: str) -> bool:
task = self._running_tasks.get(call_id)
if task is not None:
Expand Down Expand Up @@ -348,11 +353,17 @@ async def _deliver_reply(self, session: AgentSession) -> None:
# TODO: use a LLM to verify if another reply is needed?
return

pending_call_ids = [
item.call_id for item in pending_items if item.type == "function_call_output"
]
# format the reply instructions from the pending chat items
if callable(self._reply_instructions):
instructions = self._reply_instructions(pending_items)
else:
pending_call_ids = [
item.call_id for item in pending_items if item.type == "function_call_output"
]
instructions = self._reply_instructions.format(pending_call_ids=pending_call_ids)

session.generate_reply(
instructions=REPLY_INSTRUCTIONS.format(pending_call_ids=pending_call_ids),
instructions=instructions,
tool_choice="none",
)

Expand Down
Loading