Documentation
¶
Index ¶
- Constants
- Variables
- type APIError
- type APIVersion
- type Client
- type ClientConfig
- type ContentBlock
- type ErrorResponse
- type ImageSource
- type InputMessage
- type MessageRequest
- type MessageRequestMetadata
- type MessageResponse
- type MessageStream
- type MessageStreamDelta
- type MessageStreamError
- type MessageStreamEvent
- type MessageStreamEventType
- type StopReason
- type Tool
- type ToolChoice
- type ToolInputSchema
- type ToolResultContent
- type Usage
Constants ¶
const ( MessageRoleAssistant = "assistant" MessageRoleUser = "user" )
const ( TextContentObjectType = "text" ImageContentObjectType = "image" ToolUseContentObjectType = "tool_use" ToolResultContentObjectType = "tool_result" )
const ( ImageJPEGMediaType = "image/jpeg" ImagePNGMediaType = "image/png" ImageGIFMediaType = "image/gif" ImageWebPMediaType = "image/webp" )
const ( Claude35SonnetModel = "claude-3-5-sonnet-20240620" Claude3OpusModel = "claude-3-opus-20240229" Claude3SonnetModel = "claude-3-sonnet-20240229" Claude3HaikuModel = "claude-3-haiku-20240307" )
const ( AutoToolChoiceType = "auto" AnyToolChoiceType = "any" ToolToolChoiceType = "tool" )
const ImageSourceType = "base64"
const ObjectToolInputSchemaType = "object"
Variables ¶
var ( ErrContentFieldsMisused = errors.New("can't use both Content and ContentBlocks properties simultaneously") ErrChatCompletionStreamNotSupported = errors.New("streaming is not supported with this method, please use CreateMessageStream") //nolint:lll ErrModelNotAvailable = errors.New("this model is not available for Anthropic Messages API") )
Functions ¶
This section is empty.
Types ¶
type APIVersion ¶
type APIVersion string
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Anthropic API Client for making requests
func NewClientWithConfig ¶
func NewClientWithConfig(config ClientConfig) *Client
NewClientWithConfig creates an Anthropic API client with specified configuration
func (*Client) CreateMessage ¶
func (c *Client) CreateMessage(ctx context.Context, request MessageRequest) (response MessageResponse, err error)
CreateMessage - API call to Anthropic Messages API to create a message completion
func (*Client) CreateMessageStream ¶ added in v1.0.0
func (c *Client) CreateMessageStream(ctx context.Context, request MessageRequest) (stream *MessageStream, err error)
CreateMessageStream — API call to create a message w/ streaming support. It sets whether to stream back partial progress. If set, tokens will be sent as server-sent events as they become available, with the stream terminated by the last event of type "message_stop"
See Recv() and RecvAll() methods of MessageStream for more details of how to receive data from stream.
type ClientConfig ¶
type ClientConfig struct { BaseUrl string APIVersion APIVersion HTTPClient *http.Client // contains filtered or unexported fields }
ClientConfig is a configuration of client
func DefaultConfig ¶
func DefaultConfig(apiKey string) ClientConfig
DefaultConfig creates a standard configuration with api key. This method is called when creating new client with NewClient
type ContentBlock ¶
type ContentBlock struct { Type string `json:"type"` // For Text type Text string `json:"text,omitempty"` // For Image type Source ImageSource `json:"source,omitempty"` // For Tool Use type ID string `json:"id,omitempty"` Name string `json:"name,omitempty"` Input map[string]interface{} `json:"input,omitempty"` // For Tool Result type ToolUseId string `json:"tool_use_id,omitempty"` IsError bool `json:"is_error,omitempty"` ToolResultContent ToolResultContent `json:"content,omitempty"` }
ContentBlock is used to provide the InputMessage with multiple input or input other than a simple string
func (ContentBlock) MarshalJSON ¶
func (cb ContentBlock) MarshalJSON() ([]byte, error)
type ErrorResponse ¶
type ImageSource ¶
type InputMessage ¶
type InputMessage struct { Role string `json:"role"` Content string `json:"content"` ContentBlocks []ContentBlock }
InputMessage stores content of message request. When creating new message with Client.CreateMessage, Role field is always equal to "user". Content field is used to pass just one string of content. ContentBlocks are used to pass multiple input content and/or content other than a string, like an image.
Note that Content and ContentBlocks fields cannot be used simultaneously in one InputMessage.
func (InputMessage) MarshalJSON ¶
func (m InputMessage) MarshalJSON() ([]byte, error)
func (*InputMessage) UnmarshalJSON ¶
func (m *InputMessage) UnmarshalJSON(bs []byte) error
type MessageRequest ¶
type MessageRequest struct { Model string `json:"model"` Messages []InputMessage `json:"messages"` MaxTokens int `json:"max_tokens"` Temperature int `json:"temperature,omitempty"` StopSequences []string `json:"stop_sequences,omitempty"` Metadata *MessageRequestMetadata `json:"metadata,omitempty"` Stream bool `json:"stream,omitempty"` System string `json:"system,omitempty"` TopK int `json:"top_k,omitempty"` TopP int `json:"top_p,omitempty"` Tools []Tool `json:"tools,omitempty"` ToolChoice *ToolChoice `json:"tool_choice,omitempty"` }
MessageRequest represents a request structure for Anthropic Messages API
type MessageRequestMetadata ¶
type MessageRequestMetadata struct {
UserId string `json:"user_id,omitempty"`
}
type MessageResponse ¶
type MessageResponse struct { ID string `json:"id"` Type string `json:"type"` Content []ContentBlock `json:"content"` Role string `json:"role"` Model string `json:"model"` StopReason StopReason `json:"stop_reason"` StopSequence string `json:"stop_sequence,omitempty"` Usage Usage `json:"usage"` }
type MessageStream ¶ added in v1.0.0
type MessageStream struct {
// contains filtered or unexported fields
}
func (MessageStream) Recv ¶ added in v1.0.0
func (stream MessageStream) Recv() (MessageStreamDelta, error)
Recv is the same as RecvAll() but receives only events with the type "content_block_delta", which carry the content of the response, and returns them as MessageStreamDelta
func (MessageStream) RecvAll ¶ added in v1.0.0
func (stream MessageStream) RecvAll() (response MessageStreamEvent, err error)
RecvAll receives all types of events from Anthropic Messages API and returns them as MessageStreamEvent If you want to process all events, check the type of event first to know what fields are available.
type MessageStreamDelta ¶ added in v1.0.0
type MessageStreamError ¶ added in v1.0.0
type MessageStreamEvent ¶ added in v1.0.0
type MessageStreamEvent struct { Type MessageStreamEventType `json:"type"` Index int `json:"index,omitempty"` // For message_start type Message MessageResponse `json:"message,omitempty"` // For content_block_start type ContentBlock ContentBlock `json:"content_block,omitempty"` // For content_block_delta type Delta MessageStreamDelta `json:"delta,omitempty"` // For error type Error MessageStreamError `json:"error,omitempty"` }
type MessageStreamEventType ¶ added in v1.0.0
type MessageStreamEventType string
const ( MessageStartStreamEventType MessageStreamEventType = "message_start" ContentBlockStartStreamEventType MessageStreamEventType = "content_block_start" PingStreamEventType MessageStreamEventType = "ping" ContentBlockDeltaStreamEventType MessageStreamEventType = "content_block_delta" MessageDeltaStreamEventType MessageStreamEventType = "message_delta" MessageStopStreamEventType MessageStreamEventType = "message_stop" ContentBlockStopStreamEventType MessageStreamEventType = "content_block_stop" ErrStreamEventType MessageStreamEventType = "error" )
type StopReason ¶
type StopReason string
const ( StopReasonEndTurn StopReason = "end_turn" StopReasonMaxTokens StopReason = "max_tokens" StopReasonStopSequence StopReason = "stop_sequence" StopReasonToolUser StopReason = "tool_use" )
type ToolChoice ¶
type ToolInputSchema ¶
type ToolResultContent ¶
type ToolResultContent struct { Type string `json:"type"` // For Text type Text string `json:"text,omitempty"` // For Image type Source ImageSource `json:"source,omitempty"` }
func (ToolResultContent) MarshalJSON ¶
func (trs ToolResultContent) MarshalJSON() ([]byte, error)