better-sandwich-65834
10/03/2025, 3:33 PMasync def _is_company_loan_question(
self, context: RunContext[SessionState], question: str
) -> bool:
"""Determine if a question is related to company information or loan topics using LLM.
Args:
context: The RunContext containing session/user data
question: The customer's question
Returns:
True if the question is about company/loan topics, False for off-topic questions
"""
# Use LLM to classify the question without generating customer-facing output
# Personalize system prompt using session state
company_name = context.userdata.company_name
customer_full_name = (
f"{context.userdata.customer_data.firstname} {context.userdata.customer_data.lastname}"
)
system_prompt = (
f"You are a polite and professional automated assistant for {company_name}. "
f"You are speaking with {customer_full_name}. "
f"Classify the following customer question: respond with only 'YES' if it is about {company_name}'s services, loans, company information, or anything you can help with as a representative of {company_name}. "
f"Respond with only 'NO' if it is about unrelated topics such as weather, sports, news, recipes, or anything outside {company_name}'s business."
)
classification_messages = [
llm.ChatMessage(role="system", content=[system_prompt]),
llm.ChatMessage(role="user", content=[f"Customer question: '{question}'"]),
]
# Create a minimal chat context for classification
classification_ctx = llm.ChatContext(items=classification_messages)
# Get classification from LLM (use self.session.llm)
llm_result = await self.session.llm.chat(chat_ctx=classification_ctx)
response_text = ""
async for chunk in llm_result:
if isinstance(chunk, llm.ChatChunk) and chunk.choices:
if chunk.choices[0].delta and chunk.choices[0].delta.content:
response_text += chunk.choices[0].delta.content
# Return True if response contains "YES", False otherwise
return "YES" in response_text.upper()
llm_result = await self.session.llm.chat(chat_ctx=classification_ctx)
^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'RealtimeModelBeta' object has no attribute 'chat'
how to handle this properlytall-airline-16756
10/03/2025, 3:33 PMtall-airline-16756
10/03/2025, 3:34 PMbetter-sandwich-65834
10/03/2025, 3:35 PMtall-airline-16756
10/03/2025, 3:36 PM@function_tool decorator. Each tool should have a clear name, description, argument schema, and a handler function to make things smooth.
• Register your tools either when initializing your agent or dynamically during a session using the update_tools() method—whichever fits your workflow best.
• Set up the tool_choice parameter to guide how the tool gets selected. You can choose automatic selection, require a specific tool, or specify one explicitly.
• If your tasks are a bit more complex, you might want to enable parallel_tool_calls so the model can call multiple tools at once.
Here’s a simple example to illustrate:
from livekit.agents import AgentContext, JobContext
from livekit.agents.llm import ChatContext
@function_tool(description="Get the current time, optionally for a specific timezone.")
async def get_time(ctx: JobContext, tool_use_id: str, zone: str | None = None) -> str:
return "The current time is 10:30 AM EST."
If you'd like to dive deeper, I highly recommend checking out the Tool definition and use guide. Also, the Python Agents Examples repository is full of helpful examples to inspire you.
Sources: livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | docs/index.yaml | External data and RAG | LiveKit Docs | Tool definition and use | LiveKit Docs | OpenAI LLM plugin guide | LiveKit Docs