From 49c9c21d576101e581feec75aa5d0e6baa4177d7 Mon Sep 17 00:00:00 2001 From: songjvcheng Date: Tue, 30 Dec 2025 17:40:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B8=BAguiji=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/llm_handler.go | 41 +++++++++++++++++++++++++++++++++++++++-- service/llm_service.go | 32 ++++++++++++++++++++++++-------- 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/handler/llm_handler.go b/handler/llm_handler.go index 717b485..10d8b51 100644 --- a/handler/llm_handler.go +++ b/handler/llm_handler.go @@ -2,7 +2,10 @@ package handler import ( "encoding/json" + "fmt" + "hash/fnv" "net/http" + "strconv" "time" "gongzheng_minimax/service" @@ -10,6 +13,14 @@ import ( "github.com/gin-gonic/gin" ) +type ChatExtRequest struct { + Sid string `json:"sid"` + DhCode string `json:"dh-code"` + DhQuestion string `json:"dh-question"` // 映射为 content + DhConversationID string `json:"dh-conversation-id"` // 需要转换为 conversation_id + DhContext []interface{} `json:"dh-context"` // 上下文 +} + // LLMHandler handles HTTP requests for the LLM service type LLMHandler struct { llmService *service.LLMService @@ -81,13 +92,39 @@ func (h *LLMHandler) Chat(c *gin.Context) { // ChatExt handles external QA chat requests func (h *LLMHandler) ChatExt(c *gin.Context) { - var requestData map[string]interface{} + var requestData ChatExtRequest if err := c.ShouldBindJSON(&requestData); err != nil { + fmt.Printf("Error binding JSON: %v\n", err) c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request data"}) return } - response, err := h.llmService.CallExtQAAPI(requestData) + fmt.Printf("Received ChatExt request: %+v\n", requestData) + + // 将UUID转换为19位数字字符串 + h64 := fnv.New64a() + h64.Write([]byte(requestData.DhConversationID)) + hashValue := h64.Sum64() + conversationID := strconv.FormatUint(hashValue, 10) + // 确保长度为19位,不足补0,超过截取(FNV64通常是19-20位数字) + if len(conversationID) < 19 { + conversationID = fmt.Sprintf("%019s", conversationID) + } else if len(conversationID) > 19 { + conversationID = conversationID[:19] + } + + fmt.Printf("Converted ConversationID: %s -> %s\n", requestData.DhConversationID, conversationID) + + // 构造 Service 层需要的参数 map + serviceData := map[string]interface{}{ + "tag_ids": []string{"1", "2"}, + "conversation_id": conversationID, + "content": requestData.DhQuestion, + } + + fmt.Printf("Calling Service with data: %+v\n", serviceData) + + response, err := h.llmService.CallExtQAAPI(serviceData) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return diff --git a/service/llm_service.go b/service/llm_service.go index b45d5ca..5009a75 100644 --- a/service/llm_service.go +++ b/service/llm_service.go @@ -129,9 +129,9 @@ type LLMOurRequestPayload struct { // ExtQARequestPayload represents the payload for the external QA API type ExtQARequestPayload struct { - TagIDs []int `json:"tag_ids"` - ConversationID string `json:"conversation_id"` - Content string `json:"content"` + TagIDs []string `json:"tag_ids"` + ConversationID string `json:"conversation_id"` + Content string `json:"content"` } // ExtQAResponse represents the response from the external QA API @@ -1091,11 +1091,18 @@ func (s *LLMService) TrimAudioSilence(audioData string) (string, error) { // CallExtQAAPI handles the external QA API call func (s *LLMService) CallExtQAAPI(data map[string]interface{}) (interface{}, error) { - var tagIDs []int - if tagIDsRaw, ok := data["tag_ids"].([]interface{}); ok { + var tagIDs []string + // 优先尝试直接断言为 []string + if ids, ok := data["tag_ids"].([]string); ok { + tagIDs = ids + } else if tagIDsRaw, ok := data["tag_ids"].([]interface{}); ok { for _, v := range tagIDsRaw { - if id, ok := v.(float64); ok { - tagIDs = append(tagIDs, int(id)) + // 尝试转换为 string + if id, ok := v.(string); ok { + tagIDs = append(tagIDs, id) + } else if id, ok := v.(float64); ok { + // 兼容数字类型,转为字符串 + tagIDs = append(tagIDs, fmt.Sprintf("%d", int(id))) } } } @@ -1112,6 +1119,8 @@ func (s *LLMService) CallExtQAAPI(data map[string]interface{}) (interface{}, err } url := "http://47.100.108.206:30028/api/qa/v1/chat/completionForExt" + fmt.Printf("Sending request to %s with payload: %s\n", url, string(jsonData)) + req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { return nil, fmt.Errorf("error creating request: %v", err) @@ -1126,10 +1135,15 @@ func (s *LLMService) handleStreamingResponseForExt(req *http.Request, data map[s client := &http.Client{} resp, err := client.Do(req) if err != nil { + fmt.Printf("Error making external request: %v\n", err) return nil, fmt.Errorf("error making request: %v", err) } + fmt.Printf("External API response status: %d\n", resp.StatusCode) + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + fmt.Printf("External API error body: %s\n", string(body)) resp.Body.Close() return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) } @@ -1164,9 +1178,11 @@ func (s *LLMService) handleStreamingResponseForExt(req *http.Request, data map[s continue } + fmt.Printf("Processing line: %s\n", line) + var response ExtQAResponse if err := json.Unmarshal([]byte(line), &response); err != nil { - // fmt.Printf("Error unmarshaling JSON: %v, line: %s\n", err, line) + fmt.Printf("Error unmarshaling JSON: %v, line: %s\n", err, line) continue }