Documentation
¶
Index ¶
- Constants
- func WithApiKey(apiKey string) func(*Client)
- func WithCustomHeaders(headers map[string]string) func(*Client)
- func WithMaxRetries(maxRetries int) func(*Client)
- func WithTimeout(timeout time.Duration) func(*Client)
- func WithVersion(version string) func(*Client)
- type AnthropicModel
- type Client
- func (c *Client) CompletionRequest(ctx context.Context, payload CompletionPayload) (CompletionResponse, error)
- func (c *Client) MessageRequest(ctx context.Context, payload MessagePayload) (MessageResponse, error)
- func (c *Client) MessageStreamRequest(ctx context.Context, payload MessagePayload) (io.ReadCloser, context.CancelFunc, error)
- func (c *Client) StreamingCompletionRequest(ctx context.Context, payload CompletionPayload) (*StreamingCompletionResponse, error)
- type CompleteOptions
- type CompletionEvent
- type CompletionEventData
- type CompletionMessage
- type CompletionPayload
- type CompletionResponse
- type CompletionSSEDecoder
- type ContentBlock
- type ContentBlockDelta
- type ContentBlockStart
- type ContentBlockStop
- type ContentType
- type Conversation
- type DecodeOptions
- type ErrorData
- type ErrorDetail
- type ErrorResponse
- type EventData
- type HttpClient
- type ImageSource
- type Message
- type MessageContent
- type MessageDelta
- type MessageEvent
- type MessageEventPayload
- type MessagePayload
- type MessageResponse
- type MessageSSEDecoder
- type MessageStart
- type MessageStopData
- type Metadata
- type PingData
- type Role
- type RoleType
- type StreamingCompletionResponse
- type Usage
Constants ¶
const ( DefaultMaxRetries = 3 DefaultTimeout = time.Minute DefaultVersion = "2023-06-01" RequestTypeComplete = "complete" RequestTypeMessages = "messages" )
const ( ContentTypeText ContentType = "text" ContentTypeImage ContentType = "image" RoleTypeUser RoleType = "user" RoleTypeAssistant RoleType = "assistant" )
Variables ¶
This section is empty.
Functions ¶
func WithApiKey ¶
WithApiKey is an option to provide an API key for the Client.
func WithCustomHeaders ¶
WithCustomHeaders is an option to set custom headers for the Client.
func WithMaxRetries ¶
WithMaxRetries is an option to set the maximum number of retries for the Client.
func WithTimeout ¶
WithTimeout is an option to set the timeout for the Client.
func WithVersion ¶
WithVersion is an option to set the API version for the Client.
Types ¶
type AnthropicModel ¶
type AnthropicModel string
AnthropicModel is the model to be used for the completion request. TODO: model may not include completions or messages API support (add flags)
const ( ModelClaude3Dot5Sonnet AnthropicModel = "claude-3-5-sonnet-20240620" ModelClaude3Opus AnthropicModel = "claude-3-opus-20240229" ModelClaude3Sonnet AnthropicModel = "claude-3-sonnet-20240229" ModelClaude3Haiku AnthropicModel = "claude-3-haiku-20240307" ModelClaude2 AnthropicModel = "claude-2.0" ModelClaude2Dot1 AnthropicModel = "claude-2.1" ModelClaudeInstant1Dot2 AnthropicModel = "claude-instant-1.2" )
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a structure holding all necessary fields for making requests to the API.
func NewClient ¶
NewClient creates and returns a new Client. It applies the provided options to the client. If no API key is provided as an option, it looks for the API key in the environment variable ANTHROPIC_API_KEY.
func (*Client) CompletionRequest ¶
func (c *Client) CompletionRequest(ctx context.Context, payload CompletionPayload) (CompletionResponse, error)
CompletionRequest sends a complete request to the server and returns the response or error.
func (*Client) MessageRequest ¶
func (c *Client) MessageRequest(ctx context.Context, payload MessagePayload) (MessageResponse, error)
MessageRequest sends a message to the model and returns the response.
func (*Client) MessageStreamRequest ¶ added in v0.9.0
func (c *Client) MessageStreamRequest(ctx context.Context, payload MessagePayload) (io.ReadCloser, context.CancelFunc, error)
MessageStreamRequest sends a message to the model and returns the body for the user to consume
func (*Client) StreamingCompletionRequest ¶
func (c *Client) StreamingCompletionRequest(ctx context.Context, payload CompletionPayload) (*StreamingCompletionResponse, error)
StreamingCompletionRequest is a method for Client that sends a request to the server with streaming enabled. It marshals the payload into a JSON object and sends it to the server in a POST request. If the request is successful, it returns a pointer to a CompleteStreamResponse object. Otherwise, it returns an error.
type CompleteOptions ¶
type CompleteOptions struct {
Metadata any `json:"metadata,omitempty"`
StopSequences []string `json:"stop_sequences,omitempty"`
Stream bool `json:"stream,omitempty"`
Temperature float64 `json:"temperature,omitempty"`
TopK int `json:"top_k,omitempty"`
TopP float64 `json:"top_p,omitempty"`
}
CompleteOptions holds optional parameters for the complete request.
type CompletionEvent ¶ added in v0.9.0
type CompletionEvent struct {
Event string
Data *CompletionEventData
ID string
Retry int
}
CompletionEvent represents a single Server-Sent CompletionEvent. It includes the event type, data, ID, and retry fields.
type CompletionEventData ¶ added in v0.9.0
type CompletionEventData struct {
Completion string `json:"completion"`
StopReason string `json:"stop_reason"`
Model string `json:"model"`
Stop string `json:"stop"`
LogID string `json:"log_id"`
}
CompletionEventData represents the data payload in a Server-Sent Events (SSE) message.
type CompletionMessage ¶
CompletionMessage represents a single message in a conversation. It includes the Role of the sender and the Content of the message.
type CompletionPayload ¶
type CompletionPayload struct {
MaxTokensToSample int `json:"max_tokens_to_sample"`
Model AnthropicModel `json:"model"`
Prompt string `json:"prompt"`
CompleteOptions
}
CompletionPayload contains the necessary data for the completion request.
type CompletionResponse ¶
type CompletionResponse struct {
Completion string `json:"completion"`
StopReason string `json:"stop_reason"`
Model string `json:"model"`
}
CompletionResponse contains the completion result or error details.
type CompletionSSEDecoder ¶ added in v0.9.0
CompletionSSEDecoder is a decoder for Server-Sent Events. It maintains a buffer reader and the current event being processed.
func NewCompletionSSEDecoder ¶ added in v0.9.0
func NewCompletionSSEDecoder(r io.Reader) *CompletionSSEDecoder
NewCompletionSSEDecoder initializes a new SSEDecoder with the provided reader.
func (*CompletionSSEDecoder) Decode ¶ added in v0.9.0
func (d *CompletionSSEDecoder) Decode() (*CompletionEvent, error)
Decode reads from the buffered reader line by line, parses Server-Sent Events and sets fields on the current event. It returns the complete event when encountering an empty line, and nil otherwise. It will return EOF when nothing is left.
type ContentBlock ¶
ContentBlock is a block of content in a message response. Currently the model will only return text content.
type ContentBlockDelta ¶ added in v0.9.0
type ContentBlockDelta struct {
Type string `json:"type"`
Index int `json:"index"`
Delta struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"delta"`
}
ContentBlockDelta carries new content for a content block in the message stream.
type ContentBlockStart ¶ added in v0.9.0
type ContentBlockStart struct {
Type string `json:"type"`
Index int `json:"index"`
ContentBlock struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"content_block"`
}
ContentBlockStart marks the start of a new content block in the message stream.
type ContentBlockStop ¶ added in v0.9.0
ContentBlockStop marks the end of a content block in the message stream.
type Conversation ¶
type Conversation struct {
Messages []CompletionMessage
}
Conversation is a structure holding all messages of the conversation.
func NewConversation ¶
func NewConversation() *Conversation
NewConversation creates and returns a new Conversation.
func (*Conversation) AddMessage ¶
func (c *Conversation) AddMessage(role Role, content string)
AddMessage appends a new message to the conversation. Role indicates if the message is from the "Assistant" or the "User".
func (*Conversation) GeneratePrompt ¶
func (c *Conversation) GeneratePrompt() string
GeneratePrompt formats the conversation into a string which can be used as a prompt for the assistant. The prompt ends with an empty Assistant message, indicating where the assistant's next message should go.
type DecodeOptions ¶ added in v0.9.0
type DecodeOptions struct {
ContentOnly bool
}
DecodeOptions are options for decoding the SSE stream.
type ErrorData ¶ added in v0.9.0
type ErrorData struct {
Type string `json:"type"`
Error struct {
Type string `json:"type"`
Message string `json:"message"`
} `json:"error"`
}
ErrorData is the event type for errors.
type ErrorDetail ¶
ErrorDetail describes the error type and message.
type ErrorResponse ¶
type ErrorResponse struct {
Error ErrorDetail `json:"error"`
}
ErrorResponse holds the error details in the response.
type EventData ¶ added in v0.9.0
EventData contains content which will be whatever the model output and Data which is the full data from the event
type ImageSource ¶
type ImageSource struct {
Type string `json:"type"`
MediaType string `json:"media_type"`
Data string `json:"data"`
}
ImageSource describes an image that is sent to the model in base64 (type). The following media types are accepted: image/jpeg, image/png, image/gif, image/webp.
type Message ¶
type Message struct {
Role RoleType `json:"role"`
Content []MessageContent `json:"content"`
}
Message is composed of a role and content. The role is either "user" or "assistant" and the content is a list of message content, which may contain text or images.
type MessageContent ¶
type MessageContent struct {
Type ContentType `json:"type,omitempty"`
Text *string `json:"text,omitempty"`
Image *ImageSource `json:"source,omitempty"`
}
MessageContent is the content of a message. It can be either text or an image.
type MessageDelta ¶ added in v0.9.0
type MessageDelta struct {
Type string `json:"type"`
Delta interface{} `json:"delta"`
Usage struct {
OutputTokens int `json:"output_tokens"`
} `json:"usage"`
}
MessageDelta events indicate top-level changes to the final message.
type MessageEvent ¶ added in v0.9.0
type MessageEvent struct {
Message *MessageEventPayload
Err *error
}
MessageEvent is the event type for messages. It contains the message payload and an error if one occurred.
type MessageEventPayload ¶ added in v0.9.0
MessageEventPayload is the decoded event from anthropic
type MessagePayload ¶
type MessagePayload struct {
// The model to use for the request.
Model AnthropicModel `json:"model"`
// The messages to send to the model.
Messages []Message `json:"messages"`
// The maximum number of tokens to generate.
MaxTokens int `json:"max_tokens"`
// Sequences that will cause the model to stop generating.
StopSequences []string `json:"stop_sequences,omitempty"`
// Amount of randomness injected into the response.
Temperature *float64 `json:"temperature,omitempty"`
// Nucleus sampling.
TopP *float64 `json:"top_p,omitempty"`
// Only sample from the top K options for each subsequent token.
TopK *int `json:"top_k,omitempty"`
// An object describing metadata about the request.
Metadata *Metadata `json:"metadata,omitempty"`
// System prompt to provide to the model.
System *string `json:"system,omitempty"`
// Stream the response using server-sent events.
Stream *bool `json:"stream,omitempty"`
}
MessagePayload is the request payload for the /messages endpoint.
type MessageResponse ¶
type MessageResponse struct {
ID string `json:"id"`
Type string `json:"type"`
Role RoleType `json:"role"`
Content []ContentBlock `json:"content"`
Model string `json:"model"`
StopReason string `json:"stop_reason"`
StopSequence string `json:"stop_sequence,omitempty"`
Usage Usage `json:"usage"`
}
MessageResponse is the response payload for the /messages endpoint.
type MessageSSEDecoder ¶ added in v0.9.0
type MessageSSEDecoder struct {
// contains filtered or unexported fields
}
MessageSSEDecoder is a decoder for the SSE stream from the message endpoint.
func NewMessageSSEDecoder ¶ added in v0.9.0
func NewMessageSSEDecoder(reader io.Reader) *MessageSSEDecoder
NewMessageSSEDecoder creates a new MessageSSEDecoder.
func (*MessageSSEDecoder) Decode ¶ added in v0.9.0
func (d *MessageSSEDecoder) Decode(opts ...DecodeOptions) (*MessageEventPayload, error)
Decode reads the next event from the SSE stream.
type MessageStart ¶ added in v0.9.0
type MessageStart struct {
Type string `json:"type"`
Message struct {
ID string `json:"id"`
Type string `json:"type"`
Role string `json:"role"`
Content []string `json:"content"`
Model string `json:"model"`
StopReason string `json:"stop_reason"`
StopSequence string `json:"stop_sequence"`
Usage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
} `json:"usage"`
} `json:"message"`
}
MessageStart is one of the data types for events and it represents the start of a a stream of messages. It contains metadata about the request.
type MessageStopData ¶ added in v0.9.0
type MessageStopData struct {
Type string `json:"type"`
}
MessageStopData is the final event in a message stream.
type Metadata ¶
type Metadata struct {
UserID string `json:"user_id,omitempty"`
}
Metadata is an object describing metadata about the request. At the moment this only supports a user ID.
type PingData ¶ added in v0.9.0
type PingData struct {
Type string `json:"type"`
}
PingData is a ping event
type Role ¶
type Role string
Role represents the role of a participant in a conversation. It could either be a "Human" or an "Assistant".
type StreamingCompletionResponse ¶
type StreamingCompletionResponse struct {
// contains filtered or unexported fields
}
StreamingCompletionResponse contains the server sent events decoder, the response body from the request, and a cancel function to enforce a timeout.
func (StreamingCompletionResponse) Cancel ¶
func (c StreamingCompletionResponse) Cancel()
Cancel is a method for CompleteStreamResponse that invokes the associated cancel function to stop the request prematurely.
func (StreamingCompletionResponse) Close ¶
func (c StreamingCompletionResponse) Close() error
Close is a method for CompleteStreamResponse that closes the response body. If the response body has been read, Close returns nil. Otherwise, it returns an error.
func (StreamingCompletionResponse) Decode ¶
func (c StreamingCompletionResponse) Decode() (*CompletionEvent, error)
Decode is a method for CompleteStreamResponse that returns the next event from the server-sent events decoder, or an error if one occurred.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
completion
command
|
|
|
completion_stream
command
|
|
|
messages
command
|
|
|
messages_stream
command
|
|