Guide: Create an AI Memory
A step-by-step guide to create an AI memory using BaseAI.
In this guide, we will learn how to create an AI memory using BaseAI and sync it with a Git repository. We will:
- Initialize BaseAI: Setup BaseAI in your Next.js app.
- Create an AI Memory: Generate an AI memory to track changes in your Git repository.
- Add doc metadata: Add metadata to each document in the memory.
- Add Langbase API Key: Add your Langbase API key to the
.env
file. - Deploy the Memory: Deploy the AI memory to Langbase.
BaseAI makes it easy to build an AI memory within a code repo and deploy it to Langbase serverless AI cloud.
Prerequisites: Generate Langbase API Key
We will use BaseAI and Langbase SDK in this guide. To work with both, you need to generate an API key. Visit User/Org API key documentation page to learn more.
Why use BaseAI?
It's useful if your company docs are present inside a Git repository. An AI memory created via BaseAI can track changes in your repository. This is useful:
- When you add a new doc to repository, you can deploy and only add the new doc to Langbase.
- When you update a doc in repository, you can sync the changes to Langbase.
- When you delete a doc in repository, you can deploy and delete the doc from Langbase.
Let's now create an AI memory using BaseAI.
Step #1Initialize BaseAI
Run the following command to initialize BaseAI in your Next.js app:
npx baseai@latest init
Step #2Create an AI Memory
Run the following command to create an AI memory:
npx baseai@latest memory
It will also prompt you if you want to create a memory from the current project git repository. Select yes. This will create a memory at baseai/memory/company-docs.ts
and track files in the current git repository. It prints the path of the memory created.
Open that file in your editor, it looks like this:
baseai/memory/company-docs.ts
import {MemoryI} from '@baseai/core';
const docsMemory = (): MemoryI => ({
name: 'company-docs',
description: "All docs as memory for an AI agent pipe",
git: {
enabled: true,
include: ['**/*'],
gitignore: true,
deployedAt: '',
embeddedAt: ''
}
});
export default docsMemory;
Below is the explanation of the fields in the memory file:
- enabled: Set to true to enable tracking of git repository.
- include: Follows glob pattern to include files from the git repository. You can change the pattern to include only specific files or directories where docs are present.
- gitignore: Set to true to include .gitignore file in the memory.
- deployedAt: Set to the commit hash where the memory was last deployed. It is used to track the changes in the memory for deployment. Try to avoid changing this field manually.
- embeddedAt: Set to the commit hash where the memory was last embedded. It is used to track the changes in the memory for local development. Try to avoid changing this field manually.
Step #3Add doc metadata
Go ahead and add metadata to each document, like its hosting URL. The AI agent will then include the URL in its response when referencing the document.
Let's edit the memory file to add a documents
field. Set meta
to a function that returns an object with string key/value pairs, like a url
for each document.
baseai/memory/company-docs.ts
import {MemoryI} from '@baseai/core';
const docsMemory = (): MemoryI => ({
name: 'company-docs',
description: 'All docs as memory for an AI agent pipe',
git: {
enabled: true,
include: ['**/*'],
gitignore: true,
deployedAt: '',
embeddedAt: '',
},
documents: {
meta: doc => {
// generate a URL for each document
const url = `https://example.com/${doc.path}`;
return {
url,
name: doc.name,
};
},
},
});
export default docsMemory;
The doc
is of type MemoryDocumentI
.
MemoryDocumentI Object
interface MemoryDocumentI {
name: string;
size: string;
content: string;
blob: Blob;
path: string;
}
Following are the properties of the MemoryDocumentI
object:
- name: name of the document.
- size: size of the document.
- content: content of the document.
- blob: blob of the document.
- path: path of the document.
If your docs have path based URL structure, you can use the path
field to generate the URL.
Step #4Commit changes
Once you are done, commit all the changes in the repository. This is important because the BaseAI will use the latest commit hash as a reference to track changes in the memory.
git add .
git commit -m "Setup AI BaseAI memory"
Step #5Add Langbase API Key
Add your Langbase API key to the .env
file:
LANGBASE_API_KEY=YOUR_API_KEY
Step #6Deploy the Memory
Run the following command to deploy the memory to serverless cloud:
npx baseai@latest deploy -m <MEMORY-NAME>
Go ahead and replace <MEMORY-NAME>
with the name of the memory. In this case, it is company-docs
. This will deploy the AI memory and all its documents to Langbase.
Next time you make changes to the git repository, you can run the deploy command again to update the memory. Make sure to commit all the changes before deploying.
Next steps
Now that you have created an AI memory, you can proceed to create an AI agent pipe using Langbase.