Documentation
¶
Index ¶
- Variables
- type ContentBlock
- type ContentSource
- type Conversation
- func (conversation *Conversation) AddMessage(role string, content *[]ContentBlock)
- func (conversation *Conversation) MergeIfLastTwoAssistant()
- func (conversation *Conversation) REPL()
- func (conversation *Conversation) Send(text string) (reply string, stopReason string, inputTokens int, outputTokens int, err error)
- func (conversation *Conversation) SendUntilDone(text string) (output string, stopReason string, inputTokens int, outputTokens int, err error)
- type Message
- type Messages
- type Response
- type SampleSettings
Constants ¶
This section is empty.
Variables ¶
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.
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.