优化为guiji格式
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 42s
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 42s
This commit is contained in:
parent
a5a4412644
commit
49c9c21d57
@ -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
|
||||
|
||||
@ -129,7 +129,7 @@ type LLMOurRequestPayload struct {
|
||||
|
||||
// ExtQARequestPayload represents the payload for the external QA API
|
||||
type ExtQARequestPayload struct {
|
||||
TagIDs []int `json:"tag_ids"`
|
||||
TagIDs []string `json:"tag_ids"`
|
||||
ConversationID string `json:"conversation_id"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user