calm-eye-57311
10/02/2025, 10:27 AManalysis_llm = openai.LLM(
model="gpt-4.1-mini"
)tall-airline-16756
10/02/2025, 10:27 AMtall-airline-16756
10/02/2025, 10:28 AMLLM class from the LiveKit Agents framework. You'll want to specify the model, API key, and secret. Here's an example to get you going:
from livekit.agents.inference.llm import LLM
llm_instance = LLM(
model="gpt-4.1",
api_key="your_api_key",
api_secret="your_api_secret"
)
1. Prepare the Chat Context: Next, use ChatContext to build your conversation history or prompt. This helps the model understand the context of the chat:
from livekit.agents.llm import ChatContext
chat_ctx = ChatContext().append(text="Hello, how can I help you?", role="user")
1. Make the Request: Now, call the chat() method on your LLM instance, passing in the ChatContext you prepared:
stream = llm_instance.chat(chat_ctx=chat_ctx)
You can then iterate over the response stream asynchronously to process the output as it comes in, like this:
async for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content)
If you have any questions or need a hand with any of these steps, please don’t hesitate to ask. I’m here to help!
Sources: tests/test_llm.py | Baseten LLM integration guide | LiveKit Docs | Azure OpenAI LLM integration guide | LiveKit Docs | Amazon Bedrock LLM integration guide | LiveKit Docs | Ollama integration guide | LiveKit Docscalm-eye-57311
10/02/2025, 10:30 AMtall-airline-16756
10/02/2025, 10:31 AMLLM.chat() method and await the full response instead of processing it bit by bit.
2. If needed, override the llm_node method to return a complete string rather than a stream.
3. All the LLM providers we support (like OpenAI and Google Gemini) offer both streaming and non-streaming options, so you can tailor it to what works best for you.
If you’d like, I’m happy to share more details or some examples to help you get started!
Sources: Docs::Agents | JavaScript Agents & LLM Integration | livekit/livekit_composite | DeepWiki | livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/llm.py | chain-of-thought-tts/README.mdoc | Large language model (LLM) integrations | LiveKit Docscalm-eye-57311
10/02/2025, 10:54 AMtall-airline-16756
10/02/2025, 10:54 AMresponse_format parameter when calling the LLM. Let me walk you through the steps:
1. Define a Schema: You can use TypedDict or Pydantic to set up the expected JSON structure. For example:
from typing_extensions import TypedDict
class ResponseSchema(TypedDict):
action: str
response: str
1. Pass the Schema: When you call llm.chat(), just include the schema in the response_format parameter like this:
async with llm.chat(
chat_ctx=chat_ctx,
response_format=ResponseSchema
) as stream:
async for chunk in stream:
print(chunk)
1. Validate JSON: If you’re working with models that don’t have native JSON support, a good approach is to combine strict prompting with some post-processing to make sure the JSON output is valid.
If you’d like, I’m here to provide more examples or help you step-by-step—just let me know!
Sources: Tool definition and use | LiveKit Docs | examples/voice_agents/structured_output.py | Docs::Agents | tools/server/server.cpp | meditation_assistant/assistant_functions.py