messages

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const URL = claude.URL + "/v1/messages"

URL is the URL for the Messages API.

Variables

View Source
var SUPPORTED_MIME_TYPES = []string{"image/jpeg", "image/png", "image/gif", "image/webp"}

Slice of supported mime types.

View Source
var StopReasons = map[string]string{
	"end_turn":      "the model reached a natural stopping point",
	"max_tokens":    "we exceeded the requested max_tokens or the model's maximum",
	"stop_sequence": "one of your provided custom stop_sequences was generated",
	"tool_use":      "the model requests use of a tool",
}

StopReasons is a map of stop reasons.

Functions

This section is empty.

Types

type Content

type Content struct {
	// Type is the type of content.
	Type string `json:"type"`

	// Text is the text of the content.
	Text string `json:"text,omitempty"`

	// Id is the unique object identifier for a tool_use block.
	Id string `json:"id,omitempty"`

	// Input is the input request for a tool_use block.
	Input interface{} `json:"input,omitempty"`

	// Name is the name of the tool used in a tool_use block.
	Name string `json:"name,omitempty"`

	Source *MediaSource `json:"source,omitempty"`
}

Content is the content of the message.

type Conversation added in v0.0.2

type Conversation struct {
	// Id is the unique object identifier.
	Id string `json:"id"`

	// Model is the model used in the conversation.
	Model *string `json:"model"`

	// Created is the time the conversation was created.
	Created time.Time `json:"created"`

	// Updated is the time the conversation was updated.
	Updated time.Time `json:"updated"`

	// Messages is a list of messages in the conversation.
	Messages []*Message `json:"messages"`
}

Conversation is a conversation.

type ErrConflictingOptions

type ErrConflictingOptions struct {
	Err error
	Msg string
}

func (*ErrConflictingOptions) Error

func (e *ErrConflictingOptions) Error() string

type ErrFetchingMimeType

type ErrFetchingMimeType struct {
	Err error
	Msg string
}

func (*ErrFetchingMimeType) Error

func (e *ErrFetchingMimeType) Error() string

type ErrInvalidModel

type ErrInvalidModel struct {
	Err error
	Msg string
}

func (*ErrInvalidModel) Error

func (e *ErrInvalidModel) Error() string

type ErrLoadingGOB added in v0.0.2

type ErrLoadingGOB struct {
	Err error
	Msg string
}

func (*ErrLoadingGOB) Error added in v0.0.2

func (e *ErrLoadingGOB) Error() string

type ErrMarshalingInput

type ErrMarshalingInput struct {
	Err error
	Msg string
}

func (*ErrMarshalingInput) Error

func (e *ErrMarshalingInput) Error() string

type ErrMarshalingReply

type ErrMarshalingReply struct {
	Err error
	Msg string
}

func (*ErrMarshalingReply) Error

func (e *ErrMarshalingReply) Error() string

type ErrMaxTokensExceeded

type ErrMaxTokensExceeded struct {
	Err       error
	Msg       string
	Model     string
	MaxTokens int
}

func (*ErrMaxTokensExceeded) Error

func (e *ErrMaxTokensExceeded) Error() string

type ErrMissingClaude

type ErrMissingClaude struct {
	Err error
	Msg string
}

func (*ErrMissingClaude) Error

func (e *ErrMissingClaude) Error() string

type ErrMissingModel

type ErrMissingModel struct {
	Err error
	Msg string
}

func (*ErrMissingModel) Error

func (e *ErrMissingModel) Error() string

type ErrOpeningFile added in v0.0.2

type ErrOpeningFile struct {
	Err error
	Msg string
}

func (*ErrOpeningFile) Error added in v0.0.2

func (e *ErrOpeningFile) Error() string

type ErrReadingFile

type ErrReadingFile struct {
	Err error
	Msg string
}

func (*ErrReadingFile) Error

func (e *ErrReadingFile) Error() string

type ErrSavingGOB added in v0.0.2

type ErrSavingGOB struct {
	Err error
	Msg string
}

func (*ErrSavingGOB) Error added in v0.0.2

func (e *ErrSavingGOB) Error() string

type ErrStreamingMessage added in v0.1.0

type ErrStreamingMessage struct {
	Err error
	Msg string
}

func (*ErrStreamingMessage) Error added in v0.1.0

func (e *ErrStreamingMessage) Error() string

type ErrToolUseNotSupported added in v0.1.0

type ErrToolUseNotSupported struct {
	Err error
	Msg string
}

func (*ErrToolUseNotSupported) Error added in v0.1.0

func (e *ErrToolUseNotSupported) Error() string

type ErrUnsupportedMimeType

type ErrUnsupportedMimeType struct {
	Err      error
	Msg      string
	MimeType string
}

func (*ErrUnsupportedMimeType) Error

func (e *ErrUnsupportedMimeType) Error() string

type ErrUnsupportedOption

type ErrUnsupportedOption struct {
	Err error
	Msg string
}

func (*ErrUnsupportedOption) Error

func (e *ErrUnsupportedOption) Error() string

type MediaSource

type MediaSource struct {
	// Type is the type of media source.
	// The only type is "base64".
	Type string `json:"type"`

	// MediaType is the media type of the data.
	// Valid image types: image/jpeg, image/png, image/gif, and image/webp.
	MediaType string `json:"media_type"`

	// Data is the base64 encoded data.
	Data string `json:"data"`
}

MediaSource is the source of the media.

type Message

type Message struct {
	// Role is the conversational role of the message.
	// Specify a single user-role message, or you can include multiple "user" and "assistant" messages.
	// The first message must always use the "user" role.
	Role string `json:"role"`

	// MessageContent is the content of the message.
	MessageContent []*Content `json:"content"`
}

Message is a sinble nput message.

type Messages

type Messages struct {
	// contains filtered or unexported fields
}

Messages is the messages configuration.

func New

func New(opts ...func(*Messages)) (*Messages, error)

New creates a new Messages configuration.

func (*Messages) AddRoleAssistant

func (messages *Messages) AddRoleAssistant(content string)

func (*Messages) AddRoleUser

func (messages *Messages) AddRoleUser(content string)

func (*Messages) AddRoleUserMedia

func (messages *Messages) AddRoleUserMedia(fqpn string, prompt string) error

func (*Messages) AddTool added in v0.1.0

func (messages *Messages) AddTool(tool *Tool)

func (*Messages) GetMessageJSON added in v0.1.0

func (messages *Messages) GetMessageJSON() (*[]byte, error)

func (*Messages) Load added in v0.0.2

func (messages *Messages) Load() error

func (*Messages) Save added in v0.0.2

func (messages *Messages) Save() error

func (*Messages) Send

func (messages *Messages) Send() (*Response, error)

func (*Messages) SetMaxTokens

func (messages *Messages) SetMaxTokens(n int) error

func (*Messages) SetStreaming added in v0.1.0

func (messages *Messages) SetStreaming(stream bool)

func (*Messages) SetSystemPrompt

func (messages *Messages) SetSystemPrompt(p string)

func (*Messages) SetUserId

func (messages *Messages) SetUserId(id string)

UserId sets the user id.

func (*Messages) Stream

func (messages *Messages) Stream(ctx context.Context) StreamResults

type Metadata

type Metadata struct {
	// UserId is an external identifier for the user who is associated with the request.
	// This should be a uuid, hash value, or other opaque identifier.
	// Anthropic may use this id to help detect abuse.
	// Do not include any identifying information such as name, email address, or phone number.
	UserId string `json:"user_id,omitempty"`
}

Metadata is an object describing metadata about the request.

type Option

type Option func(config *Messages)

Option is a configuration option.

func WithClaude

func WithClaude(claud *claude.Claude) Option

WithClaude sets the Claude configuration.

func WithConversationFile added in v0.0.2

func WithConversationFile(fpqn *string) Option

func WithHaiku

func WithHaiku() Option

func WithMaxTokens

func WithMaxTokens(n int) Option

func WithOpus

func WithOpus() Option

func WithSonnet

func WithSonnet() Option

type Request

type Request struct {
	// Model is the model that will complete your prompt.
	// Required.
	// See models (https://docs.anthropic.com/claude/docs/models-overview) for additional details and options.
	Model string `json:"model"`

	// Messages is the messages to send to the API.
	// Required.
	Messages []*Message `json:"messages"`

	// System is a system prompt is a way of providing context and instructions to Claude, such as specifying a particular goal or role.
	System string `json:"system,omitempty"`

	// MaxToken is the maximum number of tokens to generate before stopping.
	// Required.
	MaxTokens int `json:"max_tokens"`

	// Metadata is an object describing metadata about the request.
	Metadata *Metadata `json:"metadata,omitempty"`

	// StopSequences is a list of strings that, if generated, will cause the model to stop generating tokens.
	StopSequences []string `json:"stop_sequences,omitempty"`

	// Stream is a boolean that indicates whether the model should generate a single response or a stream of responses.
	// Default is false.
	Stream bool `json:"stream"`

	// Temperature is a float that controls the randomness of the model's output. The higher the temperature, the more random the output.
	Temperature *float32 `json:"temperature,omitempty"`

	// Tools are definitions of tools that the model may use
	Tools []*Tool `json:"tools,omitempty"`

	// TopP is an integer that controls nucleus sampling. The higher the top_p, the more diverse the output.
	// Recommended for advanced use cases only. You usually only need to use temperature.
	// You should either alter temperature or top_p, but not both.
	TopP *int `json:"top_p,omitempty"`

	// TopK is an integer that specifies sampling from the top K options for each subsequent token.
	// Recommended for advanced use cases only. You usually only need to use temperature.
	TopK *int `json:"top_k,omitempty"`
	// contains filtered or unexported fields
}

https://docs.anthropic.com/claude/reference/messages_post Request is the request to send to the Messages API.

type Response

type Response struct {
	// Id is the unique object identifier.
	// Required.
	// The format and length of IDs may change over time.
	Id string `json:"id"`

	// Object type.
	// Required.
	// For Messages, this is always "message".
	Type string `json:"type"`

	// Conversational role of the generated message.
	// Required.
	// This will always be "assistant".
	Role string `json:"role"`

	// Content generated by the model.
	// Required.
	// This is an array of content blocks, each of which has a type that determines its shape. Currently, the only type in responses is "text".
	Content []*Content `json:"content"`

	// The model that handled the request.
	// Required.
	Model string `json:"model"`

	// StopReason is the reason that we stopped.
	// Required.
	StopReason string `json:"stop_reason"`

	// StopSequences indicates which custom stop sequence was generated, if any.
	// Required.
	StopSequences string `json:"stop_sequences"`

	// Usage is the usage of the API billing and rate-limit data.
	// Required.
	Usage Usage `json:"usage"`
}

https://docs.anthropic.com/claude/reference/messages_post Response is the response from the Messages API.

type StreamResults added in v0.1.0

type StreamResults struct {
	Response <-chan StreamingMessageResponse
	Error    <-chan error
}

type StreamingMessageContentBlockDelta added in v0.1.0

type StreamingMessageContentBlockDelta struct {
	Type  string `json:"type"`
	Index int    `json:"index"`
	Delta struct {
		Type string `json:"type"`
		Text string `json:"text"`
	} `json:"delta"`
}

type StreamingMessageDelta added in v0.1.0

type StreamingMessageDelta struct {
	Type  string `json:"type"`
	Delta struct {
		StopReason   string `json:"stop_reason"`
		StopSequence string `json:"stop_sequence"`
	} `json:"delta"`
	Usage struct {
		OutputTokens int `json:"output_tokens"`
	}
}

type StreamingMessageError added in v0.1.0

type StreamingMessageError struct {
	Type  string `json:"type"`
	Error struct {
		Type    string `json:"type"`
		Message string `json:"message"`
	} `json:"error"`
}

type StreamingMessageResponse added in v0.1.0

type StreamingMessageResponse struct {
	MessageStart   *StreamingMessageStart             `json:"message_start"`
	MessageDelta   *StreamingMessageDelta             `json:"message_delta"`
	ContentBlock   *StreamingMessageContentBlockDelta `json:"content_block_delta"`
	MessageStop    *StreamingMessageStop              `json:"message_stop"`
	StreamingError *StreamingMessageError             `json:"streaming_error"`
}

type StreamingMessageStart added in v0.1.0

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

type StreamingMessageStop added in v0.1.0

type StreamingMessageStop struct {
	Type  string `json:"type"`
	Delta struct {
		StopReason   string `json:"stop_reason"`
		EndTurn      bool   `json:"end_turn"`
		StopSequence string `json:"stop_sequence"`
	} `json:"delta"`
	Usage struct {
		OutputTokens int `json:"output_tokens"`
	}
}

type Tool added in v0.1.0

type Tool struct {
	// Name is the name of the tool.
	Name string `json:"name"`

	// Description is the optional description of the tool.
	Description string `json:"description"`

	// Input_schema specified the JSON schema for the tool input shape that the model will produce in tool_use output content blocks.
	InputSchema *jsonschema.Schema `json:"input_schema"`
}

Tool defines a tool that the model may use.

type ToolReply added in v0.1.0

type ToolReply struct {

	// Id is the unique object identifier for a tool_use block.
	Id string `json:"id"`

	// Name is the name of the tool used in a tool_use block.
	Name string `json:"name"`

	// Input is the input request for a tool_use block.
	Input interface{} `json:"input"`

	// Type is the type of content. Should ne "tool_use".
	Type string `json:"type"`
}

ToolReply is the reply from the tool.

type Usage added in v0.1.0

type Usage struct {
	InputTokens int `json:"input_tokens"`
	// OutputTokens is the number of tokens generated by the model.
	// Required.
	OutputTokens int `json:"output_tokens"`
}

Jump to

Keyboard shortcuts

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