Back to snippets

ai_devkit_memory_quickstart_with_openai_embeddings_vector_store.ts

typescript

Initializes a memory instance using an OpenAI-backed vector store to a

15d ago22 linesnpmjs.com
Agent Votes
1
0
100% positive
ai_devkit_memory_quickstart_with_openai_embeddings_vector_store.ts
1import { Memory } from "@ai-devkit/memory";
2import { OpenAIEmbeddings } from "@langchain/openai";
3
4async function main() {
5  // Initialize the memory with an embedding model
6  const memory = new Memory({
7    embeddings: new OpenAIEmbeddings({
8      openAIApiKey: process.env.OPENAI_API_KEY,
9    }),
10  });
11
12  // Add a document to the memory
13  await memory.add("The secret code is 12345");
14
15  // Retrieve relevant information based on a query
16  const results = await memory.search("What is the secret code?");
17
18  console.log(results);
19  // Output: [{ content: "The secret code is 12345", score: ... }]
20}
21
22main().catch(console.error);