```async def _is_company_loan_question( self, ...
# ask-ai
b
Copy code
async 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 properly
t
Thanks for reaching out, Aleh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The error seems to indicate that the 'chat' method might not be available in the 'RealtimeModelBeta' class. Could you please check if this method is something you intended to use? Also, are you working with the 'RealtimeModelBeta' class using the documented methods like 'session()' or 'generate_reply()'? If you could share a bit more about your setup and what you're aiming to achieve, I'd be happy to help you sort this out! Sources: livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | Using the Testing Framework with Realtime Models
b
i want to decide witch tool to call based on user input
t
I'm glad you're exploring how to decide which tool to call based on user input in LiveKit! You can define and register tools as functions within your agent, and the LLM will thoughtfully evaluate the user's request to pick the most suitable tool based on the conversation context. Here’s a friendly guide to get you started: • Define your tools using the
@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:
Copy code
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