forked from langchain4j/langchain4j
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integration with vearch (langchain4j#525)
support langchain4j#425 . Due to my local environment problem (`vearch` docker container start failed in Apple M1), I do the integration test in remote `vearch` (I start up `vearch` container in remote host using docker), and it works fine. (But I don't check using `Testcontainers` to start up) Two more things need discussion and your opinion: 1. There is a translation between `RelevantScore` and `CosineSimilarity` in `findRelevant` method, I don't know if that's correct, because `vearch` do not support cosine similarity, so I use inner product instead (same as cosine similarity if vector is normalized). Should we normalize vector before adding it to the embedding store? 2. There are many contraints in creating `vearch` space (retrieval types have different parameters). Should we check it or just let users to check themselves? (see [Create Space](https://vearch.readthedocs.io/en/latest/use_op/op_space.html#create-space)). Currently I implement it by using many inner static class (see `RetrievalParam` and `RetrievalType`, in `SpaceEngine` it will do some constraint check.)
- Loading branch information
Showing
32 changed files
with
1,643 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?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/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>dev.langchain4j</groupId> | ||
<artifactId>langchain4j-parent</artifactId> | ||
<version>0.26.0-SNAPSHOT</version> | ||
<relativePath>../langchain4j-parent/pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>langchain4j-vearch</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<name>LangChain4j integration with Vearch</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>dev.langchain4j</groupId> | ||
<artifactId>langchain4j-core</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.squareup.retrofit2</groupId> | ||
<artifactId>retrofit</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.squareup.retrofit2</groupId> | ||
<artifactId>converter-gson</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.squareup.okhttp3</groupId> | ||
<artifactId>okhttp</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>dev.langchain4j</groupId> | ||
<artifactId>langchain4j-embeddings-all-minilm-l6-v2-q</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>dev.langchain4j</groupId> | ||
<artifactId>langchain4j-core</artifactId> | ||
<classifier>tests</classifier> | ||
<type>test-jar</type> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>testcontainers</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.tinylog</groupId> | ||
<artifactId>tinylog-impl</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.tinylog</groupId> | ||
<artifactId>slf4j-tinylog</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
16 changes: 16 additions & 0 deletions
16
langchain4j-vearch/src/main/java/dev/langchain4j/store/embedding/vearch/BulkRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package dev.langchain4j.store.embedding.vearch; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
class BulkRequest { | ||
|
||
private List<Map<String, Object>> documents; | ||
} |
17 changes: 17 additions & 0 deletions
17
langchain4j-vearch/src/main/java/dev/langchain4j/store/embedding/vearch/BulkResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package dev.langchain4j.store.embedding.vearch; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
class BulkResponse { | ||
|
||
private Integer status; | ||
private String error; | ||
@SerializedName("_id") | ||
private String id; | ||
} |
13 changes: 13 additions & 0 deletions
13
...4j-vearch/src/main/java/dev/langchain4j/store/embedding/vearch/CreateDatabaseRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package dev.langchain4j.store.embedding.vearch; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
class CreateDatabaseRequest { | ||
|
||
private String name; | ||
} |
14 changes: 14 additions & 0 deletions
14
...j-vearch/src/main/java/dev/langchain4j/store/embedding/vearch/CreateDatabaseResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.langchain4j.store.embedding.vearch; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
class CreateDatabaseResponse { | ||
|
||
private Long id; | ||
private String name; | ||
} |
24 changes: 24 additions & 0 deletions
24
...ain4j-vearch/src/main/java/dev/langchain4j/store/embedding/vearch/CreateSpaceRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package dev.langchain4j.store.embedding.vearch; | ||
|
||
import dev.langchain4j.store.embedding.vearch.ModelParam; | ||
import dev.langchain4j.store.embedding.vearch.SpaceEngine; | ||
import dev.langchain4j.store.embedding.vearch.SpacePropertyParam; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
class CreateSpaceRequest { | ||
|
||
private String name; | ||
private Integer partitionNum; | ||
private Integer replicaNum; | ||
private SpaceEngine engine; | ||
private Map<String, SpacePropertyParam> properties; | ||
private List<ModelParam> models; | ||
} |
14 changes: 14 additions & 0 deletions
14
...in4j-vearch/src/main/java/dev/langchain4j/store/embedding/vearch/CreateSpaceResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.langchain4j.store.embedding.vearch; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
class CreateSpaceResponse { | ||
|
||
private Integer id; | ||
private String name; | ||
} |
14 changes: 14 additions & 0 deletions
14
...n4j-vearch/src/main/java/dev/langchain4j/store/embedding/vearch/ListDatabaseResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.langchain4j.store.embedding.vearch; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
class ListDatabaseResponse { | ||
|
||
private Integer id; | ||
private String name; | ||
} |
14 changes: 14 additions & 0 deletions
14
...hain4j-vearch/src/main/java/dev/langchain4j/store/embedding/vearch/ListSpaceResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.langchain4j.store.embedding.vearch; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
public class ListSpaceResponse { | ||
|
||
private Integer id; | ||
private String name; | ||
} |
17 changes: 17 additions & 0 deletions
17
langchain4j-vearch/src/main/java/dev/langchain4j/store/embedding/vearch/MetricType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package dev.langchain4j.store.embedding.vearch; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
/** | ||
* if metric type is not set when searching, it will use the parameter specified when building the space | ||
* | ||
* <p>LangChain4j currently only support {@link MetricType#INNER_PRODUCT}</p> | ||
*/ | ||
public enum MetricType { | ||
|
||
/** | ||
* Inner Product | ||
*/ | ||
@SerializedName("InnerProduct") | ||
INNER_PRODUCT | ||
} |
17 changes: 17 additions & 0 deletions
17
langchain4j-vearch/src/main/java/dev/langchain4j/store/embedding/vearch/ModelParam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package dev.langchain4j.store.embedding.vearch; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
public class ModelParam { | ||
|
||
private String modelId; | ||
private List<String> fields; | ||
private String out; | ||
} |
15 changes: 15 additions & 0 deletions
15
langchain4j-vearch/src/main/java/dev/langchain4j/store/embedding/vearch/ResponseWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package dev.langchain4j.store.embedding.vearch; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
class ResponseWrapper<T> { | ||
|
||
private Integer code; | ||
private String msg; | ||
private T data; | ||
} |
Oops, something went wrong.