优化
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 38s

This commit is contained in:
宋居成 2025-12-30 18:12:43 +08:00
parent 1781cfcf29
commit 24f72168dd
2 changed files with 126 additions and 14 deletions

View File

@ -117,14 +117,14 @@ func (h *LLMHandler) ChatExt(c *gin.Context) {
// 构造 Service 层需要的参数 map // 构造 Service 层需要的参数 map
serviceData := map[string]interface{}{ serviceData := map[string]interface{}{
"tag_ids": []string{"1", "2"}, "tag_ids": []int{1, 11, 29},
"conversation_id": conversationID, "conversation_id": conversationID,
"content": requestData.DhQuestion, "content": requestData.DhQuestion,
} }
fmt.Printf("Calling Service with data: %+v\n", serviceData) fmt.Printf("Calling Service with data: %+v\n", serviceData)
response, err := h.llmService.CallExtQAAPI(serviceData) response, err := h.llmService.CallExtQAAPIStreamDirect(serviceData)
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return return

View File

@ -129,7 +129,7 @@ type LLMOurRequestPayload struct {
// ExtQARequestPayload represents the payload for the external QA API // ExtQARequestPayload represents the payload for the external QA API
type ExtQARequestPayload struct { type ExtQARequestPayload struct {
TagIDs []string `json:"tag_ids"` TagIDs []int `json:"tag_ids"`
ConversationID string `json:"conversation_id"` ConversationID string `json:"conversation_id"`
Content string `json:"content"` Content string `json:"content"`
} }
@ -1091,18 +1091,14 @@ func (s *LLMService) TrimAudioSilence(audioData string) (string, error) {
// CallExtQAAPI handles the external QA API call // CallExtQAAPI handles the external QA API call
func (s *LLMService) CallExtQAAPI(data map[string]interface{}) (interface{}, error) { func (s *LLMService) CallExtQAAPI(data map[string]interface{}) (interface{}, error) {
var tagIDs []string var tagIDs []int
// 优先尝试直接断言为 []string // 优先尝试直接断言为 []int
if ids, ok := data["tag_ids"].([]string); ok { if ids, ok := data["tag_ids"].([]int); ok {
tagIDs = ids tagIDs = ids
} else if tagIDsRaw, ok := data["tag_ids"].([]interface{}); ok { } else if tagIDsRaw, ok := data["tag_ids"].([]interface{}); ok {
for _, v := range tagIDsRaw { for _, v := range tagIDsRaw {
// 尝试转换为 string if id, ok := v.(float64); ok {
if id, ok := v.(string); ok { tagIDs = append(tagIDs, int(id))
tagIDs = append(tagIDs, id)
} else if id, ok := v.(float64); ok {
// 兼容数字类型,转为字符串
tagIDs = append(tagIDs, fmt.Sprintf("%d", int(id)))
} }
} }
} }
@ -1130,6 +1126,122 @@ func (s *LLMService) CallExtQAAPI(data map[string]interface{}) (interface{}, err
return s.handleStreamingResponseForExt(req, data) return s.handleStreamingResponseForExt(req, data)
} }
// CallExtQAAPIStreamDirect handles the external QA API call and streams the response directly
func (s *LLMService) CallExtQAAPIStreamDirect(data map[string]interface{}) (interface{}, error) {
var tagIDs []int
// 优先尝试直接断言为 []int
if ids, ok := data["tag_ids"].([]int); 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))
}
}
}
payload := ExtQARequestPayload{
TagIDs: tagIDs,
ConversationID: getString(data, "conversation_id"),
Content: getString(data, "content"),
}
jsonData, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("error marshaling payload: %v", 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)
}
req.Header.Set("Content-Type", "application/json")
return s.handleStreamingResponseForExtDirect(req, data)
}
// handleStreamingResponseForExtDirect processes streaming responses from the external QA API and returns Message channel
func (s *LLMService) handleStreamingResponseForExtDirect(req *http.Request, data map[string]interface{}) (chan Message, error) {
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)
}
messageChan := make(chan Message, 100)
go func() {
defer resp.Body.Close()
defer close(messageChan)
reader := bufio.NewReader(resp.Body)
conversationID := getString(data, "conversation_id")
// Use current time as task ID since external API might not provide it in every chunk
taskID := fmt.Sprintf("task_%d", time.Now().UnixNano())
for {
line, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
fmt.Printf("Error reading line: %v\n", err)
continue
}
line = strings.TrimSpace(line)
if line == "" {
continue
}
// Remove "data: " prefix if present
if strings.HasPrefix(line, "data:") {
line = strings.TrimSpace(strings.TrimPrefix(line, "data:"))
}
if line == "" {
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)
continue
}
// Direct forwarding logic
answer := response.Output.Text
isEnd := response.Output.FinishReason == "stop" || response.Output.FinishReason == "length"
messageChan <- Message{
Answer: answer,
IsEnd: isEnd,
ConversationID: conversationID,
TaskID: taskID,
}
if isEnd {
return
}
}
}()
return messageChan, nil
}
// handleStreamingResponseForExt processes streaming responses from the external QA API // handleStreamingResponseForExt processes streaming responses from the external QA API
func (s *LLMService) handleStreamingResponseForExt(req *http.Request, data map[string]interface{}) (chan Message, error) { func (s *LLMService) handleStreamingResponseForExt(req *http.Request, data map[string]interface{}) (chan Message, error) {
client := &http.Client{} client := &http.Client{}