HOw do I create a personalized LLM agent class?
# ask-ai
b
HOw do I create a personalized LLM agent class?
t
Thanks for reaching out, aile. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To create a personalized LLM agent class in LiveKit, you can extend the
Agent
class and customize it with your specific instructions, LLM configuration, and personalization logic. Below are the steps to achieve this: Basic Agent Class Structure
Copy code
from livekit.agents import Agent
from livekit.plugins import openai, deepgram, silero

class PersonalizedAssistant(Agent):
    def __init__(self):
        super().__init__(
            instructions="You are a helpful voice AI assistant.",
            stt=deepgram.STT(),
            llm=openai.LLM(model="gpt-4o-mini"),
            tts=openai.TTS(),
            vad=silero.VAD.load()
        )

    async def on_enter(self) -> None:
        await self.session.generate_reply(instructions="Greet the user and ask how you can help them.")
Advanced Personalized Agent with User Data You can create more sophisticated personalized agents that store and use user information:
Copy code
from livekit.agents import Agent, function_tool
from livekit.agents.voice import RunContext
from dataclasses import dataclass
from typing import Optional

@dataclass
class UserData:
    first_name: Optional[str] = None
    last_name: Optional[str] = None
    customer_id: Optional[str] = None
    preferences: dict = None

    def is_identified(self) -> bool:
        return self.first_name is not None and self.last_name is not None

class PersonalizedAgent(Agent):
    def __init__(self):
        super().__init__(
            instructions="You are a personalized AI assistant.",
            stt=deepgram.STT(),
            llm=openai.LLM(model="gpt-4o-mini"),
            tts=openai.TTS(),
            vad=silero.VAD.load()
        )

    async def on_enter(self) -> None:
        userdata: UserData = self.session.userdata
        
        # Create personalized instructions based on user data
        custom_instructions = self.instructions
        if userdata.is_identified():
            custom_instructions += f"\n\nYou are speaking with {userdata.first_name} {userdata.last_name}."

        # Update chat context with personalized information
        chat_ctx = self.chat_ctx.copy()
        chat_ctx.add_message(
            role="system",
            content=f"Current user: {userdata.first_name or 'Unknown'}"
        )
        await self.update_chat_ctx(chat_ctx)
        
        await self.session.generate_reply()

    @function_tool
    async def identify_user(self, first_name: str, last_name: str):
        """Identify the user by their name."""
        userdata: UserData = self.session.userdata
        userdata.first_name = first_name
        userdata.last_name = last_name
        
        return f"Thank you, {first_name}. I've identified you in the system."
Key Features for Personalization • Dynamic Instructions: Modify instructions based on user data • Function Tools: Add custom functions for user interaction • Context Management: Maintain conversation history and user preferences • State Management: Store and retrieve user information across sessions • Custom Responses: Generate personalized responses based on user data This approach allows you to create highly personalized AI agents that can remember user information, adapt their behavior, and provide customized experiences within the LiveKit framework. Sources: Workflows | LiveKit Docs | pipeline-llm/cerebras_llm.py | livekit-plugins/livekit-plugins-anthropic/livekit/plugins/anthropic/llm.py