本文主要研究一下如何使用langchain4j对接Chroma向量数据库
docker run -d \
--name chromadb \
-p 8000:8000 \
-v "$(pwd)/chroma_data:/chroma/chroma" \
-e IS_PERSISTENT=TRUE \
-e ANONYMIZED_TELEMETRY=TRUE \
docker.1ms.run/chromadb/chroma:latest
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-chroma</artifactId>
<version>1.0.0-beta1</version>
</dependency>
public class JlamaChromaExample {
public static void main(String[] args) {
String chromaEndpoint = "http://localhost:8000";
EmbeddingStore<TextSegment> embeddingStore = ChromaEmbeddingStore
.builder()
.baseUrl(chromaEndpoint)
.collectionName("test1_collection")
.logRequests(true)
.logResponses(true)
.build();
EmbeddingModel embeddingModel = JlamaEmbeddingModel.builder()
.modelName("intfloat/e5-small-v2")
.build();
TextSegment segment1 = TextSegment.from("I like football.");
Embedding embedding1 = embeddingModel.embed(segment1).content();
embeddingStore.add(embedding1, segment1);
TextSegment segment2 = TextSegment.from("The weather is good today.");
Embedding embedding2 = embeddingModel.embed(segment2).content();
embeddingStore.add(embedding2, segment2);
Embedding queryEmbedding = embeddingModel.embed("What is your favourite sport?").content();
List<EmbeddingMatch<TextSegment>> relevant = embeddingStore.findRelevant(queryEmbedding, 1);
EmbeddingMatch<TextSegment> embeddingMatch = relevant.get(0);
System.out.println(embeddingMatch.score()); // 0.8144288493114709
System.out.println(embeddingMatch.embedded().text()); // I like football.
}
}
这里使用了Jlama提供的JlamaEmbeddingModel,官方示例的AllMiniLmL6V2EmbeddingModel在mac下会报错
ai.djl.engine.EngineException: Unexpected flavor: cpu
输出如下
WARNING: Using incubator modules: jdk.incubator.vector
INFO c.g.tjake.jlama.model.AbstractModel - Model type = F32, Working memory type = F32, Quantized memory type = F32
WARN c.g.t.j.t.o.TensorOperationsProvider - Native operations not available. Consider adding 'com.github.tjake:jlama-native' to the classpath
INFO c.g.t.j.t.o.TensorOperationsProvider - Using Panama Vector Operations (OffHeap)
0.8279024262570531
I like football.
langchain4j提供了langchain4j-chroma模块用于访问Chroma。需要注意的是
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。