📢 Announcing our research paper: Zentry achieves 26% higher accuracy than OpenAI Memory, 91% lower latency, and 90% token savings! Read the paper to learn how we're revolutionizing AI agent memory.

Integrate Zentry’s memory capabilities with OpenAI’s Inbuilt Tools to create AI agents with persistent memory.

Getting Started

Installation

npm install Zentryai openai zod

Environment Setup

Save your Zentry and OpenAI API keys in a .env file:

Zentry_API_KEY=your_Zentry_api_key
OPENAI_API_KEY=your_openai_api_key

Get your Zentry API key from the Zentry Dashboard.

Configuration

const ZentryConfig = {
    apiKey: process.env.Zentry_API_KEY,
    user_id: "sample-user",
};

const openAIClient = new OpenAI();
const ZentryClient = new MemoryClient(ZentryConfig);

Adding Memories

Store user preferences, past interactions, or any relevant information:

async function addUserPreferences() {
    const ZentryClient = new MemoryClient(ZentryConfig);
    
    const userPreferences = "I Love BMW, Audi and Porsche. I Hate Mercedes. I love Red cars and Maroon cars. I have a budget of 120K to 150K USD. I like Audi the most.";
    
    await ZentryClient.add([{
        role: "user",
        content: userPreferences,
    }], ZentryConfig);
}

await addUserPreferences();

Retrieving Memories

Search for relevant memories based on the current user input:

const relevantMemories = await ZentryClient.search(userInput, ZentryConfig);

Structured Responses with Zod

Define structured response schemas to get consistent output formats:

// Define the schema for a car recommendation
const CarSchema = z.object({
  car_name: z.string(),
  car_price: z.string(),
  car_url: z.string(),
  car_image: z.string(),
  car_description: z.string(),
});

// Schema for a list of car recommendations
const Cars = z.object({
  cars: z.array(CarSchema),
});

// Create a function tool based on the schema
const carRecommendationTool = zodResponsesFunction({ 
    name: "carRecommendations", 
    parameters: Cars 
});

// Use the tool in your OpenAI request
const response = await openAIClient.responses.create({
    model: "gpt-4o",
    tools: [{ type: "web_search_preview" }, carRecommendationTool],
    input: `${getMemoryString(relevantMemories)}\n${userInput}`,
});

Combine memory with web search for up-to-date recommendations:

const response = await openAIClient.responses.create({
    model: "gpt-4o",
    tools: [{ type: "web_search_preview" }, carRecommendationTool],
    input: `${getMemoryString(relevantMemories)}\n${userInput}`,
});

Examples

Complete Car Recommendation System

import MemoryClient from "Zentryai";
import { OpenAI } from "openai";
import { zodResponsesFunction } from "openai/helpers/zod";
import { z } from "zod";
import dotenv from 'dotenv';

dotenv.config();

const ZentryConfig = {
    apiKey: process.env.Zentry_API_KEY,
    user_id: "sample-user",
};

async function run() {
    // Responses without memories
    console.log("\n\nRESPONSES WITHOUT MEMORIES\n\n");
    await main();

    // Adding sample memories
    await addSampleMemories();

    // Responses with memories
    console.log("\n\nRESPONSES WITH MEMORIES\n\n");
    await main(true);
}

// OpenAI Response Schema
const CarSchema = z.object({
  car_name: z.string(),
  car_price: z.string(),
  car_url: z.string(),
  car_image: z.string(),
  car_description: z.string(),
});

const Cars = z.object({
  cars: z.array(CarSchema),
});

async function main(memory = false) {
  const openAIClient = new OpenAI();
  const ZentryClient = new MemoryClient(ZentryConfig);

  const input = "Suggest me some cars that I can buy today.";

  const tool = zodResponsesFunction({ name: "carRecommendations", parameters: Cars });

  // Store the user input as a memory
  await ZentryClient.add([{
    role: "user",
    content: input,
  }], ZentryConfig);

  // Search for relevant memories
  let relevantMemories = []
  if (memory) {
    relevantMemories = await ZentryClient.search(input, ZentryConfig);
  }

  const response = await openAIClient.responses.create({
    model: "gpt-4o",
    tools: [{ type: "web_search_preview" }, tool],
    input: `${getMemoryString(relevantMemories)}\n${input}`,
  });

  console.log(response.output);
}

async function addSampleMemories() {
  const ZentryClient = new MemoryClient(ZentryConfig);

  const myInterests = "I Love BMW, Audi and Porsche. I Hate Mercedes. I love Red cars and Maroon cars. I have a budget of 120K to 150K USD. I like Audi the most.";
  
  await ZentryClient.add([{
    role: "user",
    content: myInterests,
  }], ZentryConfig);
}

const getMemoryString = (memories) => {
    const MEMORY_STRING_PREFIX = "These are the memories I have stored. Give more weightage to the question by users and try to answer that first. You have to modify your answer based on the memories I have provided. If the memories are irrelevant you can ignore them. Also don't reply to this section of the prompt, or the memories, they are only for your reference. The MEMORIES of the USER are: \n\n";
    const memoryString = memories.map((mem) => `${mem.memory}`).join("\n") ?? "";
    return memoryString.length > 0 ? `${MEMORY_STRING_PREFIX}${memoryString}` : "";
};

run().catch(console.error);

Responses

{
  "cars": [
    {
      "car_name": "Toyota Camry",
      "car_price": "$25,000",
      "car_url": "https://www.toyota.com/camry/",
      "car_image": "https://link-to-toyota-camry-image.com",
      "car_description": "Reliable mid-size sedan with great fuel efficiency."
    },
    {
      "car_name": "Honda Accord",
      "car_price": "$26,000",
      "car_url": "https://www.honda.com/accord/",
      "car_image": "https://link-to-honda-accord-image.com",
      "car_description": "Comfortable and spacious with advanced safety features."
    },
    {
      "car_name": "Ford Mustang",
      "car_price": "$28,000",
      "car_url": "https://www.ford.com/mustang/",
      "car_image": "https://link-to-ford-mustang-image.com",
      "car_description": "Iconic sports car with powerful engine options."
    },
    {
      "car_name": "Tesla Model 3",
      "car_price": "$38,000",
      "car_url": "https://www.tesla.com/model3",
      "car_image": "https://link-to-tesla-model3-image.com",
      "car_description": "Electric vehicle with advanced technology and long range."
    },
    {
      "car_name": "Chevrolet Equinox",
      "car_price": "$24,000",
      "car_url": "https://www.chevrolet.com/equinox/",
      "car_image": "https://link-to-chevron-equinox-image.com",
      "car_description": "Compact SUV with a spacious interior and user-friendly technology."
    }
  ]
}

Resources