Skip to content

Commit

Permalink
fix: update robustness result decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Dec 11, 2023
1 parent f280e20 commit 0d45c5b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
20 changes: 20 additions & 0 deletions adapter/chatgpt/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"chat/utils"
"errors"
"fmt"
"regexp"
"strings"
)

Expand Down Expand Up @@ -157,6 +158,21 @@ func getToolCalls(form *ChatStreamResponse) *globals.ToolCalls {
return form.Data.Choices[0].Delta.ToolCalls
}

func getRobustnessResult(chunk string) string {
exp := `\"content\":\"(.*?)\"`
compile, err := regexp.Compile(exp)
if err != nil {
return ""
}

matches := compile.FindStringSubmatch(chunk)
if len(matches) > 1 {
return matches[1]
} else {
return ""
}
}

func (c *ChatInstance) ProcessLine(obj utils.Buffer, instruct bool, buf, data string) (string, error) {
item := processFormat(buf + data)
if isDone(item) {
Expand All @@ -177,6 +193,10 @@ func (c *ChatInstance) ProcessLine(obj utils.Buffer, instruct bool, buf, data st
}

if err := processChatErrorResponse(item); err == nil || err.Data.Error.Message == "" {
if res := getRobustnessResult(item); res != "" {
return res, nil
}

globals.Warn(fmt.Sprintf("chatgpt error: cannot parse response: %s", item))
return data, errors.New("parser error: cannot parse response")
} else {
Expand Down
20 changes: 20 additions & 0 deletions adapter/oneapi/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"chat/utils"
"errors"
"fmt"
"regexp"
"strings"
)

Expand Down Expand Up @@ -80,6 +81,21 @@ func getToolCalls(form *ChatStreamResponse) *globals.ToolCalls {
return form.Data.Choices[0].Delta.ToolCalls
}

func getRobustnessResult(chunk string) string {
exp := `\"content\":\"(.*?)\"`
compile, err := regexp.Compile(exp)
if err != nil {
return ""
}

matches := compile.FindStringSubmatch(chunk)
if len(matches) > 1 {
return matches[1]
} else {
return ""
}
}

func (c *ChatInstance) ProcessLine(obj utils.Buffer, buf, data string) (string, error) {
item := processFormat(buf + data)
if isDone(item) {
Expand All @@ -93,6 +109,10 @@ func (c *ChatInstance) ProcessLine(obj utils.Buffer, buf, data string) (string,
}

if err := processChatErrorResponse(item); err == nil {
if res := getRobustnessResult(item); res != "" {
return res, nil
}

globals.Warn(fmt.Sprintf("oneapi error: cannot parse response: %s", item))
return data, errors.New("parser error: cannot parse response")
} else {
Expand Down
13 changes: 13 additions & 0 deletions channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"chat/utils"
"errors"
"fmt"
"net/url"
"strings"
)

Expand Down Expand Up @@ -74,6 +75,14 @@ func (c *Channel) GetEndpoint() string {
return c.Endpoint
}

func (c *Channel) GetDomain() string {
if instance, err := url.Parse(c.GetEndpoint()); err == nil {
return instance.Host
}

return c.GetEndpoint()
}

func (c *Channel) GetMapper() string {
return c.Mapper
}
Expand Down Expand Up @@ -148,5 +157,9 @@ func (c *Channel) ProcessError(err error) error {
content = strings.Replace(content, c.GetEndpoint(), replacer, -1)
}

if domain := c.GetDomain(); strings.Contains(content, domain) {
content = strings.Replace(content, domain, "channel", -1)
}

return errors.New(content)
}

0 comments on commit 0d45c5b

Please sign in to comment.