chunk-config.service.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. import { Injectable, Logger, BadRequestException } from '@nestjs/common';
  2. import { ConfigService } from '@nestjs/config';
  3. import { ModelConfigService } from '../model-config/model-config.service';
  4. import { TenantService } from '../tenant/tenant.service';
  5. // import { UserSettingService } from '../user-setting/user-setting.service';
  6. /**
  7. * チャンク設定サービス
  8. * チャンクパラメータの検証と管理を担当し、モデルの制限や環境変数の設定に適合していることを確認します
  9. *
  10. * 制限の優先順位:
  11. * 1. 環境変数 (MAX_CHUNK_SIZE, MAX_OVERLAP_SIZE)
  12. * 2. データベース内のモデル設定 (maxInputTokens, maxBatchSize)
  13. * 3. デフォルト値
  14. */
  15. import {
  16. DEFAULT_CHUNK_SIZE,
  17. MIN_CHUNK_SIZE,
  18. DEFAULT_CHUNK_OVERLAP,
  19. MIN_CHUNK_OVERLAP,
  20. DEFAULT_MAX_OVERLAP_RATIO,
  21. DEFAULT_MAX_BATCH_SIZE,
  22. DEFAULT_VECTOR_DIMENSIONS
  23. } from '../common/constants';
  24. import { I18nService } from '../i18n/i18n.service';
  25. @Injectable()
  26. export class ChunkConfigService {
  27. private readonly logger = new Logger(ChunkConfigService.name);
  28. // デフォルト設定
  29. private readonly DEFAULTS = {
  30. chunkSize: DEFAULT_CHUNK_SIZE,
  31. chunkOverlap: DEFAULT_CHUNK_OVERLAP,
  32. minChunkSize: MIN_CHUNK_SIZE,
  33. minChunkOverlap: MIN_CHUNK_OVERLAP,
  34. maxOverlapRatio: DEFAULT_MAX_OVERLAP_RATIO, // 重なりはチャンクサイズの50%まで
  35. maxBatchSize: DEFAULT_MAX_BATCH_SIZE, // デフォルトのバッチ制限
  36. expectedDimensions: DEFAULT_VECTOR_DIMENSIONS, // デフォルトのベクトル次元
  37. };
  38. // 環境変数で設定された上限(優先的に使用)
  39. private readonly envMaxChunkSize: number;
  40. private readonly envMaxOverlapSize: number;
  41. constructor(
  42. private configService: ConfigService,
  43. private modelConfigService: ModelConfigService,
  44. private i18nService: I18nService,
  45. private tenantService: TenantService,
  46. ) {
  47. // 環境変数からグローバルな上限設定を読み込む
  48. this.envMaxChunkSize = parseInt(
  49. this.configService.get<string>('MAX_CHUNK_SIZE', '8191')
  50. );
  51. this.envMaxOverlapSize = parseInt(
  52. this.configService.get<string>('MAX_OVERLAP_SIZE', '2000')
  53. );
  54. this.logger.log(
  55. `環境変数設定の上限: MAX_CHUNK_SIZE=${this.envMaxChunkSize}, MAX_OVERLAP_SIZE=${this.envMaxOverlapSize}`
  56. );
  57. }
  58. /**
  59. * モデルの制限設定を取得(データベースから読み込み)
  60. */
  61. async getModelLimits(modelId: string, userId: string, tenantId?: string): Promise<{
  62. maxInputTokens: number;
  63. maxBatchSize: number;
  64. expectedDimensions: number;
  65. providerName: string;
  66. isVectorModel: boolean;
  67. }> {
  68. const modelConfig = await this.modelConfigService.findOne(modelId, userId, tenantId || '');
  69. if (!modelConfig || modelConfig.type !== 'embedding') {
  70. throw new BadRequestException(this.i18nService.formatMessage('embeddingModelNotFound', { id: modelId }));
  71. }
  72. // データベースのフィールドから制限を取得し、デフォルト値で補完
  73. const maxInputTokens = modelConfig.maxInputTokens || this.envMaxChunkSize;
  74. const maxBatchSize = modelConfig.maxBatchSize || this.DEFAULTS.maxBatchSize;
  75. const expectedDimensions = modelConfig.dimensions || parseInt(this.configService.get('DEFAULT_VECTOR_DIMENSIONS', String(this.DEFAULTS.expectedDimensions)));
  76. const providerName = modelConfig.providerName || '不明';
  77. const isVectorModel = modelConfig.isVectorModel || false;
  78. this.logger.log(
  79. this.i18nService.formatMessage('configLoaded', { name: modelConfig.name, id: modelConfig.modelId }) + '\n' +
  80. ` - プロバイダー: ${providerName}\n` +
  81. ` - Token制限: ${maxInputTokens}\n` +
  82. ` - バッチ制限: ${maxBatchSize}\n` +
  83. ` - ベクトル次元: ${expectedDimensions}\n` +
  84. ` - ベクトルモデルか: ${isVectorModel}`,
  85. );
  86. return {
  87. maxInputTokens,
  88. maxBatchSize,
  89. expectedDimensions,
  90. providerName,
  91. isVectorModel,
  92. };
  93. }
  94. /**
  95. * チャンク設定を検証および修正
  96. * 優先順位: 環境変数の上限 > モデルの制限 > ユーザー設定
  97. */
  98. async validateChunkConfig(
  99. chunkSize: number,
  100. chunkOverlap: number,
  101. modelId: string,
  102. userId: string,
  103. tenantId?: string,
  104. ): Promise<{
  105. chunkSize: number;
  106. chunkOverlap: number;
  107. warnings: string[];
  108. effectiveMaxChunkSize: number;
  109. effectiveMaxOverlapSize: number;
  110. }> {
  111. const warnings: string[] = [];
  112. const limits = await this.getModelLimits(modelId, userId, tenantId);
  113. // 1. 最終的な上限を計算(環境変数とモデル制限の小さい方を選択)
  114. const effectiveMaxChunkSize = Math.min(
  115. this.envMaxChunkSize,
  116. limits.maxInputTokens,
  117. );
  118. const effectiveMaxOverlapSize = Math.min(
  119. this.envMaxOverlapSize,
  120. Math.floor(effectiveMaxChunkSize * this.DEFAULTS.maxOverlapRatio),
  121. );
  122. // 2. チャンクサイズの上限を検証
  123. if (chunkSize > effectiveMaxChunkSize) {
  124. const reason =
  125. this.envMaxChunkSize < limits.maxInputTokens
  126. ? `${this.i18nService.getMessage('environmentLimit')} ${this.envMaxChunkSize}`
  127. : `${this.i18nService.getMessage('modelLimit')} ${limits.maxInputTokens}`;
  128. warnings.push(
  129. this.i18nService.formatMessage('chunkOverflow', {
  130. size: chunkSize,
  131. max: effectiveMaxChunkSize,
  132. reason
  133. })
  134. );
  135. chunkSize = effectiveMaxChunkSize;
  136. }
  137. // 3. チャンクサイズの下限を検証
  138. if (chunkSize < this.DEFAULTS.minChunkSize) {
  139. warnings.push(
  140. this.i18nService.formatMessage('chunkUnderflow', {
  141. size: chunkSize,
  142. min: this.DEFAULTS.minChunkSize
  143. })
  144. );
  145. chunkSize = this.DEFAULTS.minChunkSize;
  146. }
  147. // 4. 重なりサイズの上限を検証(環境変数優先)
  148. if (chunkOverlap > effectiveMaxOverlapSize) {
  149. warnings.push(
  150. this.i18nService.formatMessage('overlapOverflow', {
  151. size: chunkOverlap,
  152. max: effectiveMaxOverlapSize
  153. })
  154. );
  155. chunkOverlap = effectiveMaxOverlapSize;
  156. }
  157. // 5. 重なりサイズがチャンクサイズの50%を超えないことを検証
  158. const maxOverlapByRatio = Math.floor(
  159. chunkSize * this.DEFAULTS.maxOverlapRatio,
  160. );
  161. if (chunkOverlap > maxOverlapByRatio) {
  162. warnings.push(
  163. this.i18nService.formatMessage('overlapRatioExceeded', {
  164. size: chunkOverlap,
  165. max: maxOverlapByRatio
  166. })
  167. );
  168. chunkOverlap = maxOverlapByRatio;
  169. }
  170. if (chunkOverlap < this.DEFAULTS.minChunkOverlap) {
  171. warnings.push(
  172. this.i18nService.formatMessage('overlapUnderflow', {
  173. size: chunkOverlap,
  174. min: this.DEFAULTS.minChunkOverlap
  175. })
  176. );
  177. chunkOverlap = this.DEFAULTS.minChunkOverlap;
  178. }
  179. // 6. バッチ処理の安全チェックを追加
  180. // バッチ処理時、複数のテキストの合計長がモデルの制限を超えないようにする必要があります
  181. const safetyMargin = 0.8; // 80% 安全マージン、バッチ処理のためにスペースを確保
  182. const safeChunkSize = Math.floor(effectiveMaxChunkSize * safetyMargin);
  183. if (chunkSize > safeChunkSize) {
  184. warnings.push(
  185. this.i18nService.formatMessage('batchOverflowWarning', {
  186. safeSize: safeChunkSize,
  187. size: chunkSize,
  188. percent: Math.round(safetyMargin * 100)
  189. })
  190. );
  191. }
  192. // 7. 推定チャンク数が妥当かチェック
  193. const estimatedChunkCount = this.estimateChunkCount(
  194. 1000000, // 1MB のテキストを想定
  195. chunkSize,
  196. );
  197. if (estimatedChunkCount > 50000) {
  198. warnings.push(
  199. this.i18nService.formatMessage('estimatedChunkCountExcessive', { count: estimatedChunkCount })
  200. );
  201. }
  202. return {
  203. chunkSize,
  204. chunkOverlap,
  205. warnings,
  206. effectiveMaxChunkSize,
  207. effectiveMaxOverlapSize,
  208. };
  209. }
  210. /**
  211. * 推奨されるバッチサイズを取得
  212. */
  213. async getRecommendedBatchSize(
  214. modelId: string,
  215. userId: string,
  216. tenantId?: string,
  217. currentBatchSize: number = 100,
  218. ): Promise<number> {
  219. const limits = await this.getModelLimits(modelId, userId, tenantId);
  220. // 設定値とモデル制限の小さい方を選択
  221. const recommended = Math.min(
  222. currentBatchSize,
  223. limits.maxBatchSize,
  224. 200, // 安全のための上限
  225. );
  226. if (recommended < currentBatchSize) {
  227. this.logger.warn(
  228. this.i18nService.formatMessage('batchSizeAdjusted', {
  229. old: currentBatchSize,
  230. new: recommended,
  231. limit: limits.maxBatchSize
  232. })
  233. );
  234. }
  235. return Math.max(10, recommended); // 最低10個
  236. }
  237. /**
  238. * チャンク数を推定
  239. */
  240. estimateChunkCount(textLength: number, chunkSize: number): number {
  241. const chunkSizeInChars = chunkSize * 4; // 1 token ≈ 4 chars
  242. return Math.ceil(textLength / chunkSizeInChars);
  243. }
  244. /**
  245. * ベクトル次元の検証
  246. */
  247. async validateDimensions(
  248. modelId: string,
  249. userId: string,
  250. actualDimensions: number,
  251. tenantId?: string,
  252. ): Promise<boolean> {
  253. const limits = await this.getModelLimits(modelId, userId, tenantId);
  254. if (actualDimensions !== limits.expectedDimensions) {
  255. this.logger.warn(
  256. this.i18nService.formatMessage('dimensionMismatch', {
  257. id: modelId,
  258. expected: limits.expectedDimensions,
  259. actual: actualDimensions
  260. })
  261. );
  262. return false;
  263. }
  264. return true;
  265. }
  266. /**
  267. * 設定概要を取得(ログ用)
  268. */
  269. async getConfigSummary(
  270. chunkSize: number,
  271. chunkOverlap: number,
  272. modelId: string,
  273. userId: string,
  274. tenantId?: string,
  275. ): Promise<string> {
  276. const limits = await this.getModelLimits(modelId, userId, tenantId);
  277. return [
  278. `モデル: ${modelId}`,
  279. `チャンクサイズ: ${chunkSize} tokens (制限: ${limits.maxInputTokens})`,
  280. `重なりサイズ: ${chunkOverlap} tokens`,
  281. `バッチサイズ: ${limits.maxBatchSize}`,
  282. `ベクトル次元: ${limits.expectedDimensions}`,
  283. ].join(', ');
  284. }
  285. /**
  286. * フロントエンド用の設定制限を取得
  287. * フロントエンドのスライダーの上限設定に使用
  288. */
  289. async getFrontendLimits(
  290. modelId: string,
  291. userId: string,
  292. tenantId?: string,
  293. ): Promise<{
  294. maxChunkSize: number;
  295. maxOverlapSize: number;
  296. minOverlapSize: number;
  297. defaultChunkSize: number;
  298. defaultOverlapSize: number;
  299. modelInfo: {
  300. name: string;
  301. maxInputTokens: number;
  302. maxBatchSize: number;
  303. expectedDimensions: number;
  304. };
  305. }> {
  306. const limits = await this.getModelLimits(modelId, userId, tenantId);
  307. // 最終的な上限を計算(環境変数とモデル制限の小さい方を選択)
  308. const maxChunkSize = Math.min(this.envMaxChunkSize, limits.maxInputTokens);
  309. const maxOverlapSize = Math.min(
  310. this.envMaxOverlapSize,
  311. Math.floor(maxChunkSize * this.DEFAULTS.maxOverlapRatio),
  312. );
  313. // モデル設定名を取得
  314. const modelConfig = await this.modelConfigService.findOne(modelId, userId, tenantId || '');
  315. const modelName = modelConfig?.name || 'Unknown';
  316. // テナントまたはユーザー設定からデフォルト値を取得
  317. let defaultChunkSize = this.DEFAULTS.chunkSize;
  318. let defaultOverlapSize = this.DEFAULTS.chunkOverlap;
  319. if (tenantId) {
  320. const tenantSettings = await this.tenantService.getSettings(tenantId);
  321. if (tenantSettings.chunkSize) defaultChunkSize = tenantSettings.chunkSize;
  322. if (tenantSettings.chunkOverlap) defaultOverlapSize = tenantSettings.chunkOverlap;
  323. }
  324. return {
  325. maxChunkSize,
  326. maxOverlapSize,
  327. minOverlapSize: this.DEFAULTS.minChunkOverlap,
  328. defaultChunkSize: Math.min(defaultChunkSize, maxChunkSize),
  329. defaultOverlapSize: Math.max(this.DEFAULTS.minChunkOverlap, Math.min(defaultOverlapSize, maxOverlapSize)),
  330. modelInfo: {
  331. name: modelName,
  332. maxInputTokens: limits.maxInputTokens,
  333. maxBatchSize: limits.maxBatchSize,
  334. expectedDimensions: limits.expectedDimensions,
  335. },
  336. };
  337. }
  338. }