diff --git a/examples/nodejs/memory/memory.text.add.ts b/examples/nodejs/memory/memory.text.add.ts new file mode 100644 index 0000000..eaf97c2 --- /dev/null +++ b/examples/nodejs/memory/memory.text.add.ts @@ -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); + }); \ No newline at end of file diff --git a/packages/langbase/src/langbase/langbase.ts b/packages/langbase/src/langbase/langbase.ts index b6e886f..8854ad5 100644 --- a/packages/langbase/src/langbase/langbase.ts +++ b/packages/langbase/src/langbase/langbase.ts @@ -366,6 +366,13 @@ export interface MemoryRetryDocEmbedOptions { documentName: string; } +export interface MemoryAddTextOptions { + memoryName: string; + text: string; + documentName?: string; + metadata?: Record; +} + export interface MemoryCreateResponse extends MemoryBaseResponse { chunk_size: number; chunk_overlap: number; @@ -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; @@ -549,6 +563,7 @@ export class Langbase { options: MemoryRetrieveOptions, ) => Promise; list: () => Promise; + add: (options: MemoryAddTextOptions) => Promise; documents: { list: ( options: MemoryListDocOptions, @@ -577,6 +592,7 @@ export class Langbase { options: MemoryRetrieveOptions, ) => Promise; list: () => Promise; + add: (options: MemoryAddTextOptions) => Promise; documents: { list: ( options: MemoryListDocOptions, @@ -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), @@ -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), @@ -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 { + return this.request.post({ + endpoint: '/v1/memory/text', + body: options, + }); + } + /** * Performs a web search using the Langbase API. *