feat: Get Embedding demension from backend
This commit is contained in:
@@ -213,6 +213,27 @@ export function checkRemoteModel(modelConfig: {
|
||||
});
|
||||
}
|
||||
|
||||
// 测试 Embedding 模型(本地/远程)是否可用
|
||||
export function testEmbeddingModel(modelConfig: {
|
||||
source: 'local' | 'remote';
|
||||
modelName: string;
|
||||
baseUrl?: string;
|
||||
apiKey?: string;
|
||||
dimension?: number;
|
||||
}): Promise<{ available: boolean; message?: string; dimension?: number }> {
|
||||
return new Promise((resolve, reject) => {
|
||||
post('/api/v1/initialization/embedding/test', modelConfig)
|
||||
.then((response: any) => {
|
||||
resolve(response.data || {});
|
||||
})
|
||||
.catch((error: any) => {
|
||||
console.error('测试Embedding模型失败:', error);
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
export function checkRerankModel(modelConfig: {
|
||||
modelName: string;
|
||||
baseUrl: string;
|
||||
|
||||
@@ -238,11 +238,23 @@
|
||||
<!-- 向量维度设置 -->
|
||||
<div class="form-row">
|
||||
<t-form-item label="维度" name="embedding.dimension">
|
||||
<t-input v-model="formData.embedding.dimension"
|
||||
:disabled="hasFiles"
|
||||
placeholder="请输入向量维度"
|
||||
style="width: 100px;"
|
||||
@input="onDimensionInput" />
|
||||
<div class="dimension-input-with-action">
|
||||
<t-input v-model="formData.embedding.dimension"
|
||||
:disabled="hasFiles"
|
||||
placeholder="请输入向量维度"
|
||||
style="width: 100px;"
|
||||
@input="onDimensionInput" />
|
||||
<t-button
|
||||
size="small"
|
||||
variant="outline"
|
||||
class="detect-dim-btn"
|
||||
:loading="embeddingDimDetecting"
|
||||
:disabled="hasFiles"
|
||||
@click="detectEmbeddingDimension"
|
||||
>
|
||||
检测维度
|
||||
</t-button>
|
||||
</div>
|
||||
</t-form-item>
|
||||
|
||||
<!-- 下载进度:下载中时显示 -->
|
||||
@@ -415,7 +427,7 @@
|
||||
<div class="form-row">
|
||||
<t-form-item label="模型名称" name="multimodal.vlm.modelName">
|
||||
<div class="model-input-with-status">
|
||||
<t-input v-model="formData.multimodal.vlm.modelName" placeholder="例如: llava:13b"
|
||||
<t-input v-model="formData.multimodal.vlm.modelName" placeholder="例如: qwen2.5vl:3b"
|
||||
@blur="onModelNameChange('vlm')"
|
||||
@input="onModelNameInput('vlm')"
|
||||
@keyup.enter="onModelNameChange('vlm')" />
|
||||
@@ -512,7 +524,7 @@
|
||||
|
||||
<div class="form-row">
|
||||
<t-form-item label="Path Prefix" name="multimodal.minio.pathPrefix">
|
||||
<t-input v-model="formData.multimodal.minio.pathPrefix" placeholder="例如: images/" />
|
||||
<t-input v-model="formData.multimodal.minio.pathPrefix" placeholder="例如: images" />
|
||||
</t-form-item>
|
||||
</div>
|
||||
</div>
|
||||
@@ -550,7 +562,7 @@
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<t-form-item v-if="formData.storageType === 'cos'" label="Path Prefix" name="multimodal.cos.pathPrefix">
|
||||
<t-input v-model="formData.multimodal.cos.pathPrefix" placeholder="例如: images/"
|
||||
<t-input v-model="formData.multimodal.cos.pathPrefix" placeholder="例如: images"
|
||||
@blur="onCosConfigChange" />
|
||||
</t-form-item>
|
||||
</div>
|
||||
@@ -765,8 +777,9 @@ import {
|
||||
type DownloadTask,
|
||||
checkRerankModel,
|
||||
testMultimodalFunction,
|
||||
listOllamaModels
|
||||
} from '@/api/initialization';
|
||||
listOllamaModels,
|
||||
testEmbeddingModel
|
||||
} from '@/api/initialization';
|
||||
|
||||
const router = useRouter();
|
||||
type TFormRef = {
|
||||
@@ -919,6 +932,9 @@ const multimodalTest = reactive({
|
||||
|
||||
const imageUpload = ref(null);
|
||||
|
||||
// Embedding 维度检测状态
|
||||
const embeddingDimDetecting = ref(false);
|
||||
|
||||
// 左侧导航区段
|
||||
type Section = { id: string; label: string };
|
||||
const sections: Section[] = [
|
||||
@@ -2226,6 +2242,49 @@ const checkAllConfiguredModels = async () => {
|
||||
const onDimensionInput = (event: any) => {
|
||||
formData.embedding.dimension = Number(event.target.value);
|
||||
};
|
||||
|
||||
// 检测并自动填写 Embedding 维度
|
||||
const detectEmbeddingDimension = async () => {
|
||||
if (hasFiles.value) return;
|
||||
// 校验必填:模型来源 + 模型名称;若远程还需 BaseURL
|
||||
const source = (formData.embedding.source || '').trim();
|
||||
const modelName = (formData.embedding.modelName || '').trim();
|
||||
const baseUrl = (formData.embedding.baseUrl || '').trim();
|
||||
|
||||
if (!source || !modelName || (source === 'remote' && !baseUrl)) {
|
||||
// 触发对应字段校验提示
|
||||
const fields: string[] = ['embedding.source', 'embedding.modelName'];
|
||||
if (source === 'remote') fields.push('embedding.baseUrl');
|
||||
try { await form.value?.validate(fields); } catch {}
|
||||
MessagePlugin.warning('请先完整填写Embedding配置');
|
||||
return;
|
||||
}
|
||||
|
||||
embeddingDimDetecting.value = true;
|
||||
try {
|
||||
const res = await testEmbeddingModel({
|
||||
source: source as 'local' | 'remote',
|
||||
modelName,
|
||||
baseUrl: source === 'remote' ? baseUrl : undefined,
|
||||
apiKey: formData.embedding.apiKey || undefined,
|
||||
dimension: formData.embedding.dimension || undefined,
|
||||
});
|
||||
const available = !!res.available;
|
||||
const message = res.message || '';
|
||||
const dim = Number(res.dimension || 0);
|
||||
if (available && dim > 0) {
|
||||
formData.embedding.dimension = dim;
|
||||
MessagePlugin.success(`检测成功,维度已自动填写为 ${dim}`);
|
||||
} else {
|
||||
MessagePlugin.error(message || '检测失败');
|
||||
}
|
||||
} catch (e: any) {
|
||||
const msg = e?.message || '检测失败,请检查配置';
|
||||
MessagePlugin.error(msg);
|
||||
} finally {
|
||||
embeddingDimDetecting.value = false;
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<style lang="less" scoped>
|
||||
@@ -2233,7 +2292,7 @@ const onDimensionInput = (event: any) => {
|
||||
.init-layout {
|
||||
display: grid;
|
||||
grid-template-columns: 220px 1fr;
|
||||
gap: 16px;
|
||||
gap: 24px;
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
@@ -2311,10 +2370,13 @@ const onDimensionInput = (event: any) => {
|
||||
box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.3);
|
||||
}
|
||||
|
||||
.init-main { min-width: 0; }
|
||||
.ollama-summary-card {
|
||||
.init-main {
|
||||
min-width: 0;
|
||||
max-width: 960px;
|
||||
margin: 0 auto 16px auto;
|
||||
}
|
||||
.ollama-summary-card {
|
||||
max-width: 100%;
|
||||
margin: 0 0 16px 0;
|
||||
background: #ffffff;
|
||||
border: 1px solid #e9edf5;
|
||||
border-radius: 12px;
|
||||
@@ -2400,8 +2462,8 @@ const onDimensionInput = (event: any) => {
|
||||
|
||||
:deep(.t-form) {
|
||||
/* 表单容器卡片化:更宽一点、更浅的阴影与细边框 */
|
||||
max-width: 960px;
|
||||
margin: 0 auto;
|
||||
max-width: 100%;
|
||||
margin: 0;
|
||||
background: #fff;
|
||||
padding: 32px 36px;
|
||||
border-radius: 14px;
|
||||
@@ -2740,6 +2802,29 @@ const onDimensionInput = (event: any) => {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 统一表单项上下间距 */
|
||||
:deep(.t-form .t-form-item) {
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
/* 统一输入类控件高度与内边距(Input/Select/InputNumber) */
|
||||
:deep(.t-input .t-input__inner),
|
||||
:deep(.t-select .t-input__inner),
|
||||
:deep(.t-input-number .t-input__inner) {
|
||||
height: 40px;
|
||||
padding: 8px 12px;
|
||||
}
|
||||
|
||||
/* 统一按钮基础大小(不影响特制的提交按钮) */
|
||||
:deep(.t-button) {
|
||||
height: 36px;
|
||||
padding: 0 14px;
|
||||
}
|
||||
:deep(.t-button.t-size-s) {
|
||||
height: 28px;
|
||||
padding: 0 10px;
|
||||
}
|
||||
|
||||
:deep(.t-radio-button) {
|
||||
.t-radio-button__inner {
|
||||
@@ -2775,7 +2860,7 @@ const onDimensionInput = (event: any) => {
|
||||
/* 模型输入框与状态图标布局 */
|
||||
.model-input-with-status {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
/* 模型名称输入框样式 */
|
||||
@@ -2893,9 +2978,22 @@ const onDimensionInput = (event: any) => {
|
||||
.url-input-with-check {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
gap: 8px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
/* 维度输入框与“检测维度”按钮同一行布局 */
|
||||
.dimension-input-with-action {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
.dimension-input-with-action :deep(.t-input) {
|
||||
flex: 0 0 auto;
|
||||
}
|
||||
.detect-dim-btn {
|
||||
flex: 0 0 auto;
|
||||
}
|
||||
|
||||
.url-input-with-check :deep(.t-input) {
|
||||
flex: 1;
|
||||
@@ -2916,6 +3014,7 @@ const onDimensionInput = (event: any) => {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
min-width: 22px;
|
||||
}
|
||||
|
||||
.check-action .status-icon,
|
||||
@@ -3021,6 +3120,10 @@ const onDimensionInput = (event: any) => {
|
||||
|
||||
.model-status-icon {
|
||||
flex-shrink: 0;
|
||||
width: 22px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.model-status-icon .status-icon {
|
||||
@@ -3082,7 +3185,7 @@ const onDimensionInput = (event: any) => {
|
||||
}
|
||||
|
||||
.form-row {
|
||||
margin-bottom: 20px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.embedding-dimension {
|
||||
@@ -3104,7 +3207,7 @@ const onDimensionInput = (event: any) => {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: flex-start;
|
||||
margin-left: -100px;
|
||||
margin-left: 0;
|
||||
|
||||
.switch-label {
|
||||
margin-left: 10px;
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/Tencent/WeKnora/internal/config"
|
||||
"github.com/Tencent/WeKnora/internal/errors"
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/models/embedding"
|
||||
"github.com/Tencent/WeKnora/internal/models/utils/ollama"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
@@ -1273,6 +1274,66 @@ func (h *InitializationHandler) CheckRemoteModel(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// TestEmbeddingModel 测试 Embedding 接口(本地或远程)是否可用
|
||||
func (h *InitializationHandler) TestEmbeddingModel(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
logger.Info(ctx, "Testing embedding model connectivity and functionality")
|
||||
|
||||
var req struct {
|
||||
Source string `json:"source" binding:"required"`
|
||||
ModelName string `json:"modelName" binding:"required"`
|
||||
BaseURL string `json:"baseUrl"`
|
||||
APIKey string `json:"apiKey"`
|
||||
Dimension int `json:"dimension"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
logger.Error(ctx, "Failed to parse embedding test request", err)
|
||||
c.Error(errors.NewBadRequestError(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// 构造 embedder 配置
|
||||
cfg := embedding.Config{
|
||||
Source: types.ModelSource(strings.ToLower(req.Source)),
|
||||
BaseURL: req.BaseURL,
|
||||
ModelName: req.ModelName,
|
||||
APIKey: req.APIKey,
|
||||
TruncatePromptTokens: 256,
|
||||
Dimensions: req.Dimension,
|
||||
ModelID: "",
|
||||
}
|
||||
|
||||
emb, err := embedding.NewEmbedder(cfg)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{"model": req.ModelName})
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": gin.H{`available`: false, `message`: fmt.Sprintf("创建Embedder失败: %v", err), `dimension`: 0},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 执行一次最小化 embedding 调用
|
||||
sample := "hello"
|
||||
vec, err := emb.Embed(ctx, sample)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{"model": req.ModelName})
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": gin.H{`available`: false, `message`: fmt.Sprintf("调用Embedding失败: %v", err), `dimension`: 0},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Embedding test succeeded, dim=%d", len(vec))
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": gin.H{`available`: true, `message`: fmt.Sprintf("测试成功,向量维度=%d", len(vec)), `dimension`: len(vec)},
|
||||
})
|
||||
}
|
||||
|
||||
// checkRemoteModelConnection 检查远程模型连接的内部方法
|
||||
func (h *InitializationHandler) checkRemoteModelConnection(ctx context.Context,
|
||||
model *types.Model,
|
||||
@@ -1318,7 +1379,7 @@ func (h *InitializationHandler) checkRemoteModelConnection(ctx context.Context,
|
||||
// 检查响应状态
|
||||
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
|
||||
// 连接成功,现在检查模型是否存在
|
||||
return h.checkModelExistence(ctx, resp, model.Name)
|
||||
return true, "连接正常,请自行确保模型存在"
|
||||
} else if resp.StatusCode == 401 {
|
||||
return false, "认证失败,请检查API Key"
|
||||
} else if resp.StatusCode == 403 {
|
||||
|
||||
@@ -78,6 +78,7 @@ func NewRouter(params RouterParams) *gin.Engine {
|
||||
|
||||
// 远程API相关接口(不需要认证)
|
||||
r.POST("/api/v1/initialization/remote/check", params.InitializationHandler.CheckRemoteModel)
|
||||
r.POST("/api/v1/initialization/embedding/test", params.InitializationHandler.TestEmbeddingModel)
|
||||
r.POST("/api/v1/initialization/rerank/check", params.InitializationHandler.CheckRerankModel)
|
||||
r.POST("/api/v1/initialization/multimodal/test", params.InitializationHandler.TestMultimodalFunction)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user