i want an example of RAG based question answer age...
# ask-ai
e
i want an example of RAG based question answer agent in node where can i find it
t
Thanks for reaching out, abhinav. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To create a RAG-based question-answer agent in Node.js, you can use LiveKit's
onUserTurnCompleted
lifecycle hook to retrieve relevant context and inject it into the chat context before the LLM generates a response. Here's a basic example:
Copy code
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::Agents
e
give me the full code
t
Thank you for reaching out! While there isn’t a complete, runnable Node.js example for a RAG-based question-answer agent available in the sources, I’m happy to share a conceptual template that follows the patterns described:
Copy code
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> {
  // 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