|
| 1 | +/** |
| 2 | + * Copyright (C) 2024 AIDC-AI |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.alibaba.langengine.arangodb; |
| 17 | + |
| 18 | +import com.alibaba.langengine.core.util.WorkPropertiesUtils; |
| 19 | +import lombok.Data; |
| 20 | + |
| 21 | + |
| 22 | +@Data |
| 23 | +public class ArangoDBConfiguration { |
| 24 | + |
| 25 | + /** |
| 26 | + * ArangoDB 连接主机 |
| 27 | + */ |
| 28 | + public static final String ARANGODB_HOST = WorkPropertiesUtils.get("arangodb_host", "localhost"); |
| 29 | + |
| 30 | + /** |
| 31 | + * ArangoDB 连接端口 |
| 32 | + */ |
| 33 | + public static final int ARANGODB_PORT = Integer.parseInt(WorkPropertiesUtils.get("arangodb_port", "8529")); |
| 34 | + |
| 35 | + /** |
| 36 | + * ArangoDB 用户名 |
| 37 | + */ |
| 38 | + public static final String ARANGODB_USERNAME = WorkPropertiesUtils.get("arangodb_username", "root"); |
| 39 | + |
| 40 | + /** |
| 41 | + * ArangoDB 密码 |
| 42 | + */ |
| 43 | + public static final String ARANGODB_PASSWORD = WorkPropertiesUtils.get("arangodb_password", ""); |
| 44 | + |
| 45 | + /** |
| 46 | + * ArangoDB 数据库名称 |
| 47 | + */ |
| 48 | + public static final String ARANGODB_DATABASE = WorkPropertiesUtils.get("arangodb_database", "langengine"); |
| 49 | + |
| 50 | + /** |
| 51 | + * 默认向量维度 |
| 52 | + */ |
| 53 | + public static final int DEFAULT_VECTOR_DIMENSION = 768; |
| 54 | + |
| 55 | + /** |
| 56 | + * 默认相似度阈值 |
| 57 | + */ |
| 58 | + public static final double DEFAULT_SIMILARITY_THRESHOLD = 0.7; |
| 59 | + |
| 60 | + /** |
| 61 | + * 默认批处理大小 |
| 62 | + */ |
| 63 | + public static final int DEFAULT_BATCH_SIZE = 100; |
| 64 | + |
| 65 | + /** |
| 66 | + * 默认最大缓存大小 |
| 67 | + */ |
| 68 | + public static final int DEFAULT_MAX_CACHE_SIZE = 1000; |
| 69 | + |
| 70 | + /** |
| 71 | + * 连接超时时间(毫秒) |
| 72 | + */ |
| 73 | + public static final int DEFAULT_TIMEOUT_MS = 30000; |
| 74 | + |
| 75 | + /** |
| 76 | + * 默认相似度距离函数 |
| 77 | + */ |
| 78 | + public static final String DEFAULT_DISTANCE_FUNCTION = "COSINE"; |
| 79 | + |
| 80 | + /** |
| 81 | + * 默认集合名称前缀 |
| 82 | + */ |
| 83 | + public static final String DEFAULT_COLLECTION_PREFIX = "vectors_"; |
| 84 | + |
| 85 | + /** |
| 86 | + * 默认索引名称后缀 |
| 87 | + */ |
| 88 | + public static final String DEFAULT_INDEX_SUFFIX = "_vector_idx"; |
| 89 | + |
| 90 | + // 实例配置字段 |
| 91 | + private String host = ARANGODB_HOST; |
| 92 | + private int port = ARANGODB_PORT; |
| 93 | + private String username = ARANGODB_USERNAME; |
| 94 | + private String password = ARANGODB_PASSWORD; |
| 95 | + private String database = ARANGODB_DATABASE; |
| 96 | + private int vectorDimension = DEFAULT_VECTOR_DIMENSION; |
| 97 | + private double similarityThreshold = DEFAULT_SIMILARITY_THRESHOLD; |
| 98 | + private int batchSize = DEFAULT_BATCH_SIZE; |
| 99 | + private int maxCacheSize = DEFAULT_MAX_CACHE_SIZE; |
| 100 | + private int timeoutMs = DEFAULT_TIMEOUT_MS; |
| 101 | + private String distanceFunction = DEFAULT_DISTANCE_FUNCTION; |
| 102 | + |
| 103 | + /** |
| 104 | + * 构造函数 |
| 105 | + */ |
| 106 | + public ArangoDBConfiguration() {} |
| 107 | + |
| 108 | + /** |
| 109 | + * 带参数的构造函数 |
| 110 | + */ |
| 111 | + public ArangoDBConfiguration(String host, int port, String username, String password, String database) { |
| 112 | + this.host = host; |
| 113 | + this.port = port; |
| 114 | + this.username = username; |
| 115 | + this.password = password; |
| 116 | + this.database = database; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * 验证配置 |
| 121 | + */ |
| 122 | + public void validate() { |
| 123 | + if (host == null || host.trim().isEmpty()) { |
| 124 | + throw new IllegalArgumentException("ArangoDB host cannot be null or empty"); |
| 125 | + } |
| 126 | + if (port <= 0 || port > 65535) { |
| 127 | + throw new IllegalArgumentException("ArangoDB port must be between 1 and 65535"); |
| 128 | + } |
| 129 | + if (username == null || username.trim().isEmpty()) { |
| 130 | + throw new IllegalArgumentException("ArangoDB username cannot be null or empty"); |
| 131 | + } |
| 132 | + if (database == null || database.trim().isEmpty()) { |
| 133 | + throw new IllegalArgumentException("ArangoDB database cannot be null or empty"); |
| 134 | + } |
| 135 | + if (vectorDimension <= 0) { |
| 136 | + throw new IllegalArgumentException("Vector dimension must be positive"); |
| 137 | + } |
| 138 | + if (similarityThreshold < 0.0 || similarityThreshold > 1.0) { |
| 139 | + throw new IllegalArgumentException("Similarity threshold must be between 0.0 and 1.0"); |
| 140 | + } |
| 141 | + if (batchSize <= 0) { |
| 142 | + throw new IllegalArgumentException("Batch size must be positive"); |
| 143 | + } |
| 144 | + if (maxCacheSize <= 0) { |
| 145 | + throw new IllegalArgumentException("Max cache size must be positive"); |
| 146 | + } |
| 147 | + if (timeoutMs <= 0) { |
| 148 | + throw new IllegalArgumentException("Timeout must be positive"); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments