feat: 支持 Embedding 模型连接测试

- 添加 model_type 字段区分 chat 和 embedding 模型
- 根据 model_type 使用不同的 API 端点和请求格式
- embedding 模型使用 /v1/embeddings 端点

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-07 17:18:30 +08:00
parent 52a4ffd7a9
commit 0c63c3b44d
2 changed files with 40 additions and 25 deletions

View File

@@ -58,6 +58,7 @@ type UpdateModelRequest struct {
type TestModelRequest struct {
Provider string `json:"provider" binding:"required"`
Model string `json:"model" binding:"required"`
ModelType string `json:"model_type" binding:"required"`
APIKey string `json:"api_key" binding:"required"`
BaseURL string `json:"base_url" binding:"required"`
APIEndpoint string `json:"api_endpoint"`

View File

@@ -114,8 +114,6 @@ func (s *ModelService) Delete(id string) error {
// TestConnection 测试模型连接
func (s *ModelService) TestConnection(req model.TestModelRequest) (*model.TestModelResponse, error) {
log.Printf("[TestConnection] 开始测试连接: provider=%s, model=%s, base_url=%s, api_endpoint=%s", req.Provider, req.Model, req.BaseURL, req.APIEndpoint)
// 构建请求 URL
baseURL := req.BaseURL
// 去掉 base_url 末尾的斜杠
@@ -126,31 +124,49 @@ func (s *ModelService) TestConnection(req model.TestModelRequest) (*model.TestMo
apiEndpoint := strings.TrimLeft(req.APIEndpoint, "/")
baseURL = baseURL + "/" + apiEndpoint
} else {
// 默认端点 - 根据不同 provider 设置
switch req.Provider {
case "OpenAI":
baseURL = baseURL + "/v1/chat/completions"
case "Ollama":
baseURL = baseURL + "/api/chat"
case "ali", "Ali", "aliyun", "Aliyun":
// 阿里云 DashScope 兼容 OpenAI 格式,需要添加 /chat/completions
// base_url 格式: https://dashscope.aliyuncs.com/compatible-mode/v1
baseURL = baseURL + "/chat/completions"
// 根据 model_type 确定端点
switch req.ModelType {
case "embedding":
// embedding 模型使用 /v1/embeddings
switch req.Provider {
case "Ollama":
baseURL = baseURL + "/api/embeddings"
default:
baseURL = baseURL + "/v1/embeddings"
}
default:
// 默认使用 OpenAI 兼容格式
baseURL = baseURL + "/v1/chat/completions"
// chat 模型使用 /chat/completions
switch req.Provider {
case "OpenAI":
baseURL = baseURL + "/v1/chat/completions"
case "Ollama":
baseURL = baseURL + "/api/chat"
case "ali", "Ali", "aliyun", "Aliyun":
// 阿里云 DashScope 兼容 OpenAI 格式
baseURL = baseURL + "/chat/completions"
default:
// 默认使用 OpenAI 兼容格式
baseURL = baseURL + "/v1/chat/completions"
}
}
}
log.Printf("[TestConnection] 请求 URL: %s", baseURL)
// 构建请求体
requestBody := map[string]interface{}{
"model": req.Model,
"messages": []map[string]string{
{"role": "user", "content": "Hello"},
},
"max_tokens": 10,
// 构建请求体 - 根据 model_type 使用不同的格式
var requestBody map[string]interface{}
if req.ModelType == "embedding" {
requestBody = map[string]interface{}{
"model": req.Model,
"input": "Hello",
"format": "float",
}
} else {
requestBody = map[string]interface{}{
"model": req.Model,
"messages": []map[string]string{
{"role": "user", "content": "Hello"},
},
"max_tokens": 10,
}
}
body, err := json.Marshal(requestBody)
@@ -191,8 +207,6 @@ func (s *ModelService) TestConnection(req model.TestModelRequest) (*model.TestMo
return &model.TestModelResponse{Success: false, Message: err.Error()}, nil
}
log.Printf("[TestConnection] 响应状态码: %d, 响应体: %s", resp.StatusCode, string(respBody))
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return &model.TestModelResponse{Success: true, Message: "Connection successful"}, nil
}