ProcessChunkEmbedding 处理分块向量化
(eid int64, chunkID int64)
| 129 | |
| 130 | // ProcessChunkEmbedding 处理分块向量化 |
| 131 | func (s *EmbeddingService) ProcessChunkEmbedding(eid int64, chunkID int64) error { |
| 132 | // 获取分块信息 |
| 133 | chunk, err := model.GetDocumentChunkByID(eid, chunkID) |
| 134 | if err != nil { |
| 135 | return fmt.Errorf("获取分块信息失败: %v", err) |
| 136 | } |
| 137 | |
| 138 | // 检查是否已经向量化 |
| 139 | if model.IsDocumentChunkEmbeddingSucceeded(chunk.EmbeddingStatus) { |
| 140 | return nil |
| 141 | } |
| 142 | |
| 143 | // 更新状态为处理中 |
| 144 | err = model.UpdateChunkEmbeddingStatus(eid, chunkID, model.DocumentChunkEmbeddingStatusIndexing, "") |
| 145 | if err != nil { |
| 146 | return fmt.Errorf("更新状态失败: %v", err) |
| 147 | } |
| 148 | |
| 149 | // 获取配置 |
| 150 | configService := NewChunkConfigService(s.db) |
| 151 | config, err := configService.GetConfig(eid, &chunk.LibraryID, model.ChunkTypeDefault) |
| 152 | if err != nil { |
| 153 | s.updateEmbeddingStatusWithError(eid, chunkID, "获取配置失败") |
| 154 | return fmt.Errorf("获取配置失败: %v", err) |
| 155 | } |
| 156 | |
| 157 | if config.EmbeddingChannelID == nil { |
| 158 | s.updateEmbeddingStatusWithError(eid, chunkID, "未配置向量化渠道") |
| 159 | return fmt.Errorf("未配置向量化渠道") |
| 160 | } |
| 161 | |
| 162 | // 获取渠道配置 |
| 163 | channel, err := model.GetChannelByID(*config.EmbeddingChannelID) |
| 164 | if err != nil { |
| 165 | s.updateEmbeddingStatusWithError(eid, chunkID, "获取渠道配置失败") |
| 166 | return fmt.Errorf("获取渠道配置失败: %v", err) |
| 167 | } |
| 168 | |
| 169 | // 获取模型名称 |
| 170 | var modelName string |
| 171 | if config.EmbeddingModelName != nil { |
| 172 | modelName = *config.EmbeddingModelName |
| 173 | } else { |
| 174 | // 如果没有配置模型名称,使用默认模型(按渠道常量判断) |
| 175 | switch channel.Type { |
| 176 | case channeltype.OpenAI: |
| 177 | modelName = "text-embedding-3-small" |
| 178 | case channeltype.Azure: |
| 179 | modelName = "text-embedding-3-small" |
| 180 | case channeltype.Ali: // Qwen (百炼 compatible-mode) |
| 181 | modelName = "text-embedding-v4" |
| 182 | case model.ChannelApiTypeAppBuilderModel: // 千帆向量模型 |
| 183 | modelName = "bge-large-zh" |
| 184 | default: |
| 185 | modelName = "text-embedding-3-small" |
| 186 | } |
| 187 | } |
| 188 |
no test coverage detected