anthropic

package module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

README

go-anthropic

Go Reference Go Report Card codecov Sanity check

Anthropic Claude API wrapper for Go (Unofficial). Support:

  • Completions
  • Streaming Completions
  • Messages
  • Streaming Messages
  • Vision

Installation

go get github.com/liushuangls/go-anthropic

Currently, go-anthropic requires Go version 1.21 or greater.

Usage

Messages example usage:
package main

import (
	"errors"
	"fmt"

	"github.com/liushuangls/go-anthropic"
)

func main() {
	client := anthropic.NewClient("your anthropic apikey")
	resp, err := client.CreateMessages(context.Background(), anthropic.MessagesRequest{
		Model: anthropic.ModelClaudeInstant1Dot2,
		Messages: []anthropic.Message{
			anthropic.NewUserTextMessage("What is your name?"),
		},
		MaxTokens: 1000,
	})
	if err != nil {
		var e *anthropic.APIError
		if errors.As(err, &e) {
			fmt.Printf("Messages error, type: %s, message: %s", e.Type, e.Message)
		} else {
			fmt.Printf("Messages error: %v\n", err)
        }
		return
	}
	fmt.Println(resp.Content[0].Text)
}
Messages stream example usage:
package main

import (
	"errors"
	"fmt"

	"github.com/liushuangls/go-anthropic"
)

func main() {
	client := anthropic.NewClient("your anthropic apikey")
	resp, err := client.CreateMessagesStream(context.Background(),  anthropic.MessagesStreamRequest{
		MessagesRequest: anthropic.MessagesRequest{
			Model: anthropic.ModelClaudeInstant1Dot2,
			Messages: []anthropic.Message{
				anthropic.NewUserTextMessage("What is your name?"),
			},
			MaxTokens:   1000,
		},
		OnContentBlockDelta: func(data anthropic.MessagesEventContentBlockDeltaData) {
			fmt.Printf("Stream Content: %s\n", data.Delta.Text)
		},
	})
	if err != nil {
		var e *anthropic.APIError
		if errors.As(err, &e) {
			fmt.Printf("Messages stream error, type: %s, message: %s", e.Type, e.Message)
		} else {
			fmt.Printf("Messages stream error: %v\n", err)
        }
		return
	}
	fmt.Println(resp.Content[0].Text)
}
Messages Vision example usage:
package main

import (
	"errors"
	"fmt"

	"github.com/liushuangls/go-anthropic"
)

func main() {
	client := anthropic.NewClient("your anthropic apikey")

	imagePath := "xxx"
	imageMediaType := "image/jpeg"
	imageFile, err := os.Open(imagePath)
	if err != nil {
		panic(err)
	}
	imageData, err := io.ReadAll(imageFile)
	if err != nil {
		panic(err)
	}

	resp, err := client.CreateMessages(context.Background(), anthropic.MessagesRequest{
		Model: anthropic.ModelClaude3Opus20240229,
		Messages: []anthropic.Message{
			{
				Role: anthropic.RoleUser,
				Content: []anthropic.MessageContent{
					anthropic.NewImageMessageContent(anthropic.MessageContentImageSource{
						Type:      "base64",
						MediaType: imageMediaType,
						Data:      imageData,
					}),
					anthropic.NewTextMessageContent("Describe this image."),
				},
			},
		},
		MaxTokens: 1000,
	})
	if err != nil {
		var e *anthropic.APIError
		if errors.As(err, &e) {
			fmt.Printf("Messages error, type: %s, message: %s", e.Type, e.Message)
		} else {
			fmt.Printf("Messages error: %v\n", err)
        }
		return
	}
	fmt.Println(resp.Content[0].Text)
}

Acknowledgments

The following project had particular influence on go-anthropic is design.

Documentation

Index

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"
	ModelClaude3Haiku20240307  = "claude-3-haiku-20240307"
)
View Source
const (
	RoleUser      = "user"
	RoleAssistant = "assistant"
)
View Source
const (
	APIVersion20230601 = "2023-06-01"
)

Variables

View Source
var (
	ErrTooManyEmptyStreamMessages = errors.New("stream has sent too many empty messages")
)

Functions

This section is empty.

Types

type APIError

type APIError struct {
	Type    ErrType `json:"type"`
	Message string  `json:"message"`
}

APIError provides error information returned by the Anthropic API.

func (*APIError) Error

func (e *APIError) Error() string

func (*APIError) IsApiErr added in v1.4.2

func (e *APIError) IsApiErr() bool

func (*APIError) IsAuthenticationErr added in v1.4.2

func (e *APIError) IsAuthenticationErr() bool

func (*APIError) IsInvalidRequestErr added in v1.4.1

func (e *APIError) IsInvalidRequestErr() bool

func (*APIError) IsNotFoundErr added in v1.4.2

func (e *APIError) IsNotFoundErr() bool

func (*APIError) IsOverloadedErr added in v1.4.2

func (e *APIError) IsOverloadedErr() bool

func (*APIError) IsPermissionErr added in v1.4.2

func (e *APIError) IsPermissionErr() bool

func (*APIError) IsRateLimitErr added in v1.4.2

func (e *APIError) IsRateLimitErr() bool

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 added in v1.2.0

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 added in v1.2.0

func (c *Client) CreateMessagesStream(ctx context.Context, request MessagesStreamRequest) (response MessagesResponse, err error)

type ClientConfig

type ClientConfig struct {
	BaseURL    string
	APIVersion 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 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 added in v1.2.1

func (c *CompleteRequest) SetTemperature(t float32)

func (*CompleteRequest) SetTopK added in v1.2.1

func (c *CompleteRequest) SetTopK(k int)

func (*CompleteRequest) SetTopP added in v1.2.1

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"`
}

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 added in v1.6.0

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 ErrorResponse struct {
	Type  string    `json:"type"`
	Error *APIError `json:"error,omitempty"`
}

type Message

type Message struct {
	Role    string           `json:"role"`
	Content []MessageContent `json:"content"`
}

func NewAssistantTextMessage added in v1.4.0

func NewAssistantTextMessage(text string) Message

func NewUserTextMessage added in v1.4.0

func NewUserTextMessage(text string) Message

func (Message) GetFirstContent added in v1.4.0

func (m Message) GetFirstContent() MessageContent

type MessageContent added in v1.4.0

type MessageContent struct {
	Type   string                     `json:"type"`
	Text   *string                    `json:"text,omitempty"`
	Source *MessageContentImageSource `json:"source,omitempty"`
}

func NewImageMessageContent added in v1.4.0

func NewImageMessageContent(source MessageContentImageSource) MessageContent

func NewTextMessageContent added in v1.4.0

func NewTextMessageContent(text string) MessageContent

func (MessageContent) GetText added in v1.4.0

func (m MessageContent) GetText() string

func (MessageContent) IsImageContent added in v1.4.0

func (m MessageContent) IsImageContent() bool

func (MessageContent) IsTextContent added in v1.4.0

func (m MessageContent) IsTextContent() bool

type MessageContentImageSource added in v1.4.0

type MessageContentImageSource struct {
	Type      string `json:"type"`
	MediaType string `json:"media_type"`
	Data      any    `json:"data"`
}

type MessagesContent

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

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 MessagesContent `json:"delta"`
}

type MessagesEventContentBlockStartData

type MessagesEventContentBlockStartData struct {
	Type         MessagesEvent   `json:"type"`
	Index        int             `json:"index"`
	ContentBlock MessagesContent `json:"content_block"`
}

type MessagesEventContentBlockStopData

type MessagesEventContentBlockStopData struct {
	Type  string `json:"type"`
	Index int    `json:"index"`
}

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"`
}

func (*MessagesRequest) SetTemperature added in v1.2.1

func (m *MessagesRequest) SetTemperature(t float32)

func (*MessagesRequest) SetTopK added in v1.2.1

func (m *MessagesRequest) SetTopK(k int)

func (*MessagesRequest) SetTopP added in v1.2.1

func (m *MessagesRequest) SetTopP(p float32)

type MessagesResponse

type MessagesResponse struct {
	ID           string               `json:"id"`
	Type         MessagesResponseType `json:"type"`
	Role         string               `json:"role"`
	Content      []MessagesContent    `json:"content"`
	Model        string               `json:"model"`
	StopReason   string               `json:"stop_reason"`
	StopSequence string               `json:"stop_sequence"`
	Usage        MessagesUsage        `json:"usage"`
}

func (MessagesResponse) GetFirstContentText added in v1.2.2

func (m MessagesResponse) GetFirstContentText() string

GetFirstContentText get Content[0].Text avoid panic

type MessagesResponseType

type MessagesResponseType string
const (
	MessagesResponseMsg MessagesResponseType = "message"
	MessagesResponseErr MessagesResponseType = "error"
)

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)  `json:"-"`
	OnMessageDelta      func(MessagesEventMessageDeltaData)      `json:"-"`
	OnMessageStop       func(MessagesEventMessageStopData)       `json:"-"`
}

type MessagesUsage

type MessagesUsage struct {
	InputTokens  int `json:"input_tokens"`
	OutputTokens int `json:"output_tokens"`
}

type RequestError

type RequestError struct {
	StatusCode int
	Err        error
}

RequestError provides information about generic request errors.

func (*RequestError) Error

func (e *RequestError) Error() string

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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