初始化
This commit is contained in:
28
easy-agents-store/easy-agents-store-qcloud/pom.xml
Normal file
28
easy-agents-store/easy-agents-store-qcloud/pom.xml
Normal file
@@ -0,0 +1,28 @@
|
||||
<?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-qcloud</name>
|
||||
<artifactId>easy-agents-store-qcloud</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>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
* 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.qcloud;
|
||||
|
||||
import com.easyagents.core.document.Document;
|
||||
import com.easyagents.core.model.client.HttpClient;
|
||||
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.StringUtil;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* doc https://cloud.tencent.com/document/product/1709/95121
|
||||
*/
|
||||
public class QCloudVectorStore extends DocumentStore {
|
||||
|
||||
private QCloudVectorStoreConfig config;
|
||||
|
||||
private final HttpClient httpUtil = new HttpClient();
|
||||
|
||||
|
||||
public QCloudVectorStore(QCloudVectorStoreConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public StoreResult doStore(List<Document> documents, StoreOptions options) {
|
||||
if (documents == null || documents.isEmpty()) {
|
||||
return StoreResult.success();
|
||||
}
|
||||
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Content-Type", "application/json");
|
||||
headers.put("Authorization", "Bearer account=" + config.getAccount() + "&api_key=" + config.getApiKey());
|
||||
|
||||
Map<String, Object> payloadMap = new HashMap<>();
|
||||
payloadMap.put("database", config.getDatabase());
|
||||
payloadMap.put("collection", options.getCollectionNameOrDefault(config.getDefaultCollectionName()));
|
||||
|
||||
|
||||
List<Map<String, Object>> payloadDocs = new ArrayList<>();
|
||||
for (Document vectorDocument : documents) {
|
||||
Map<String, Object> document = new HashMap<>();
|
||||
if (vectorDocument.getMetadataMap() != null) {
|
||||
document.putAll(vectorDocument.getMetadataMap());
|
||||
}
|
||||
document.put("vector", vectorDocument.getVector());
|
||||
document.put("id", vectorDocument.getId());
|
||||
payloadDocs.add(document);
|
||||
}
|
||||
payloadMap.put("documents", payloadDocs);
|
||||
|
||||
String payload = JSON.toJSONString(payloadMap);
|
||||
httpUtil.post(config.getHost() + "/document/upsert", headers, payload);
|
||||
return StoreResult.successWithIds(documents);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public StoreResult doDelete(Collection<?> ids, StoreOptions options) {
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Content-Type", "application/json");
|
||||
headers.put("Authorization", "Bearer account=" + config.getAccount() + "&api_key=" + config.getApiKey());
|
||||
|
||||
Map<String, Object> payloadMap = new HashMap<>();
|
||||
payloadMap.put("database", config.getDatabase());
|
||||
payloadMap.put("collection", options.getCollectionNameOrDefault(config.getDefaultCollectionName()));
|
||||
|
||||
Map<String, Object> documentIdsObj = new HashMap<>();
|
||||
documentIdsObj.put("documentIds", ids);
|
||||
payloadMap.put("query", documentIdsObj);
|
||||
|
||||
String payload = JSON.toJSONString(payloadMap);
|
||||
|
||||
httpUtil.post(config.getHost() + "/document/delete", headers, payload);
|
||||
|
||||
return StoreResult.success();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public StoreResult doUpdate(List<Document> documents, StoreOptions options) {
|
||||
if (documents == null || documents.isEmpty()) {
|
||||
return StoreResult.success();
|
||||
}
|
||||
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Content-Type", "application/json");
|
||||
headers.put("Authorization", "Bearer account=" + config.getAccount() + "&api_key=" + config.getApiKey());
|
||||
|
||||
Map<String, Object> payloadMap = new HashMap<>();
|
||||
payloadMap.put("database", config.getDatabase());
|
||||
payloadMap.put("collection", options.getCollectionNameOrDefault(config.getDefaultCollectionName()));
|
||||
|
||||
for (Document document : documents) {
|
||||
Map<String, Object> documentIdsObj = new HashMap<>();
|
||||
documentIdsObj.put("documentIds", Collections.singletonList(document.getId()));
|
||||
payloadMap.put("query", documentIdsObj);
|
||||
payloadMap.put("update", document.getMetadataMap());
|
||||
String payload = JSON.toJSONString(payloadMap);
|
||||
httpUtil.post(config.getHost() + "/document/update", headers, payload);
|
||||
}
|
||||
|
||||
return StoreResult.successWithIds(documents);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Document> doSearch(SearchWrapper searchWrapper, StoreOptions options) {
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Content-Type", "application/json");
|
||||
headers.put("Authorization", "Bearer account=" + config.getAccount() + "&api_key=" + config.getApiKey());
|
||||
Map<String, Object> payloadMap = new HashMap<>();
|
||||
payloadMap.put("database", config.getDatabase());
|
||||
payloadMap.put("collection", options.getCollectionNameOrDefault(config.getDefaultCollectionName()));
|
||||
|
||||
Map<String, Object> searchMap = new HashMap<>();
|
||||
searchMap.put("vectors", Collections.singletonList(searchWrapper.getVector()));
|
||||
|
||||
if (searchWrapper.getMaxResults() != null) {
|
||||
searchMap.put("limit", searchWrapper.getMaxResults());
|
||||
}
|
||||
|
||||
payloadMap.put("search", searchMap);
|
||||
|
||||
|
||||
String payload = JSON.toJSONString(payloadMap);
|
||||
|
||||
// https://cloud.tencent.com/document/product/1709/95123
|
||||
String response = httpUtil.post(config.getHost() + "/document/search", headers, payload);
|
||||
if (StringUtil.noText(response)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<Document> result = new ArrayList<>();
|
||||
JSONObject rootObject = JSON.parseObject(response);
|
||||
int code = rootObject.getIntValue("code");
|
||||
if (code != 0) {
|
||||
LoggerFactory.getLogger(QCloudVectorStore.class).error("can not search in QCloudVectorStore, code:" + code + ", message: " + rootObject.getString("msg"));
|
||||
return null;
|
||||
}
|
||||
|
||||
JSONArray rootDocs = rootObject.getJSONArray("documents");
|
||||
for (int i = 0; i < rootDocs.size(); i++) {
|
||||
JSONArray docs = rootDocs.getJSONArray(i);
|
||||
for (int j = 0; j < docs.size(); j++) {
|
||||
JSONObject doc = docs.getJSONObject(j);
|
||||
Document vd = new Document();
|
||||
vd.setId(doc.getString("id"));
|
||||
doc.remove("id");
|
||||
vd.addMetadata(doc);
|
||||
result.add(vd);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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.qcloud;
|
||||
|
||||
import com.easyagents.core.store.DocumentStoreConfig;
|
||||
import com.easyagents.core.util.StringUtil;
|
||||
|
||||
public class QCloudVectorStoreConfig implements DocumentStoreConfig {
|
||||
|
||||
private String host;
|
||||
private String apiKey;
|
||||
private String account;
|
||||
private String database;
|
||||
|
||||
private String defaultCollectionName;
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public String getApiKey() {
|
||||
return apiKey;
|
||||
}
|
||||
|
||||
public void setApiKey(String apiKey) {
|
||||
this.apiKey = apiKey;
|
||||
}
|
||||
|
||||
public String getAccount() {
|
||||
return account;
|
||||
}
|
||||
|
||||
public void setAccount(String account) {
|
||||
this.account = account;
|
||||
}
|
||||
|
||||
public String getDatabase() {
|
||||
return database;
|
||||
}
|
||||
|
||||
public void setDatabase(String database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
public String getDefaultCollectionName() {
|
||||
return defaultCollectionName;
|
||||
}
|
||||
|
||||
public void setDefaultCollectionName(String defaultCollectionName) {
|
||||
this.defaultCollectionName = defaultCollectionName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkAvailable() {
|
||||
return StringUtil.hasText(this.host, this.apiKey, this.account, this.database, this.defaultCollectionName);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user