Custom Knowledge Base - Pinecone Database

Hi, anyone explain process this code. In: https://docs.vapi.ai/knowledge-base/migrating-from-trieve#step-3-create-custom-knowledge-base-in-vapi
Step 2: Set Up Your Custom Server

How to link:
Agent -> knowledge base file -> pinecode database.
How i can make sure, the agent search the correct document in pinecone index.
is this part used as ID: pinecone.Index('your-knowledge-base');

app.post('/kb/search', async (req, res) => {
  try {
    const { message } = req.body;
    const userMessages = message.messages.filter(msg => msg.role === 'user');
    const latestQuery = userMessages[userMessages.length - 1]?.content || '';
    // Search your vector database
    const index = pinecone.Index('your-knowledge-base');
    const searchResults = await index.query({
      vector: await getEmbedding(latestQuery),
      topK: 5,
      includeMetadata: true
    });
    // Format response
    const documents = searchResults.matches.map(match => ({
      content: match.metadata.content,
      similarity: match.score,
      uuid: match.id
    }));
    res.json({ documents });
  } catch (error) {
    res.status(500).json({ error: 'Search failed' });
  }

@ivy
Essential migration guide for Trieve users - service ending November 1st, 2025
Was this page helpful?