can i use an llm for a one-off request?
# ask-ai
w
can i use an llm for a one-off request?
t
Thanks for reaching out, Mohammad. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to guide you through using an LLM for a one-off request in LiveKit! You can take advantage of the standalone LLM interface, which lets you interact directly with the LLM without needing a full agent pipeline—perfect for handling single queries smoothly. Here’s how you can do it: start by creating an LLM instance from your preferred provider plugin (like OpenAI or Anthropic), then use its
chat
method with a
ChatContext
that includes your message. The LLM will send back a stream of
ChatChunk
objects, which you can iterate over to get the response step-by-step. For example, if you’re using the OpenAI plugin, it would look like this:
Copy code
from livekit.agents import ChatContext
from livekit.plugins import openai

llm = openai.LLM(model="gpt-4o-mini")
chat_ctx = ChatContext()
chat_ctx.add_message(role="user", content="Hello, this is a test message!")

async with llm.chat(chat_ctx=chat_ctx) as stream:
    async for chunk in stream:
        print("Received chunk:", chunk.delta)
Just make sure you have the right plugin installed for your provider (for example,
pip install "livekit-agents[openai]~=1.2"
) and that your API keys are set up as environment variables. If you have any questions or want me to walk you through any part of this, please don’t hesitate to ask—I’m here to help! Sources: Large language model (LLM) integrations | LiveKit Docs | Docs::Agents | Groq LLM integration guide | LiveKit Docs | Baseten LLM integration guide | LiveKit Docs | tests/test_llm.py
w
how can i give the llm instructions?
t
I'm happy to guide you on how to give instructions to an LLM in LiveKit by specifying them as a system message. Here are the main ways you can do this: • During Initialization: You can include the
instructions
parameter when creating the LLM instance or agent. For example:
Copy code
from livekit.plugins import openai

llm = openai.LLM(model="gpt-4o-mini", instructions="You are a helpful assistant.")
• Dynamic Updates: If you need to adjust instructions during an active session, you can use the
update_instructions
method. This is great for adapting behavior based on the context:
Copy code
async def on_enter(self):
    self.update_instructions("You are a helpful agent speaking on the phone.")
    self.session.generate_reply()
• Manual Updates: For more advanced scenarios, like integrating external data, you can directly modify the system message in the chat context:
Copy code
chat_ctx.items.insert(0, llm.ChatMessage(role="system", content=["New instructions"]))
If anything isn’t clear or if you’d like a hand with the implementation, please don’t hesitate to ask. I’m here to help! Sources: web/src/components/instructions.tsx | basics/change_agent_instructions.py | examples/voice_agents/llamaindex-rag/retrieval.py | Adjusting Model Parameters | LiveKit Docs | Docs::Agents