average-architect-28889
07/25/2025, 11:10 AMtall-airline-16756
07/25/2025, 11:10 AMtall-airline-16756
07/25/2025, 11:11 AMfunction_tool with LLM for calling custom functions and managing chat context, follow these steps:
1. Define Custom Functions:
Use the @function_tool decorator to define your custom functions. For example:
from livekit.agents import function_tool
@function_tool
def get_current_time(timezone: str = "UTC") -> str:
"""Get the current time in the specified timezone"""
from datetime import datetime
import pytz
tz = pytz.timezone(timezone)
return datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S %Z")
2. Use Functions with LLM:
Pass your function tools to the LLM's chat method along with the chat context:
# Create chat context
chat_ctx = ChatContext()
chat_ctx.add_message(role="user", content="What time is it in New York?")
# Get response with function calling
async with llm.chat(chat_ctx=chat_ctx, tools=[get_current_time]) as stream:
async for chunk in stream:
# Handle function calls and response chunks
pass
3. Automatic Chat Context Management:
Function calls and their outputs are automatically added to the chat context. The system tracks:
• FunctionCall: Represents the LLM's request to call a function
• FunctionCallOutput: Contains the result of the function execution
• ChatMessage: Regular messages from users and assistants
4. Function Call Flow:
1. LLM receives chat context with function definitions
2. LLM decides to call a function and sends a function call request
3. Your application executes the function
4. Function result is sent back to the LLM
5. LLM continues with the function result to generate the final response
5. Adding Function Messages to Chat Context:
Function calls and outputs are automatically included in the chat context:
# Function calls and outputs are automatically added to chat_ctx.items
tool_messages = new_calls + new_fnc_outputs
if generate_tool_reply:
chat_ctx.items.extend(tool_messages)
By following these steps, you can effectively integrate custom functions with LLM and manage their interactions within the chat context.
Sources: LLM Integrations | livekit/livekit_composite | DeepWiki | livekit-agents/livekit/agents/llm/_provider_format/openai.py | complex-agents/personal_shopper/personal_shopper.pyaverage-architect-28889
07/25/2025, 11:14 AMtall-airline-16756
07/25/2025, 11:15 AMaverage-architect-28889
07/25/2025, 11:17 AMtall-airline-16756
07/25/2025, 11:18 AMmerge method in the ChatContext class, you've got some cool options to filter what gets included:
• `exclude_function_call`: Want to keep function calls out? Just set this to True.
• `exclude_instructions`: If you'd rather not merge system or developer messages, this parameter has got your back.
2. The created_at Field:
Think of created_at like a timestamp that remembers exactly when a message was born. It's set up like this:
created_at: float = Field(default_factory=time.time)
It uses time.time() to capture the precise moment of creation - pretty neat, right?
3. Merge Process and Chronological Ordering:
The system is super smart about keeping things in order. Check out how it makes sure messages stay in the right sequence:
if item.id not in existing_ids:
idx = self.find_insertion_index(created_at=item.created_at)
self._items.insert(idx, item)
This ensures your messages are always lined up just the way they should be, preserving their original timeline.
By understanding these little details, you'll be a pro at managing message merges and keeping track of when things actually happened. Hope this helps make things crystal clear!
Sources: livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real...