Documentation
¶
Index ¶
- Constants
- Variables
- type APIError
- func (e *APIError) Error() string
- func (e *APIError) IsApiErr() bool
- func (e *APIError) IsAuthenticationErr() bool
- func (e *APIError) IsInvalidRequestErr() bool
- func (e *APIError) IsNotFoundErr() bool
- func (e *APIError) IsOverloadedErr() bool
- func (e *APIError) IsPermissionErr() bool
- func (e *APIError) IsRateLimitErr() bool
- type Client
- func (c *Client) CreateComplete(ctx context.Context, request CompleteRequest) (response CompleteResponse, err error)
- func (c *Client) CreateCompleteStream(ctx context.Context, request CompleteStreamRequest) (response CompleteResponse, err error)
- func (c *Client) CreateMessages(ctx context.Context, request MessagesRequest) (response MessagesResponse, err error)
- func (c *Client) CreateMessagesStream(ctx context.Context, request MessagesStreamRequest) (response MessagesResponse, err error)
- type ClientConfig
- type ClientOption
- type CompleteEvent
- type CompleteRequest
- type CompleteResponse
- type CompleteStreamPingData
- type CompleteStreamRequest
- type ErrType
- type ErrorResponse
- type Message
- type MessageContent
- func NewImageMessageContent(source MessageContentImageSource) MessageContent
- func NewTextMessageContent(text string) MessageContent
- func NewToolResultMessageContent(toolUseID, content string, isError bool) MessageContent
- func NewToolUseMessageContent(toolUseID, name string, input json.RawMessage) MessageContent
- type MessageContentImageSource
- type MessageContentToolResult
- type MessageContentToolUse
- type MessagesContentType
- type MessagesEvent
- type MessagesEventContentBlockDeltaData
- type MessagesEventContentBlockStartData
- type MessagesEventContentBlockStopData
- type MessagesEventMessageDeltaData
- type MessagesEventMessageStartData
- type MessagesEventMessageStopData
- type MessagesEventPingData
- type MessagesRequest
- type MessagesResponse
- type MessagesResponseType
- type MessagesStopReason
- type MessagesStreamRequest
- type MessagesUsage
- type RateLimitHeaders
- type RequestError
- type Response
- type ToolChoice
- type ToolDefinition
Constants ¶
View Source
const ( ModelClaudeInstant1Dot2 = "claude-instant-1.2" ModelClaude2Dot0 = "claude-2.0" ModelClaude2Dot1 = "claude-2.1" ModelClaude3Opus20240229 = "claude-3-opus-20240229" ModelClaude3Sonnet20240229 = "claude-3-sonnet-20240229" ModelClaude3Dot5Sonnet20240620 = "claude-3-5-sonnet-20240620" ModelClaude3Haiku20240307 = "claude-3-haiku-20240307" )
View Source
const ( RoleUser = "user" RoleAssistant = "assistant" )
View Source
const ( BetaTools20240404 = "tools-2024-04-04" BetaTools20240516 = "tools-2024-05-16" )
View Source
const (
APIVersion20230601 = "2023-06-01"
)
Variables ¶
View Source
var (
ErrSteamingNotSupportTools = errors.New("streaming is not yet supported tools")
)
View Source
var (
ErrTooManyEmptyStreamMessages = errors.New("stream has sent too many empty messages")
)
Functions ¶
This section is empty.
Types ¶
type APIError ¶
APIError provides error information returned by the Anthropic API.
func (*APIError) IsAuthenticationErr ¶
func (*APIError) IsInvalidRequestErr ¶
func (*APIError) IsNotFoundErr ¶
func (*APIError) IsOverloadedErr ¶
func (*APIError) IsPermissionErr ¶
func (*APIError) IsRateLimitErr ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func NewClient ¶
func NewClient(apikey string, opts ...ClientOption) *Client
NewClient create new Anthropic API client
func (*Client) CreateComplete ¶
func (c *Client) CreateComplete(ctx context.Context, request CompleteRequest) (response CompleteResponse, err error)
func (*Client) CreateCompleteStream ¶
func (c *Client) CreateCompleteStream(ctx context.Context, request CompleteStreamRequest) (response CompleteResponse, err error)
func (*Client) CreateMessages ¶
func (c *Client) CreateMessages(ctx context.Context, request MessagesRequest) (response MessagesResponse, err error)
func (*Client) CreateMessagesStream ¶
func (c *Client) CreateMessagesStream(ctx context.Context, request MessagesStreamRequest) (response MessagesResponse, err error)
type ClientConfig ¶
type ClientConfig struct { BaseURL string APIVersion string BetaVersion string HTTPClient *http.Client EmptyMessagesLimit uint // contains filtered or unexported fields }
ClientConfig is a configuration of a client.
type ClientOption ¶
type ClientOption func(c *ClientConfig)
func WithAPIVersion ¶
func WithAPIVersion(apiVersion string) ClientOption
func WithBaseURL ¶
func WithBaseURL(baseUrl string) ClientOption
func WithBetaVersion ¶
func WithBetaVersion(betaVersion string) ClientOption
func WithEmptyMessagesLimit ¶
func WithEmptyMessagesLimit(limit uint) ClientOption
func WithHTTPClient ¶
func WithHTTPClient(cli *http.Client) ClientOption
type CompleteEvent ¶
type CompleteEvent string
const ( CompleteEventError CompleteEvent = "error" CompleteEventCompletion CompleteEvent = "completion" CompleteEventPing CompleteEvent = "ping" )
type CompleteRequest ¶
type CompleteRequest struct { Model string `json:"model"` Prompt string `json:"prompt"` MaxTokensToSample int `json:"max_tokens_to_sample"` StopSequences []string `json:"stop_sequences,omitempty"` Temperature *float32 `json:"temperature,omitempty"` TopP *float32 `json:"top_p,omitempty"` TopK *int `json:"top_k,omitempty"` MetaData map[string]any `json:"meta_data,omitempty"` Stream bool `json:"stream,omitempty"` }
func (*CompleteRequest) SetTemperature ¶
func (c *CompleteRequest) SetTemperature(t float32)
func (*CompleteRequest) SetTopK ¶
func (c *CompleteRequest) SetTopK(k int)
func (*CompleteRequest) SetTopP ¶
func (c *CompleteRequest) SetTopP(p float32)
type CompleteResponse ¶
type CompleteResponse struct { Type string `json:"type"` ID string `json:"id"` Completion string `json:"completion"` // possible values are: stop_sequence、max_tokens、null StopReason string `json:"stop_reason"` Model string `json:"model"` // contains filtered or unexported fields }
func (*CompleteResponse) GetRateLimitHeaders ¶
func (h *CompleteResponse) GetRateLimitHeaders() RateLimitHeaders
type CompleteStreamPingData ¶
type CompleteStreamPingData struct {
Type string `json:"type"`
}
type CompleteStreamRequest ¶
type CompleteStreamRequest struct { CompleteRequest OnCompletion func(CompleteResponse) `json:"-"` OnPing func(CompleteStreamPingData) `json:"-"` OnError func(ErrorResponse) `json:"-"` }
type ErrType ¶
type ErrType string
const ( // ErrTypeInvalidRequest There was an issue with the format or content of your request. ErrTypeInvalidRequest ErrType = "invalid_request_error" // ErrTypeAuthentication There's an issue with your API key. ErrTypeAuthentication ErrType = "authentication_error" // ErrTypePermission Your API key does not have permission to use the specified resource. ErrTypePermission ErrType = "permission_error" // ErrTypeNotFound The requested resource was not found. ErrTypeNotFound ErrType = "not_found_error" // ErrTypeRateLimit Your account has hit a rate limit. ErrTypeRateLimit ErrType = "rate_limit_error" // ErrTypeApi An unexpected error has occurred internal to Anthropic's systems. ErrTypeApi ErrType = "api_error" // ErrTypeOverloaded Anthropic's API is temporarily overloaded. ErrTypeOverloaded ErrType = "overloaded_error" )
type ErrorResponse ¶
type Message ¶
type Message struct { Role string `json:"role"` Content []MessageContent `json:"content"` }
func NewAssistantTextMessage ¶
func NewToolResultsMessage ¶
func NewUserTextMessage ¶
func (Message) GetFirstContent ¶
func (m Message) GetFirstContent() MessageContent
type MessageContent ¶
type MessageContent struct { Type MessagesContentType `json:"type"` Text *string `json:"text,omitempty"` Source *MessageContentImageSource `json:"source,omitempty"` *MessageContentToolResult *MessageContentToolUse PartialJson *string `json:"partial_json,omitempty"` }
func NewImageMessageContent ¶
func NewImageMessageContent(source MessageContentImageSource) MessageContent
func NewTextMessageContent ¶
func NewTextMessageContent(text string) MessageContent
func NewToolResultMessageContent ¶
func NewToolResultMessageContent(toolUseID, content string, isError bool) MessageContent
func NewToolUseMessageContent ¶
func NewToolUseMessageContent(toolUseID, name string, input json.RawMessage) MessageContent
func (*MessageContent) ConcatText ¶
func (m *MessageContent) ConcatText(s string)
func (*MessageContent) GetText ¶
func (m *MessageContent) GetText() string
func (*MessageContent) MergeContentDelta ¶
func (m *MessageContent) MergeContentDelta(mc MessageContent)
type MessageContentToolResult ¶
type MessageContentToolResult struct { ToolUseID *string `json:"tool_use_id,omitempty"` Content []MessageContent `json:"content,omitempty"` IsError *bool `json:"is_error,omitempty"` }
func NewMessageContentToolResult ¶
func NewMessageContentToolResult(toolUseID, content string, isError bool) *MessageContentToolResult
type MessageContentToolUse ¶
type MessageContentToolUse struct { ID string `json:"id,omitempty"` Name string `json:"name,omitempty"` Input json.RawMessage `json:"input,omitempty"` }
func (*MessageContentToolUse) UnmarshalInput ¶
func (c *MessageContentToolUse) UnmarshalInput(v any) error
type MessagesContentType ¶
type MessagesContentType string
const ( MessagesContentTypeText MessagesContentType = "text" MessagesContentTypeTextDelta MessagesContentType = "text_delta" MessagesContentTypeImage MessagesContentType = "image" MessagesContentTypeToolResult MessagesContentType = "tool_result" MessagesContentTypeToolUse MessagesContentType = "tool_use" MessagesContentTypeInputJsonDelta MessagesContentType = "input_json_delta" )
type MessagesEvent ¶
type MessagesEvent string
MessagesEvent docs: https://docs.anthropic.com/claude/reference/messages-streaming
const ( MessagesEventError MessagesEvent = "error" MessagesEventMessageStart MessagesEvent = "message_start" MessagesEventContentBlockStart MessagesEvent = "content_block_start" MessagesEventPing MessagesEvent = "ping" MessagesEventContentBlockDelta MessagesEvent = "content_block_delta" MessagesEventContentBlockStop MessagesEvent = "content_block_stop" MessagesEventMessageDelta MessagesEvent = "message_delta" MessagesEventMessageStop MessagesEvent = "message_stop" )
type MessagesEventContentBlockDeltaData ¶
type MessagesEventContentBlockDeltaData struct { Type string `json:"type"` Index int `json:"index"` Delta MessageContent `json:"delta"` }
type MessagesEventContentBlockStartData ¶
type MessagesEventContentBlockStartData struct { Type MessagesEvent `json:"type"` Index int `json:"index"` ContentBlock MessageContent `json:"content_block"` }
type MessagesEventMessageDeltaData ¶
type MessagesEventMessageDeltaData struct { Type string `json:"type"` Delta MessagesResponse `json:"delta"` Usage MessagesUsage `json:"usage"` }
type MessagesEventMessageStartData ¶
type MessagesEventMessageStartData struct { Type MessagesEvent `json:"type"` Message MessagesResponse `json:"message"` }
type MessagesEventMessageStopData ¶
type MessagesEventMessageStopData struct {
Type string `json:"type"`
}
type MessagesEventPingData ¶
type MessagesEventPingData struct {
Type string `json:"type"`
}
type MessagesRequest ¶
type MessagesRequest struct { Model string `json:"model"` Messages []Message `json:"messages"` MaxTokens int `json:"max_tokens"` System string `json:"system,omitempty"` Metadata map[string]any `json:"metadata,omitempty"` StopSequences []string `json:"stop_sequences,omitempty"` Stream bool `json:"stream,omitempty"` Temperature *float32 `json:"temperature,omitempty"` TopP *float32 `json:"top_p,omitempty"` TopK *int `json:"top_k,omitempty"` Tools []ToolDefinition `json:"tools,omitempty"` ToolChoice *ToolChoice `json:"tool_choice,omitempty"` }
func (*MessagesRequest) SetTemperature ¶
func (m *MessagesRequest) SetTemperature(t float32)
func (*MessagesRequest) SetTopK ¶
func (m *MessagesRequest) SetTopK(k int)
func (*MessagesRequest) SetTopP ¶
func (m *MessagesRequest) SetTopP(p float32)
type MessagesResponse ¶
type MessagesResponse struct { ID string `json:"id"` Type MessagesResponseType `json:"type"` Role string `json:"role"` Content []MessageContent `json:"content"` Model string `json:"model"` StopReason MessagesStopReason `json:"stop_reason"` StopSequence string `json:"stop_sequence"` Usage MessagesUsage `json:"usage"` // contains filtered or unexported fields }
func (MessagesResponse) GetFirstContentText ¶
func (m MessagesResponse) GetFirstContentText() string
GetFirstContentText get Content[0].Text avoid panic
func (*MessagesResponse) GetRateLimitHeaders ¶
func (h *MessagesResponse) GetRateLimitHeaders() RateLimitHeaders
type MessagesResponseType ¶
type MessagesResponseType string
const ( MessagesResponseTypeMessage MessagesResponseType = "message" MessagesResponseTypeError MessagesResponseType = "error" )
type MessagesStopReason ¶
type MessagesStopReason string
const ( MessagesStopReasonEndTurn MessagesStopReason = "end_turn" MessagesStopReasonMaxTokens MessagesStopReason = "max_tokens" MessagesStopReasonStopSequence MessagesStopReason = "stop_sequence" MessagesStopReasonToolUse MessagesStopReason = "tool_use" )
type MessagesStreamRequest ¶
type MessagesStreamRequest struct { MessagesRequest OnError func(ErrorResponse) `json:"-"` OnPing func(MessagesEventPingData) `json:"-"` OnMessageStart func(MessagesEventMessageStartData) `json:"-"` OnContentBlockStart func(MessagesEventContentBlockStartData) `json:"-"` OnContentBlockDelta func(MessagesEventContentBlockDeltaData) `json:"-"` OnContentBlockStop func(MessagesEventContentBlockStopData, MessageContent) `json:"-"` OnMessageDelta func(MessagesEventMessageDeltaData) `json:"-"` OnMessageStop func(MessagesEventMessageStopData) `json:"-"` }
type MessagesUsage ¶
type RateLimitHeaders ¶
type RateLimitHeaders struct { // The maximum number of requests allowed within the rate limit window. RequestsLimit int `json:"anthropic-ratelimit-requests-limit"` // The number of requests remaining within the current rate limit window. RequestsRemaining int `json:"anthropic-ratelimit-requests-remaining"` // The time when the request rate limit window will reset, provided in RFC 3339 format. RequestsReset time.Time `json:"anthropic-ratelimit-requests-reset"` // The maximum number of tokens allowed within the rate limit window. TokensLimit int `json:"anthropic-ratelimit-tokens-limit"` // The number of tokens remaining, rounded to the nearest thousand, within the current rate limit window. TokensRemaining int `json:"anthropic-ratelimit-tokens-remaining"` // The time when the token rate limit window will reset, provided in RFC 3339 format. TokensReset time.Time `json:"anthropic-ratelimit-tokens-reset"` // The number of seconds until the rate limit window resets. RetryAfter int `json:"retry-after"` }
type RequestError ¶
RequestError provides information about generic request errors.
func (*RequestError) Error ¶
func (e *RequestError) Error() string
type ToolChoice ¶
type ToolDefinition ¶
type ToolDefinition struct { Name string `json:"name"` Description string `json:"description,omitempty"` // InputSchema is an object describing the tool. // You can pass json.RawMessage to describe the schema, // or you can pass in a struct which serializes to the proper JSON schema. // The jsonschema package is provided for convenience, but you should // consider another specialized library if you require more complex schemas. InputSchema any `json:"input_schema"` }
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
internal
|
|
Package jsonschema provides very simple functionality for representing a JSON schema as a (nested) struct.
|
Package jsonschema provides very simple functionality for representing a JSON schema as a (nested) struct. |
Click to show internal directories.
Click to hide internal directories.