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

feat(community): MariaDB vector store implementation. #7360

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rusher
Copy link

@rusher rusher commented Dec 13, 2024

Description

This is the MariaDB vectorstore implementation. see https://mariadb.org/projects/mariadb-vector/

Key Features

  • provide mariadb implementation to store and request embeddings
  • provide a filter unifying proposal

Test

  • test cases added

Filtering proposal

This PR contains a proposal to unify filtering data. I might have not understood stuctured query well, but goal of this framework is to permit unifying behavior and it doesn't feel this part has succeed in it.

I'll detail what i've submitted, i'll let you judge if accurate or not.
There is now a new package "@langchain/core/filter"

an example of database filter implementation:

class TestFilterExpressionConverter
  extends BaseFilterExpressionConverter
  implements FilterExpressionConverter
{
  constructor() {
    super();
  }

  convertExpression(expression: Expression): string {
    return super.convertExpression(expression);
  }

  convertExpressionToContext(expression: Expression, context: StringBuilder): void {
    super.convertOperandToContext(expression.left, context);
    super.convertSymbolToContext(expression, context);
    super.convertOperandToContext(expression.right!, context);
  }

  convertKeyToContext(key: Key, context: StringBuilder): void {
    context.append(`'$.${key.key}'`);
  }

  writeValueRangeStart(_listValue: Value, context: StringBuilder): void {
    context.append("[");
  }

  writeValueRangeEnd(_listValue: Value, context: StringBuilder): void {
    context.append("]");
  }

  writeGroupStart(_group: Group, context: StringBuilder): void {
    context.append("(");
  }

  writeGroupEnd(_group: Group, context: StringBuilder): void {
    context.append(")");
  }
}

A builder permit to create the filter, and vectorstore implementation will convert it to expected format :

const b = new FilterExpressionBuilder();
const impl = new TestFilterExpressionConverter();

let exp = b.eq("a", 2015); 
console.log(impl.convertExpression(exp));
// "'$.a' = 2015"


exp = b.in("a", [2015, 2018]); 
console.log(impl.convertExpression(exp));
// "'$.a' IN [2015,2018]"

// name = 'martin' AND (firstname = 'john' OR firstname = 'jack')
exp = b.and(
            b.eq("name", "martin"),
            b.group(b.or(b.eq("firstname", "john"), b.eq("firstname", "jack")))
        ); 
console.log(impl.convertExpression(exp));
// "'$.name' = 'martin' AND '$.firstname' = 'john'"

// the opposite
exp = b.not(
          b.and(
            b.eq("name", "martin"),
            b.group(b.or(b.eq("firstname", "john"), b.eq("firstname", "jack")))
          )
        ); 
console.log(impl.convertExpression(exp));
// "'$.name' != 'martin' OR ('$.firstname' != 'john' AND '$.firstname' != 'jack')"

(Ideally, a text parser using antlr to generate the expression from a standardized query would next the next step)

When requiring the corresponding SQL to a filter expression, each vector store needs to implement a filter implementation, extending BaseFilterExpressionConverter. This prevent SQL injection, unify behavior, and permit any kind of filtering, like IN, NOT, gouping ...

(a database filtering implementation must be probably maximum 20 significant lines of code, mainly because each database use his specific JSON functions)

@dosubot dosubot bot added the size:XXL This PR changes 1000+ lines, ignoring generated files. label Dec 13, 2024
Copy link

vercel bot commented Dec 13, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchainjs-docs ❌ Failed (Inspect) Jan 3, 2025 9:17am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchainjs-api-refs ⬜️ Ignored (Inspect) Jan 3, 2025 9:17am

@dosubot dosubot bot added auto:documentation Changes to documentation and examples, like .md, .rst, .ipynb files. Changes to the docs/ folder auto:enhancement A large net-new component, integration, or chain. Use sparingly. The largest features labels Dec 13, 2024
@rusher
Copy link
Author

rusher commented Dec 19, 2024

Can i still help to get this PR merged ? about the vercel failure, i cannot have access to it. so i leave it to you to indicate what's wrong

Copy link
Collaborator

@jacoblee93 jacoblee93 left a comment

Choose a reason for hiding this comment

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

Really sorry about the delay - let's move the core work you've done into community for now and make it MariaDB specific

I think it is interesting but there are a lot of filters to generalize across

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is interesting but too broadly scoped

Let's put it in a separate PR

Copy link
Author

Choose a reason for hiding this comment

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

i would like too, but i don't now how to makes mariadb implementation without this filter other leed to injection.

Maybe a version like Dictionnaly in python ( like filter={ 'id': {'$in': [1, 5, 2, 9]} } ). It doesn't seem appropriate too.

what is the solution ? makes a different PR for the filter part, and wait for it for now ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's just move it to langchain-community/utils/mariadb.ts for now

Copy link
Author

Choose a reason for hiding this comment

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

just to be sure we're on the same page: are you going to move this yourself in langchain-community/utils/mariadb.ts or should I move the filter part in mariadb.ts?

libs/langchain-community/langchain.config.js Show resolved Hide resolved
@rusher
Copy link
Author

rusher commented Jan 3, 2025

(i've add the missing requiresOptionalDependencies and rebased to current version)

@jacoblee93
Copy link
Collaborator

Sorry for the delay again, let's just move the core changes to community

Generally speaking we don't allow core changes and community changes in the same PR, and are much more selective about what goes in core

@jacoblee93 jacoblee93 changed the title MariaDB vector store implementation. feat(community): MariaDB vector store implementation. Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:documentation Changes to documentation and examples, like .md, .rst, .ipynb files. Changes to the docs/ folder auto:enhancement A large net-new component, integration, or chain. Use sparingly. The largest features size:XXL This PR changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants