-
Notifications
You must be signed in to change notification settings - Fork 2k
add support for OpenSearch vector store #508
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
Changes from 1 commit
e655a36
b15b308
1701ca1
f08c8a7
7b77b19
9c252bc
4230038
f698902
0eaf7d0
df92dff
2cdd9b5
be6081f
6178b3d
8c04b05
d3f2a8f
8fa688f
3b79a1a
9fbc09a
e9d7398
65e6880
563f9b4
b21427c
9aa97b5
b0add71
9e865f0
6b9f8d3
a5923f5
db6f7cd
773b7bd
7e03a15
5f9ecdd
a50969e
daf131b
6fd3df0
d30e989
6f24154
f5d9d70
7e1c1cc
3d07101
639073e
4c6876b
dce8b80
bd1ac01
b05d9b7
7bb833e
13c504e
f6e5537
ac61b82
a8cad11
1a9d6de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>org.springframework.ai</groupId> | ||
| <artifactId>spring-ai</artifactId> | ||
| <version>1.0.0-SNAPSHOT</version> | ||
| <relativePath>../../pom.xml</relativePath> | ||
| </parent> | ||
| <artifactId>spring-ai-opensearch-store</artifactId> | ||
| <packaging>jar</packaging> | ||
| <name>Spring AI Vector Store - OpenSearch</name> | ||
| <description>Spring AI OpenSearch Vector Store</description> | ||
| <url>https://github.com/spring-projects/spring-ai</url> | ||
|
|
||
| <scm> | ||
| <url>https://github.com/spring-projects/spring-ai</url> | ||
| <connection>git://github.com/spring-projects/spring-ai.git</connection> | ||
| <developerConnection>[email protected]:spring-projects/spring-ai.git</developerConnection> | ||
| </scm> | ||
|
|
||
| <properties> | ||
| <!-- testing --> | ||
| <hikari-cp.version>4.0.3</hikari-cp.version> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.springframework.ai</groupId> | ||
| <artifactId>spring-ai-core</artifactId> | ||
| <version>${parent.version}</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.opensearch.client</groupId> | ||
| <artifactId>opensearch-java</artifactId> | ||
| <version>2.9.1</version> | ||
|
||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.apache.httpcomponents.client5</groupId> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a test dependency only? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a required dependency for OpenSearch client. See https://opensearch.org/docs/latest/clients/java/#installing-the-client-using-apache-httpclient-5-transport. |
||
| <artifactId>httpclient5</artifactId> | ||
| <version>5.3.1</version> | ||
| </dependency> | ||
|
|
||
| <!-- TESTING --> | ||
| <dependency> | ||
| <groupId>org.springframework.ai</groupId> | ||
| <artifactId>spring-ai-openai</artifactId> | ||
| <version>${parent.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
|
|
||
| <dependency> | ||
| <groupId>org.springframework.ai</groupId> | ||
| <artifactId>spring-ai-test</artifactId> | ||
| <version>${parent.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-test</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.opensearch</groupId> | ||
| <artifactId>opensearch-testcontainers</artifactId> | ||
| <version>2.0.1</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.testcontainers</groupId> | ||
| <artifactId>junit-jupiter</artifactId> | ||
| <version>${testcontainers.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| </dependencies> | ||
|
|
||
| </project> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| /* | ||
| * Copyright 2023 - 2024 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.springframework.ai.vectorstore; | ||
|
|
||
| import org.springframework.ai.vectorstore.filter.Filter; | ||
| import org.springframework.ai.vectorstore.filter.Filter.Expression; | ||
| import org.springframework.ai.vectorstore.filter.Filter.Key; | ||
| import org.springframework.ai.vectorstore.filter.converter.AbstractFilterExpressionConverter; | ||
|
|
||
| import java.text.ParseException; | ||
| import java.text.SimpleDateFormat; | ||
| import java.util.Date; | ||
| import java.util.List; | ||
| import java.util.TimeZone; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * @author Jemin Huh | ||
| * @since 1.0.0 | ||
| */ | ||
| public class OpenSearchAiSearchFilterExpressionConverter extends AbstractFilterExpressionConverter { | ||
|
|
||
| private static final Pattern DATE_FORMAT_PATTERN = Pattern.compile("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z"); | ||
|
|
||
| private final SimpleDateFormat dateFormat; | ||
|
|
||
| public OpenSearchAiSearchFilterExpressionConverter() { | ||
| this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); | ||
| this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doExpression(Expression expression, StringBuilder context) { | ||
| if (expression.type() == Filter.ExpressionType.IN || expression.type() == Filter.ExpressionType.NIN) { | ||
| context.append(getOperationSymbol(expression)); | ||
| context.append("("); | ||
| this.convertOperand(expression.left(), context); | ||
| this.convertOperand(expression.right(), context); | ||
| context.append(")"); | ||
| } | ||
| else { | ||
| this.convertOperand(expression.left(), context); | ||
| context.append(getOperationSymbol(expression)); | ||
| this.convertOperand(expression.right(), context); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected void doStartValueRange(Filter.Value listValue, StringBuilder context) { | ||
| } | ||
|
|
||
| @Override | ||
| protected void doEndValueRange(Filter.Value listValue, StringBuilder context) { | ||
| } | ||
|
|
||
| @Override | ||
| protected void doAddValueRangeSpitter(Filter.Value listValue, StringBuilder context) { | ||
| context.append(" OR "); | ||
| } | ||
|
|
||
| private String getOperationSymbol(Expression exp) { | ||
| return switch (exp.type()) { | ||
| case AND -> " AND "; | ||
| case OR -> " OR "; | ||
| case EQ, IN -> ""; | ||
| case NE -> " NOT "; | ||
| case LT -> "<"; | ||
| case LTE -> "<="; | ||
| case GT -> ">"; | ||
| case GTE -> ">="; | ||
| case NIN -> "NOT "; | ||
| default -> throw new RuntimeException("Not supported expression type: " + exp.type()); | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public void doKey(Key key, StringBuilder context) { | ||
| var identifier = hasOuterQuotes(key.key()) ? removeOuterQuotes(key.key()) : key.key(); | ||
| var prefixedIdentifier = withMetaPrefix(identifier); | ||
| context.append(prefixedIdentifier.trim()).append(":"); | ||
| } | ||
|
|
||
| public String withMetaPrefix(String identifier) { | ||
| return "metadata." + identifier; | ||
| } | ||
|
|
||
| @Override | ||
| protected void doValue(Filter.Value filterValue, StringBuilder context) { | ||
| if (filterValue.value() instanceof List list) { | ||
| int c = 0; | ||
| for (Object v : list) { | ||
| context.append(v); | ||
| if (c++ < list.size() - 1) { | ||
| this.doAddValueRangeSpitter(filterValue, context); | ||
| } | ||
| } | ||
| } | ||
| else { | ||
| this.doSingleValue(filterValue.value(), context); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected void doSingleValue(Object value, StringBuilder context) { | ||
| if (value instanceof Date date) { | ||
| context.append(this.dateFormat.format(date)); | ||
| } | ||
| else if (value instanceof String text) { | ||
| if (DATE_FORMAT_PATTERN.matcher(text).matches()) { | ||
| try { | ||
| Date date = this.dateFormat.parse(text); | ||
| context.append(this.dateFormat.format(date)); | ||
| } | ||
| catch (ParseException e) { | ||
| throw new IllegalArgumentException("Invalid date type:" + text, e); | ||
| } | ||
| } | ||
| else { | ||
| context.append(text); | ||
| } | ||
| } | ||
| else { | ||
| context.append(value); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void doStartGroup(Filter.Group group, StringBuilder context) { | ||
| context.append("("); | ||
| } | ||
|
|
||
| @Override | ||
| public void doEndGroup(Filter.Group group, StringBuilder context) { | ||
| context.append(")"); | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And the spring-ai-opensearch-store dependency to the spring-bom