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

Fix llms and chat tests failing, rework llms accordingly to new Watsonx #31

Merged
merged 55 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
317a6af
Fix llms and chat tests failing, rework llms accordingly to new Watso…
FilipZmijewski Nov 13, 2024
933c90f
Fixed missing tool_choice in call options of chat (#30)
FilipZmijewski Nov 12, 2024
d4e5bc4
Remove unused import
FilipZmijewski Nov 13, 2024
ebbeaa6
Fix
FilipZmijewski Nov 13, 2024
b7b980b
fix[community]: Fixed missing tool_choice in call options of chat IBM…
FilipZmijewski Nov 12, 2024
4a712a8
fix(anthropic, bedrock): Remove message merging logic (#7196)
bracesproul Nov 13, 2024
65a6f51
feat(community): Release 0.3.14 (#7197)
bracesproul Nov 13, 2024
9b312bd
feat(anthropic): Release 0.3.8 (#7198)
bracesproul Nov 13, 2024
30bdafd
fix(google-genai): Force tool usage in WSO (#7213)
bracesproul Nov 16, 2024
1e9908f
fix(google-genai): Release 0.1.4 (#7215)
bracesproul Nov 16, 2024
7bf0eaf
feat(community): Adds an HTML loader for URLS (#7184)
philnash Nov 17, 2024
d939328
feat(community): allow metadata generics to flow through LibSQLVector…
cdierkens Nov 17, 2024
157e1d6
feat(community): replace `vectordb` package with new `@lancedb/lanced…
wjones127 Nov 17, 2024
2aac482
feat(cosmosdbnosql): Add Chat History Integration (#7057)
aditishree1 Nov 17, 2024
9b2de04
chore(azure-cosmosdb): Release 0.2.2 (#7219)
jacoblee93 Nov 17, 2024
d6a0663
feat(community): add filters to LibSQLVectorStore (#7209)
cdierkens Nov 17, 2024
ebb06ef
chore(community): Release 0.3.15 (#7221)
jacoblee93 Nov 17, 2024
d65690a
fix(langchain): Fix structured parser with triple backticks, adds tes…
johnguirgis Nov 17, 2024
67193c1
chore(langchain): add throw err for JsonOutputFunctionsParser.parse (…
Makoq Nov 17, 2024
5fcc0c4
fix(langchain): Fix serialization for initChatModel (#7222)
jacoblee93 Nov 17, 2024
6d7398e
chore(langchain): Release 0.3.6 (#7223)
jacoblee93 Nov 17, 2024
c37d90e
feat(pinecone): Add support for Pinecone `/embed` endpoint (#7203)
aulorbe Nov 17, 2024
70206f0
chore(pinecone): Release 0.1.3 (#7224)
jacoblee93 Nov 17, 2024
6ab8bcc
fix(openai): Support o1 streaming (#7229)
bracesproul Nov 18, 2024
27d0adc
chore(openai): Release 0.3.14 (#7230)
jacoblee93 Nov 18, 2024
105461f
docs: Add missing streaming concept (#7228)
eyurtsev Nov 20, 2024
d5e616d
fix(ci): Update release script to be more lenient (#7234)
bracesproul Nov 22, 2024
2c071da
feat(mistral): Mistral 1.3.1 migration (#7218)
CarterMorris Nov 22, 2024
3f9dfd0
chore(mistral): Release 0.2.0 (#7239)
jacoblee93 Nov 22, 2024
0c18758
feat(azurecosmosdb): Vector Store Add DiskANN index for CosmosDB (#7225)
crisjc-e Nov 22, 2024
661c9e2
chore(azure-cosmosdb): Release 0.2.3 (#7241)
jacoblee93 Nov 22, 2024
6b42b80
fix(community): bedrock parsing array content/tool blocks (#7244)
bracesproul Nov 22, 2024
f555135
fix(ci): Fix release workspace (#7245)
bracesproul Nov 22, 2024
552d054
feat(community): Incorporate BM25 score in the results (#7236)
felipediel Nov 25, 2024
b40f981
feat(community): Stagehand tools integration (#7177)
filip-michalsky Nov 25, 2024
c62a644
Update Pinecone indexing example to show use of deletionProtection (#…
aulorbe Nov 25, 2024
d28b74a
feat(community): Add rerank solution to existing IBM community implem…
FilipZmijewski Nov 25, 2024
080e0c7
fix(core): Fix issue in .d.ts typing for protected type (#7259)
jacoblee93 Nov 25, 2024
256f2a8
fix(community): PrismaVectorStore handle empty array in filter (#7254)
shan-mx Nov 25, 2024
87f7283
fix(core): Move type (#7246)
jacoblee93 Nov 25, 2024
ccf278f
chore(core): Release 0.3.19 (#7260)
jacoblee93 Nov 25, 2024
a8c3927
chore(community): Release 0.3.16 (#7261)
jacoblee93 Nov 25, 2024
4ddb593
fix(google-common): Anthropic util using getType instead of _getType …
bracesproul Nov 26, 2024
85d56f3
release(google-common): 0.1.3 (#7265)
bracesproul Nov 26, 2024
0f0ccd5
fix(google-gauth/webauth): Bump Google common dep (#7266)
bracesproul Nov 26, 2024
27b34b2
release(google-webauth): 0.1.3 (#7267)
bracesproul Nov 26, 2024
8a59fdc
fix(google-vertexai/web): Bump Google auth deps (#7269)
bracesproul Nov 26, 2024
f90dac0
release(google-vertexai): 0.1.3 (#7270)
bracesproul Nov 26, 2024
7b05e28
release(google-gauth): 0.1.3 (#7271)
bracesproul Nov 26, 2024
018a4f6
release(google-vertexai-web): 0.1.3 (#7272)
bracesproul Nov 26, 2024
c7ed778
Main ibm community update to main (#34)
FilipZmijewski Nov 26, 2024
e2078ba
Change compressDocuments() method, add tests regarding changes"
FilipZmijewski Nov 27, 2024
5e6b43b
Remove unused import
FilipZmijewski Nov 27, 2024
24e0b0b
Merge branch 'main' into fix/ibm_implementation
FilipZmijewski Nov 27, 2024
87363cb
Update ibm.ts
FilipZmijewski Nov 27, 2024
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
Prev Previous commit
Next Next commit
feat(community): add filters to LibSQLVectorStore (langchain-ai#7209)
Co-authored-by: jacoblee93 <jacoblee93@gmail.com>
  • Loading branch information
2 people authored and FilipZmijewski committed Nov 27, 2024
commit d6a06637981172f7e652a8858c0f0e68ca3dcb37
61 changes: 61 additions & 0 deletions libs/langchain-community/src/utils/sqlite_where_builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { InStatement, InValue } from "@libsql/client";

export type WhereCondition<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Metadata extends Record<string, any> = Record<string, any>
> = {
[Key in keyof Metadata]:
| {
operator: "=" | ">" | "<" | ">=" | "<=" | "<>" | "LIKE";
value: InValue;
}
| {
operator: "IN";
value: InValue[];
};
};

type WhereInStatement = Exclude<InStatement, string>;

export class SqliteWhereBuilder {
private conditions: WhereCondition;

constructor(conditions: WhereCondition) {
this.conditions = conditions;
}

buildWhereClause(): WhereInStatement {
const sqlParts: string[] = [];
const args: Record<string, InValue> = {};

for (const [column, condition] of Object.entries(this.conditions)) {
const { operator, value } = condition;

if (operator === "IN") {
const placeholders = value
.map((_, index) => `:${column}${index}`)
.join(", ");
sqlParts.push(
`json_extract(metadata, '$.${column}') IN (${placeholders})`
);

const values = value.reduce(
(previousValue: Record<string, InValue>, currentValue, index) => {
return { ...previousValue, [`${column}${index}`]: currentValue };
},
{}
);

Object.assign(args, values);
} else {
sqlParts.push(
`json_extract(metadata, '$.${column}') ${operator} :${column}`
);
args[column] = value;
}
}

const sql = sqlParts.length ? `${sqlParts.join(" AND ")}` : "";
return { sql, args };
}
}
38 changes: 32 additions & 6 deletions libs/langchain-community/src/vectorstores/libsql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { Document } from "@langchain/core/documents";
import type { EmbeddingsInterface } from "@langchain/core/embeddings";
import { VectorStore } from "@langchain/core/vectorstores";
import type { Client, InStatement } from "@libsql/client";
import {
SqliteWhereBuilder,
WhereCondition,
} from "../utils/sqlite_where_builder.js";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type MetadataDefault = Record<string, any>;
Expand All @@ -24,7 +28,7 @@ export interface LibSQLVectorStoreArgs {
export class LibSQLVectorStore<
Metadata extends MetadataDefault = MetadataDefault
> extends VectorStore {
declare FilterType: (doc: Document<Metadata>) => boolean;
declare FilterType: string | InStatement | WhereCondition<Metadata>;

private db;

Expand Down Expand Up @@ -111,9 +115,8 @@ export class LibSQLVectorStore<
*/
async similaritySearchVectorWithScore(
query: number[],
k: number
// filter is currently unused
// filter?: this["FilterType"]
k: number,
filter?: this["FilterType"]
): Promise<[Document<Metadata>, number][]> {
// Potential SQL injection risk if query vector is not properly sanitized.
if (!query.every((num) => typeof num === "number" && !Number.isNaN(num))) {
Expand All @@ -122,12 +125,35 @@ export class LibSQLVectorStore<

const queryVector = `[${query.join(",")}]`;

const sql: InStatement = {
const sql = {
sql: `SELECT ${this.table}.rowid as id, ${this.table}.content, ${this.table}.metadata, vector_distance_cos(${this.table}.${this.column}, vector(:queryVector)) AS distance
FROM vector_top_k('idx_${this.table}_${this.column}', vector(:queryVector), CAST(:k AS INTEGER)) as top_k
JOIN ${this.table} ON top_k.rowid = ${this.table}.rowid`,
args: { queryVector, k },
};
} satisfies InStatement;

// Filter is a raw sql where clause, so append it to the join
if (typeof filter === "string") {
sql.sql += ` AND ${filter}`;
} else if (typeof filter === "object") {
// Filter is an in statement.
if ("sql" in filter) {
sql.sql += ` AND ${filter.sql}`;
sql.args = {
...filter.args,
...sql.args,
};
} else {
const builder = new SqliteWhereBuilder(filter);
const where = builder.buildWhereClause();

sql.sql += ` AND ${where.sql}`;
sql.args = {
...where.args,
...sql.args,
};
}
}

const results = await this.db.execute(sql);

Expand Down
118 changes: 83 additions & 35 deletions libs/langchain-community/src/vectorstores/tests/libsql.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ describe("LibSQLVectorStore (local)", () => {
const store = new LibSQLVectorStore(embeddings, config);

const ids = await store.addDocuments([
{
new Document({
pageContent: "hello",
metadata: { a: 1 },
},
}),
]);

expect(ids).toHaveLength(1);
Expand Down Expand Up @@ -117,10 +117,10 @@ describe("LibSQLVectorStore (local)", () => {
const store = new LibSQLVectorStore(embeddings, config);

const ids = await store.addDocuments([
{
new Document({
pageContent: "hello world",
metadata: { a: 1 },
},
}),
]);

expect(ids).toHaveLength(1);
Expand Down Expand Up @@ -154,18 +154,15 @@ describe("LibSQLVectorStore (local)", () => {
const store = new LibSQLVectorStore(embeddings, config);

const ids = await store.addDocuments([
{
new Document({
pageContent: "the quick brown fox",
metadata: { a: 1 },
},
{
}),
new Document({
pageContent: "jumped over the lazy dog",
metadata: { a: 2 },
},
{
}),
new Document({
pageContent: "hello world",
metadata: { a: 3 },
},
}),
]);

expect(ids).toHaveLength(3);
Expand All @@ -186,7 +183,7 @@ describe("LibSQLVectorStore (local)", () => {
).toBe(true);
});

test("a document can be deleted by id", async () => {
test("a similarity search with a filter can be performed", async () => {
await client.batch([
`DROP TABLE IF EXISTS vectors;`,
`CREATE TABLE IF NOT EXISTS vectors (
Expand All @@ -201,18 +198,72 @@ describe("LibSQLVectorStore (local)", () => {
const store = new LibSQLVectorStore(embeddings, config);

const ids = await store.addDocuments([
{
new Document({
pageContent: "the quick brown fox",
metadata: { a: 1 },
metadata: {
label: "1",
},
}),
new Document({
pageContent: "jumped over the lazy dog",
metadata: {
label: "2",
},
}),
new Document({
pageContent: "hello world",
metadata: {
label: "1",
},
}),
]);

expect(ids).toHaveLength(3);
expect(ids.every((id) => typeof id === "string")).toBe(true);

const results = await store.similaritySearch("the quick brown dog", 10, {
label: {
operator: "=",
value: "1",
},
{
});

expect(results).toHaveLength(2);
expect(results.map((result) => result.pageContent)).toEqual([
"the quick brown fox",
"hello world",
]);
expect(
results.map((result) => result.id).every((id) => typeof id === "string")
).toBe(true);
});

test("a document can be deleted by id", async () => {
await client.batch([
`DROP TABLE IF EXISTS vectors;`,
`CREATE TABLE IF NOT EXISTS vectors (
content TEXT,
metadata JSON,
embedding F32_BLOB(1024)
);`,
`CREATE INDEX IF NOT EXISTS idx_vectors_embedding
ON vectors (libsql_vector_idx(embedding));`,
]);

const store = new LibSQLVectorStore(embeddings, config);

const ids = await store.addDocuments([
new Document({
pageContent: "the quick brown fox",
}),
new Document({
pageContent: "jumped over the lazy dog",
metadata: { a: 2 },
},
{
}),
new Document({
pageContent: "hello world",
metadata: { a: 3 },
},
}),
]);

expect(ids).toHaveLength(3);
Expand Down Expand Up @@ -247,18 +298,15 @@ describe("LibSQLVectorStore (local)", () => {
const store = new LibSQLVectorStore(embeddings, config);

const ids = await store.addDocuments([
{
new Document({
pageContent: "the quick brown fox",
metadata: { a: 1 },
},
{
}),
new Document({
pageContent: "jumped over the lazy dog",
metadata: { a: 2 },
},
{
}),
new Document({
pageContent: "hello world",
metadata: { a: 3 },
},
}),
]);

expect(ids).toHaveLength(3);
Expand Down Expand Up @@ -289,18 +337,18 @@ describe("LibSQLVectorStore (local)", () => {
const store = new LibSQLVectorStore(embeddings, config);

const ids = await store.addDocuments([
{
new Document({
pageContent: "the quick brown fox",
metadata: { a: 1 },
},
{
}),
new Document({
pageContent: "jumped over the lazy dog",
metadata: { a: 2 },
},
{
}),
new Document({
pageContent: "hello world",
metadata: { a: 3 },
},
}),
]);

expect(ids).toHaveLength(3);
Expand Down