one-api/relay/channel/anthropic/model.go
Laisky.Cai fdde066252 Refactor: Anthropic model response proto update
- Refactor content response

- Update channel adaptor to support `claude_model`
- Remove `null` stop reasons from content responses
- Add logging for error responses
- Change content start, ping, and message delta types to return true
- Set stop reason to end turn when response does not include a stop reason
- Set content response stop reason to null
- Add error handling for unmarshalling stream responses
- Rename `Completion` to `Text` in type definitions and `StopReason` to `Delta.StopReason`
- Count tokens in the `Delta.Text` field of the response instead of `Completion`
- Remove `Model` from the full text response
- Trim \r from incoming data
2024-03-05 05:05:57 +00:00

49 lines
1.2 KiB
Go

package anthropic
import (
"github.com/songquanpeng/one-api/relay/model"
)
type Metadata struct {
UserId string `json:"user_id"`
}
type Request struct {
model.GeneralOpenAIRequest
// System anthropic messages API use system to represent the system prompt
System string `json:"system"`
}
type Error struct {
Type string `json:"type"`
Message string `json:"message"`
}
type ResponseType string
const (
TypeError ResponseType = "error"
TypeStart ResponseType = "message_start"
TypeContentStart ResponseType = "content_block_start"
TypeContent ResponseType = "content_block_delta"
TypePing ResponseType = "ping"
TypeContentStop ResponseType = "content_block_stop"
TypeMessageDelta ResponseType = "message_delta"
TypeMessageStop ResponseType = "message_stop"
)
// https://docs.anthropic.com/claude/reference/messages-streaming
type Response struct {
Type ResponseType `json:"type"`
Index int `json:"index,omitempty"`
Delta struct {
Type string `json:"type,omitempty"`
Text string `json:"text,omitempty"`
StopReason string `json:"stop_reason,omitempty"`
} `json:"delta,omitempty"`
Error struct {
Type string `json:"type"`
Message string `json:"message"`
} `json:"error,omitempty"`
}