elegant-battery-64445
09/15/2025, 10:33 PMtall-airline-16756
09/15/2025, 10:33 PMtall-airline-16756
09/15/2025, 10:34 PMonUserTurnCompleted lifecycle hook to retrieve relevant context and inject it into the chat context before the LLM generates a response. Here's a basic example:
import { llm } from '@livekit/agents';
class MyRagAgent extends voice.Agent {
async onUserTurnCompleted(
turnCtx: llm.ChatContext,
newMessage: llm.ChatMessage,
): Promise<void> {
// Step 1: Retrieve relevant context using the user's message
const ragContent = await myRagLookup(newMessage.textContent);
// Step 2: Inject retrieved context into chat context as a system/assistant message
turnCtx.addMessage({
role: 'assistant',
content: `Relevant context: ${ragContent}`,
});
// Optional: Persist this context for future turns
// await this.updateChatCtx(turnCtx);
}
}
async function myRagLookup(query: string): Promise<string> {
const response = await fetch(<RAG_SERVICE_URL>, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query }),
});
const data = await response.json();
return data.content;
}
In this example, myRagLookup() is a placeholder function where you can implement retrieval logic using a vector database or external API. Just replace <RAG_SERVICE_URL> with the URL of your retrieval service.
If you'd like, I’m here to help guide you through setting up the retrieval service or connecting to a specific vector database. Just let me know how I can assist!
Sources: Pipeline nodes and hooks | LiveKit Docs | External data and RAG | LiveKit Docs | examples/voice_agents/llamaindex-rag/chat_engine.py | rag/rag_handler.py | Docs::Agentselegant-battery-64445
09/15/2025, 10:34 PMtall-airline-16756
09/15/2025, 10:35 PMimport { llm } from '@livekit/agents';
class MyRagAgent extends voice.Agent {
async onUserTurnCompleted(
turnCtx: llm.ChatContext,
newMessage: llm.ChatMessage,
): Promise<void> {
// Step 1: Retrieve relevant context using the user's message
const ragContent = await myRagLookup(newMessage.textContent);
// Step 2: Inject retrieved context into chat context as a system/assistant message
turnCtx.addMessage({
role: 'assistant',
content: `Relevant context: ${ragContent}`,
});
// Optional: Persist this context for future turns
// await this.updateChatCtx(turnCtx);
}
}
async function myRagLookup(query: string): Promise<string> {
// Replace <RAG_SERVICE_URL> with your retrieval service endpoint
const response = await fetch(<RAG_SERVICE_URL>, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query }),
});
const data = await response.json();
return data.content;
}
// Initialize and run the agent
const agent = new MyRagAgent();
agent.start();
Here’s a quick overview to help you understand this template:
• myRagLookup() acts as a placeholder for your retrieval logic—it might connect to a vector database or an external API depending on your setup.
• Don’t forget to replace <RAG_SERVICE_URL> with the actual URL of your retrieval service.
If you’d like, I’m here to help you dive deeper into any specific part, like connecting to a vector database or generating embeddings. Just let me know how I can assist you further!
Sources: External data and RAG | LiveKit Docs | rag/rag_handler.py | Docs::Agents | examples/voice_agents/llamaindex-rag/query_engine.py