Skip to content

Commit

Permalink
fix(coze): bufio.Scanner: token too long
Browse files Browse the repository at this point in the history
  • Loading branch information
bincooo committed Sep 16, 2024
1 parent e15614f commit bb50730
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 24 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/antonfisher/nested-logrus-formatter v1.3.1
github.com/bincooo/claude-api v1.0.5-0.20240814184031-56ad31d90db8
github.com/bincooo/cohere-api v0.0.0-20240620172202-4b7697448b46
github.com/bincooo/coze-api v1.0.2-0.20240828163652-7f48a7682de1
github.com/bincooo/coze-api v1.0.2-0.20240916011933-70b4afd71f2a
github.com/bincooo/edge-api v1.0.4-0.20240801160633-4131c13986a5
github.com/bincooo/emit.io v1.0.1-0.20240814182208-c5236f8f202c
github.com/bincooo/vecmul.com v0.0.0-20240806095224-6e571cbe7b7e
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ github.com/bincooo/claude-api v1.0.5-0.20240814184031-56ad31d90db8 h1:YTa3k9RjFS
github.com/bincooo/claude-api v1.0.5-0.20240814184031-56ad31d90db8/go.mod h1:1C9dP0HFj9CjYUfz4l3gyoNGXKafPlXDTfBaIrh8ZLY=
github.com/bincooo/cohere-api v0.0.0-20240620172202-4b7697448b46 h1:12sHtk9r99icBCTBnsHaYep5DjH4gu6AzceFNSf216U=
github.com/bincooo/cohere-api v0.0.0-20240620172202-4b7697448b46/go.mod h1:X2PunEb/JOvEnkKkQnlm27UZnn96TR53hGH5TZ55Ak4=
github.com/bincooo/coze-api v1.0.2-0.20240828163652-7f48a7682de1 h1:nLgQQD6i4+NXRVGrSyvPsDG3/mF+tPcV2PStKEKubdc=
github.com/bincooo/coze-api v1.0.2-0.20240828163652-7f48a7682de1/go.mod h1:DbHf5dv8h0bGrQoWoCyVTFJzsfqTOb7asqzn3gTfTas=
github.com/bincooo/coze-api v1.0.2-0.20240916011933-70b4afd71f2a h1:DhtIVdNcucEIJQFy1KZe3O6njDSkzkpUrPWYM2SdlRw=
github.com/bincooo/coze-api v1.0.2-0.20240916011933-70b4afd71f2a/go.mod h1:DbHf5dv8h0bGrQoWoCyVTFJzsfqTOb7asqzn3gTfTas=
github.com/bincooo/edge-api v1.0.4-0.20240801160633-4131c13986a5 h1:LXwjKFfwyOSinPSKMRI8mRNUkkLhJoexl8jGn/t9ZOk=
github.com/bincooo/edge-api v1.0.4-0.20240801160633-4131c13986a5/go.mod h1:OBBrqmW8rUMTsF/sPH1GsPQT5K9QKJCZkHdYkTIR09Q=
github.com/bincooo/emit.io v1.0.1-0.20240814182208-c5236f8f202c h1:+E1m0U39B0AA54+/p75+yz+GoN1yAFQ99SCuRs9tRsM=
Expand Down
2 changes: 1 addition & 1 deletion internal/common/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func Test_download(t *testing.T) {
file, err := Download("http://127.0.0.1:7890", "https://krebzonide-sdxl-turbo-with-refiner.hf.space/file=/tmp/gradio/e8cec4458822c7cd2308e8d36949cd3c1c446196/image.png", "png")
file, err := Download(nil, "http://127.0.0.1:7890", "https://krebzonide-sdxl-turbo-with-refiner.hf.space/file=/tmp/gradio/e8cec4458822c7cd2308e8d36949cd3c1c446196/image.png", "png", nil)
if err != nil {
t.Fatal(err)
}
Expand Down
32 changes: 19 additions & 13 deletions internal/common/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,33 @@ func TestMessageCombiner(t *testing.T) {
return ""
}
}
nMessages := TextMessageCombiner[map[string]string](messages, func(previous, next string, message map[string]string, buffer *bytes.Buffer) []map[string]string {
role := message["role"]
if condition(role) != condition(next) {
if buffer.Len() != 0 {
buffer.WriteByte('\n')
nMessages, _ := TextMessageCombiner[map[string]string](messages, func(opts struct {
Previous string
Next string
Message map[string]string
Buffer *bytes.Buffer
Initial func() pkg.Keyv[interface{}]
}) ([]map[string]string, error) {
role := opts.Message["role"]
if condition(role) != condition(opts.Next) {
if opts.Buffer.Len() != 0 {
opts.Buffer.WriteByte('\n')
}
buffer.WriteString(message["content"])
defer buffer.Reset()
opts.Buffer.WriteString(opts.Message["content"])
defer opts.Buffer.Reset()
return []map[string]string{
{
"role": condition(role),
"content": buffer.String(),
"content": opts.Buffer.String(),
},
}
}, nil
}

if buffer.Len() != 0 {
buffer.WriteByte('\n')
if opts.Buffer.Len() != 0 {
opts.Buffer.WriteByte('\n')
}
buffer.WriteString(message["content"])
return nil
opts.Buffer.WriteString(opts.Message["content"])
return nil, nil
})

for _, msg := range nMessages {
Expand Down
6 changes: 3 additions & 3 deletions internal/plugin/llm/coze/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (API) Completion(ctx *gin.Context) {
}
}

tor := func(r string) string {
toRole := func(r string) string {
switch r {
case "user":
return user
Expand Down Expand Up @@ -272,11 +272,11 @@ func (API) Completion(ctx *gin.Context) {
} else {
var newP []coze.Message
for _, message := range pMessages {
message.Role = tor(message.Role)
message.Role = toRole(message.Role)
newP = append(newP, message)
}
query = coze.MergeMessages(newP)
query = query[:len(query)-13] + "<|" + tor("assistant") + "|>"
query = query[:len(query)-13] + "<|" + toRole("assistant") + "|>"
}

chatResponse, err := chat.Reply(common.GetGinContext(ctx), coze.Text, query)
Expand Down
6 changes: 6 additions & 0 deletions internal/plugin/llm/coze/websdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ func initTasks(opts ...*obj) (exec bool) {
continue
}

if cookies, ok := value.keyv["cookies"]; ok {
logger.Info("已有cookies:", cookies)
cookiesPollContainer.Add(value.keyv)
continue
}

timeout, cancel := context.WithTimeout(context.Background(), 120*time.Second)
payload := make(map[string]interface{})
copyMap(payload, value.keyv)
Expand Down
3 changes: 3 additions & 0 deletions internal/plugin/llm/interpreter/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func waitResponse(ctx *gin.Context, matchers []common.Matcher, r *http.Response,

for {
if !scanner.Scan() {
if err := scanner.Err(); err != nil {
logger.Error(err)
}
break
}

Expand Down
8 changes: 7 additions & 1 deletion internal/plugin/llm/v1/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func waitMessage(r *http.Response, cancel func(str string) bool) (content string

for {
if !scanner.Scan() {
if err = scanner.Err(); err != nil {
logger.Error(err)
}
break
}

Expand All @@ -55,7 +58,7 @@ func waitMessage(r *http.Response, cancel func(str string) bool) (content string
var chat pkg.ChatResponse
err = json.Unmarshal([]byte(data), &chat)
if err != nil {
logger.Error(err.Error())
logger.Error(err)
continue
}

Expand Down Expand Up @@ -116,6 +119,9 @@ func waitResponse(ctx *gin.Context, matchers []common.Matcher, r *http.Response,

for {
if !scanner.Scan() {
if err := scanner.Err(); err != nil {
logger.Error(err)
}
break
}

Expand Down
3 changes: 0 additions & 3 deletions internal/vars/js/src/script.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const Config = {
"FullColon": true,
"xmlPlot": true,
"SkipRestricted": false,
"padtxt": "1000,1000,15000",
}
}

Expand Down Expand Up @@ -212,8 +211,6 @@ const xmlPlot_merge = (content, mergeTag, nonsys) => {

/******************************** */
const legacy = false, messagesAPI = !legacy && !/<\|completeAPI\|>/.test(prompt) || /<\|messagesAPI\|>/.test(prompt), fusion = true, wedge = '\r';
const stopSet = /<\|stopSet *(\[.*?\]) *\|>/.exec(prompt)?.[1], stopRevoke = /<\|stopRevoke *(\[.*?\]) *\|>/.exec(prompt)?.[1];
if (stop_sequences || stopSet || stopRevoke) stop_sequences = JSON.parse(stopSet || '[]').concat(stop_sequences).concat(['\n\nHuman:', '\n\nAssistant:']).filter(item => !JSON.parse(stopRevoke || '[]').includes(item) && item);
prompt = Config.Settings.xmlPlot ? xmlPlot(prompt, legacy) : apiKey ? `\n\nHuman: ${genericFixes(prompt)}\n\nAssistant:` : genericFixes(prompt).trim();
Config.Settings.FullColon && (prompt = !legacy ?
prompt.replace(fusion ? /\n(?!\nAssistant:\s*$)(?=\n(Human|Assistant):)/gs : apiKey ? /(?<!\n\nHuman:.*)\n(?=\nAssistant:)|\n(?=\nHuman:)(?!.*\n\nAssistant:)/gs : /\n(?=\n(Human|Assistant):)/g, '\n' + wedge) :
Expand Down
4 changes: 4 additions & 0 deletions pkg/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,7 @@ func (kv Keyv[V]) IsString(key string) bool {
}
return false
}

func (kv Keyv[V]) IsEmpty(key string) bool {
return kv.GetString(key) == ""
}

0 comments on commit bb50730

Please sign in to comment.