Skip to content

Commit

Permalink
Simplifying caching without tmp files
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrunkat committed Jul 28, 2023
1 parent 1a4872f commit 3aabcc8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
3 changes: 1 addition & 2 deletions templates/js-langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"apify": "^3.0.0",
"hnswlib-node": "^1.4.2",
"langchain": "^0.0.82",
"tar": "^6.1.14",
"temp-write": "^5.0.0"
"tar": "^6.1.14"
},
"scripts": {
"start": "node src/main.js",
Expand Down
12 changes: 5 additions & 7 deletions templates/js-langchain/src/vector_index_cache.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Actor } from 'apify';
import { createHash } from 'crypto';
import { readFile } from 'node:fs/promises';
import tempWrite from 'temp-write';
import { finished } from 'node:stream/promises';
import { Readable } from 'node:stream';
import tar from 'tar';

const VECTOR_INDEX_CACHE_STORE_NAME = 'vector-index-cache';
Expand All @@ -26,10 +26,9 @@ function getIndexCacheKey(config) {
*/
export async function cacheVectorIndex(config, indexPath) {
const vectorIndexCacheStore = await Actor.openKeyValueStore(VECTOR_INDEX_CACHE_STORE_NAME);
const tempFilePath = await tempWrite(tar.c({}, [indexPath]));
const packedVectorIndex = await readFile(tempFilePath);
const packedVectorIndexStream = tar.c({}, [indexPath]);

await vectorIndexCacheStore.setValue(getIndexCacheKey(config), packedVectorIndex, { contentType: 'application/tar' });
await vectorIndexCacheStore.setValue(getIndexCacheKey(config), packedVectorIndexStream, { contentType: 'application/tar' });
}

/**
Expand All @@ -44,8 +43,7 @@ export async function retrieveVectorIndex(config) {
const vectorIndexRecord = await vectorIndexCacheStore.getValue(getIndexCacheKey(config));
if (!vectorIndexRecord) return false;

const tempFilePath = await tempWrite(vectorIndexRecord);
await tar.x({ file: tempFilePath, strip: 1, C: '.' });
await finished(Readable.from(vectorIndexRecord).pipe(tar.x({ strip: 1, C: '.' })));

return true;
}

0 comments on commit 3aabcc8

Please sign in to comment.