feat: 测试路径
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
|||||||
"gpt-load/internal/models"
|
"gpt-load/internal/models"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -67,7 +68,14 @@ func (ch *AnthropicChannel) ValidateKey(ctx context.Context, key string) (bool,
|
|||||||
return false, fmt.Errorf("no upstream URL configured for channel %s", ch.Name)
|
return false, fmt.Errorf("no upstream URL configured for channel %s", ch.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
reqURL := upstreamURL.String() + "/v1/messages"
|
validationEndpoint := ch.ValidationEndpoint
|
||||||
|
if validationEndpoint == "" {
|
||||||
|
validationEndpoint = "/v1/messages"
|
||||||
|
}
|
||||||
|
reqURL, err := url.JoinPath(upstreamURL.String(), validationEndpoint)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("failed to join upstream URL and validation endpoint: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Use a minimal, low-cost payload for validation
|
// Use a minimal, low-cost payload for validation
|
||||||
payload := gin.H{
|
payload := gin.H{
|
||||||
|
@@ -28,6 +28,7 @@ type BaseChannel struct {
|
|||||||
HTTPClient *http.Client
|
HTTPClient *http.Client
|
||||||
StreamClient *http.Client
|
StreamClient *http.Client
|
||||||
TestModel string
|
TestModel string
|
||||||
|
ValidationEndpoint string
|
||||||
upstreamLock sync.Mutex
|
upstreamLock sync.Mutex
|
||||||
|
|
||||||
// Cached fields from the group for stale check
|
// Cached fields from the group for stale check
|
||||||
@@ -96,6 +97,9 @@ func (b *BaseChannel) IsConfigStale(group *models.Group) bool {
|
|||||||
if b.TestModel != group.TestModel {
|
if b.TestModel != group.TestModel {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
if b.ValidationEndpoint != group.ValidationEndpoint {
|
||||||
|
return true
|
||||||
|
}
|
||||||
if !bytes.Equal(b.groupUpstreams, group.Upstreams) {
|
if !bytes.Equal(b.groupUpstreams, group.Upstreams) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@@ -145,6 +145,7 @@ func (f *Factory) newBaseChannel(name string, group *models.Group) (*BaseChannel
|
|||||||
HTTPClient: httpClient,
|
HTTPClient: httpClient,
|
||||||
StreamClient: streamClient,
|
StreamClient: streamClient,
|
||||||
TestModel: group.TestModel,
|
TestModel: group.TestModel,
|
||||||
|
ValidationEndpoint: group.ValidationEndpoint,
|
||||||
channelType: group.ChannelType,
|
channelType: group.ChannelType,
|
||||||
groupUpstreams: group.Upstreams,
|
groupUpstreams: group.Upstreams,
|
||||||
effectiveConfig: &group.EffectiveConfig,
|
effectiveConfig: &group.EffectiveConfig,
|
||||||
|
@@ -9,6 +9,7 @@ import (
|
|||||||
"gpt-load/internal/models"
|
"gpt-load/internal/models"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -65,7 +66,12 @@ func (ch *GeminiChannel) ValidateKey(ctx context.Context, key string) (bool, err
|
|||||||
return false, fmt.Errorf("no upstream URL configured for channel %s", ch.Name)
|
return false, fmt.Errorf("no upstream URL configured for channel %s", ch.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
reqURL := fmt.Sprintf("%s/v1beta/models/%s:generateContent?key=%s", upstreamURL.String(), ch.TestModel, key)
|
// Safely join the path segments
|
||||||
|
reqURL, err := url.JoinPath(upstreamURL.String(), "v1beta", "models", ch.TestModel+":generateContent")
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("failed to create gemini validation path: %w", err)
|
||||||
|
}
|
||||||
|
reqURL += "?key=" + key
|
||||||
|
|
||||||
payload := gin.H{
|
payload := gin.H{
|
||||||
"contents": []gin.H{
|
"contents": []gin.H{
|
||||||
|
@@ -9,6 +9,7 @@ import (
|
|||||||
"gpt-load/internal/models"
|
"gpt-load/internal/models"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -66,7 +67,14 @@ func (ch *OpenAIChannel) ValidateKey(ctx context.Context, key string) (bool, err
|
|||||||
return false, fmt.Errorf("no upstream URL configured for channel %s", ch.Name)
|
return false, fmt.Errorf("no upstream URL configured for channel %s", ch.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
reqURL := upstreamURL.String() + "/v1/chat/completions"
|
validationEndpoint := ch.ValidationEndpoint
|
||||||
|
if validationEndpoint == "" {
|
||||||
|
validationEndpoint = "/v1/chat/completions"
|
||||||
|
}
|
||||||
|
reqURL, err := url.JoinPath(upstreamURL.String(), validationEndpoint)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("failed to join upstream URL and validation endpoint: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Use a minimal, low-cost payload for validation
|
// Use a minimal, low-cost payload for validation
|
||||||
payload := gin.H{
|
payload := gin.H{
|
||||||
|
@@ -88,6 +88,20 @@ func isValidGroupName(name string) bool {
|
|||||||
return match
|
return match
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isValidValidationEndpoint checks if the validation endpoint is a valid path.
|
||||||
|
func isValidValidationEndpoint(endpoint string) bool {
|
||||||
|
if endpoint == "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if !strings.HasPrefix(endpoint, "/") {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if strings.Contains(endpoint, "://") {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// validateAndCleanConfig validates the group config against the GroupConfig struct and system-defined rules.
|
// validateAndCleanConfig validates the group config against the GroupConfig struct and system-defined rules.
|
||||||
func (s *Server) validateAndCleanConfig(configMap map[string]any) (map[string]any, error) {
|
func (s *Server) validateAndCleanConfig(configMap map[string]any) (map[string]any, error) {
|
||||||
if configMap == nil {
|
if configMap == nil {
|
||||||
@@ -180,6 +194,12 @@ func (s *Server) CreateGroup(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
validationEndpoint := strings.TrimSpace(req.ValidationEndpoint)
|
||||||
|
if !isValidValidationEndpoint(validationEndpoint) {
|
||||||
|
response.Error(c, app_errors.NewAPIError(app_errors.ErrValidation, "无效的测试路径。如果提供,必须是以 / 开头的有效路径,且不能是完整的URL。"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
group := models.Group{
|
group := models.Group{
|
||||||
Name: name,
|
Name: name,
|
||||||
DisplayName: strings.TrimSpace(req.DisplayName),
|
DisplayName: strings.TrimSpace(req.DisplayName),
|
||||||
@@ -188,6 +208,7 @@ func (s *Server) CreateGroup(c *gin.Context) {
|
|||||||
ChannelType: channelType,
|
ChannelType: channelType,
|
||||||
Sort: req.Sort,
|
Sort: req.Sort,
|
||||||
TestModel: testModel,
|
TestModel: testModel,
|
||||||
|
ValidationEndpoint: validationEndpoint,
|
||||||
ParamOverrides: req.ParamOverrides,
|
ParamOverrides: req.ParamOverrides,
|
||||||
Config: cleanedConfig,
|
Config: cleanedConfig,
|
||||||
}
|
}
|
||||||
@@ -229,6 +250,7 @@ type GroupUpdateRequest struct {
|
|||||||
ChannelType *string `json:"channel_type,omitempty"`
|
ChannelType *string `json:"channel_type,omitempty"`
|
||||||
Sort *int `json:"sort"`
|
Sort *int `json:"sort"`
|
||||||
TestModel string `json:"test_model"`
|
TestModel string `json:"test_model"`
|
||||||
|
ValidationEndpoint *string `json:"validation_endpoint,omitempty"`
|
||||||
ParamOverrides map[string]any `json:"param_overrides"`
|
ParamOverrides map[string]any `json:"param_overrides"`
|
||||||
Config map[string]any `json:"config"`
|
Config map[string]any `json:"config"`
|
||||||
}
|
}
|
||||||
@@ -311,6 +333,15 @@ func (s *Server) UpdateGroup(c *gin.Context) {
|
|||||||
if req.ParamOverrides != nil {
|
if req.ParamOverrides != nil {
|
||||||
group.ParamOverrides = req.ParamOverrides
|
group.ParamOverrides = req.ParamOverrides
|
||||||
}
|
}
|
||||||
|
if req.ValidationEndpoint != nil {
|
||||||
|
validationEndpoint := strings.TrimSpace(*req.ValidationEndpoint)
|
||||||
|
if !isValidValidationEndpoint(validationEndpoint) {
|
||||||
|
response.Error(c, app_errors.NewAPIError(app_errors.ErrValidation, "无效的测试路径。如果提供,必须是以 / 开头的有效路径,且不能是完整的URL。"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
group.ValidationEndpoint = validationEndpoint
|
||||||
|
}
|
||||||
|
|
||||||
if req.Config != nil {
|
if req.Config != nil {
|
||||||
cleanedConfig, err := s.validateAndCleanConfig(req.Config)
|
cleanedConfig, err := s.validateAndCleanConfig(req.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -348,6 +379,7 @@ type GroupResponse struct {
|
|||||||
ChannelType string `json:"channel_type"`
|
ChannelType string `json:"channel_type"`
|
||||||
Sort int `json:"sort"`
|
Sort int `json:"sort"`
|
||||||
TestModel string `json:"test_model"`
|
TestModel string `json:"test_model"`
|
||||||
|
ValidationEndpoint string `json:"validation_endpoint"`
|
||||||
ParamOverrides datatypes.JSONMap `json:"param_overrides"`
|
ParamOverrides datatypes.JSONMap `json:"param_overrides"`
|
||||||
Config datatypes.JSONMap `json:"config"`
|
Config datatypes.JSONMap `json:"config"`
|
||||||
LastValidatedAt *time.Time `json:"last_validated_at"`
|
LastValidatedAt *time.Time `json:"last_validated_at"`
|
||||||
@@ -377,6 +409,7 @@ func (s *Server) newGroupResponse(group *models.Group) *GroupResponse {
|
|||||||
ChannelType: group.ChannelType,
|
ChannelType: group.ChannelType,
|
||||||
Sort: group.Sort,
|
Sort: group.Sort,
|
||||||
TestModel: group.TestModel,
|
TestModel: group.TestModel,
|
||||||
|
ValidationEndpoint: group.ValidationEndpoint,
|
||||||
ParamOverrides: group.ParamOverrides,
|
ParamOverrides: group.ParamOverrides,
|
||||||
Config: group.Config,
|
Config: group.Config,
|
||||||
LastValidatedAt: group.LastValidatedAt,
|
LastValidatedAt: group.LastValidatedAt,
|
||||||
|
@@ -47,6 +47,7 @@ type Group struct {
|
|||||||
DisplayName string `gorm:"type:varchar(255)" json:"display_name"`
|
DisplayName string `gorm:"type:varchar(255)" json:"display_name"`
|
||||||
Description string `gorm:"type:varchar(512)" json:"description"`
|
Description string `gorm:"type:varchar(512)" json:"description"`
|
||||||
Upstreams datatypes.JSON `gorm:"type:json;not null" json:"upstreams"`
|
Upstreams datatypes.JSON `gorm:"type:json;not null" json:"upstreams"`
|
||||||
|
ValidationEndpoint string `gorm:"type:varchar(255)" json:"validation_endpoint"`
|
||||||
ChannelType string `gorm:"type:varchar(50);not null" json:"channel_type"`
|
ChannelType string `gorm:"type:varchar(50);not null" json:"channel_type"`
|
||||||
Sort int `gorm:"default:0" json:"sort"`
|
Sort int `gorm:"default:0" json:"sort"`
|
||||||
TestModel string `gorm:"type:varchar(255);not null" json:"test_model"`
|
TestModel string `gorm:"type:varchar(255);not null" json:"test_model"`
|
||||||
|
@@ -54,6 +54,7 @@ interface GroupFormData {
|
|||||||
channel_type: "openai" | "gemini" | "anthropic";
|
channel_type: "openai" | "gemini" | "anthropic";
|
||||||
sort: number;
|
sort: number;
|
||||||
test_model: string;
|
test_model: string;
|
||||||
|
validation_endpoint: string;
|
||||||
param_overrides: string;
|
param_overrides: string;
|
||||||
config: Record<string, number>;
|
config: Record<string, number>;
|
||||||
configItems: ConfigItem[];
|
configItems: ConfigItem[];
|
||||||
@@ -73,6 +74,7 @@ const formData = reactive<GroupFormData>({
|
|||||||
channel_type: "openai",
|
channel_type: "openai",
|
||||||
sort: 1,
|
sort: 1,
|
||||||
test_model: "",
|
test_model: "",
|
||||||
|
validation_endpoint: "",
|
||||||
param_overrides: "",
|
param_overrides: "",
|
||||||
config: {},
|
config: {},
|
||||||
configItems: [] as ConfigItem[],
|
configItems: [] as ConfigItem[],
|
||||||
@@ -177,6 +179,7 @@ function resetForm() {
|
|||||||
channel_type: "openai",
|
channel_type: "openai",
|
||||||
sort: 1,
|
sort: 1,
|
||||||
test_model: "",
|
test_model: "",
|
||||||
|
validation_endpoint: "",
|
||||||
param_overrides: "",
|
param_overrides: "",
|
||||||
config: {},
|
config: {},
|
||||||
configItems: [],
|
configItems: [],
|
||||||
@@ -203,6 +206,7 @@ function loadGroupData() {
|
|||||||
channel_type: props.group.channel_type || "openai",
|
channel_type: props.group.channel_type || "openai",
|
||||||
sort: props.group.sort || 1,
|
sort: props.group.sort || 1,
|
||||||
test_model: props.group.test_model || "",
|
test_model: props.group.test_model || "",
|
||||||
|
validation_endpoint: props.group.validation_endpoint || "",
|
||||||
param_overrides: JSON.stringify(props.group.param_overrides || {}, null, 2),
|
param_overrides: JSON.stringify(props.group.param_overrides || {}, null, 2),
|
||||||
config: {},
|
config: {},
|
||||||
configItems,
|
configItems,
|
||||||
@@ -231,6 +235,8 @@ function addUpstream() {
|
|||||||
function removeUpstream(index: number) {
|
function removeUpstream(index: number) {
|
||||||
if (formData.upstreams.length > 1) {
|
if (formData.upstreams.length > 1) {
|
||||||
formData.upstreams.splice(index, 1);
|
formData.upstreams.splice(index, 1);
|
||||||
|
} else {
|
||||||
|
message.warning("至少需要保留一个上游地址");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -305,6 +311,7 @@ async function handleSubmit() {
|
|||||||
channel_type: formData.channel_type,
|
channel_type: formData.channel_type,
|
||||||
sort: formData.sort,
|
sort: formData.sort,
|
||||||
test_model: formData.test_model,
|
test_model: formData.test_model,
|
||||||
|
validation_endpoint: formData.validation_endpoint,
|
||||||
param_overrides: formData.param_overrides ? paramOverrides : undefined,
|
param_overrides: formData.param_overrides ? paramOverrides : undefined,
|
||||||
config,
|
config,
|
||||||
};
|
};
|
||||||
@@ -376,6 +383,17 @@ async function handleSubmit() {
|
|||||||
<n-input v-model:value="formData.test_model" :placeholder="testModelPlaceholder" />
|
<n-input v-model:value="formData.test_model" :placeholder="testModelPlaceholder" />
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
|
|
||||||
|
<n-form-item
|
||||||
|
label="测试路径"
|
||||||
|
path="validation_endpoint"
|
||||||
|
v-if="formData.channel_type !== 'gemini'"
|
||||||
|
>
|
||||||
|
<n-input
|
||||||
|
v-model:value="formData.validation_endpoint"
|
||||||
|
placeholder="可选,自定义用于验证key的API路径"
|
||||||
|
/>
|
||||||
|
</n-form-item>
|
||||||
|
|
||||||
<n-form-item label="排序" path="sort">
|
<n-form-item label="排序" path="sort">
|
||||||
<n-input-number
|
<n-input-number
|
||||||
v-model:value="formData.sort"
|
v-model:value="formData.sort"
|
||||||
|
@@ -318,6 +318,11 @@ function resetPage() {
|
|||||||
{{ group?.sort || 0 }}
|
{{ group?.sort || 0 }}
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
</n-grid-item>
|
</n-grid-item>
|
||||||
|
<n-grid-item v-if="group?.channel_type !== 'gemini'">
|
||||||
|
<n-form-item label="测试路径:">
|
||||||
|
{{ group?.validation_endpoint }}
|
||||||
|
</n-form-item>
|
||||||
|
</n-grid-item>
|
||||||
<n-grid-item>
|
<n-grid-item>
|
||||||
<n-form-item label="描述:">
|
<n-form-item label="描述:">
|
||||||
{{ group?.description || "-" }}
|
{{ group?.description || "-" }}
|
||||||
|
@@ -38,6 +38,7 @@ export interface Group {
|
|||||||
test_model: string;
|
test_model: string;
|
||||||
channel_type: "openai" | "gemini" | "anthropic";
|
channel_type: "openai" | "gemini" | "anthropic";
|
||||||
upstreams: UpstreamInfo[];
|
upstreams: UpstreamInfo[];
|
||||||
|
validation_endpoint: string;
|
||||||
config: Record<string, unknown>;
|
config: Record<string, unknown>;
|
||||||
api_keys?: APIKey[];
|
api_keys?: APIKey[];
|
||||||
endpoint?: string;
|
endpoint?: string;
|
||||||
|
Reference in New Issue
Block a user