Skip to content

Commit

Permalink
Add entrypoint, use native error class instead of Neo4jError
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 committed Dec 14, 2024
1 parent 7ac3b00 commit 9a1a2e7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 30 deletions.
4 changes: 4 additions & 0 deletions libs/langchain-community/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,10 @@ stores/message/mongodb.cjs
stores/message/mongodb.js
stores/message/mongodb.d.ts
stores/message/mongodb.d.cts
stores/message/neo4j.cjs
stores/message/neo4j.js
stores/message/neo4j.d.ts
stores/message/neo4j.d.cts
stores/message/planetscale.cjs
stores/message/planetscale.js
stores/message/planetscale.d.ts
Expand Down
2 changes: 2 additions & 0 deletions libs/langchain-community/langchain.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ export const config = {
"stores/message/ioredis": "stores/message/ioredis",
"stores/message/momento": "stores/message/momento",
"stores/message/mongodb": "stores/message/mongodb",
"stores/message/neo4j": "stores/message/neo4j",
"stores/message/planetscale": "stores/message/planetscale",
"stores/message/postgres": "stores/message/postgres",
"stores/message/redis": "stores/message/redis",
Expand Down Expand Up @@ -472,6 +473,7 @@ export const config = {
"stores/message/ipfs_datastore",
"stores/message/momento",
"stores/message/mongodb",
"stores/message/neo4j",
"stores/message/planetscale",
"stores/message/postgres",
"stores/message/redis",
Expand Down
13 changes: 13 additions & 0 deletions libs/langchain-community/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2512,6 +2512,15 @@
"import": "./stores/message/mongodb.js",
"require": "./stores/message/mongodb.cjs"
},
"./stores/message/neo4j": {
"types": {
"import": "./stores/message/neo4j.d.ts",
"require": "./stores/message/neo4j.d.cts",
"default": "./stores/message/neo4j.d.ts"
},
"import": "./stores/message/neo4j.js",
"require": "./stores/message/neo4j.cjs"
},
"./stores/message/planetscale": {
"types": {
"import": "./stores/message/planetscale.d.ts",
Expand Down Expand Up @@ -3919,6 +3928,10 @@
"stores/message/mongodb.js",
"stores/message/mongodb.d.ts",
"stores/message/mongodb.d.cts",
"stores/message/neo4j.cjs",
"stores/message/neo4j.js",
"stores/message/neo4j.d.ts",
"stores/message/neo4j.d.cts",
"stores/message/planetscale.cjs",
"stores/message/planetscale.js",
"stores/message/planetscale.d.ts",
Expand Down
1 change: 1 addition & 0 deletions libs/langchain-community/src/load/import_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export const optionalImportEntrypoints: string[] = [
"langchain_community/stores/message/ioredis",
"langchain_community/stores/message/momento",
"langchain_community/stores/message/mongodb",
"langchain_community/stores/message/neo4j",
"langchain_community/stores/message/planetscale",
"langchain_community/stores/message/postgres",
"langchain_community/stores/message/redis",
Expand Down
48 changes: 18 additions & 30 deletions libs/langchain-community/src/stores/message/neo4j.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import neo4j, { Driver, Record, Neo4jError, auth } from "neo4j-driver";
import neo4j, { Driver, Record, auth } from "neo4j-driver";
import { v4 as uuidv4 } from "uuid";
import { BaseListChatMessageHistory } from "@langchain/core/chat_history";
import {
Expand Down Expand Up @@ -54,17 +54,13 @@ export class Neo4jChatMessageHistory extends BaseListChatMessageHistory {
if (url && username && password) {
try {
this.driver = neo4j.driver(url, auth.basic(username, password));
} catch (e) {
throw new Neo4jError({
message:
"Could not create a Neo4j driver instance. Please check the connection details.",
cause: e,
});
} catch (e: any) {
throw new Error(
`Could not create a Neo4j driver instance. Please check the connection details.\nCause: ${e.message}`
);
}
} else {
throw new Neo4jError({
message: "Neo4j connection details not provided.",
});
throw new Error("Neo4j connection details not provided.");
}
}

Expand All @@ -75,11 +71,10 @@ export class Neo4jChatMessageHistory extends BaseListChatMessageHistory {

try {
await instance.verifyConnectivity();
} catch (e) {
throw new Neo4jError({
message: `Could not verify connection to the Neo4j database. Cause: ${e}`,
cause: e,
});
} catch (e: any) {
throw new Error(
`Could not verify connection to the Neo4j database.\nCause: ${e.message}`
);
}

return instance;
Expand Down Expand Up @@ -112,11 +107,8 @@ export class Neo4jChatMessageHistory extends BaseListChatMessageHistory {
const results = records.map((record: Record) => record.get("result"));

return mapStoredMessagesToChatMessages(results);
} catch (e) {
throw new Neo4jError({
message: `Ohno! Couldn't get messages. Cause: ${e}`,
cause: e,
});
} catch (e: any) {
throw new Error(`Ohno! Couldn't get messages.\nCause: ${e.message}`);
}
}

Expand All @@ -139,11 +131,8 @@ export class Neo4jChatMessageHistory extends BaseListChatMessageHistory {
type: message.getType(),
content: message.content,
});
} catch (e) {
throw new Neo4jError({
message: `Ohno! Couldn't add message. Cause: ${e}`,
cause: e,
});
} catch (e: any) {
throw new Error(`Ohno! Couldn't add message.\nCause: ${e.message}`);
}
}

Expand All @@ -158,11 +147,10 @@ export class Neo4jChatMessageHistory extends BaseListChatMessageHistory {
await this.driver.executeQuery(clearMessagesCypherQuery, {
sessionId: this.sessionId,
});
} catch (e) {
throw new Neo4jError({
message: `Ohno! Couldn't clear chat history. Cause: ${e}`,
cause: e,
});
} catch (e: any) {
throw new Error(
`Ohno! Couldn't clear chat history.\nCause: ${e.message}`
);
}
}

Expand Down

0 comments on commit 9a1a2e7

Please sign in to comment.