Skip to content

Commit

Permalink
Beta125 (#124)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Hoshinonyaruko authored May 19, 2024
1 parent ea82b87 commit 5414683
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
2 changes: 1 addition & 1 deletion applogic/chatglm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion applogic/gensokyo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
46 changes: 34 additions & 12 deletions applogic/hunyuan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" {
Expand Down Expand Up @@ -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()
Expand All @@ -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,
Expand All @@ -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",
})

Expand All @@ -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{}{
Expand All @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,10 @@ func getGptTokenInternal(options ...string) string {
return getGptTokenInternal() // 递归调用内部函数,不传递任何参数
}

if gptToken == "" {
return getGptTokenInternal() // 递归调用内部函数,不传递任何参数
}

return gptToken
}

Expand Down Expand Up @@ -484,6 +488,10 @@ func getMaxTokenGptInternal(options ...string) int {
return getMaxTokenGptInternal() // 递归调用内部函数,不传递任何参数
}

if maxTokenGpt == 0 {
return getMaxTokenGptInternal() // 递归调用内部函数,不传递任何参数
}

return maxTokenGpt
}

Expand Down Expand Up @@ -2486,6 +2494,10 @@ func getGlmApiPathInternal(options ...string) string {
return getGlmApiPathInternal() // 递归调用内部函数,不传递任何参数
}

if apiPath == "" {
return getGlmApiPathInternal() // 递归调用内部函数,不传递任何参数
}

return apiPath
}

Expand Down Expand Up @@ -2520,6 +2532,10 @@ func getGlmModelInternal(options ...string) string {
return getGlmModelInternal() // 递归调用内部函数,不传递任何参数
}

if model == "" {
return getGlmModelInternal() // 递归调用内部函数,不传递任何参数
}

return model
}

Expand Down Expand Up @@ -2554,6 +2570,10 @@ func getGlmApiKeyInternal(options ...string) string {
return getGlmApiKeyInternal() // 递归调用内部函数,不传递任何参数
}

if apiKey == "" {
return getGlmApiKeyInternal() // 递归调用内部函数,不传递任何参数
}

return apiKey
}

Expand Down Expand Up @@ -2588,6 +2608,10 @@ func getGlmMaxTokensInternal(options ...string) int {
return getGlmMaxTokensInternal() // 递归调用内部函数,不传递任何参数
}

if maxTokens == 0 {
return getGlmMaxTokensInternal() // 递归调用内部函数,不传递任何参数
}

return maxTokens
}

Expand Down Expand Up @@ -2622,6 +2646,10 @@ func getGlmTemperatureInternal(options ...string) float64 {
return getGlmTemperatureInternal() // 递归调用内部函数,不传递任何参数
}

if temperature == 0 {
return getGlmTemperatureInternal() // 递归调用内部函数,不传递任何参数
}

return temperature
}

Expand Down Expand Up @@ -2696,6 +2724,10 @@ func getGlmTopPInternal(options ...string) float64 {
return getGlmTopPInternal() // 递归调用内部函数,不传递任何参数
}

if topP == 0 {
return getGlmTopPInternal() // 递归调用内部函数,不传递任何参数
}

return topP
}

Expand Down

0 comments on commit 5414683

Please sign in to comment.