anthrogo

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2024 License: MIT Imports: 13 Imported by: 4

README

anthropic go (anthrogo)

Go Reference Go Report Card codecov

This is a simple client for using Anthropic's api to get claude completions. It is not an official client. Contributions are welcome!

Installation

go get github.com/dleviminzi/anthrogo

Basic usage

Message API
func main() {
	c, err := anthrogo.NewClient()
	if err != nil {
		log.Fatal(err)
		os.Exit(1)
	}

	systemPrompt := "you are an expert in all things bananas"

	// Read user input for the prompt
	reader := bufio.NewReader(os.Stdin)
	fmt.Print("Enter your prompt: ")
	userPrompt, _ := reader.ReadString('\n')
	userPrompt = strings.TrimSuffix(userPrompt, "\n")

	resp, err := c.MessageRequest(context.Background(), anthrogo.MessagePayload{
		Model: anthrogo.ModelClaude3Opus,
		Messages: []anthrogo.Message{{
			Role: anthrogo.RoleTypeUser,
			Content: []anthrogo.MessageContent{{
				Type: anthrogo.ContentTypeText,
				Text: &userPrompt,
			}},
		}},
		System:    &systemPrompt,
		MaxTokens: 1000,
	})
	if err != nil {
		log.Fatal(err)
		os.Exit(1)
	}

	fmt.Println(resp.Content[0].Text)
}

Message Streaming

message-streaming-example

func main() {
	c, err := anthrogo.NewClient()
	if err != nil {
		log.Fatal(err)
		os.Exit(1)
	}
	
	systemPrompt := "you are an expert in all things bananas"
	
	// Read user input for the prompt
	reader := bufio.NewReader(os.Stdin)
	fmt.Print("Enter your prompt: ")
	userPrompt, _ := reader.ReadString('\n')
	userPrompt = strings.TrimSuffix(userPrompt, "\n")
	
	r, _, err := c.MessageStreamRequest(context.Background(), anthrogo.MessagePayload{
		Model: anthrogo.ModelClaude3Opus,
		Messages: []anthrogo.Message{{
			Role: anthrogo.RoleTypeUser,
			Content: []anthrogo.MessageContent{{
				Type: anthrogo.ContentTypeText,
				Text: &userPrompt,
			}},
		}},
		System:    &systemPrompt,
		MaxTokens: 1000,
	})
	if err != nil {
		log.Fatal(err)
		os.Exit(1)
	}
	defer r.Close()
	
	// Create an SSEDecoder
	decoder := anthrogo.NewMessageSSEDecoder(r)
	for {
		message, err := decoder.Decode(anthrogo.DecodeOptions{ContentOnly: true})
		if err != nil {
			if err == io.EOF {
				break
			}
			fmt.Print(err)
			continue
		}
	
		if message.Event == "message_stop" {
			break
		}
	
		fmt.Print(message.Data.Content)
	}
}
Completions (old api)
func main() {
	c, err := anthrogo.NewClient()
	if err != nil {
		log.Fatal(err)
		os.Exit(1)
	}

	// Read user input for the prompt
	reader := bufio.NewReader(os.Stdin)
	fmt.Print("Enter your prompt: ")
	userPrompt, _ := reader.ReadString('\n')
	userPrompt = strings.TrimSuffix(userPrompt, "\n")

	// Create conversation with user input
	conversation := anthrogo.NewConversation()
	conversation.AddMessage(anthrogo.RoleHuman, userPrompt)

	// Set up the payload and send completion stream request
	resp, err := c.CompletionRequest(context.Background(), anthrogo.CompletionPayload{
		MaxTokensToSample: 256,
		Model:             anthrogo.ModelClaude2,
		Prompt:            conversation.GeneratePrompt(),
	})
	if err != nil {
		log.Fatal(err)
		os.Exit(1)
	}

	fmt.Println(resp.Completion)

	// Add claude's response to conversation for further prompting...
	conversation.AddMessage(anthrogo.RoleAssistant, resp.Completion)
}

Completion Streaming

streaming-completion-example (trimmed).webm

Code
func main() {
	// Create a new client
	// optionally provide api key otherwise we will look for it in ANTHROPIC_API_KEY variable
	c, err := anthrogo.NewClient()
	if err != nil {
		log.Fatal(err)
		os.Exit(1)
	}

	// Read user input for the prompt
	reader := bufio.NewReader(os.Stdin)
	fmt.Print("Enter your prompt: ")
	userPrompt, _ := reader.ReadString('\n')
	userPrompt = strings.TrimSuffix(userPrompt, "\n")

	// Create conversation with user input
	conversation := anthrogo.NewConversation()
	conversation.AddMessage(anthrogo.RoleHuman, userPrompt)

	// Set up the payload and send completion stream request
	completeStreamResp, _ := c.StreamingCompletionRequest(context.Background(), anthrogo.CompletionPayload{
		MaxTokensToSample: 256,
		Model:             anthrogo.ModelClaude2,
		Prompt:            conversation.GeneratePrompt(),
		CompleteOptions: anthrogo.CompleteOptions{
			Stream:      true,
			Temperature: 1,
		},
	})

	// Ensure that the request is canceled after timeout (default 1 minute)
	defer completeStreamResp.Cancel()

	// Ensure that the stream response body is closed when the function returns
	defer completeStreamResp.Close()

	// Continually read from the response until an error or EOF is encountered
	for {
		event, err := completeStreamResp.Decode()
		if err != nil {
			if err == io.EOF {
				break
			} else {
				fmt.Println(err)
				os.Exit(1)
			}
		}

		if event != nil {
			fmt.Printf("%s", event.Data.Completion)
		}
	}
}

Documentation

Index

Constants

View Source
const (
	DefaultMaxRetries = 3
	DefaultTimeout    = time.Minute
	DefaultVersion    = "2023-06-01"

	RequestTypeComplete = "complete"
	RequestTypeMessages = "messages"
)
View Source
const (
	ContentTypeText  ContentType = "text"
	ContentTypeImage ContentType = "image"

	RoleTypeUser      RoleType = "user"
	RoleTypeAssistant RoleType = "assistant"
)

Variables

This section is empty.

Functions

func WithApiKey

func WithApiKey(apiKey string) func(*Client)

WithApiKey is an option to provide an API key for the Client.

func WithCustomHeaders

func WithCustomHeaders(headers map[string]string) func(*Client)

WithCustomHeaders is an option to set custom headers for the Client.

func WithMaxRetries

func WithMaxRetries(maxRetries int) func(*Client)

WithMaxRetries is an option to set the maximum number of retries for the Client.

func WithTimeout

func WithTimeout(timeout time.Duration) func(*Client)

WithTimeout is an option to set the timeout for the Client.

func WithVersion

func WithVersion(version string) func(*Client)

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

func NewClient(options ...func(*Client)) (*Client, error)

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

type CompletionMessage struct {
	Role    Role
	Content string
}

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

type CompletionSSEDecoder struct {
	Reader *bufio.Reader
	// contains filtered or unexported fields
}

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

type ContentBlock struct {
	Type string `json:"type"`
	Text string `json:"text,omitempty"`
}

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

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

ContentBlockStop marks the end of a content block in the message stream.

type ContentType

type ContentType string

ContentType is the type of content in a message.

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

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

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

type EventData struct {
	Content string
	Data    any
}

EventData contains content which will be whatever the model output and Data which is the full data from the event

type HttpClient

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

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

type MessageEventPayload struct {
	Event string
	Data  EventData
}

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

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".

const (
	RoleHuman     Role = "Human"
	RoleAssistant Role = "Assistant"
)

type RoleType

type RoleType string

RoleType is the role of the message.

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

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

Decode is a method for CompleteStreamResponse that returns the next event from the server-sent events decoder, or an error if one occurred.

type Usage

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

Usage contains information about the number of input and output tokens.

Directories

Path Synopsis
examples
completion command
messages command
messages_stream command

Jump to

Keyboard shortcuts

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