Documentation
¶
Overview ¶
Package llm provides a unified interface for interacting with LLMs.
Index ¶
- Constants
- func ContentsAttr(contents []Content) slog.Attr
- func CostUSDFromResponse(headers http.Header) float64
- func DumpToFile(typ string, url string, content []byte) error
- func EmptySchema() json.RawMessage
- func MustSchema(schema string) json.RawMessage
- func UseSimplifiedPatch(svc Service) bool
- type Content
- type ContentType
- type Message
- type MessageRole
- type Request
- type Response
- type Service
- type SimplifiedPatcher
- type StopReason
- type SystemContent
- type Tool
- type ToolChoice
- type ToolChoiceType
- type ToolOut
- type ToolUse
- type Usage
Constants ¶
const ( MessageRoleUser MessageRole = iota MessageRoleAssistant ContentTypeText ContentType = iota ContentTypeThinking ContentTypeRedactedThinking ContentTypeToolUse ContentTypeToolResult ToolChoiceTypeAuto ToolChoiceType = iota // default ToolChoiceTypeAny // any tool, but must use one ToolChoiceTypeNone // no tools allowed ToolChoiceTypeTool // must use the tool specified in the Name field StopReasonStopSequence StopReason = iota StopReasonMaxTokens StopReasonEndTurn StopReasonToolUse StopReasonRefusal )
Variables ¶
This section is empty.
Functions ¶
func ContentsAttr ¶
ContentsAttr returns contents as a slog.Attr. It is meant for logging.
func CostUSDFromResponse ¶
func DumpToFile ¶ added in v0.0.22
DumpToFile writes LLM communication content to a timestamped file in ~/.cache/sketch/. For requests, it includes the URL followed by the content. For responses, it only includes the content. The typ parameter is used as a prefix in the filename ("request", "response").
func EmptySchema ¶
func EmptySchema() json.RawMessage
func MustSchema ¶
func MustSchema(schema string) json.RawMessage
MustSchema validates that schema is a valid JSON schema and returns it as a json.RawMessage. It panics if the schema is invalid. The schema must have at least type="object" and a properties key.
func UseSimplifiedPatch ¶ added in v0.0.28
Types ¶
type Content ¶
type Content struct {
ID string
Type ContentType
Text string
// Media type for image content
MediaType string
// for thinking
Thinking string
Data string
Signature string
// for tool_use
ToolName string
ToolInput json.RawMessage
// for tool_result
ToolUseID string
ToolError bool
ToolResult []Content
// timing information for tool_result; added externally; not sent to the LLM
ToolUseStartTime *time.Time
ToolUseEndTime *time.Time
// Display is content to be displayed to the user, copied from ToolOut
Display any
Cache bool
}
func StringContent ¶
func TextContent ¶
TextContent creates a simple text content for tool results. This is a helper function to create the most common type of tool result content.
type ContentType ¶
type ContentType int
func (ContentType) String ¶
func (i ContentType) String() string
type Message ¶
type Message struct {
Role MessageRole
Content []Content
ToolUse *ToolUse // use to control whether/which tool to use
}
Message represents a message in the conversation.
func UserStringMessage ¶
UserStringMessage creates a user message with a single text content item.
type MessageRole ¶
type MessageRole int
func (MessageRole) String ¶
func (i MessageRole) String() string
type Request ¶
type Request struct {
Messages []Message
ToolChoice *ToolChoice
Tools []*Tool
System []SystemContent
}
type Response ¶
type SimplifiedPatcher ¶ added in v0.0.28
type SimplifiedPatcher interface {
// UseSimplifiedPatch reports whether the service should use the simplified patch input schema.
UseSimplifiedPatch() bool
}
type StopReason ¶
type StopReason int
func (StopReason) String ¶
func (i StopReason) String() string
type SystemContent ¶
type Tool ¶
type Tool struct {
Name string
// Type is used by the text editor tool; see
// https://docs.anthropic.com/en/docs/build-with-claude/tool-use/text-editor-tool
Type string
Description string
InputSchema json.RawMessage
// EndsTurn indicates that this tool should cause the model to end its turn when used
EndsTurn bool
// The Run function is automatically called when the tool is used.
// Run functions may be called concurrently with each other and themselves.
// The input to Run function is the input to the tool, as provided by Claude, in compliance with the input schema.
// The outputs from Run will be sent back to Claude.
// If you do not want to respond to the tool call request from Claude, return ErrDoNotRespond.
// ctx contains extra (rarely used) tool call information; retrieve it with ToolCallInfoFromContext.
Run func(ctx context.Context, input json.RawMessage) ToolOut `json:"-"`
}
Tool represents a tool available to an LLM.
type ToolChoice ¶
type ToolChoice struct {
Type ToolChoiceType
Name string
}
type ToolChoiceType ¶
type ToolChoiceType int
func (ToolChoiceType) String ¶
func (i ToolChoiceType) String() string
type ToolOut ¶ added in v0.0.20
type ToolOut struct {
// LLMContent is the output of the tool to be sent back to the LLM.
// May be nil on error.
LLMContent []Content
// Display is content to be displayed to the user.
// The type of content is set by the tool and coordinated with the UIs.
// It should be JSON-serializable.
Display any
// Error is the error (if any) that occurred during the tool run.
// The text contents of the error will be sent back to the LLM.
// If non-nil, LLMContent will be ignored.
Error error
}
ToolOut represents the output of a tool run.
func ErrorToolOut ¶ added in v0.0.20
func ErrorfToolOut ¶ added in v0.0.20
type Usage ¶
type Usage struct {
InputTokens uint64 `json:"input_tokens"`
CacheCreationInputTokens uint64 `json:"cache_creation_input_tokens"`
CacheReadInputTokens uint64 `json:"cache_read_input_tokens"`
OutputTokens uint64 `json:"output_tokens"`
CostUSD float64 `json:"cost_usd"`
}
Usage represents the billing and rate-limit usage. Most LLM structs do not have JSON tags, to avoid accidental direct use in specific providers. However, the front-end uses this struct, and it relies on its JSON serialization. Do NOT use this struct directly when implementing an llm.Service.