@@ -29,10 +29,16 @@ type Config struct {
2929 MaxRetries int // 最大重试次数
3030 Timeout int // 请求超时时间(秒)
3131 VerifySSL bool // 是否验证SSL证书
32- ModelName string // 模型名称
32+ ModelName string // 默认模型名称
3333 BearerToken string // Bearer Token (默认提供公开Token)
3434}
3535
36+ // 支持的模型列表
37+ var SupportedModels = []string {
38+ "hunyuan-t1-latest" ,
39+ "hunyuan-turbos-latest" ,
40+ }
41+
3642// 腾讯混元 API 目标URL
3743const (
3844 TargetURL = "https://llm.hunyuan.tencent.com/aide/api/v2/triton_image/demo_text_chat/"
@@ -57,7 +63,7 @@ func parseFlags() *Config {
5763 flag .IntVar (& cfg .MaxRetries , "max-retries" , 3 , "Maximum number of retries for failed requests" )
5864 flag .IntVar (& cfg .Timeout , "timeout" , 300 , "Request timeout in seconds" )
5965 flag .BoolVar (& cfg .VerifySSL , "verify-ssl" , true , "Verify SSL certificates" )
60- flag .StringVar (& cfg .ModelName , "model" , "hunyuan-t1-latest" , "Hunyuan model name" )
66+ flag .StringVar (& cfg .ModelName , "model" , "hunyuan-t1-latest" , "Default Hunyuan model name" )
6167 flag .StringVar (& cfg .BearerToken , "token" , "7auGXNATFSKl7dF" , "Bearer token for Hunyuan API" )
6268 flag .Parse ()
6369
@@ -234,8 +240,8 @@ func main() {
234240 // 初始化日志
235241 initLogger (appConfig .LogLevel )
236242
237- logInfo ("启动服务: TargetURL=%s, Address=%s, Port=%s, Version=%s, LogLevel=%s, BearerToken=***" ,
238- TargetURL , appConfig .Address , appConfig .Port , Version , appConfig .LogLevel )
243+ logInfo ("启动服务: TargetURL=%s, Address=%s, Port=%s, Version=%s, LogLevel=%s, 支持模型=%v, BearerToken=***" ,
244+ TargetURL , appConfig .Address , appConfig .Port , Version , appConfig .LogLevel , SupportedModels )
239245
240246 // 配置更高的并发处理能力
241247 http .DefaultTransport .(* http.Transport ).MaxIdleConnsPerHost = 100
@@ -415,6 +421,16 @@ func generateQueryID() string {
415421 return fmt .Sprintf ("%s%d" , getRandomString (8 ), time .Now ().UnixNano ())
416422}
417423
424+ // 判断模型是否在支持列表中
425+ func isModelSupported (modelName string ) bool {
426+ for _ , supportedModel := range SupportedModels {
427+ if modelName == supportedModel {
428+ return true
429+ }
430+ }
431+ return false
432+ }
433+
418434// 处理模型列表请求
419435func handleModelsRequest (w http.ResponseWriter , r * http.Request ) {
420436 logInfo ("处理模型列表请求" )
@@ -423,21 +439,25 @@ func handleModelsRequest(w http.ResponseWriter, r *http.Request) {
423439 w .Header ().Set ("Content-Type" , "application/json" )
424440 w .WriteHeader (http .StatusOK )
425441
442+ // 构建模型数据
443+ modelData := make ([]map [string ]interface {}, 0 , len (SupportedModels ))
444+ for _ , model := range SupportedModels {
445+ modelData = append (modelData , map [string ]interface {}{
446+ "id" : model ,
447+ "object" : "model" ,
448+ "created" : time .Now ().Unix (),
449+ "owned_by" : "TencentCloud" ,
450+ "capabilities" : map [string ]interface {}{
451+ "chat" : true ,
452+ "completions" : true ,
453+ "reasoning" : true ,
454+ },
455+ })
456+ }
457+
426458 modelsList := map [string ]interface {}{
427459 "object" : "list" ,
428- "data" : []map [string ]interface {}{
429- {
430- "id" : "hunyuan-t1-latest" ,
431- "object" : "model" ,
432- "created" : time .Now ().Unix (),
433- "owned_by" : "TencentCloud" ,
434- "capabilities" : map [string ]interface {}{
435- "chat" : true ,
436- "completions" : true ,
437- "reasoning" : true ,
438- },
439- },
440- },
460+ "data" : modelData ,
441461 }
442462
443463 json .NewEncoder (w ).Encode (modelsList )
@@ -468,10 +488,23 @@ func handleChatCompletionRequest(w http.ResponseWriter, r *http.Request) {
468488 // 是否使用流式处理
469489 isStream := apiReq .Stream
470490
491+ // 确定使用的模型
492+ modelName := appConfig .ModelName
493+ if apiReq .Model != "" {
494+ // 检查请求的模型是否是我们支持的
495+ if isModelSupported (apiReq .Model ) {
496+ modelName = apiReq .Model
497+ } else {
498+ logWarn ("[reqID:%s] 请求的模型 %s 不支持,使用默认模型 %s" , reqID , apiReq .Model , modelName )
499+ }
500+ }
501+
502+ logInfo ("[reqID:%s] 使用模型: %s" , reqID , modelName )
503+
471504 // 创建混元API请求
472505 hunyuanReq := HunyuanRequest {
473506 Stream : true , // 混元API总是使用流式响应
474- Model : appConfig . ModelName ,
507+ Model : modelName ,
475508 QueryID : generateQueryID (),
476509 Messages : apiReq .Messages ,
477510 StreamModeration : true ,
@@ -593,7 +626,7 @@ func handleStreamingRequest(w http.ResponseWriter, r *http.Request, hunyuanReq H
593626 }
594627
595628 // 发送角色块
596- roleChunk := createRoleChunk (respID , createdTime )
629+ roleChunk := createRoleChunk (respID , createdTime , hunyuanReq . Model )
597630 w .Write ([]byte ("data: " + string (roleChunk ) + "\n \n " ))
598631 flusher .Flush ()
599632
@@ -643,14 +676,14 @@ func handleStreamingRequest(w http.ResponseWriter, r *http.Request, hunyuanReq H
643676 for _ , choice := range hunyuanResp .Choices {
644677 if choice .Delta .Content != "" {
645678 // 发送内容块
646- contentChunk := createContentChunk (respID , createdTime , choice .Delta .Content )
679+ contentChunk := createContentChunk (respID , createdTime , hunyuanReq . Model , choice .Delta .Content )
647680 w .Write ([]byte ("data: " + string (contentChunk ) + "\n \n " ))
648681 flusher .Flush ()
649682 }
650683
651684 if choice .Delta .ReasoningContent != "" {
652685 // 发送推理内容块
653- reasoningChunk := createReasoningChunk (respID , createdTime , choice .Delta .ReasoningContent )
686+ reasoningChunk := createReasoningChunk (respID , createdTime , hunyuanReq . Model , choice .Delta .ReasoningContent )
654687 w .Write ([]byte ("data: " + string (reasoningChunk ) + "\n \n " ))
655688 flusher .Flush ()
656689 }
@@ -659,7 +692,7 @@ func handleStreamingRequest(w http.ResponseWriter, r *http.Request, hunyuanReq H
659692 if choice .FinishReason != nil {
660693 finishReason := * choice .FinishReason
661694 if finishReason != "" {
662- doneChunk := createDoneChunk (respID , createdTime , finishReason )
695+ doneChunk := createDoneChunk (respID , createdTime , hunyuanReq . Model , finishReason )
663696 w .Write ([]byte ("data: " + string (doneChunk ) + "\n \n " ))
664697 flusher .Flush ()
665698 }
@@ -670,7 +703,7 @@ func handleStreamingRequest(w http.ResponseWriter, r *http.Request, hunyuanReq H
670703
671704 // 发送结束信号(如果没有正常结束)
672705 finishReason := "stop"
673- doneChunk := createDoneChunk (respID , createdTime , finishReason )
706+ doneChunk := createDoneChunk (respID , createdTime , hunyuanReq . Model , finishReason )
674707 w .Write ([]byte ("data: " + string (doneChunk ) + "\n \n " ))
675708 w .Write ([]byte ("data: [DONE]\n \n " ))
676709 flusher .Flush ()
@@ -830,12 +863,12 @@ func extractFullContentFromStream(bodyBytes []byte, reqID string) (string, strin
830863}
831864
832865// 创建角色块
833- func createRoleChunk (id string , created int64 ) []byte {
866+ func createRoleChunk (id string , created int64 , model string ) []byte {
834867 chunk := StreamChunk {
835868 ID : id ,
836869 Object : "chat.completion.chunk" ,
837870 Created : created ,
838- Model : appConfig . ModelName ,
871+ Model : model ,
839872 Choices : []struct {
840873 Index int `json:"index"`
841874 FinishReason * string `json:"finish_reason,omitempty"`
@@ -863,12 +896,12 @@ func createRoleChunk(id string, created int64) []byte {
863896}
864897
865898// 创建内容块
866- func createContentChunk (id string , created int64 , content string ) []byte {
899+ func createContentChunk (id string , created int64 , model string , content string ) []byte {
867900 chunk := StreamChunk {
868901 ID : id ,
869902 Object : "chat.completion.chunk" ,
870903 Created : created ,
871- Model : appConfig . ModelName ,
904+ Model : model ,
872905 Choices : []struct {
873906 Index int `json:"index"`
874907 FinishReason * string `json:"finish_reason,omitempty"`
@@ -896,12 +929,12 @@ func createContentChunk(id string, created int64, content string) []byte {
896929}
897930
898931// 创建推理内容块
899- func createReasoningChunk (id string , created int64 , reasoningContent string ) []byte {
932+ func createReasoningChunk (id string , created int64 , model string , reasoningContent string ) []byte {
900933 chunk := StreamChunk {
901934 ID : id ,
902935 Object : "chat.completion.chunk" ,
903936 Created : created ,
904- Model : appConfig . ModelName ,
937+ Model : model ,
905938 Choices : []struct {
906939 Index int `json:"index"`
907940 FinishReason * string `json:"finish_reason,omitempty"`
@@ -929,13 +962,13 @@ func createReasoningChunk(id string, created int64, reasoningContent string) []b
929962}
930963
931964// 创建完成块
932- func createDoneChunk (id string , created int64 , reason string ) []byte {
965+ func createDoneChunk (id string , created int64 , model string , reason string ) []byte {
933966 finishReason := reason
934967 chunk := StreamChunk {
935968 ID : id ,
936969 Object : "chat.completion.chunk" ,
937970 Created : created ,
938- Model : appConfig . ModelName ,
971+ Model : model ,
939972 Choices : []struct {
940973 Index int `json:"index"`
941974 FinishReason * string `json:"finish_reason,omitempty"`
0 commit comments