Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions examples/nodejs/memory/memory.text.add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {Langbase} from 'langbase';

/**
* 1. Run memory.create.ts file to create a memory first.
* 2. Add your API key to the environment variables or replace it in the code.
*/

const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY!,
});

async function main() {
// Basic text addition
const basicResult = await langbase.memories.add({
memoryName: 'my-knowledge-base',
text: 'This is important information about machine learning fundamentals. It covers supervised learning, unsupervised learning, and reinforcement learning concepts.',
});

console.log('✅ Basic text added:', basicResult);

// Text addition with custom name and metadata
const detailedResult = await langbase.memories.add({
memoryName: 'my-knowledge-base',
text: 'Deep learning is a subset of machine learning that uses artificial neural networks with multiple layers to model and understand complex patterns in data. It has revolutionized fields like computer vision, natural language processing, and speech recognition.',
documentName: 'deep-learning-intro',
metadata: {
category: 'machine-learning',
topic: 'deep-learning',
difficulty: 'intermediate',
source: 'manual-entry',
},
});

console.log('✅ Detailed text added:', detailedResult);

// Multiple text entries
const texts = [
{
text: 'Supervised learning uses labeled training data to learn a mapping from inputs to outputs.',
documentName: 'supervised-learning',
metadata: { type: 'definition', category: 'ml-concepts' },
},
{
text: 'Unsupervised learning finds hidden patterns in data without using labeled examples.',
documentName: 'unsupervised-learning',
metadata: { type: 'definition', category: 'ml-concepts' },
},
{
text: 'Reinforcement learning learns optimal actions through trial and error interactions with an environment.',
documentName: 'reinforcement-learning',
metadata: { type: 'definition', category: 'ml-concepts' },
},
];

console.log('\n📝 Adding multiple texts...');
for (const item of texts) {
const result = await langbase.memories.add({
memoryName: 'my-knowledge-base',
...item,
});
console.log(`✅ Added: ${result.documentName}`);
}

console.log('\n🎉 All texts have been added to the memory!');
console.log('You can now query this memory to retrieve relevant information.');
}

main()
.then(() => {
console.log('\n✨ Text addition completed successfully');
})
.catch(error => {
console.error('❌ Error:', error);
});
37 changes: 37 additions & 0 deletions packages/langbase/src/langbase/langbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,13 @@ export interface MemoryRetryDocEmbedOptions {
documentName: string;
}

export interface MemoryAddTextOptions {
memoryName: string;
text: string;
documentName?: string;
metadata?: Record<string, string>;
}

export interface MemoryCreateResponse extends MemoryBaseResponse {
chunk_size: number;
chunk_overlap: number;
Expand All @@ -382,6 +389,13 @@ export interface MemoryDeleteResponse extends BaseDeleteResponse {}
export interface MemoryDeleteDocResponse extends BaseDeleteResponse {}
export interface MemoryRetryDocEmbedResponse extends BaseDeleteResponse {}

export interface MemoryAddTextResponse {
documentName: string;
status: 'queued';
memoryName: string;
url: string;
}

export interface MemoryRetrieveResponse {
text: string;
similarity: number;
Expand Down Expand Up @@ -549,6 +563,7 @@ export class Langbase {
options: MemoryRetrieveOptions,
) => Promise<MemoryRetrieveResponse[]>;
list: () => Promise<MemoryListResponse[]>;
add: (options: MemoryAddTextOptions) => Promise<MemoryAddTextResponse>;
documents: {
list: (
options: MemoryListDocOptions,
Expand Down Expand Up @@ -577,6 +592,7 @@ export class Langbase {
options: MemoryRetrieveOptions,
) => Promise<MemoryRetrieveResponse[]>;
list: () => Promise<MemoryListResponse[]>;
add: (options: MemoryAddTextOptions) => Promise<MemoryAddTextResponse>;
documents: {
list: (
options: MemoryListDocOptions,
Expand Down Expand Up @@ -675,6 +691,7 @@ export class Langbase {
delete: this.deleteMemory.bind(this),
retrieve: this.retrieveMemory.bind(this),
list: this.listMemory.bind(this),
add: this.addTextToMemory.bind(this),
documents: {
list: this.listDocs.bind(this),
delete: this.deleteDoc.bind(this),
Expand All @@ -691,6 +708,7 @@ export class Langbase {
delete: this.deleteMemory.bind(this),
retrieve: this.retrieveMemory.bind(this),
list: this.listMemory.bind(this),
add: this.addTextToMemory.bind(this),
documents: {
list: this.listDocs.bind(this),
delete: this.deleteDoc.bind(this),
Expand Down Expand Up @@ -965,6 +983,25 @@ export class Langbase {
});
}

/**
* Adds text directly to a memory without file upload.
*
* @param options - The options for adding text to memory.
* @param options.memoryName - The name of the memory to add text to.
* @param options.text - The text content to add to the memory.
* @param options.documentName - Optional custom document name.
* @param options.metadata - Optional metadata for the text document.
* @returns A promise that resolves to the response of the text addition operation.
*/
private async addTextToMemory(
options: MemoryAddTextOptions,
): Promise<MemoryAddTextResponse> {
return this.request.post({
endpoint: '/v1/memory/text',
body: options,
});
}

/**
* Performs a web search using the Langbase API.
*
Expand Down
Loading