Skip to content

Commit

Permalink
fix claude event id buf error
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Oct 29, 2023
1 parent e9f107b commit cb70452
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
54 changes: 36 additions & 18 deletions adapter/claude/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,37 +66,55 @@ func (c *ChatInstance) CreateChatRequest(props *ChatProps) (string, error) {
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
func (c *ChatInstance) CreateStreamChatRequest(props *ChatProps, hook globals.Hook) error {
buf := ""

return utils.EventSource(
"POST",
c.GetChatEndpoint(),
c.GetChatHeaders(),
c.GetChatBody(props, true),
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:") {
return nil
} else {
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 {
if resp, err := c.ProcessLine(buf, data); err == nil && len(resp) > 0 {
buf = ""
if err := hook(resp); err != nil {
return err
}
return nil
} else {
buf = buf + data
}

globals.Warn(fmt.Sprintf("anthropic error: cannot parse response: %s", data))
return nil
})
}
4 changes: 2 additions & 2 deletions globals/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func init() {

Logger.SetOutput(&lumberjack.Logger{
Filename: "logs/chat.log",
MaxSize: 20,
MaxBackups: 20,
MaxSize: 1,
MaxBackups: 500,
MaxAge: 1,
})

Expand Down

0 comments on commit cb70452

Please sign in to comment.