From 5414683ff14e47721afa685b20d89aa3139ccc7e Mon Sep 17 00:00:00 2001
From: SanaeFox <36219542+Hoshinonyaruko@users.noreply.github.com>
Date: Mon, 20 May 2024 00:50:41 +0800
Subject: [PATCH] Beta125 (#124)

* beta1

* beta2

* beta3

* beta4

* beta5

* beta6

* beta7

* beta8

* beta9

* beta10

* beta11

* beta12

* beta13

* beta14

* beta15

* beta16

* beta16

* beta19

* beta20

* beta21

* beta22

* beta23

* beta24

* beta25

* beta27

* beta28

* beta29

* beta30

* beta31

* beta33

* beta34

* beta35

* beta36

* beta37

* beta38

* beta39

* beta40

* beta41

* beta42

* beta43

* beta44

* beta45

* beta45

* beta46

* beat48

* beta49

* beta50

* beta51

* beta52

* beta53

* beta54

* beta55

* beta57

* beta58

* beta59

* beta61

* beta62

* beta63

* beta63

* beta64

* beta65

* beta66

* beta67

* beta70

* beta71

* beta72

* beta72

* beta74

* beta75

* beta76

* beta77

* beta78

* beta79

* beta80

* beta81

* beta82

* beta83

* beta85

* beta86

* beta87

* beta88

* beta89

* beta90

* beta91

* beta92

* beta93

* beta94

* beta94

* beta96

* beta97

* beta98

* beta99

* beta100

* beta101

* beta102

* beta104

* beta105

* beta106

* beta107

* beta108

* beta109

* beta110

* beta111

* beta112

* beta113

* beta115

* beta116

* beta117

* beta118

* beta119

* beta120

* beta121

* beta122

* beta123

* beta124

* beta125
---
 applogic/chatglm.go  |  2 +-
 applogic/gensokyo.go |  2 +-
 applogic/hunyuan.go  | 46 ++++++++++++++++++++++++++++++++------------
 config/config.go     | 32 ++++++++++++++++++++++++++++++
 4 files changed, 68 insertions(+), 14 deletions(-)

diff --git a/applogic/chatglm.go b/applogic/chatglm.go
index 14b4aac..382f57a 100644
--- a/applogic/chatglm.go
+++ b/applogic/chatglm.go
@@ -59,7 +59,7 @@ func (app *App) ChatHandlerGlm(w http.ResponseWriter, r *http.Request) {
 
 	// 读取URL参数 "userid"
 	useridstr := r.URL.Query().Get("userid")
-	if promptstr != "" {
+	if useridstr != "" {
 		// prompt 参数存在,可以根据需要进一步处理或记录
 		fmtf.Printf("Received userid parameter: %s\n", useridstr)
 	}
diff --git a/applogic/gensokyo.go b/applogic/gensokyo.go
index eb5c435..9c320ed 100644
--- a/applogic/gensokyo.go
+++ b/applogic/gensokyo.go
@@ -558,7 +558,7 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
 		}
 
 		// glm会根据userid参数来封禁用户
-		if config.GetApiType() == 5 {
+		if config.GetApiType() == 5 || basePath == "/conversation_glm" {
 			urlParams.Add("userid", strconv.FormatInt(message.UserID, 10))
 		}
 
diff --git a/applogic/hunyuan.go b/applogic/hunyuan.go
index 2efd13a..a8d96ae 100644
--- a/applogic/hunyuan.go
+++ b/applogic/hunyuan.go
@@ -7,6 +7,7 @@ import (
 	"strings"
 	"sync"
 
+	"github.com/google/uuid"
 	"github.com/hoshinonyaruko/gensokyo-llm/config"
 	"github.com/hoshinonyaruko/gensokyo-llm/fmtf"
 	"github.com/hoshinonyaruko/gensokyo-llm/hunyuan"
@@ -15,8 +16,12 @@ import (
 	"github.com/hoshinonyaruko/gensokyo-llm/utils"
 )
 
-var messageBuilder strings.Builder
-var groupUserMessages sync.Map
+var (
+	messageBuilder               strings.Builder
+	groupUserMessages            sync.Map
+	mutexhunyuan                 sync.Mutex
+	lastCompleteResponseshunyuan sync.Map // 存储每个conversationId的完整累积信息
+)
 
 func (app *App) ChatHandlerHunyuan(w http.ResponseWriter, r *http.Request) {
 	if r.Method != "POST" {
@@ -655,10 +660,16 @@ func (app *App) ChatHandlerHunyuan(w http.ResponseWriter, r *http.Request) {
 				return
 			}
 
-			var responseTextBuilder strings.Builder
-			var totalUsage structs.UsageInfo
+			// 生成一个随机的UUID
+			randomUUID, err := uuid.NewRandom()
+			if err != nil {
+				http.Error(w, "Failed to generate UUID", http.StatusInternalServerError)
+				return
+			}
 
+			var totalUsage structs.UsageInfo // 有并发问题
 			for event := range response.BaseSSEResponse.Events {
+
 				if event.Err != nil {
 					fmtf.Fprintf(w, "data: %s\n\n", fmtf.Sprintf("接收事件时发生错误: %v", event.Err))
 					flusher.Flush()
@@ -673,13 +684,24 @@ func (app *App) ChatHandlerHunyuan(w http.ResponseWriter, r *http.Request) {
 					continue
 				}
 
+				// 在修改共享资源之前锁定Mutex
+				mutexhunyuan.Lock()
+				// 由于同一个上下文中 msg.ConversationID是相同的,而我们要区分更细粒度 所以添加UUID openai的api则设计了更细粒度的stramid,可以直接使用
+				conversationId := msg.ConversationID + randomUUID.String()
+				// 读取完整信息
+				completeResponse, _ := lastCompleteResponseshunyuan.LoadOrStore(conversationId, "")
+				// 提取出本次请求的响应
 				responseText, usageInfo := utils.ExtractEventDetails(eventData)
-				responseTextBuilder.WriteString(responseText)
+				// 更新存储的完整累积信息
+				updatedCompleteResponse := completeResponse.(string) + responseText
+				lastCompleteResponseshunyuan.Store(conversationId, updatedCompleteResponse)
+				// 完成修改后解锁Mutex
+				mutexhunyuan.Unlock()
+
 				totalUsage.PromptTokens += usageInfo.PromptTokens
 				totalUsage.CompletionTokens += usageInfo.CompletionTokens
 
-				// 发送当前事件的响应数据,但不包含assistantMessageID
-				//fmtf.Printf("发送当前事件的响应数据,但不包含assistantMessageID\n")
+				// 构建并发送当前事件的响应数据
 				tempResponseMap := map[string]interface{}{
 					"response":       responseText,
 					"conversationId": msg.ConversationID,
@@ -693,12 +715,13 @@ func (app *App) ChatHandlerHunyuan(w http.ResponseWriter, r *http.Request) {
 			}
 
 			// 处理完所有事件后,生成并发送包含assistantMessageID的最终响应
-			responseText := responseTextBuilder.String()
-			fmtf.Printf("处理完所有事件后,生成并发送包含assistantMessageID的最终响应:%v\n", responseText)
+			conversationId := msg.ConversationID + randomUUID.String()
+			completeResponse, _ := lastCompleteResponseshunyuan.LoadOrStore(conversationId, "")
+			fmtf.Printf("处理完所有事件后,生成并发送包含assistantMessageID的最终响应:%v\n", completeResponse.(string))
 			assistantMessageID, err := app.addMessage(structs.Message{
 				ConversationID:  msg.ConversationID,
 				ParentMessageID: userMessageID,
-				Text:            responseText,
+				Text:            completeResponse.(string),
 				Role:            "assistant",
 			})
 
@@ -708,7 +731,7 @@ func (app *App) ChatHandlerHunyuan(w http.ResponseWriter, r *http.Request) {
 			}
 
 			finalResponseMap := map[string]interface{}{
-				"response":       responseText,
+				"response":       completeResponse.(string),
 				"conversationId": msg.ConversationID,
 				"messageId":      assistantMessageID,
 				"details": map[string]interface{}{
@@ -720,7 +743,6 @@ func (app *App) ChatHandlerHunyuan(w http.ResponseWriter, r *http.Request) {
 			flusher.Flush()
 		}
 	}
-
 }
 
 func truncateHistoryHunYuan(history []structs.Message, prompt string, promptstr string) []structs.Message {
diff --git a/config/config.go b/config/config.go
index 5fda48c..cb2f2f9 100644
--- a/config/config.go
+++ b/config/config.go
@@ -450,6 +450,10 @@ func getGptTokenInternal(options ...string) string {
 		return getGptTokenInternal() // 递归调用内部函数,不传递任何参数
 	}
 
+	if gptToken == "" {
+		return getGptTokenInternal() // 递归调用内部函数,不传递任何参数
+	}
+
 	return gptToken
 }
 
@@ -484,6 +488,10 @@ func getMaxTokenGptInternal(options ...string) int {
 		return getMaxTokenGptInternal() // 递归调用内部函数,不传递任何参数
 	}
 
+	if maxTokenGpt == 0 {
+		return getMaxTokenGptInternal() // 递归调用内部函数,不传递任何参数
+	}
+
 	return maxTokenGpt
 }
 
@@ -2486,6 +2494,10 @@ func getGlmApiPathInternal(options ...string) string {
 		return getGlmApiPathInternal() // 递归调用内部函数,不传递任何参数
 	}
 
+	if apiPath == "" {
+		return getGlmApiPathInternal() // 递归调用内部函数,不传递任何参数
+	}
+
 	return apiPath
 }
 
@@ -2520,6 +2532,10 @@ func getGlmModelInternal(options ...string) string {
 		return getGlmModelInternal() // 递归调用内部函数,不传递任何参数
 	}
 
+	if model == "" {
+		return getGlmModelInternal() // 递归调用内部函数,不传递任何参数
+	}
+
 	return model
 }
 
@@ -2554,6 +2570,10 @@ func getGlmApiKeyInternal(options ...string) string {
 		return getGlmApiKeyInternal() // 递归调用内部函数,不传递任何参数
 	}
 
+	if apiKey == "" {
+		return getGlmApiKeyInternal() // 递归调用内部函数,不传递任何参数
+	}
+
 	return apiKey
 }
 
@@ -2588,6 +2608,10 @@ func getGlmMaxTokensInternal(options ...string) int {
 		return getGlmMaxTokensInternal() // 递归调用内部函数,不传递任何参数
 	}
 
+	if maxTokens == 0 {
+		return getGlmMaxTokensInternal() // 递归调用内部函数,不传递任何参数
+	}
+
 	return maxTokens
 }
 
@@ -2622,6 +2646,10 @@ func getGlmTemperatureInternal(options ...string) float64 {
 		return getGlmTemperatureInternal() // 递归调用内部函数,不传递任何参数
 	}
 
+	if temperature == 0 {
+		return getGlmTemperatureInternal() // 递归调用内部函数,不传递任何参数
+	}
+
 	return temperature
 }
 
@@ -2696,6 +2724,10 @@ func getGlmTopPInternal(options ...string) float64 {
 		return getGlmTopPInternal() // 递归调用内部函数,不传递任何参数
 	}
 
+	if topP == 0 {
+		return getGlmTopPInternal() // 递归调用内部函数,不传递任何参数
+	}
+
 	return topP
 }