初始化

This commit is contained in:
2026-02-22 18:55:40 +08:00
commit 8392cdd861
496 changed files with 45020 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
<?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-rerank</artifactId>
<version>${revision}</version>
</parent>
<name>easy-agents-rerank-default</name>
<artifactId>easy-agents-rerank-default</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>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,130 @@
/*
* 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.rerank;
import com.easyagents.core.document.Document;
import com.easyagents.core.model.client.HttpClient;
import com.easyagents.core.model.rerank.BaseRerankModel;
import com.easyagents.core.model.rerank.RerankException;
import com.easyagents.core.model.rerank.RerankOptions;
import com.easyagents.core.util.Maps;
import com.easyagents.core.util.StringUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONPath;
import java.util.*;
public class DefaultRerankModel extends BaseRerankModel<DefaultRerankModelConfig> {
private HttpClient httpClient = new HttpClient();
public DefaultRerankModel(DefaultRerankModelConfig config) {
super(config);
}
public HttpClient getHttpClient() {
return httpClient;
}
public void setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
}
@Override
public List<Document> rerank(String query, List<Document> documents, RerankOptions options) {
DefaultRerankModelConfig config = getConfig();
String url = config.getEndpoint() + config.getRequestPath();
Map<String, String> headers = new HashMap<>(2);
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer " + config.getApiKey());
List<String> payloadDocuments = new ArrayList<>(documents.size());
for (Document document : documents) {
payloadDocuments.add(document.getContent());
}
String payload = Maps.of("model", options.getModelOrDefault(config.getModel()))
.set("query", query)
.set("documents", payloadDocuments)
.toJSON();
String response = httpClient.post(url, headers, payload);
if (StringUtil.noText(response)) {
throw new RerankException("empty response");
}
//{
// "model": "Qwen3-Reranker-4B",
// "usage": {
// "totalTokens": 0,
// "promptTokens": 0
// },
// "results": [
// {
// "index": 0,
// "document": {
// "text": "Use pandas: `import pandas as pd; df = pd.read_csv('data.csv')`"
// },
// "relevance_score": 0.95654296875
// },
// {
// "index": 3,
// "document": {
// "text": "CSV means Comma Separated Values. Python files can be opened using read() method."
// },
// "relevance_score": 0.822265625
// },
// {
// "index": 1,
// "document": {
// "text": "You can read CSV files with numpy.loadtxt()"
// },
// "relevance_score": 0.310791015625
// },
// {
// "index": 2,
// "document": {
// "text": "To write JSON files, use json.dump() in Python"
// },
// "relevance_score": 0.00009608268737792969
// }
// ]
//}
JSONObject jsonObject = JSON.parseObject(response);
JSONArray results = (JSONArray) JSONPath.eval(jsonObject, config.getResultsJsonPath());
if (results == null || results.isEmpty()) {
throw new RerankException("empty results");
}
for (int i = 0; i < results.size(); i++) {
JSONObject result = results.getJSONObject(i);
int index = result.getIntValue(config.getIndexJsonKey());
Document document = documents.get(index);
document.setScore(result.getDoubleValue(config.getScoreJsonKey()));
}
// 对 documents 排序, score 越大的越靠前
documents.sort(Comparator.comparingDouble(Document::getScore).reversed());
return documents;
}
}

View File

@@ -0,0 +1,51 @@
/*
* 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.rerank;
import com.easyagents.core.model.config.BaseModelConfig;
public class DefaultRerankModelConfig extends BaseModelConfig {
private String resultsJsonPath = "$.results";
private String indexJsonKey = "index";
private String scoreJsonKey = "relevance_score";
public String getResultsJsonPath() {
return resultsJsonPath;
}
public void setResultsJsonPath(String resultsJsonPath) {
this.resultsJsonPath = resultsJsonPath;
}
public String getIndexJsonKey() {
return indexJsonKey;
}
public void setIndexJsonKey(String indexJsonKey) {
this.indexJsonKey = indexJsonKey;
}
public String getScoreJsonKey() {
return scoreJsonKey;
}
public void setScoreJsonKey(String scoreJsonKey) {
this.scoreJsonKey = scoreJsonKey;
}
}

View File

@@ -0,0 +1,51 @@
/*
* 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.rereank.gitee;
import com.easyagents.core.document.Document;
import com.easyagents.core.model.rerank.RerankException;
import com.easyagents.rerank.DefaultRerankModel;
import com.easyagents.rerank.DefaultRerankModelConfig;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class DefaultRerankModelTest {
@Test(expected = RerankException.class)
public void testRerank() {
DefaultRerankModelConfig config = new DefaultRerankModelConfig();
config.setEndpoint("https://ai.gitee.com");
config.setRequestPath("/v1/rerank");
config.setModel("Qwen3-Reranker-8B");
config.setApiKey("*****");
DefaultRerankModel model = new DefaultRerankModel(config);
List<Document> documents = new ArrayList<>();
documents.add(Document.of("Paris is the capital of France."));
documents.add(Document.of("London is the capital of England."));
documents.add(Document.of("Tokyo is the capital of Japan."));
documents.add(Document.of("Beijing is the capital of China."));
documents.add(Document.of("Washington, D.C. is the capital of the United States."));
documents.add(Document.of("Moscow is the capital of Russia."));
List<Document> rerank = model.rerank("What is the capital of France?", documents);
System.out.println(rerank);
}
}