初始化
This commit is contained in:
41
easy-agents-store/easy-agents-store-vectorexdb/pom.xml
Normal file
41
easy-agents-store/easy-agents-store-vectorexdb/pom.xml
Normal file
@@ -0,0 +1,41 @@
|
||||
<?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>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-store</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<name>easy-agents-store-vectorexdb</name>
|
||||
<artifactId>easy-agents-store-vectorexdb</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-core</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.github.javpower</groupId>
|
||||
<artifactId>vectorex-core</artifactId>
|
||||
<version>1.5.3</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.easyagents</groupId>
|
||||
<artifactId>easy-agents-chat-openai</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.store.vectorex;
|
||||
|
||||
import com.easyagents.core.document.Document;
|
||||
import com.easyagents.core.store.DocumentStore;
|
||||
import com.easyagents.core.store.SearchWrapper;
|
||||
import com.easyagents.core.store.StoreOptions;
|
||||
import com.easyagents.core.store.StoreResult;
|
||||
import com.easyagents.core.util.CollectionUtil;
|
||||
import com.google.common.collect.Lists;
|
||||
import io.github.javpower.vectorex.keynote.core.DbData;
|
||||
import io.github.javpower.vectorex.keynote.core.VectorData;
|
||||
import io.github.javpower.vectorex.keynote.core.VectorSearchResult;
|
||||
import io.github.javpower.vectorex.keynote.model.MetricType;
|
||||
import io.github.javpower.vectorex.keynote.model.VectorFiled;
|
||||
import io.github.javpower.vectorexcore.VectoRexClient;
|
||||
import io.github.javpower.vectorexcore.entity.KeyValue;
|
||||
import io.github.javpower.vectorexcore.entity.ScalarField;
|
||||
import io.github.javpower.vectorexcore.entity.VectoRexEntity;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class VectoRexStore extends DocumentStore {
|
||||
|
||||
private final VectoRexStoreConfig config;
|
||||
private final VectoRexClient client;
|
||||
private final String defaultCollectionName;
|
||||
private boolean isCreateCollection=false;
|
||||
|
||||
public VectoRexStore(VectoRexStoreConfig config) {
|
||||
this.config = config;
|
||||
this.defaultCollectionName=config.getDefaultCollectionName();
|
||||
this.client = new VectoRexClient(config.getUri());
|
||||
}
|
||||
@Override
|
||||
public StoreResult doStore(List<Document> documents, StoreOptions options) {
|
||||
List<DbData> data=new ArrayList<>();
|
||||
for (Document doc : documents) {
|
||||
Map<String, Object> dict=new HashMap<>();
|
||||
dict.put("id",String.valueOf(doc.getId()));
|
||||
dict.put("content", doc.getContent());
|
||||
dict.put("vector", doc.getVector());
|
||||
DbData dbData=new DbData();
|
||||
dbData.setId(String.valueOf(doc.getId()));
|
||||
dbData.setMetadata(dict);
|
||||
VectorData vd=new VectorData(dbData.getId(),doc.getVector());
|
||||
vd.setName("vector");
|
||||
dbData.setVectorFiled(Lists.newArrayList(vd));
|
||||
data.add(dbData);
|
||||
}
|
||||
String collectionName = options.getCollectionNameOrDefault(defaultCollectionName);
|
||||
if(config.isAutoCreateCollection()&&!isCreateCollection){
|
||||
List<VectoRexEntity> collections = client.getCollections();
|
||||
if(CollectionUtil.noItems(collections)||collections.stream().noneMatch(e -> e.getCollectionName().equals(collectionName))){
|
||||
createCollection(collectionName);
|
||||
}else {
|
||||
isCreateCollection=true;
|
||||
}
|
||||
}
|
||||
if(CollectionUtil.hasItems(data)){
|
||||
client.getStore(collectionName).saveAll(data);
|
||||
}
|
||||
return StoreResult.successWithIds(documents);
|
||||
}
|
||||
|
||||
|
||||
private void createCollection(String collectionName) {
|
||||
VectorFiled vectorFiled = new VectorFiled();
|
||||
vectorFiled.setDimensions(this.getEmbeddingModel().dimensions());
|
||||
vectorFiled.setName("vector");
|
||||
vectorFiled.setMetricType(MetricType.FLOAT_COSINE_DISTANCE);
|
||||
VectoRexEntity entity=new VectoRexEntity();
|
||||
entity.setCollectionName(collectionName);
|
||||
List<KeyValue<String, VectorFiled>> vectorFiles=new ArrayList<>();
|
||||
vectorFiles.add(new KeyValue<>("vector",vectorFiled));
|
||||
List<KeyValue<String, ScalarField>> scalarFields=new ArrayList<>();
|
||||
ScalarField id = new ScalarField();
|
||||
id.setName("id");
|
||||
id.setIsPrimaryKey(true);
|
||||
scalarFields.add(new KeyValue<>("id",id));
|
||||
ScalarField content = new ScalarField();
|
||||
content.setName("content");
|
||||
content.setIsPrimaryKey(false);
|
||||
scalarFields.add(new KeyValue<>("content",content));
|
||||
entity.setVectorFileds(vectorFiles);
|
||||
entity.setScalarFields(scalarFields);
|
||||
client.createCollection(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StoreResult doDelete(Collection<?> ids, StoreOptions options) {
|
||||
client.getStore(options.getCollectionNameOrDefault(defaultCollectionName)).deleteAll((List<String>) ids);
|
||||
return StoreResult.success();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Document> doSearch(SearchWrapper searchWrapper, StoreOptions options) {
|
||||
List<VectorSearchResult> data = client.getStore(options.getCollectionNameOrDefault(defaultCollectionName)).search("vector", searchWrapper.getVectorAsList(), searchWrapper.getMaxResults(), null);
|
||||
List<Document> documents = new ArrayList<>();
|
||||
for (VectorSearchResult result : data) {
|
||||
DbData dd = result.getData();
|
||||
Map<String, Object> metadata = dd.getMetadata();
|
||||
Document doc=new Document();
|
||||
doc.setId(result.getId());
|
||||
doc.setContent((String) metadata.get("content"));
|
||||
Object vectorObj = metadata.get("vector");
|
||||
if (vectorObj instanceof List) {
|
||||
//noinspection unchecked
|
||||
doc.setVector((List<Float>) vectorObj);
|
||||
}
|
||||
documents.add(doc);
|
||||
}
|
||||
return documents;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StoreResult doUpdate(List<Document> documents, StoreOptions options) {
|
||||
if (documents == null || documents.isEmpty()) {
|
||||
return StoreResult.success();
|
||||
}
|
||||
for (Document doc : documents) {
|
||||
Map<String, Object> dict=new HashMap<>();
|
||||
dict.put("id",String.valueOf(doc.getId()));
|
||||
dict.put("content", doc.getContent());
|
||||
dict.put("vector", doc.getVector());
|
||||
DbData dbData=new DbData();
|
||||
dbData.setId(String.valueOf(doc.getId()));
|
||||
dbData.setMetadata(dict);
|
||||
VectorData vd=new VectorData(dbData.getId(),doc.getVector());
|
||||
vd.setName("vector");
|
||||
dbData.setVectorFiled(Lists.newArrayList(vd));
|
||||
client.getStore(options.getCollectionNameOrDefault(defaultCollectionName)).update(dbData);
|
||||
}
|
||||
return StoreResult.successWithIds(documents);
|
||||
}
|
||||
|
||||
public VectoRexClient getClient() {
|
||||
return client;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.store.vectorex;
|
||||
|
||||
import com.easyagents.core.store.DocumentStoreConfig;
|
||||
import com.easyagents.core.util.StringUtil;
|
||||
|
||||
|
||||
public class VectoRexStoreConfig implements DocumentStoreConfig {
|
||||
|
||||
private String uri;
|
||||
private String defaultCollectionName;
|
||||
private boolean autoCreateCollection = true;
|
||||
|
||||
public String getUri() {
|
||||
return uri;
|
||||
}
|
||||
|
||||
public void setUri(String uri) {
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
|
||||
public String getDefaultCollectionName() {
|
||||
return defaultCollectionName;
|
||||
}
|
||||
|
||||
public void setDefaultCollectionName(String defaultCollectionName) {
|
||||
this.defaultCollectionName = defaultCollectionName;
|
||||
}
|
||||
|
||||
public boolean isAutoCreateCollection() {
|
||||
return autoCreateCollection;
|
||||
}
|
||||
|
||||
public void setAutoCreateCollection(boolean autoCreateCollection) {
|
||||
this.autoCreateCollection = autoCreateCollection;
|
||||
}
|
||||
@Override
|
||||
public boolean checkAvailable() {
|
||||
return StringUtil.hasText(this.uri);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* <p>
|
||||
* 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 com.easyagents.store.vectorex.test;
|
||||
|
||||
import com.easyagents.core.document.Document;
|
||||
import com.easyagents.core.model.chat.ChatModel;
|
||||
import com.easyagents.core.store.SearchWrapper;
|
||||
import com.easyagents.core.store.StoreResult;
|
||||
|
||||
import com.easyagents.llm.openai.OpenAIChatModel;
|
||||
import com.easyagents.llm.openai.OpenAIChatConfig;
|
||||
import com.easyagents.store.vectorex.VectoRexStore;
|
||||
import com.easyagents.store.vectorex.VectoRexStoreConfig;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
|
||||
OpenAIChatConfig openAILlmConfig= new OpenAIChatConfig();
|
||||
openAILlmConfig.setApiKey("");
|
||||
openAILlmConfig.setEndpoint("");
|
||||
openAILlmConfig.setModel("");
|
||||
|
||||
ChatModel chatModel = new OpenAIChatModel(openAILlmConfig);
|
||||
|
||||
|
||||
VectoRexStoreConfig config = new VectoRexStoreConfig();
|
||||
config.setDefaultCollectionName("test05");
|
||||
VectoRexStore store = new VectoRexStore(config);
|
||||
// store.setEmbeddingModel(chatModel);
|
||||
|
||||
Document document = new Document();
|
||||
document.setContent("你好");
|
||||
document.setId(1);
|
||||
store.store(document);
|
||||
|
||||
SearchWrapper sw = new SearchWrapper();
|
||||
sw.setText("你好");
|
||||
|
||||
List<Document> search = store.search(sw);
|
||||
System.out.println(search);
|
||||
|
||||
|
||||
StoreResult result = store.delete("1");
|
||||
System.out.println("-------delete-----" + result);
|
||||
search = store.search(sw);
|
||||
System.out.println(search);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user