anthropic

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2024 License: GPL-3.0 Imports: 9 Imported by: 0

README

anthropic

golang interface for Anthropic's Machine Learning inference HTTP API interface, specifically the Messaging API. Colloquially, this is the API for the Claude family of Machine Learning model such as Claude Sonnet 3.5.

It presents a Conversation struct that can be used to interact with the API. The API key is read from the environment variable ANTHROPIC_API_KEY.

The Conversation struct has a Send method that takes a string input and returns a string reply, a string stop reason, input tokens, output tokens, and an error. The stop reason is a string that indicates why the conversation stopped. The input and output tokens are the tokens that were used for the input and output strings, respectively.

More interestingly, the Conversation struct contains the history of the conversation, so you can continue a conversation by calling Send with a new input string.

There is an anthropic.DefaultSettings struct that can be used to set the defaults for the Conversation struct.

You may also set the default API token for the Conversation struct by setting anthropic.DefaultApiToken. This can be useful if you have multiple Conversations.

If you want to set the API key on a per Conversation basis, you can set the ApiToken field of the Conversation struct once it is created using NewConversation.

The anthropic.DefaultSettings struct has the following fields:

var DefaultSettings = SampleSettings{
	Model:       "claude-3-5-sonnet-20240620",
	Version:     "2023-06-01",
	Beta:        "", // "max-tokens-3-5-sonnet-2024-07-15"
	MaxTokens:   4096,
	Temperature: 0.0,
}

Installation

go get github.com/wbrown/anthropic

Usage

package main

import (
    "fmt"
    "log"

    "github.com/wbrown/anthropic"
)

func main() {
    // API key is read from the environment variable ANTHROPIC_API_KEY
    // You can also set the API key by setting conversation.Settings.ApiToken
    conversation := anthropic.NewConversation("You are a friendly chatbot.")
	reply, stopReason, inputTokens, outputTokens, err :=
		conversation.Send("Hello Claude!")
	if err != nil {
		log.Fatal(err)
	}
    fmt.Println("Reply:", reply)
    fmt.Println("Stop Reason:", stopReason)
    fmt.Println("Input Tokens:", inputTokens)
    fmt.Println("Output Tokens:", outputTokens)

    reply, _, _ ,_, err = conversation.Send("How are you?")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("Reply:", reply)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultApiToken = ""

DefaultApiToken is set to the environment variable ANTHROPIC_API_KEY, if it exists. It can be overridden by setting it directly. It is used as the default API token for all conversations.

View Source
var DefaultSettings = SampleSettings{
	Model:       "claude-3-5-sonnet-20240620",
	Version:     "2023-06-01",
	Beta:        "",
	MaxTokens:   4096,
	Temperature: 0.0,
}

DefaultSettings is the default settings for the Conversation. It is used as the default settings for all Conversations, and can be overridden by setting it directly.

Functions

This section is empty.

Types

type ContentBlock

type ContentBlock struct {
	// ContentType is the type of the content block.
	// Possible values are:
	//   "text": The content is a string of text.
	//   "image": The content is an image.
	ContentType string `json:"type"`
	// Text is the text content.
	Text *string `json:"text,omitempty"`
	// Source is the source of the content.
	Source *ContentSource `json:"source,omitempty"`
	// contains filtered or unexported fields
}

ContentBlock is a single block of content in a message.

type ContentSource

type ContentSource struct {
	// Encoding type
	// Possible values are:
	//    base64: The content is base64 encoded.
	Encoding string `json:"type"`
	// MediaType type
	// Possible values are:
	//    image/png: The content is a PNG image.
	//    image/jpeg: The content is a JPEG image.
	//    image/gif: The content is a GIF image.
	//    image/webp: The content is a WebP image.
	MediaType string `json:"media_type"`
	// Data is the base64 encoded image data.
	Data string `json:"data"`
}

ContentSource is the encoded data for the content block. It is presently used for images only.

type Conversation

type Conversation struct {
	// System is the system prompt to use for the conversation.
	System *string
	// Messages is the sequence of messages in the conversation. This always
	// starts with a user message, and alternates between user and assistant.
	Messages *[]*Message
	// Usage is the usage statistics for the conversation in total.
	Usage struct {
		InputTokens  int `json:"input_tokens"`
		OutputTokens int `json:"output_tokens"`
	}
	// HttpClient is the HTTP client used for API requests
	HttpClient *http.Client
	// apiToken is the API token used for API requests
	ApiToken string `json:"-"`
	// Settings is the settings for the conversation.
	Settings *SampleSettings
}

A Conversation is a sequence of messages between a user and an assistant.

func NewConversation

func NewConversation(system string) *Conversation

NewConversation creates a new conversation with the given system prompt. It initializes the messages and usage statistics, as well as reasonable defaults.

func (*Conversation) AddMessage

func (conversation *Conversation) AddMessage(
	role string,
	content *[]ContentBlock,
)

AddMessage adds a message to the conversation with the given role and content. It used internally, and also can be used externally to manipulate conversations.

func (*Conversation) MergeIfLastTwoAssistant

func (conversation *Conversation) MergeIfLastTwoAssistant()

MergeIfLastTwoAssistant merges the last two assistant messages if they are both from the assistant. This is useful for combining messages that are split and continued due to token limits.

This is used on Conversation on all API returns, as it is a no-op if there are less than two messages, or if the last two messages are not both from the assistant.

func (*Conversation) REPL

func (conversation *Conversation) REPL()

REPL is a Read-Eval-Print Loop for a conversation. It reads input from stdin, sends it to the assistant, and prints the assistant's response. It also prints the total tokens used for the conversation, and the tokens used for the last message.

It is intended as a simple way to interact with the assistant via a console.

func (*Conversation) Send

func (conversation *Conversation) Send(text string) (
	reply string,
	stopReason string,
	inputTokens int,
	outputTokens int,
	err error,
)

Send sends a message to the assistant and returns the reply. It also returns the reason the conversation stopped, the number of input tokens used, and the number of output tokens used.

If the text is empty, it sends the message as is, and does not add a user message to the conversation. This is useful for continuing an incomplete conversation by "assistant", in the case of a stopReason of "max_tokens".

func (*Conversation) SendUntilDone

func (conversation *Conversation) SendUntilDone(
	text string,
) (
	output string,
	stopReason string,
	inputTokens int,
	outputTokens int,
	err error,
)

SendUntilDone sends a message to the assistant, and continues sending messages until the conversation is done. It returns the full output text, the reason the conversation stopped, the total number of input tokens used, the total number of output tokens used, and any error that occurred.

type Message

type Message struct {
	// Role of the message sender.
	// Possible values are:
	//   "user": The message is from the user.
	//   "assistant": The message is from the assistant.
	Role string `json:"role"`
	// Content is the content of the message, and may be a single string or
	// image, or an array of content blocks.
	Content *[]ContentBlock `json:"content"`
}

Message is a single message in a conversation.

type Messages

type Messages struct {
	// Model is the model to use for the messages.
	Model string `json:"model"`
	// MaxTokens is the maximum number of tokens to generate.
	MaxTokens int `json:"max_tokens"`
	// Temperature is the temperature to use for sampling. Ranges from 0 to 1.
	Temperature float64 `json:"temperature"`
	// System is the system prompt to use for the conversation.
	System *string `json:"system"`
	// Messages is the sequence of messages in the conversation. They must
	// alternate between user and assistant messages.
	Messages *[]*Message `json:"messages"`
}

Messages is a sequence of messages in a conversation. It usually always a user message first, and alternates between user and assistant messages.

type Response

type Response struct {
	// id: The ID of the response.
	ID string `json:"id"`
	// MessageType is the type of the message.
	MessageType string `json:"type"`
	// Role of the message sender. This is usually almost always "assistant".
	Role string `json:"role"`
	// Model is the model used for the response.
	Model string `json:"model"`
	// Content is the content of the response. This usually only has one
	// content block.
	Content *[]ContentBlock `json:"content"`
	// StopReason is the reason the response was stopped. They can be:
	//   "end_turn": The response reached the end of the turn.
	//   "max_tokens": The response reached the maximum token limit.
	//   "stop_sequence": The response reached a stop sequence.
	//   "tools_use": The model invoked one or more tools.
	StopReason string `json:"stop_reason"`
	// StopSequence is the stop sequence that caused the response to stop,
	// in the case of a StopReason of "stop_sequence"
	StopSequence *string `json:"stop_sequence"`
	// Usage is the usage statistics for the response.
	Usage struct {
		InputTokens  int `json:"input_tokens"`
		OutputTokens int `json:"output_tokens"`
	}
}

Response is the response from the Anthropic API for a message.

type SampleSettings

type SampleSettings struct {
	// Model to use for the sample.
	Model string `json:"model"`
	// Version of the model to use for the sample.
	Version string `json:"version"`
	// Beta is the beta version of the model to use for the sample.
	Beta string `json:"beta"`
	// MaxTokens is the mas number of tokens to generate.
	MaxTokens int `json:"max_tokens"`
	// The temperature to use for sampling.
	Temperature float64 `json:"temperature"`
}

SampleSettings is used to set the settings for the request. Usually it pertains to samplers.

Jump to

Keyboard shortcuts

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