Implement a simple in-memory cache for embeddings to reduce API costs.

Code Playground
JavaScript (V8)
Loading...
Console Output
// Output will appear here...
// For React: ReactDOM.createRoot(rootElement).render(<App />);
Recommended Answer

Implementation:

import OpenAI from 'openai';

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

const embeddingCache = new Map();

async function getCachedEmbedding(text) {
  if (embeddingCache.has(text)) {
    console.log("Cache hit");
    return embeddingCache.get(text);
  }
  
  console.log("Cache miss - fetching from API");
  const response = await openai.embeddings.create({
    model: "text-embedding-3-small",
    input: text
  });
  
  const embedding = response.data[0].embedding;
  embeddingCache.set(text, embedding);
  
  return embedding;
}

// Usage
// await getCachedEmbedding("Hello world");
// await getCachedEmbedding("Hello world"); // Hits cache