fix claude event id buf error

This commit is contained in:
Zhang Minghan 2023-10-29 20:10:43 +08:00
parent e9f107b561
commit cb70452baf
2 changed files with 38 additions and 20 deletions

View File

@ -66,37 +66,55 @@ func (c *ChatInstance) CreateChatRequest(props *ChatProps) (string, error) {
return "", fmt.Errorf("claude error: invalid response") return "", fmt.Errorf("claude error: invalid response")
} }
func (c *ChatInstance) ProcessLine(buf, data string) (string, error) {
// response example:
//
// event:completion
// data:{"completion":"!","stop_reason":null,"model":"claude-2.0","stop":null,"log_id":"f5f659a5807419c94cfac4a9f2f79a66e95733975714ce7f00e30689dd136b02"}
if !strings.HasPrefix(data, "data:") && strings.HasPrefix(data, "event:") {
return "", nil
} else {
data = strings.TrimSpace(strings.TrimPrefix(data, "data:"))
}
if len(data) == 0 {
return "", nil
}
if form := utils.UnmarshalForm[ChatResponse](data); form != nil {
return form.Completion, nil
}
data = buf + data
if form := utils.UnmarshalForm[ChatResponse](data); form != nil {
return form.Completion, nil
}
globals.Warn(fmt.Sprintf("anthropic error: cannot parse response: %s", data))
return "", fmt.Errorf("claude error: invalid response")
}
// CreateStreamChatRequest is the stream request for anthropic claude // CreateStreamChatRequest is the stream request for anthropic claude
func (c *ChatInstance) CreateStreamChatRequest(props *ChatProps, hook globals.Hook) error { func (c *ChatInstance) CreateStreamChatRequest(props *ChatProps, hook globals.Hook) error {
buf := ""
return utils.EventSource( return utils.EventSource(
"POST", "POST",
c.GetChatEndpoint(), c.GetChatEndpoint(),
c.GetChatHeaders(), c.GetChatHeaders(),
c.GetChatBody(props, true), c.GetChatBody(props, true),
func(data string) error { func(data string) error {
// response example:
//
// event:completion
// data:{"completion":"!","stop_reason":null,"model":"claude-2.0","stop":null,"log_id":"f5f659a5807419c94cfac4a9f2f79a66e95733975714ce7f00e30689dd136b02"}
if !strings.HasPrefix(data, "data:") && strings.HasPrefix(data, "event:") { if resp, err := c.ProcessLine(buf, data); err == nil && len(resp) > 0 {
return nil buf = ""
} else { if err := hook(resp); err != nil {
data = strings.TrimSpace(strings.TrimPrefix(data, "data:"))
}
if len(data) == 0 {
return nil
}
if form := utils.UnmarshalForm[ChatResponse](data); form != nil {
if err := hook(form.Completion); err != nil {
return err return err
} }
return nil } else {
buf = buf + data
} }
globals.Warn(fmt.Sprintf("anthropic error: cannot parse response: %s", data))
return nil return nil
}) })
} }

View File

@ -32,8 +32,8 @@ func init() {
Logger.SetOutput(&lumberjack.Logger{ Logger.SetOutput(&lumberjack.Logger{
Filename: "logs/chat.log", Filename: "logs/chat.log",
MaxSize: 20, MaxSize: 1,
MaxBackups: 20, MaxBackups: 500,
MaxAge: 1, MaxAge: 1,
}) })