初始化

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,27 @@
<?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-embedding</artifactId>
<version>${revision}</version>
</parent>
<name>easy-agents-embedding-ollama</name>
<artifactId>easy-agents-embedding-ollama</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>
</dependencies>
</project>

View File

@@ -0,0 +1,34 @@
/*
* 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.embedding.ollama;
import com.easyagents.core.model.config.BaseModelConfig;
public class OllamaEmbeddingConfig extends BaseModelConfig {
private static final String DEFAULT_EMBEDDING_MODEL = "text-embedding-ada-002";
private static final String DEFAULT_ENDPOINT = "https://api.openai.com";
private static final String DEFAULT_REQUEST_PATH = "/v1/embeddings";
public OllamaEmbeddingConfig() {
super();
this.setModel(DEFAULT_EMBEDDING_MODEL);
this.setEndpoint(DEFAULT_ENDPOINT);
this.setRequestPath(DEFAULT_REQUEST_PATH);
}
}

View File

@@ -0,0 +1,92 @@
/*
* 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.embedding.ollama;
import com.easyagents.core.document.Document;
import com.easyagents.core.model.client.HttpClient;
import com.easyagents.core.model.embedding.BaseEmbeddingModel;
import com.easyagents.core.model.embedding.EmbeddingOptions;
import com.easyagents.core.model.exception.ModelException;
import com.easyagents.core.store.VectorData;
import com.easyagents.core.util.JSONUtil;
import com.easyagents.core.util.Maps;
import com.easyagents.core.util.StringUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class OllamaEmbeddingModel extends BaseEmbeddingModel<OllamaEmbeddingConfig> {
private HttpClient httpClient = new HttpClient();
public OllamaEmbeddingModel(OllamaEmbeddingConfig config) {
super(config);
}
public HttpClient getHttpClient() {
return httpClient;
}
public void setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
}
@Override
public VectorData embed(Document document, EmbeddingOptions options) {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
if (StringUtil.hasText(getConfig().getApiKey())) {
headers.put("Authorization", "Bearer " + getConfig().getApiKey());
}
String payload = Maps.of("model", options.getModelOrDefault(config.getModel()))
.set("input", document.getContent())
.setIfNotEmpty("dimensions", options.getDimensions())
.toJSON();
String endpoint = config.getEndpoint();
// https://github.com/ollama/ollama/blob/main/docs/api.md#generate-embeddings
String response = httpClient.post(endpoint + "/api/embed", headers, payload);
if (StringUtil.noText(response)) {
throw new ModelException("response is null or empty.");
}
JSONObject jsonObject = JSON.parseObject(response);
String errorMessage = JSONUtil.detectErrorMessage(jsonObject);
if (errorMessage != null) {
throw new ModelException(errorMessage);
}
VectorData vectorData = new VectorData();
double[] embedding = JSONUtil.readDoubleArray(jsonObject, "$.embeddings[0]");
vectorData.setVector(embedding);
vectorData.addMetadata("total_duration", JSONUtil.readLong(jsonObject, "$.total_duration"));
vectorData.addMetadata("load_duration", JSONUtil.readLong(jsonObject, "$.load_duration"));
vectorData.addMetadata("prompt_eval_count", JSONUtil.readInteger(jsonObject, "$.prompt_eval_count"));
vectorData.addMetadata("model", JSONUtil.readString(jsonObject, "$.model"));
return vectorData;
}
}

View File

@@ -0,0 +1,27 @@
<?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-embedding</artifactId>
<version>${revision}</version>
</parent>
<name>easy-agents-embedding-openai</name>
<artifactId>easy-agents-embedding-openai</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>
</dependencies>
</project>

View File

@@ -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.embedding.openai;
import com.easyagents.core.model.config.BaseModelConfig;
public class OpenAIEmbeddingConfig extends BaseModelConfig {
private static final String DEFAULT_EMBEDDING_MODEL = "text-embedding-ada-002";
private static final String DEFAULT_ENDPOINT = "https://api.openai.com";
private static final String DEFAULT_REQUEST_PATH = "/v1/embeddings";
public OpenAIEmbeddingConfig() {
super();
this.setModel(DEFAULT_EMBEDDING_MODEL);
this.setEndpoint(DEFAULT_ENDPOINT);
this.setRequestPath(DEFAULT_REQUEST_PATH);
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private String model = DEFAULT_EMBEDDING_MODEL;
private String endpoint = DEFAULT_ENDPOINT;
private String requestPath = DEFAULT_REQUEST_PATH;
private String apiKey;
public Builder model(String model) {
this.model = model;
return this;
}
public Builder endpoint(String endpoint) {
this.endpoint = endpoint;
return this;
}
public Builder requestPath(String requestPath) {
this.requestPath = requestPath;
return this;
}
public Builder apiKey(String apiKey) {
this.apiKey = apiKey;
return this;
}
public OpenAIEmbeddingConfig build() {
OpenAIEmbeddingConfig config = new OpenAIEmbeddingConfig();
config.setModel(this.model);
config.setEndpoint(this.endpoint);
config.setRequestPath(this.requestPath);
if (this.apiKey != null) {
config.setApiKey(this.apiKey);
}
return config;
}
}
}

View File

@@ -0,0 +1,87 @@
/*
* 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.embedding.openai;
import com.easyagents.core.document.Document;
import com.easyagents.core.model.client.HttpClient;
import com.easyagents.core.model.embedding.BaseEmbeddingModel;
import com.easyagents.core.model.embedding.EmbeddingOptions;
import com.easyagents.core.model.exception.ModelException;
import com.easyagents.core.store.VectorData;
import com.easyagents.core.util.JSONUtil;
import com.easyagents.core.util.Maps;
import com.easyagents.core.util.StringUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class OpenAIEmbeddingModel extends BaseEmbeddingModel<OpenAIEmbeddingConfig> {
private HttpClient httpClient = new HttpClient();
public OpenAIEmbeddingModel(OpenAIEmbeddingConfig config) {
super(config);
}
public HttpClient getHttpClient() {
return httpClient;
}
public void setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
}
@Override
public VectorData embed(Document document, EmbeddingOptions options) {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer " + getConfig().getApiKey());
String payload = promptToEmbeddingsPayload(document, options, config);
String endpoint = config.getEndpoint();
// https://platform.openai.com/docs/api-reference/embeddings/create
String response = httpClient.post(endpoint + config.getRequestPath(), headers, payload);
if (StringUtil.noText(response)) {
throw new ModelException("response is null or empty.");
}
JSONObject jsonObject = JSON.parseObject(response);
String errorMessage = JSONUtil.detectErrorMessage(jsonObject);
if (errorMessage != null) {
throw new ModelException(errorMessage);
}
VectorData vectorData = new VectorData();
double[] embedding = JSONUtil.readDoubleArray(jsonObject, "$.data[0].embedding");
vectorData.setVector(embedding);
return vectorData;
}
public static String promptToEmbeddingsPayload(Document text, EmbeddingOptions options, OpenAIEmbeddingConfig config) {
// https://platform.openai.com/docs/api-reference/making-requests
return Maps.of("model", options.getModelOrDefault(config.getModel()))
.set("encoding_format", options.getEncodingFormatOrDefault("float"))
.set("input", text.getContent())
.setIfNotEmpty("user", options.getUser())
.setIfNotEmpty("dimensions", options.getDimensions())
.toJSON();
}
}

View File

@@ -0,0 +1,27 @@
<?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-embedding</artifactId>
<version>${revision}</version>
</parent>
<name>easy-agents-embedding-qwen</name>
<artifactId>easy-agents-embedding-qwen</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>
</dependencies>
</project>

View File

@@ -0,0 +1,33 @@
/*
* 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.embedding.qwen;
import com.easyagents.core.model.config.BaseModelConfig;
public class QwenEmbeddingConfig extends BaseModelConfig {
private static final String DEFAULT_EMBEDDING_MODEL = "text-embedding-v1";
private static final String DEFAULT_ENDPOINT = "https://dashscope.aliyuncs.com";
private static final String DEFAULT_REQUEST_PATH = "/compatible-mode/v1/embeddings";
public QwenEmbeddingConfig() {
super();
this.setModel(DEFAULT_EMBEDDING_MODEL);
this.setEndpoint(DEFAULT_ENDPOINT);
this.setRequestPath(DEFAULT_REQUEST_PATH);
}
}

View File

@@ -0,0 +1,87 @@
/*
* 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.embedding.qwen;
import com.easyagents.core.document.Document;
import com.easyagents.core.model.client.HttpClient;
import com.easyagents.core.model.embedding.BaseEmbeddingModel;
import com.easyagents.core.model.embedding.EmbeddingOptions;
import com.easyagents.core.model.exception.ModelException;
import com.easyagents.core.store.VectorData;
import com.easyagents.core.util.JSONUtil;
import com.easyagents.core.util.Maps;
import com.easyagents.core.util.StringUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class QwenEmbeddingModel extends BaseEmbeddingModel<QwenEmbeddingConfig> {
private HttpClient httpClient = new HttpClient();
public QwenEmbeddingModel(QwenEmbeddingConfig config) {
super(config);
}
public HttpClient getHttpClient() {
return httpClient;
}
public void setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
}
@Override
public VectorData embed(Document document, EmbeddingOptions options) {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer " + getConfig().getApiKey());
String payload = promptToEmbeddingsPayload(document, options, config);
String endpoint = config.getEndpoint();
// https://platform.openai.com/docs/api-reference/embeddings/create
String response = httpClient.post(endpoint + config.getRequestPath(), headers, payload);
if (StringUtil.noText(response)) {
throw new ModelException("response is null or empty.");
}
JSONObject jsonObject = JSON.parseObject(response);
String errorMessage = JSONUtil.detectErrorMessage(jsonObject);
if (errorMessage != null) {
throw new ModelException(errorMessage);
}
VectorData vectorData = new VectorData();
double[] embedding = JSONUtil.readDoubleArray(jsonObject, "$.data[0].embedding");
vectorData.setVector(embedding);
return vectorData;
}
public static String promptToEmbeddingsPayload(Document text, EmbeddingOptions options, QwenEmbeddingConfig config) {
//https://help.aliyun.com/zh/model-studio/developer-reference/embedding-interfaces-compatible-with-openai?spm=a2c4g.11186623.0.i3
return Maps.of("model", options.getModelOrDefault(config.getModel()))
.set("encoding_format", options.getEncodingFormatOrDefault("float"))
.set("input", text.getContent())
.setIfNotEmpty("user", options.getUser())
.setIfNotEmpty("dimensions", options.getDimensions())
.toJSON();
}
}

View File

@@ -0,0 +1,27 @@
<?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-parent</artifactId>
<version>${revision}</version>
</parent>
<name>easy-agents-embedding</name>
<artifactId>easy-agents-embedding</artifactId>
<packaging>pom</packaging>
<modules>
<module>easy-agents-embedding-openai</module>
<module>easy-agents-embedding-qwen</module>
<module>easy-agents-embedding-ollama</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>