anthropicclient

package
v0.1.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultBaseURL = "https://api.anthropic.com/v1"
)

Variables

View Source
var (
	ErrInvalidEventType        = fmt.Errorf("invalid event type field type")
	ErrInvalidMessageField     = fmt.Errorf("invalid message field type")
	ErrInvalidUsageField       = fmt.Errorf("invalid usage field type")
	ErrInvalidIndexField       = fmt.Errorf("invalid index field type")
	ErrInvalidDeltaField       = fmt.Errorf("invalid delta field type")
	ErrInvalidDeltaTypeField   = fmt.Errorf("invalid delta type field type")
	ErrInvalidDeltaTextField   = fmt.Errorf("invalid delta text field type")
	ErrContentIndexOutOfRange  = fmt.Errorf("content index out of range")
	ErrFailedCastToTextContent = fmt.Errorf("failed to cast content to TextContent")
	ErrInvalidFieldType        = fmt.Errorf("invalid field type")
)
View Source
var ErrEmptyResponse = errors.New("empty response")

ErrEmptyResponse is returned when the Anthropic API returns an empty response.

Functions

This section is empty.

Types

type ChatMessage added in v0.1.13

type ChatMessage struct {
	Role    string      `json:"role"`
	Content interface{} `json:"content"`
}

type Client

type Client struct {
	Model string

	// UseLegacyTextCompletionsAPI is a flag to use the legacy text completions API.
	UseLegacyTextCompletionsAPI bool
	// contains filtered or unexported fields
}

Client is a client for the Anthropic API.

func New

func New(token string, model string, baseURL string, opts ...Option) (*Client, error)

New returns a new Anthropic client.

func (*Client) CreateCompletion

func (c *Client) CreateCompletion(ctx context.Context, r *CompletionRequest) (*Completion, error)

CreateCompletion creates a completion.

func (*Client) CreateMessage added in v0.1.13

func (c *Client) CreateMessage(ctx context.Context, r *MessageRequest) (*MessageResponsePayload, error)

CreateMessage creates message for the messages api.

type Completion

type Completion struct {
	Text string `json:"text"`
}

Completion is a completion.

type CompletionEvent added in v0.1.13

type CompletionEvent struct {
	Response *CompletionResponsePayload
	Err      error
}

type CompletionRequest

type CompletionRequest struct {
	Model       string   `json:"model"`
	Prompt      string   `json:"prompt"`
	Temperature float64  `json:"temperature"`
	MaxTokens   int      `json:"max_tokens_to_sample,omitempty"`
	StopWords   []string `json:"stop_sequences,omitempty"`
	TopP        float64  `json:"top_p,omitempty"`
	Stream      bool     `json:"stream,omitempty"`

	// StreamingFunc is a function to be called for each chunk of a streaming response.
	// Return an error to stop streaming early.
	StreamingFunc func(ctx context.Context, chunk []byte) error `json:"-"`
}

CompletionRequest is a request to create a completion.

type CompletionResponsePayload

type CompletionResponsePayload struct {
	Completion string `json:"completion,omitempty"`
	LogID      string `json:"log_id,omitempty"`
	Model      string `json:"model,omitempty"`
	Stop       string `json:"stop,omitempty"`
	StopReason string `json:"stop_reason,omitempty"`
}

type Content added in v0.1.13

type Content interface {
	GetType() string
}

Content can be TextContent or ToolUseContent depending on the type.

type Doer

type Doer interface {
	Do(req *http.Request) (*http.Response, error)
}

Doer performs a HTTP request.

type MessageEvent added in v0.1.13

type MessageEvent struct {
	Response *MessageResponsePayload
	Err      error
}

type MessageRequest added in v0.1.13

type MessageRequest struct {
	Model       string        `json:"model"`
	Messages    []ChatMessage `json:"messages"`
	System      string        `json:"system,omitempty"`
	Temperature float64       `json:"temperature"`
	MaxTokens   int           `json:"max_tokens,omitempty"`
	TopP        float64       `json:"top_p,omitempty"`
	Tools       []Tool        `json:"tools,omitempty"`
	StopWords   []string      `json:"stop_sequences,omitempty"`
	Stream      bool          `json:"stream,omitempty"`

	StreamingFunc func(ctx context.Context, chunk []byte) error `json:"-"`
}

type MessageResponsePayload added in v0.1.13

type MessageResponsePayload struct {
	Content      []Content `json:"content"`
	ID           string    `json:"id"`
	Model        string    `json:"model"`
	Role         string    `json:"role"`
	StopReason   string    `json:"stop_reason"`
	StopSequence string    `json:"stop_sequence"`
	Type         string    `json:"type"`
	Usage        struct {
		InputTokens  int `json:"input_tokens"`
		OutputTokens int `json:"output_tokens"`
	} `json:"usage"`
}

func (*MessageResponsePayload) UnmarshalJSON added in v0.1.13

func (m *MessageResponsePayload) UnmarshalJSON(data []byte) error

type Option

type Option func(*Client) error

Option is an option for the Anthropic client.

func WithAnthropicBetaHeader added in v0.1.13

func WithAnthropicBetaHeader(val string) Option

WithAnthropicBetaHeader sets the anthropic-beta header.

func WithHTTPClient

func WithHTTPClient(client Doer) Option

WithHTTPClient allows setting a custom HTTP client.

func WithLegacyTextCompletionsAPI added in v0.1.13

func WithLegacyTextCompletionsAPI(val bool) Option

WithLegacyTextCompletionsAPI enables the use of the legacy text completions API.

type TextContent added in v0.1.13

type TextContent struct {
	Type string `json:"type"`
	Text string `json:"text"`
}

func (TextContent) GetType added in v0.1.13

func (tc TextContent) GetType() string

type Tool added in v0.1.13

type Tool struct {
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	InputSchema any    `json:"input_schema,omitempty"`
}

Tool used for the request message payload.

type ToolResultContent added in v0.1.13

type ToolResultContent struct {
	Type      string `json:"type"`
	ToolUseID string `json:"tool_use_id"`
	Content   string `json:"content"`
}

func (ToolResultContent) GetType added in v0.1.13

func (trc ToolResultContent) GetType() string

type ToolUseContent added in v0.1.13

type ToolUseContent struct {
	Type  string                 `json:"type"`
	ID    string                 `json:"id"`
	Name  string                 `json:"name"`
	Input map[string]interface{} `json:"input"`
}

func (ToolUseContent) GetType added in v0.1.13

func (tuc ToolUseContent) GetType() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL