fix: update robustness result decoder

This commit is contained in:
Zhang Minghan 2023-12-11 15:44:12 +08:00
parent f280e20127
commit 0d45c5bc0b
3 changed files with 53 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import (
"chat/utils" "chat/utils"
"errors" "errors"
"fmt" "fmt"
"regexp"
"strings" "strings"
) )
@ -157,6 +158,21 @@ func getToolCalls(form *ChatStreamResponse) *globals.ToolCalls {
return form.Data.Choices[0].Delta.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) { func (c *ChatInstance) ProcessLine(obj utils.Buffer, instruct bool, buf, data string) (string, error) {
item := processFormat(buf + data) item := processFormat(buf + data)
if isDone(item) { if isDone(item) {
@ -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 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)) globals.Warn(fmt.Sprintf("chatgpt error: cannot parse response: %s", item))
return data, errors.New("parser error: cannot parse response") return data, errors.New("parser error: cannot parse response")
} else { } else {

View File

@ -5,6 +5,7 @@ import (
"chat/utils" "chat/utils"
"errors" "errors"
"fmt" "fmt"
"regexp"
"strings" "strings"
) )
@ -80,6 +81,21 @@ func getToolCalls(form *ChatStreamResponse) *globals.ToolCalls {
return form.Data.Choices[0].Delta.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) { func (c *ChatInstance) ProcessLine(obj utils.Buffer, buf, data string) (string, error) {
item := processFormat(buf + data) item := processFormat(buf + data)
if isDone(item) { if isDone(item) {
@ -93,6 +109,10 @@ func (c *ChatInstance) ProcessLine(obj utils.Buffer, buf, data string) (string,
} }
if err := processChatErrorResponse(item); err == nil { 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)) globals.Warn(fmt.Sprintf("oneapi error: cannot parse response: %s", item))
return data, errors.New("parser error: cannot parse response") return data, errors.New("parser error: cannot parse response")
} else { } else {

View File

@ -4,6 +4,7 @@ import (
"chat/utils" "chat/utils"
"errors" "errors"
"fmt" "fmt"
"net/url"
"strings" "strings"
) )
@ -74,6 +75,14 @@ func (c *Channel) GetEndpoint() string {
return c.Endpoint 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 { func (c *Channel) GetMapper() string {
return c.Mapper return c.Mapper
} }
@ -148,5 +157,9 @@ func (c *Channel) ProcessError(err error) error {
content = strings.Replace(content, c.GetEndpoint(), replacer, -1) 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) return errors.New(content)
} }