Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

community[patch]: Fix RRF normalization and lucene characters for neo4j vector #3653

Merged
merged 2 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions libs/langchain-community/src/vectorstores/neo4j_vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ export class Neo4jVectorStore extends VectorStore {
k: Number(k),
embedding: vector,
keyword_index: this.keywordIndexName,
query,
query: removeLuceneChars(query),
};
const results = await this.query(readQuery, parameters);

Expand Down Expand Up @@ -717,7 +717,10 @@ function getSearchIndexQuery(searchType: SearchType): string {
hybrid: `
CALL {
CALL db.index.vector.queryNodes($index, $k, $embedding) YIELD node, score
RETURN node, score UNION
WITH collect({node:node, score:score}) AS nodes, max(score) AS max
UNWIND nodes AS n
// We use 0 as min
RETURN n.node AS node, (n.score / max) AS score UNION
CALL db.index.fulltext.queryNodes($keyword_index, $query, {limit: $k}) YIELD node, score
WITH collect({node: node, score: score}) AS nodes, max(score) AS max
UNWIND nodes AS n
Expand All @@ -729,3 +732,32 @@ function getSearchIndexQuery(searchType: SearchType): string {

return typeToQueryMap[searchType];
}

function removeLuceneChars(text: string): string {
// Remove Lucene special characters
const specialChars = [
"+",
"-",
"&",
"|",
"!",
"(",
")",
"{",
"}",
"[",
"]",
"^",
'"',
"~",
"*",
"?",
":",
"\\",
];
let modifiedText = text;
for (const char of specialChars) {
modifiedText = modifiedText.split(char).join(" ");
}
return modifiedText.trim();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Neo4jVectorStore } from "../neo4j_vector.js";

const OS_TOKEN_COUNT = 1536;

const texts = ["foo", "bar", "baz"];
const texts = ["foo", "bar", "baz", "This is the end of the world!"];

class FakeEmbeddingsWithOsDimension extends FakeEmbeddings {
async embedDocuments(documents: string[]): Promise<number[][]> {
Expand Down Expand Up @@ -469,3 +469,50 @@ test.skip("Test fromExistingGraph multiple properties hybrid", async () => {
await neo4jVectorStore.close();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey team, I've flagged the latest change in the PR for your review. It involves accessing environment variables via process.env for setting up the url, username, and password properties in the Neo4jVectorStore instance. Let me know if you need further assistance with this.

await existingGraph.close();
});

test.skip("Test escape lucene characters", async () => {
const url = process.env.NEO4J_URI as string;
const username = process.env.NEO4J_USERNAME as string;
const password = process.env.NEO4J_PASSWORD as string;

expect(url).toBeDefined();
expect(username).toBeDefined();
expect(password).toBeDefined();

const embeddings = new FakeEmbeddingsWithOsDimension();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const metadatas: any[] = [];

const neo4jVectorStore = await Neo4jVectorStore.fromTexts(
texts,
metadatas,
embeddings,
{
url,
username,
password,
preDeleteCollection: true,
searchType: "hybrid",
}
);

const output = await neo4jVectorStore.similaritySearch(
"This is the end of the world!",
2
);
console.log(output);
const expectedResult = [
new Document({
pageContent: "This is the end of the world!",
metadata: {},
}),
new Document({
pageContent: "baz",
metadata: {},
}),
];

expect(output).toStrictEqual(expectedResult);
await dropVectorIndexes(neo4jVectorStore);
await neo4jVectorStore.close();
});