Skip to main content
Integrate Memvid with Google’s Agent Development Kit (ADK) to build Gemini-powered agents with persistent memory. The google-adk adapter provides native ADK function declarations.

Installation

npm install @memvid/sdk @google/generative-ai

Quick Start

import { use } from '@memvid/sdk';

// Open with Google ADK adapter
const mem = await use('google-adk', 'knowledge.mv2');

// Access ADK function declarations
const tools = mem.tools;       // FunctionDeclaration[] for Gemini API
const executors = mem.functions; // Function executors by name

Available Functions

The Google ADK adapter provides three function declarations:
FunctionDescription
memvid_putStore documents in memory with title, label, and text
memvid_findSearch for relevant documents by query
memvid_askAsk questions with RAG-style answer synthesis

Basic Usage with Gemini

import { use } from '@memvid/sdk';
import { GoogleGenerativeAI } from '@google/generative-ai';

// Initialize Memvid with Google ADK adapter
const mem = await use('google-adk', 'knowledge.mv2');

// Get function declarations and executors
const tools = mem.tools as any[];
const executors = mem.functions as Record<string, (args: any) => Promise<string>>;

// Create Gemini client
const geminiKey = process.env.GEMINI_API_KEY ?? process.env.GOOGLE_API_KEY;
if (!geminiKey) throw new Error("Set GEMINI_API_KEY (or legacy GOOGLE_API_KEY)");
const genAI = new GoogleGenerativeAI(geminiKey);

// Create model with Memvid tools
const model = genAI.getGenerativeModel({
  model: 'gemini-2.0-flash',
  tools: [{ functionDeclarations: tools }],
});

// Start a chat
const chat = model.startChat();
const result = await chat.sendMessage('Search for authentication information');

// Handle function calls
const response = result.response;
const parts = response.candidates?.[0]?.content?.parts || [];

for (const part of parts) {
  if (part.functionCall) {
    const { name, args } = part.functionCall;
    console.log(`Function call: ${name}`);

    // Execute the function
    if (executors[name]) {
      const funcResult = await executors[name](args as Record<string, unknown>);
      console.log(`Result: ${funcResult}`);

      // Send result back to model
      const followUp = await chat.sendMessage([{
        functionResponse: {
          name,
          response: { result: funcResult },
        },
      }]);
      console.log(`Model response: ${followUp.response.text()}`);
    }
  } else if (part.text) {
    console.log(`Response: ${part.text}`);
  }
}

Direct Tool Execution

Use the function executors directly without Gemini:
import { use } from '@memvid/sdk';

const mem = await use('google-adk', 'knowledge.mv2', { mode: 'create' });
const executors = mem.functions as Record<string, (args: any) => Promise<string>>;

// Store documents
const putResult = await executors.memvid_put({
  title: 'API Documentation',
  label: 'docs',
  text: 'Authentication uses JWT tokens with refresh capability.',
});
console.log(putResult);
// Output: Document stored with frame_id: 2

// Search documents
const findResult = await executors.memvid_find({
  query: 'authentication',
  top_k: 5,
});
console.log(findResult);
// Output: Found 1 results:
// 1. [API Documentation] (score: 2.34): Authentication uses JWT tokens...

// Ask questions
const askResult = await executors.memvid_ask({
  question: 'How does authentication work?',
  mode: 'auto',
});
console.log(askResult);
// Output: Answer: Authentication uses JWT tokens with refresh capability.
// Sources: API Documentation

Complete Agentic Example

import { use } from '@memvid/sdk';
import { GoogleGenerativeAI } from '@google/generative-ai';

async function runGeminiAgent() {
  // Initialize
  const mem = await use('google-adk', 'knowledge.mv2');
  const tools = mem.tools as any[];
  const executors = mem.functions as Record<string, (args: any) => Promise<string>>;

  // Store some knowledge first
  await executors.memvid_put({
    title: 'Gemini Overview',
    label: 'google-ai',
    text: 'Gemini is Google\\'s most capable AI model family.',
  });

  await executors.memvid_put({
    title: 'Agent Development Kit',
    label: 'frameworks',
    text: 'ADK is Google\\'s framework for building AI agents.',
  });

  // Create Gemini client
  const geminiKey = process.env.GEMINI_API_KEY ?? process.env.GOOGLE_API_KEY;
  if (!geminiKey) throw new Error("Set GEMINI_API_KEY (or legacy GOOGLE_API_KEY)");
  const genAI = new GoogleGenerativeAI(geminiKey);
  const model = genAI.getGenerativeModel({
    model: 'gemini-2.0-flash',
    tools: [{ functionDeclarations: tools }],
    systemInstruction: 'You are a helpful assistant with access to a knowledge base. ' +
      'Use memvid_find to search and memvid_ask to answer questions.',
  });

  // Run agentic loop
  const chat = model.startChat();

  async function processMessage(userMessage: string): Promise<string> {
    let result = await chat.sendMessage(userMessage);

    // Handle function calls iteratively
    while (true) {
      const parts = result.response.candidates?.[0]?.content?.parts || [];
      const functionCalls = parts.filter((p: any) => p.functionCall);

      if (functionCalls.length === 0) {
        // No more function calls, return text response
        return result.response.text() || 'No response';
      }

      // Execute all function calls
      const responses: any[] = [];
      for (const part of functionCalls) {
        const { name, args } = (part as any).functionCall;
        if (executors[name]) {
          const funcResult = await executors[name](args);
          responses.push({
            functionResponse: { name, response: { result: funcResult } },
          });
        }
      }

      // Send results back
      result = await chat.sendMessage(responses);
    }
  }

  // Example conversation
  const answer = await processMessage(
    'What is Google ADK and how does it relate to Gemini?'
  );
  console.log('Agent response:', answer);

  await mem.seal();
}

runGeminiAgent().catch(console.error);

Function Declaration Schema

The memvid_put function declaration:
{
  "name": "memvid_put",
  "description": "Store a document in Memvid memory for later retrieval.",
  "parameters": {
    "type": "object",
    "properties": {
      "title": { "type": "string", "description": "Title of the document" },
      "label": { "type": "string", "description": "Category or label" },
      "text": { "type": "string", "description": "Text content to store" },
      "metadata": { "type": "object", "description": "Optional key-value metadata" }
    },
    "required": ["title", "label", "text"]
  }
}
The memvid_find function declaration:
{
  "name": "memvid_find",
  "description": "Search Memvid memory for documents matching a query.",
  "parameters": {
    "type": "object",
    "properties": {
      "query": { "type": "string", "description": "Search query string" },
      "top_k": { "type": "number", "description": "Number of results (default: 5)" }
    },
    "required": ["query"]
  }
}
The memvid_ask function declaration:
{
  "name": "memvid_ask",
  "description": "Ask a question and get an answer from Memvid memory using RAG.",
  "parameters": {
    "type": "object",
    "properties": {
      "question": { "type": "string", "description": "Question to answer" },
      "mode": { "type": "string", "enum": ["auto", "lex", "sem"], "description": "Search mode" }
    },
    "required": ["question"]
  }
}

Best Practices

  1. Use read-only mode for retrieval agents
  2. Handle function calls appropriately in responses
  3. Use streaming for long responses
  4. Close the memory when done
const mem = await use('google-adk', 'knowledge.mv2', { readOnly: true });
try {
  // Create agent and run queries
  const geminiKey = process.env.GEMINI_API_KEY ?? process.env.GOOGLE_API_KEY;
  if (!geminiKey) throw new Error("Set GEMINI_API_KEY (or legacy GOOGLE_API_KEY)");
  const genAI = new GoogleGenerativeAI(geminiKey);
  // ... use client
} finally {
  // No explicit close needed; dropping the handle releases the shared lock.
}

Next Steps