How can I just get the LLM response?
# ask-ai
c
How can I just get the LLM response?
t
Thanks for reaching out, dexter. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks for reaching out! Could you please help me understand a bit more about what you mean by "LLM response"? Are you looking to integrate a large language model (LLM) with LiveKit, or is it something related to a particular feature or API? The more details you can share, the better I can assist you. I'm here to help!
c
I would like to pre-generate the llm response so that i can store it temporarily, if there's no interruptions in the agent message i plan to use that response in the agent.say
t
I'd love to help you out! Could you share a bit more about your setup? Are you using LiveKit's APIs to connect with an LLM, or is this something custom you've put together? Knowing more about your workflow will really help me guide you in the best way possible.
c
My setup is simple it uses the normal AgentSession from livekit agents.
t
To help you pre-generate and temporarily store LLM responses in a LiveKit
AgentSession
for use with
agent.say
, here are a couple of friendly approaches you can try: • Enable Preemptive Generation: This feature lets the agent start generating a response as soon as a final transcript is ready, which really helps reduce any waiting time. It’s available in Python and works smoothly within the STT-LLM-TTS pipeline when turn detection is on. To activate it, just set
preemptive_generation=True
when you initialize your
AgentSession
like this: `session = AgentSession( stt=deepgram.STT(), llm=openai.LLM(), tts=openai.TTS(), vad=silero.VAD.load(), preemptive_generation=True )`One thing to keep in mind: preemptive responses might get canceled and regenerated if the chat context changes during the
on_user_turn_completed
event. • Manually Generate Responses with a Fast LLM: Another great option is to use a lightweight, speedy LLM to quickly generate a placeholder response (like "Wait a moment") while the main LLM is working on the full reply. You can play this using
session.say()
without blocking the main process. `async def _fast_llm_reply() -> AsyncIterable[str]: filler_response: str = "" async for chunk in self._fast_llm.chat(chat_ctx=fast_llm_ctx).to_str_iterable(): filler_response += chunk yield chunk # Play the preemptive response self.session.say(_fast_llm_reply(), add_to_chat_ctx=False)`This way, both the filler and main response generation can happen concurrently without slowing things down. Just a few more helpful notes: • Both
session.say()
and
session.generate_reply()
return a
SpeechHandle
object, so you can easily keep track of and manage your speech output. • Preemptive generation isn’t supported in Node.js at the moment. • If you have pre-synthesized audio ready, you can pass it directly to
session.say(audio=...)
to skip TTS and make things even faster. If you have any questions or want to walk through these methods together, just let me know—I’m here to help! Sources: Docs::Agents | Agent speech and audio | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_session.py | Agent speech and audio | LiveKit Docs
c
What im trying to solve for is this, I have a special marker to determine when a question is done, it waits for the agent to finish speaking before moving onto next question and generating a new response. I was wondering if there was a way to pre-generate the next question llm response early so that by the time the marker is detected, if it wasn't interrupted we can just use agent.say
t
To help you pre-generate the next LLM response in a LiveKit
AgentSession
and use it with
agent.say
after detecting a marker, here’s a friendly step-by-step guide you can follow: • Enable Preemptive Generation: This handy feature lets the agent start creating a response as soon as a final transcript is ready. You can turn it on by setting
preemptive_generation=True
when you initialize the `AgentSession`:
Copy code
session = AgentSession(
    preemptive_generation=True,
    stt=...,  # e.g., deepgram.STT()
    llm=...,  # e.g., openai.LLM()
    tts=...,  # e.g., openai.TTS()
    vad=...
)
• Manually Pre-Generate Responses: You can also use
session.generate_reply()
to create a response ahead of time based on user input or your own instructions. Just save the resulting
SpeechHandle
for when you’re ready to use it:
Copy code
handle = session.generate_reply(
    instructions="Respond concisely to the user’s last input"
)
• Detect Markers: This part involves setting up your own logic to spot markers, like certain keywords in the transcript or events such as
on_user_turn_completed
. For example:
Copy code
async def on_user_turn_completed(self, turn_ctx, new_message):
    if "marker_keyword" in new_message.content:
        await self.session.say("Response triggered by marker.")
• Play Pre-Generated Response: When your marker is detected, you can deliver the pre-generated response smoothly using `session.say()`:
Copy code
await session.say(
    "Generated response here",
    allow_interruptions=False,
    add_to_chat_ctx=True
)
If anything feels unclear or you'd like a hand with implementing this workflow, please don’t hesitate to ask—I’m here to help! Sources: Agent speech and audio | LiveKit Docs | Docs::Agents | examples/voice_agents/basic_agent.py