Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beta130 #129

Merged
merged 128 commits into from
May 24, 2024
Merged
Changes from 1 commit
Commits
Show all changes
128 commits
Select commit Hold shift + click to select a range
ea4bcc6
beta1
Hoshinonyaruko Jan 21, 2024
5002a28
beta2
Hoshinonyaruko Jan 21, 2024
969841e
beta3
Hoshinonyaruko Jan 21, 2024
a2989c2
beta4
Hoshinonyaruko Jan 21, 2024
611cd8b
beta5
Hoshinonyaruko Jan 21, 2024
dbad42b
beta6
Hoshinonyaruko Jan 21, 2024
4f8895e
beta7
Hoshinonyaruko Jan 21, 2024
30a9cab
beta8
Hoshinonyaruko Jan 21, 2024
461d879
beta9
Hoshinonyaruko Jan 21, 2024
c90c2ee
beta10
Hoshinonyaruko Jan 21, 2024
5227c32
beta11
Hoshinonyaruko Jan 21, 2024
8e14e56
beta12
Hoshinonyaruko Jan 21, 2024
6adfb7f
beta13
Hoshinonyaruko Jan 21, 2024
b04f6c0
beta14
Hoshinonyaruko Jan 21, 2024
649e740
beta15
Hoshinonyaruko Jan 21, 2024
d55cf29
beta16
Hoshinonyaruko Jan 21, 2024
1cc351e
beta16
Hoshinonyaruko Jan 21, 2024
f1a373e
beta19
Hoshinonyaruko Jan 21, 2024
cbf2fe1
beta20
Hoshinonyaruko Jan 21, 2024
3485bfc
beta21
Hoshinonyaruko Jan 21, 2024
3e3bb1a
beta22
Hoshinonyaruko Jan 21, 2024
2e4f3fa
beta23
Hoshinonyaruko Mar 29, 2024
f74075d
beta24
Hoshinonyaruko Mar 29, 2024
f3913cf
beta25
Hoshinonyaruko Mar 29, 2024
4cfb51a
beta27
Hoshinonyaruko Mar 30, 2024
d9b26c0
beta28
Hoshinonyaruko Mar 31, 2024
19da709
beta29
Hoshinonyaruko Mar 31, 2024
e06dcbf
merge
Hoshinonyaruko Mar 31, 2024
c76923e
beta30
Hoshinonyaruko Mar 31, 2024
9ae21da
beta31
Hoshinonyaruko Apr 1, 2024
4f333bc
merge
Hoshinonyaruko Apr 1, 2024
5acba8f
beta33
Hoshinonyaruko Apr 1, 2024
c637fcd
beta34
Hoshinonyaruko Apr 1, 2024
4e85b37
beta35
Hoshinonyaruko Apr 1, 2024
970948e
beta36
Hoshinonyaruko Apr 1, 2024
14271c3
beta37
Hoshinonyaruko Apr 1, 2024
6154d1a
beta38
Hoshinonyaruko Apr 1, 2024
d5be5e6
beta39
Hoshinonyaruko Apr 3, 2024
54afe18
beta40
Hoshinonyaruko Apr 3, 2024
0b71855
beta41
Hoshinonyaruko Apr 3, 2024
8c5f9a3
merge
Hoshinonyaruko Apr 3, 2024
cf752d4
beta42
Hoshinonyaruko Apr 3, 2024
3698824
beta43
Hoshinonyaruko Apr 3, 2024
1e9c189
beta44
Hoshinonyaruko Apr 3, 2024
2adb4ac
beta45
Hoshinonyaruko Apr 4, 2024
79eb713
beta45
Hoshinonyaruko Apr 4, 2024
064f0d0
beta45
Hoshinonyaruko Apr 4, 2024
5a4a59e
beta46
Hoshinonyaruko Apr 5, 2024
df30a32
beta46
Hoshinonyaruko Apr 5, 2024
c920893
beat48
Hoshinonyaruko Apr 7, 2024
3491d5d
beta49
Hoshinonyaruko Apr 8, 2024
575b2ca
beta50
Hoshinonyaruko Apr 8, 2024
4c4bbaa
beta51
Hoshinonyaruko Apr 8, 2024
2ea728b
beta52
Hoshinonyaruko Apr 8, 2024
82bf952
beta53
Hoshinonyaruko Apr 8, 2024
869685c
beta54
Hoshinonyaruko Apr 9, 2024
1f3646e
beta55
Hoshinonyaruko Apr 9, 2024
d6bbadf
beta57
Hoshinonyaruko Apr 10, 2024
fc5086c
beta58
Hoshinonyaruko Apr 10, 2024
602ff65
beta59
Hoshinonyaruko Apr 10, 2024
a82538d
Merge branch 'main' of https://github.com/Hoshinonyaruko/Gensokyo-llm…
Hoshinonyaruko Apr 10, 2024
6433600
beta61
Hoshinonyaruko Apr 10, 2024
9ce88b3
beta62
Hoshinonyaruko Apr 12, 2024
b53617b
beta63
Hoshinonyaruko Apr 12, 2024
ff64c69
beta63
Hoshinonyaruko Apr 12, 2024
b34885f
beta64
Hoshinonyaruko Apr 13, 2024
3658648
beta65
Hoshinonyaruko Apr 13, 2024
3307439
beta66
Hoshinonyaruko Apr 15, 2024
f15c422
beta67
Hoshinonyaruko Apr 17, 2024
518de35
beta70
Hoshinonyaruko Apr 18, 2024
a65f07d
beta71
Hoshinonyaruko Apr 18, 2024
59d07c7
beta72
Hoshinonyaruko Apr 18, 2024
a16ea64
beta72
Hoshinonyaruko Apr 18, 2024
08d5864
beta74
Hoshinonyaruko Apr 19, 2024
9b09401
beta75
Hoshinonyaruko Apr 19, 2024
b51e185
beta76
Hoshinonyaruko Apr 20, 2024
c978654
beta77
Hoshinonyaruko Apr 21, 2024
6aa9f33
beta78
Hoshinonyaruko Apr 22, 2024
7b84a9d
beta79
Hoshinonyaruko Apr 25, 2024
4b96807
beta80
Hoshinonyaruko Apr 25, 2024
9b97146
beta81
Hoshinonyaruko Apr 25, 2024
f481e85
beta82
Hoshinonyaruko Apr 29, 2024
cb33ba5
beta83
Hoshinonyaruko Apr 29, 2024
7dbe4d5
beta85
Hoshinonyaruko Apr 29, 2024
2fb67a2
beta86
Hoshinonyaruko Apr 29, 2024
20f26c1
beta87
Hoshinonyaruko Apr 29, 2024
3146232
beta88
Hoshinonyaruko Apr 29, 2024
cbf731b
beta89
Hoshinonyaruko Apr 29, 2024
cc8a439
beta90
Hoshinonyaruko Apr 29, 2024
f56c272
beta91
Hoshinonyaruko Apr 29, 2024
c19f071
beta92
Hoshinonyaruko Apr 29, 2024
84d0513
beta93
Hoshinonyaruko Apr 29, 2024
c2f8e28
beta94
Hoshinonyaruko Apr 29, 2024
142f2fa
beta94
Hoshinonyaruko Apr 29, 2024
8828006
beta96
Hoshinonyaruko Apr 29, 2024
541a127
beta97
Hoshinonyaruko Apr 30, 2024
9d52ce8
beta98
Hoshinonyaruko Apr 30, 2024
16e0ed4
beta99
Hoshinonyaruko Apr 30, 2024
8f0e406
beta100
Hoshinonyaruko May 1, 2024
d242bc2
beta101
Hoshinonyaruko May 1, 2024
2ea5f4a
beta102
Hoshinonyaruko May 2, 2024
c208482
beta104
Hoshinonyaruko May 3, 2024
777aaa0
beta105
Hoshinonyaruko May 4, 2024
3ccedea
beta106
Hoshinonyaruko May 4, 2024
956c9a2
beta107
Hoshinonyaruko May 8, 2024
1a04702
beta108
Hoshinonyaruko May 8, 2024
d88d84b
beta109
Hoshinonyaruko May 8, 2024
e9a3788
beta110
Hoshinonyaruko May 9, 2024
a6dccf7
beta111
Hoshinonyaruko May 13, 2024
48a1d26
beta112
Hoshinonyaruko May 13, 2024
5cbd1a6
beta113
Hoshinonyaruko May 13, 2024
a8f8dbd
Merge branch 'main' of https://github.com/Hoshinonyaruko/Gensokyo-llm…
Hoshinonyaruko May 13, 2024
0ce3929
beta115
Hoshinonyaruko May 13, 2024
3c24828
beta116
Hoshinonyaruko May 14, 2024
d17b87e
beta117
Hoshinonyaruko May 14, 2024
a0d9d93
beta118
Hoshinonyaruko May 16, 2024
81c5f60
beta119
Hoshinonyaruko May 17, 2024
0bf4eae
beta120
Hoshinonyaruko May 19, 2024
dfff20e
beta121
Hoshinonyaruko May 19, 2024
6659607
beta122
Hoshinonyaruko May 19, 2024
89c92a0
beta123
Hoshinonyaruko May 19, 2024
a35581e
beta124
Hoshinonyaruko May 19, 2024
434c738
beta125
Hoshinonyaruko May 19, 2024
18e97cc
beta126
Hoshinonyaruko May 20, 2024
49cfe96
beta127
Hoshinonyaruko May 20, 2024
6c98d9f
beta128
Hoshinonyaruko May 23, 2024
3301dc2
beta129
Hoshinonyaruko May 24, 2024
32b6189
beta130
Hoshinonyaruko May 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
beta57
Hoshinonyaruko committed Apr 10, 2024
commit d6bbadf2c38b98b169d657fbfa8c3b1f7d430a70
317 changes: 317 additions & 0 deletions applogic/ernie_function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,317 @@
package applogic

import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"

"github.com/hoshinonyaruko/gensokyo-llm/config"
"github.com/hoshinonyaruko/gensokyo-llm/fmtf"
"github.com/hoshinonyaruko/gensokyo-llm/structs"
"github.com/hoshinonyaruko/gensokyo-llm/utils"
)

//var mutexErnie sync.Mutex

func (app *App) ChatHandlerErnieFunction(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Only POST method is allowed", http.StatusMethodNotAllowed)
return
}

var msg structs.WXRequestMessageF
err := json.NewDecoder(r.Body).Decode(&msg)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
msg.Role = "user"
//颠倒用户输入
if config.GetReverseUserPrompt() {
msg.Text = utils.ReverseString(msg.Text)
}

if msg.ConversationID == "" {
msg.ConversationID = utils.GenerateUUID()
app.createConversation(msg.ConversationID)
}

//转换一下
tempmsg := structs.Message{
ConversationID: msg.ConversationID,
ParentMessageID: msg.ParentMessageID,
Text: msg.Text,
Role: msg.Role,
CreatedAt: msg.CreatedAt,
}

userMessageID, err := app.addMessage(tempmsg)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// 构建请求负载
var payload structs.WXRequestPayloadF

// 添加当前用户消息
payload.Messages = append(payload.Messages, structs.WXMessage{
Content: msg.Text,
Role: "user",
})

TopP := config.GetWenxinTopp()
PenaltyScore := config.GetWnxinPenaltyScore()
MaxOutputTokens := config.GetWenxinMaxOutputTokens()

// 设置其他可选参数
payload.TopP = TopP
payload.PenaltyScore = PenaltyScore
payload.MaxOutputTokens = MaxOutputTokens
// 增加function
payload.Functions = append(payload.Functions, msg.WXFunction)
//payload.ResponseFormat = "json_object"
payload.ToolChoice.Type = "function"
payload.ToolChoice.Function.Name = "predict_followup_questions"

// 是否sse
if config.GetuseSse() {
payload.Stream = true
}

// 获取系统提示词,并设置system字段,如果它不为空
systemPromptContent := config.SystemPrompt() // 确保函数名正确
if systemPromptContent != "0" {
payload.System = systemPromptContent // 直接在请求负载中设置system字段
}

// 获取访问凭证和API路径
accessToken := config.GetWenxinAccessToken()
apiPath := config.GetWenxinApiPath()

// 构建请求URL
url := fmtf.Sprintf("%s?access_token=%s", apiPath, accessToken)
fmtf.Printf("%v\n", url)

// 序列化请求负载
jsonData, err := json.Marshal(payload)
if err != nil {
log.Fatalf("Error occurred during marshaling. Error: %s", err.Error())
}

fmtf.Printf("%v\n", string(jsonData))

// 创建并发送POST请求
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
log.Fatalf("Error occurred during request creation. Error: %s", err.Error())
}
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Error occurred during sending the request. Error: %s", err.Error())
}
defer resp.Body.Close()

// 读取响应头中的速率限制信息
rateLimitRequests := resp.Header.Get("X-Ratelimit-Limit-Requests")
rateLimitTokens := resp.Header.Get("X-Ratelimit-Limit-Tokens")
remainingRequests := resp.Header.Get("X-Ratelimit-Remaining-Requests")
remainingTokens := resp.Header.Get("X-Ratelimit-Remaining-Tokens")

fmtf.Printf("RateLimit: Requests %s, Tokens %s, Remaining Requests %s, Remaining Tokens %s\n",
rateLimitRequests, rateLimitTokens, remainingRequests, remainingTokens)

// 检查是否不使用SSE
if !config.GetuseSse() {
// 读取整个响应体到内存中
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Error occurred during response body reading. Error: %s", err)
}

// 首先尝试解析为简单的map来查看响应概览
var response map[string]interface{}
if err := json.Unmarshal(bodyBytes, &response); err != nil {
log.Fatalf("Error occurred during response decoding to map. Error: %s", err)
}
fmtf.Printf("%v\n", response)

// 然后尝试解析为具体的结构体以获取详细信息
var responseStruct struct {
ID string `json:"id"`
Object string `json:"object"`
Created int `json:"created"`
SentenceID int `json:"sentence_id,omitempty"`
IsEnd bool `json:"is_end,omitempty"`
IsTruncated bool `json:"is_truncated"`
Result string `json:"result"`
NeedClearHistory bool `json:"need_clear_history"`
BanRound int `json:"ban_round"`
Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
} `json:"usage"`
}

if err := json.Unmarshal(bodyBytes, &responseStruct); err != nil {
http.Error(w, fmtf.Sprintf("解析响应体出错: %v", err), http.StatusInternalServerError)
return
}
// 根据API响应构造消息和响应给客户端
assistantMessageID, err := app.addMessage(structs.Message{
ConversationID: msg.ConversationID,
ParentMessageID: userMessageID,
Text: responseStruct.Result,
Role: "assistant",
})

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// 构造响应
responseMap := map[string]interface{}{
"response": responseStruct.Result,
"conversationId": msg.ConversationID,
"messageId": assistantMessageID,
"details": map[string]interface{}{
"usage": map[string]int{
"prompt_tokens": responseStruct.Usage.PromptTokens,
"completion_tokens": responseStruct.Usage.CompletionTokens,
"total_tokens": responseStruct.Usage.TotalTokens,
},
},
}

// 设置响应头信息以反映速率限制状态
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Ratelimit-Limit-Requests", rateLimitRequests)
w.Header().Set("X-Ratelimit-Limit-Tokens", rateLimitTokens)
w.Header().Set("X-Ratelimit-Remaining-Requests", remainingRequests)
w.Header().Set("X-Ratelimit-Remaining-Tokens", remainingTokens)

// 发送JSON响应
json.NewEncoder(w).Encode(responseMap)
} else {
// SSE响应模式
// 设置SSE相关的响应头部
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")

flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
return
}

var responseTextBuilder strings.Builder
var totalUsage structs.UsageInfo

// 假设我们已经建立了与API的连接并且开始接收流式响应
// reader代表从API接收数据的流
reader := bufio.NewReader(resp.Body)
for {
// 读取流中的一行,即一个事件数据块
line, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
// 流结束
break
}
// 处理错误
fmtf.Fprintf(w, "data: %s\n\n", fmtf.Sprintf("读取流数据时发生错误: %v", err))
flusher.Flush()
continue
}

// 处理流式数据行
if strings.HasPrefix(line, "data: ") {
eventDataJSON := line[6:] // 去掉"data: "前缀

var eventData struct {
ID string `json:"id"`
Object string `json:"object"`
Created int `json:"created"`
SentenceID int `json:"sentence_id,omitempty"`
IsEnd bool `json:"is_end,omitempty"`
IsTruncated bool `json:"is_truncated"`
Result string `json:"result"`
NeedClearHistory bool `json:"need_clear_history"`
BanRound int `json:"ban_round"`
Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
} `json:"usage"`
}
// 解析JSON数据
if err := json.Unmarshal([]byte(eventDataJSON), &eventData); err != nil {
fmtf.Fprintf(w, "data: %s\n\n", fmtf.Sprintf("解析事件数据出错: %v", err))
flusher.Flush()
continue
}

// 这里处理解析后的事件数据
responseTextBuilder.WriteString(eventData.Result)
totalUsage.PromptTokens += eventData.Usage.PromptTokens
totalUsage.CompletionTokens += eventData.Usage.CompletionTokens

// 发送当前事件的响应数据,但不包含assistantMessageID
tempResponseMap := map[string]interface{}{
"response": eventData.Result,
"conversationId": msg.ConversationID,
"details": map[string]interface{}{
"usage": eventData.Usage,
},
}
tempResponseJSON, _ := json.Marshal(tempResponseMap)
fmtf.Fprintf(w, "data: %s\n\n", string(tempResponseJSON))
flusher.Flush()

// 如果这是最后一个消息
if eventData.IsEnd {
break
}
}
}

// 处理完所有事件后,生成并发送包含assistantMessageID的最终响应
//fmt.Printf("处理完所有事件后,生成并发送包含assistantMessageID的最终响应\n")
responseText := responseTextBuilder.String()
assistantMessageID, err := app.addMessage(structs.Message{
ConversationID: msg.ConversationID,
ParentMessageID: userMessageID,
Text: responseText,
Role: "assistant",
})

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

finalResponseMap := map[string]interface{}{
"response": responseText,
"conversationId": msg.ConversationID,
"messageId": assistantMessageID,
"details": map[string]interface{}{
"usage": totalUsage,
},
}
finalResponseJSON, _ := json.Marshal(finalResponseMap)
fmt.Fprintf(w, "data: %s\n\n", string(finalResponseJSON))
flusher.Flush()
}

}
10 changes: 9 additions & 1 deletion applogic/gensokyo.go
Original file line number Diff line number Diff line change
@@ -487,8 +487,16 @@ func (app *App) GensokyoHandler(w http.ResponseWriter, r *http.Request) {
}
if message.RealMessageType == "group_private" || message.MessageType == "private" {
if config.GetUsePrivateSSE() {

//发气泡和按钮
promptkeyboard := config.GetPromptkeyboard()
var promptkeyboard []string
if !config.GetUseAIPromptkeyboard() {
promptkeyboard = config.GetPromptkeyboard()
} else {
fmtf.Printf("ai生成气泡:%v", "Q"+newmsg+"A"+response)
promptkeyboard = GetPromptKeyboardAI("Q" + newmsg + "A" + response)
}

//最后一条了
messageSSE := structs.InterfaceBody{
Content: " " + "\n",
Loading