Skip to content

Commit

Permalink
style: you 消息合并处理修改
Browse files Browse the repository at this point in the history
  • Loading branch information
bincooo committed Jun 24, 2024
1 parent d869907 commit 0d6a8a9
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/bincooo/coze-api v1.0.2-0.20240620163352-ad627f0dffd2
github.com/bincooo/edge-api v1.0.4-0.20240620163255-3f676529cccd
github.com/bincooo/emit.io v0.0.0-20240622171207-c5018480b050
github.com/bincooo/you.com v0.0.0-20240623210154-24050c06a717
github.com/bincooo/you.com v0.0.0-20240624043209-de5389f9bd83
github.com/dlclark/regexp2 v1.7.0
github.com/eko/gocache/lib/v4 v4.1.6
github.com/eko/gocache/store/go_cache/v4 v4.2.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ github.com/bincooo/emit.io v0.0.0-20240622171207-c5018480b050 h1:vVstCrqaYprk3l8
github.com/bincooo/emit.io v0.0.0-20240622171207-c5018480b050/go.mod h1:Tag5SFgOt/R1ZN9YZ/DDZK/elTWHf1mNnRv4oh4TMOE=
github.com/bincooo/requests v0.0.0-20230720064210-7eae5d6c9d1e h1:38ztKJW0K6qQGitqAlcJs8O2nal60qYoS9oNZnpkZWE=
github.com/bincooo/requests v0.0.0-20230720064210-7eae5d6c9d1e/go.mod h1:0WuzYU+4cQL/hVbjoncY5TACMTbD9I+pLCdnPjfItp0=
github.com/bincooo/you.com v0.0.0-20240623210154-24050c06a717 h1:waPgEZJlj3Wx/+b4uBtZsWUaB/y7H2n+BcSrVRyf2SA=
github.com/bincooo/you.com v0.0.0-20240623210154-24050c06a717/go.mod h1:1QlkSxFhT1VGFFbWMCM31wnF2Jzuagk6Or15B/sjxXw=
github.com/bincooo/you.com v0.0.0-20240624043209-de5389f9bd83 h1:K4zmRo2+90Otn8uf7CgGdsIRuDYGOMw4pQDycU7HhM0=
github.com/bincooo/you.com v0.0.0-20240624043209-de5389f9bd83/go.mod h1:1QlkSxFhT1VGFFbWMCM31wnF2Jzuagk6Or15B/sjxXw=
github.com/bitly/go-simplejson v0.5.0 h1:6IH+V8/tVMab511d5bn4M7EwGXZf9Hj6i2xSwkNEM+Y=
github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
Expand Down
26 changes: 24 additions & 2 deletions internal/plugin/llm/you/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,29 @@ func (API) Completion(ctx *gin.Context) {
return
}

var (
retry = 3
cookies []string
)

defer func(cookies []string) {
if len(cookies) == 0 {
return
}
for _, value := range cookies {
resetMarker(value)
}
}(cookies)

label:
retry--
cookie, err := youRollContainer.Poll()
if err != nil {
logger.Error(err)
response.Error(ctx, -1, err)
return
}
defer resetMarker(cookie)
cookies = append(cookies, cookie)

var (
proxies = ctx.GetString("proxies")
Expand Down Expand Up @@ -177,8 +193,9 @@ func (API) Completion(ctx *gin.Context) {
var cancel chan error
cancel, matchers = joinMatchers(ctx, matchers)
ctx.Set(ginTokens, tokens)
is32 := tokens < 12000

ch, err := chat.Reply(common.GetGinContext(ctx), pMessages, currMessage, true /*tokens >= 32*1000*/)
ch, err := chat.Reply(common.GetGinContext(ctx), pMessages, currMessage, !is32)
if err != nil {
logger.Error(err)
var se emit.Error
Expand All @@ -196,6 +213,10 @@ func (API) Completion(ctx *gin.Context) {
_ = youRollContainer.SetMarker(cookie, 2)
code = 429
}

if retry > 0 {
goto label
}
response.Error(ctx, code, err)
return
}
Expand Down Expand Up @@ -279,6 +300,7 @@ func condition(cookie string) bool {
return false
}

//return true
chat := you.New(cookie, you.CLAUDE_2, vars.Proxies)
chat.Client(plugin.HTTPClient)
chat.CloudFlare(clearance, userAgent)
Expand Down
40 changes: 24 additions & 16 deletions internal/plugin/llm/you/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,7 @@ func mergeMessages(completion pkg.ChatCompletion) (pMessages []you.Message, text
tokens += common.CalcTokens(message.GetString("content"))
}

is32 := false
//if tokens < 32*1000 {
// is32 = true
//}

is32 := tokens < 12000
// 合并历史对话
iterator := func(opts struct {
Previous string
Expand All @@ -147,18 +143,23 @@ func mergeMessages(completion pkg.ChatCompletion) (pMessages []you.Message, text
}

prefix := ""
if !is32 && cond(role) == "user" {
prefix = "Human: "
if role == "user" && len(opts.Message["content"]) > 0 {
if !strings.HasPrefix(opts.Message["content"], "Assistant:") {
prefix = "Human: "
}
}
opts.Buffer.WriteString(prefix + opts.Message["content"])
return
}

defer opts.Buffer.Reset()
prefix := ""
if !is32 && cond(role) == "user" {
prefix = "Human: "
if role == "user" && len(opts.Message["content"]) > 0 {
if !strings.HasPrefix(opts.Message["content"], "Assistant:") {
prefix = "Human: "
}
}

opts.Buffer.WriteString(prefix + opts.Message["content"])
result = append(result, map[string]string{
"role": cond(role),
Expand All @@ -176,7 +177,9 @@ func mergeMessages(completion pkg.ChatCompletion) (pMessages []you.Message, text
text = "Please review the attached prompt"

// 获取最后一条用户消息
okey := ""
if is32 {
okey = "ok ~"
messageL := len(newMessages)
message := newMessages[messageL-1]
if message["role"] == "user" {
Expand All @@ -199,22 +202,27 @@ func mergeMessages(completion pkg.ChatCompletion) (pMessages []you.Message, text
if message["role"] == "user" {
newMessage.Question = message["content"]
} else {
newMessage.Question = " "
newMessage.Question = okey
}

pos++
if pos >= messageL-1 {
newMessage.Answer = "ok ~"
if pos > messageL-1 {
newMessage.Answer = okey
pMessages = append(pMessages, newMessage)
break
}

message = newMessages[pos]
if message["role"] == "assistant" {
prefix := ""
if !is32 {
prefix = "Assistant: "
prefix := "Assistant: "
newMessage.Answer = prefix + message["content"]
} else {
newMessage.Answer = okey
pMessages = append(pMessages, newMessage)
newMessage = you.Message{
Question: message["content"],
Answer: "",
}
newMessage.Question = prefix + message["content"]
}
pMessages = append(pMessages, newMessage)
pos++
Expand Down
2 changes: 1 addition & 1 deletion internal/plugin/llm/you/toolcall.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func completeToolCalls(ctx *gin.Context, cookie, proxies string, completion pkg.
return
}
for _, value := range cookies {
_ = youRollContainer.SetMarker(value, 0)
resetMarker(value)
}
}(cookies)

Expand Down

0 comments on commit 0d6a8a9

Please sign in to comment.