Skip to content

Commit

Permalink
Add test-exports case for CJS dynamic import (langchain-ai#179)
Browse files Browse the repository at this point in the history
Since we have this in the docs, we should test it (it does work)
  • Loading branch information
nfcampos authored Mar 2, 2023
1 parent 835def9 commit ba632cc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
50 changes: 50 additions & 0 deletions test-exports/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
async function test() {
const { default: assert } = await import("assert");
const { OpenAI } = await import("langchain");
const { loadPrompt } = await import("langchain/prompts");
const { HNSWLib } = await import("langchain/vectorstores");
const { OpenAIEmbeddings } = await import("langchain/embeddings");
const { InMemoryDocstore, Document } = await import("langchain/docstore");

// Test exports
assert(typeof OpenAI === "function");
assert(typeof loadPrompt === "function");
assert(typeof HNSWLib === "function");

// Test dynamic imports of peer dependencies
const { HierarchicalNSW } = await HNSWLib.imports();

const vs = new HNSWLib(
{
space: "ip",
numDimensions: 3,
},
new OpenAIEmbeddings({ openAIApiKey: "sk-XXXX" }),
new InMemoryDocstore(),
new HierarchicalNSW("ip", 3)
);

await vs.addVectors(
[
[0, 1, 0],
[0, 0, 1],
],
[
new Document({
pageContent: "a",
}),
new Document({
pageContent: "b",
}),
]
);

assert((await vs.similaritySearchVectorWithScore([0, 0, 1], 1)).length === 1);
}

test()
.then(() => console.log("success"))
.catch((e) => {
console.error(e);
process.exit(1);
});
4 changes: 2 additions & 2 deletions test-exports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"main": "./index.js",
"type": "module",
"scripts": {
"test": "node ./index.mjs && tsc",
"test": "node ./index.mjs && node ./index.cjs && tsc",
"format": "prettier --write \"**/*.ts\"",
"format:diff": "prettier --list-different \"**/*.ts\"",
"ci": "yarn format:diff && yarn test"
Expand All @@ -21,4 +21,4 @@
"prettier": "^2.8.3",
"typescript": "^4.9.5"
}
}
}

0 comments on commit ba632cc

Please sign in to comment.