Skip to content

Commit 41556f4

Browse files
authored
📦 NEW: Memory endpoints support (#76)
* 📦 NEW: Memory endpoints support * 👌 IMPROVE: Code * 👌 IMPROVE: Code
1 parent 5543412 commit 41556f4

File tree

10 files changed

+331
-2
lines changed

10 files changed

+331
-2
lines changed
File renamed without changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'dotenv/config';
2+
import {Memory} from 'langbase';
3+
4+
const memory = new Memory({
5+
apiKey: process.env.LANGBASE_API_KEY!,
6+
});
7+
8+
async function main() {
9+
const response = await memory.delete({
10+
name: 'memory-sdk',
11+
});
12+
13+
console.log(response);
14+
}
15+
16+
main();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'dotenv/config';
2+
import {Memory} from 'langbase';
3+
4+
const memory = new Memory({
5+
apiKey: process.env.LANGBASE_API_KEY!,
6+
});
7+
8+
async function main() {
9+
const response = await memory.deleteDoc({
10+
memoryName: 'memory-sdk',
11+
documentName: 'readme.md',
12+
});
13+
14+
console.log(response);
15+
}
16+
17+
main();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'dotenv/config';
2+
import {Memory} from 'langbase';
3+
4+
const memory = new Memory({
5+
apiKey: process.env.LANGBASE_API_KEY!,
6+
});
7+
8+
async function main() {
9+
const response = await memory.listDocs({
10+
memoryName: 'memory-sdk'
11+
});
12+
13+
console.log(response);
14+
}
15+
16+
main();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'dotenv/config';
2+
import {Memory} from 'langbase';
3+
4+
const memory = new Memory({
5+
apiKey: process.env.LANGBASE_API_KEY!,
6+
});
7+
8+
async function main() {
9+
const response = await memory.retryDocEmbed({
10+
memoryName: 'memory-sdk',
11+
documentName: 'memory.upload.doc.ts',
12+
});
13+
14+
console.log(response);
15+
}
16+
17+
main();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import 'dotenv/config';
2+
import {Memory} from 'langbase';
3+
import fs from 'fs';
4+
import path from 'path';
5+
6+
const memory = new Memory({
7+
apiKey: process.env.LANGBASE_API_KEY!,
8+
});
9+
10+
async function main() {
11+
const src = path.join(
12+
process.cwd(),
13+
'examples',
14+
'memory',
15+
'memory.upload.doc.ts',
16+
);
17+
const file = fs.readFileSync(src);
18+
19+
const response = await memory.uploadDoc({
20+
file,
21+
memoryName: 'memory-sdk',
22+
fileName: 'memory.upload.doc.ts',
23+
contentType: 'text/plain',
24+
meta: {
25+
extension: 'ts',
26+
description: 'This is a test file',
27+
},
28+
});
29+
30+
console.log(response);
31+
}
32+
33+
main();
File renamed without changes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import 'dotenv/config';
2+
import {Memory} from 'langbase';
3+
4+
const memory = new Memory({
5+
apiKey: process.env.LANGBASE_API_KEY!,
6+
});
7+
8+
async function main() {
9+
const response = await memory.retrieve({
10+
memory: [
11+
{
12+
name: 'langbase-docs',
13+
},
14+
],
15+
query: 'What are pipes in Langbase?',
16+
topK: 2,
17+
});
18+
19+
console.log(response);
20+
}
21+
22+
main();

examples/nodejs/package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@
88
"scripts": {
99
"generate-text": "npx tsx ./examples/pipes/generate-text.ts",
1010
"pipe.create": "npx tsx ./examples/pipes/pipe.create.ts",
11-
"memory.create": "npx tsx ./examples/pipes/memory.create.ts",
12-
"memory.list": "npx tsx ./examples/pipes/memory.list.ts",
11+
"memory.create": "npx tsx ./examples/memory/memory.create.ts",
12+
"memory.list": "npx tsx ./examples/memory/memory.list.ts",
13+
"memory.delete": "npx tsx ./examples/memory/memory.delete.ts",
14+
"memory.retrieve": "npx tsx ./examples/memory/memory.retrieve.ts",
15+
"memory.docs.list": "npx tsx ./examples/memory/memory.docs.list.ts",
16+
"memory.docs.delete": "npx tsx ./examples/memory/memory.docs.delete.ts",
17+
"memory.docs.upload": "npx tsx ./examples/memory/memory.docs.upload.ts",
18+
"memory.docs.retry-embed": "npx tsx ./examples/memory/memory.docs.retry-embed.ts",
1319
"pipe.update": "npx tsx ./examples/pipes/pipe.update.ts",
1420
"pipe.list": "npx tsx ./examples/pipes/pipe.list.ts",
1521
"pipe.run": "npx tsx ./examples/pipes/pipe.run.ts",

packages/langbase/src/memory/memory.ts

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,229 @@ export interface MemoryCreateOptions {
1616
description?: string;
1717
}
1818

19+
export interface MemoryDeleteOptions {
20+
name: string;
21+
}
22+
23+
export interface MemoryRetrieveOptions {
24+
query: string;
25+
memory: {
26+
name: string;
27+
}[];
28+
topK?: number;
29+
}
30+
31+
export interface MemoryListDocOptions {
32+
memoryName: string;
33+
}
34+
35+
export interface MemoryDeleteDocOptions {
36+
memoryName: string;
37+
documentName: string;
38+
}
39+
40+
export interface MemoryUploadDocOptions {
41+
memoryName: string;
42+
fileName: string;
43+
meta?: Record<string, string>;
44+
file: Buffer | File | FormData | ReadableStream;
45+
contentType:
46+
| 'application/pdf'
47+
| 'text/plain'
48+
| 'text/markdown'
49+
| 'text/csv';
50+
}
51+
52+
export interface MemoryRetryDocEmbedOptions {
53+
memoryName: string;
54+
documentName: string;
55+
}
56+
1957
export interface MemoryCreateResponse extends MemoryBaseResponse {}
2058
export interface MemoryListResponse extends MemoryBaseResponse {}
59+
export interface BaseDeleteResponse {
60+
success: boolean;
61+
}
62+
63+
export interface MemoryDeleteResponse extends BaseDeleteResponse {}
64+
export interface MemoryDeleteDocResponse extends BaseDeleteResponse {}
65+
export interface MemoryRetryDocEmbedResponse extends BaseDeleteResponse {}
66+
67+
export interface MemoryRetrieveResponse {
68+
text: string;
69+
similarity: number;
70+
meta: Record<string, string>;
71+
}
72+
73+
export interface MemoryListDocResponse {
74+
name: string;
75+
status: 'queued' | 'in_progress' | 'completed' | 'failed';
76+
status_message: string | null;
77+
metadata: {
78+
size: number;
79+
type:
80+
| 'application/pdf'
81+
| 'text/plain'
82+
| 'text/markdown'
83+
| 'text/csv'
84+
| 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
85+
| 'application/vnd.ms-excel';
86+
};
87+
enabled: boolean;
88+
chunk_size: number;
89+
chunk_overlap: number;
90+
owner_login: string;
91+
}
2192

2293
export class Memory {
2394
private request: Request;
95+
private apiKey: string;
2496

2597
constructor(options: MemoryOptions) {
2698
const baseUrl = 'https://api.langbase.com';
99+
this.apiKey = options.apiKey;
27100
this.request = new Request({apiKey: options.apiKey, baseUrl});
28101
}
29102

103+
/**
104+
* Creates a new memory on Langbase.
105+
*
106+
* @param {MemoryCreateOptions} options - The options to create the memory instance.
107+
* @param {string} options.name - The name of the memory.
108+
* @param {string} options.description - The description of the memory.
109+
* @returns {Promise<MemoryCreateResponse>} A promise that resolves to the response of the memory creation.
110+
*/
30111
async create(options: MemoryCreateOptions): Promise<MemoryCreateResponse> {
31112
return this.request.post({
32113
endpoint: '/v1/memory',
33114
body: options,
34115
});
35116
}
36117

118+
/**
119+
* Retrieves a list of all memories on Langbase.
120+
*
121+
* @returns {Promise<MemoryListResponse[]>} A promise that resolves to an array of memory list responses.
122+
*/
37123
async list(): Promise<MemoryListResponse[]> {
38124
return this.request.get({
39125
endpoint: '/v1/memory',
40126
});
41127
}
128+
129+
/**
130+
* Deletes a memory on Langbase.
131+
*
132+
* @param {MemoryDeleteOptions} options - The options for deleting the memory resource.
133+
* @param {string} options.name - The name of the memory to delete.
134+
* @returns {Promise<MemoryDeleteResponse>} A promise that resolves to the response of the delete operation.
135+
*/
136+
async delete(options: MemoryDeleteOptions): Promise<MemoryDeleteResponse> {
137+
return this.request.delete({
138+
endpoint: `/v1/memory/${options.name}`,
139+
});
140+
}
141+
142+
/**
143+
* Retrieves similar text from the memory.
144+
*
145+
* @param {MemoryRetrieveOptions} options - The options to use for retrieving memory data.
146+
* @param {string} options.query - The query text to search for.
147+
* @param {object[]} options.memory - The memory to search in.
148+
* @param {number} [options.topK] - The number of similar texts to retrieve.
149+
* @returns A promise that resolves to an array of `MemoryRetrieveResponse` objects.
150+
*/
151+
async retrieve(
152+
options: MemoryRetrieveOptions,
153+
): Promise<MemoryRetrieveResponse[]> {
154+
return this.request.post({
155+
endpoint: '/v1/memory/retrieve',
156+
body: options,
157+
});
158+
}
159+
160+
/**
161+
* Retrieves a list of documents inside a memory.
162+
*
163+
* @param {MemoryListDocOptions} options - The options for listing documents, including the memory name.
164+
* @param {string} options.memoryName - The name of the memory to list documents from.
165+
* @returns A promise that resolves to an array of `MemoryListDocResponse` objects.
166+
*/
167+
async listDocs(
168+
options: MemoryListDocOptions,
169+
): Promise<MemoryListDocResponse[]> {
170+
return this.request.get({
171+
endpoint: `/v1/memory/${options.memoryName}/documents`,
172+
});
173+
}
174+
175+
/**
176+
* Deletes a document from a memory.
177+
*
178+
* @param {MemoryDeleteDocOptions} options - The options for deleting the document.
179+
* @param {string} options.memoryName - The name of the memory to delete the document from.
180+
* @param {string} options.documentName - The name of the document to delete.
181+
* @returns A promise that resolves to a `MemoryDeleteDocResponse` indicating the result of the delete operation.
182+
*/
183+
async deleteDoc(
184+
options: MemoryDeleteDocOptions,
185+
): Promise<MemoryDeleteDocResponse> {
186+
return this.request.delete({
187+
endpoint: `/v1/memory/${options.memoryName}/documents/${options.documentName}`,
188+
});
189+
}
190+
191+
/**
192+
* Uploads a document to the memory.
193+
*
194+
* @param {MemoryUploadDocOptions} options - The options for uploading the document.
195+
* @param {string} options.memoryName - The name of the memory to upload the document to.
196+
* @param {string} options.fileName - The name of the file being uploaded.
197+
* @param {object} [options.meta] - Optional metadata associated with the document.
198+
* @param {string} options.contentType - The MIME type of the file being uploaded.
199+
* @param {Blob | Buffer} options.file - The file content to be uploaded.
200+
* @returns {Promise<Response>} The response from the upload request.
201+
* @throws Will throw an error if the upload fails.
202+
*/
203+
async uploadDoc(options: MemoryUploadDocOptions): Promise<Response> {
204+
try {
205+
const response = (await this.request.post({
206+
endpoint: `/v1/memory/documents`,
207+
body: {
208+
memoryName: options.memoryName,
209+
fileName: options.fileName,
210+
meta: options.meta,
211+
},
212+
})) as unknown as {signedUrl: string};
213+
214+
const uploadUrl = response.signedUrl;
215+
216+
return await fetch(uploadUrl, {
217+
method: 'PUT',
218+
headers: {
219+
Authorization: `Bearer ${this.apiKey}`,
220+
'Content-Type': options.contentType,
221+
},
222+
body: options.file,
223+
});
224+
} catch (error) {
225+
throw error;
226+
}
227+
}
228+
229+
/**
230+
* Retries the embedding process for a specific document in memory.
231+
*
232+
* @param options - The options required to retry the document embedding.
233+
* @param options.memoryName - The name of the memory containing the document.
234+
* @param options.documentName - The name of the document to retry embedding for.
235+
* @returns A promise that resolves to the response of the retry operation.
236+
*/
237+
async retryDocEmbed(
238+
options: MemoryRetryDocEmbedOptions,
239+
): Promise<MemoryRetryDocEmbedResponse> {
240+
return this.request.get({
241+
endpoint: `/v1/memory/${options.memoryName}/documents/${options.documentName}/embeddings/retry`,
242+
});
243+
}
42244
}

0 commit comments

Comments
 (0)