blob: 3ba6ed4d946015cab34cdf151854341c4d4c6368 [file] [log] [blame]
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -07001// Package llm provides a unified interface for interacting with LLMs.
2package llm
3
4import (
5 "context"
6 "encoding/json"
7 "fmt"
8 "log/slog"
9 "strings"
10 "time"
11)
12
13type Service interface {
14 // Do sends a request to an LLM.
15 Do(context.Context, *Request) (*Response, error)
16}
17
18// MustSchema validates that schema is a valid JSON schema and returns it as a json.RawMessage.
19// It panics if the schema is invalid.
20func MustSchema(schema string) json.RawMessage {
21 // TODO: validate schema, for now just make sure it's valid JSON
22 schema = strings.TrimSpace(schema)
23 bytes := []byte(schema)
24 if !json.Valid(bytes) {
25 panic("invalid JSON schema: " + schema)
26 }
27 return json.RawMessage(bytes)
28}
29
30type Request struct {
31 Messages []Message
32 ToolChoice *ToolChoice
33 Tools []*Tool
34 System []SystemContent
35}
36
37// Message represents a message in the conversation.
38type Message struct {
39 Role MessageRole
40 Content []Content
41 ToolUse *ToolUse // use to control whether/which tool to use
42}
43
44// ToolUse represents a tool use in the message content.
45type ToolUse struct {
46 ID string
47 Name string
48}
49
50type ToolChoice struct {
51 Type ToolChoiceType
52 Name string
53}
54
55type SystemContent struct {
56 Text string
57 Type string
58 Cache bool
59}
60
61// Tool represents a tool available to an LLM.
62type Tool struct {
63 Name string
64 // Type is used by the text editor tool; see
65 // https://docs.anthropic.com/en/docs/build-with-claude/tool-use/text-editor-tool
66 Type string
67 Description string
68 InputSchema json.RawMessage
69
70 // The Run function is automatically called when the tool is used.
71 // Run functions may be called concurrently with each other and themselves.
72 // The input to Run function is the input to the tool, as provided by Claude, in compliance with the input schema.
73 // The outputs from Run will be sent back to Claude.
74 // If you do not want to respond to the tool call request from Claude, return ErrDoNotRespond.
75 // ctx contains extra (rarely used) tool call information; retrieve it with ToolCallInfoFromContext.
76 Run func(ctx context.Context, input json.RawMessage) (string, error) `json:"-"`
77}
78
79type Content struct {
80 ID string
81 Type ContentType
82 Text string
83
84 // for thinking
85 Thinking string
86 Data string
87 Signature string
88
89 // for tool_use
90 ToolName string
91 ToolInput json.RawMessage
92
93 // for tool_result
94 ToolUseID string
95 ToolError bool
96 ToolResult string
97
98 // timing information for tool_result; added externally; not sent to the LLM
99 ToolUseStartTime *time.Time
100 ToolUseEndTime *time.Time
101
102 Cache bool
103}
104
105func StringContent(s string) Content {
106 return Content{Type: ContentTypeText, Text: s}
107}
108
109// ContentsAttr returns contents as a slog.Attr.
110// It is meant for logging.
111func ContentsAttr(contents []Content) slog.Attr {
112 var contentAttrs []any // slog.Attr
113 for _, content := range contents {
114 var attrs []any // slog.Attr
115 switch content.Type {
116 case ContentTypeText:
117 attrs = append(attrs, slog.String("text", content.Text))
118 case ContentTypeToolUse:
119 attrs = append(attrs, slog.String("tool_name", content.ToolName))
120 attrs = append(attrs, slog.String("tool_input", string(content.ToolInput)))
121 case ContentTypeToolResult:
122 attrs = append(attrs, slog.String("tool_result", content.ToolResult))
123 attrs = append(attrs, slog.Bool("tool_error", content.ToolError))
124 case ContentTypeThinking:
125 attrs = append(attrs, slog.String("thinking", content.Text))
126 default:
127 attrs = append(attrs, slog.String("unknown_content_type", content.Type.String()))
128 attrs = append(attrs, slog.Any("text", content)) // just log it all raw, better to have too much than not enough
129 }
130 contentAttrs = append(contentAttrs, slog.Group(content.ID, attrs...))
131 }
132 return slog.Group("contents", contentAttrs...)
133}
134
135type (
136 MessageRole int
137 ContentType int
138 ToolChoiceType int
139 StopReason int
140)
141
142//go:generate go tool golang.org/x/tools/cmd/stringer -type=MessageRole,ContentType,ToolChoiceType,StopReason -output=llm_string.go
143
144const (
145 MessageRoleUser MessageRole = iota
146 MessageRoleAssistant
147
148 ContentTypeText ContentType = iota
149 ContentTypeThinking
150 ContentTypeRedactedThinking
151 ContentTypeToolUse
152 ContentTypeToolResult
153
154 ToolChoiceTypeAuto ToolChoiceType = iota // default
155 ToolChoiceTypeAny // any tool, but must use one
156 ToolChoiceTypeNone // no tools allowed
157 ToolChoiceTypeTool // must use the tool specified in the Name field
158
159 StopReasonStopSequence StopReason = iota
160 StopReasonMaxTokens
161 StopReasonEndTurn
162 StopReasonToolUse
163)
164
165type Response struct {
166 ID string
167 Type string
168 Role MessageRole
169 Model string
170 Content []Content
171 StopReason StopReason
172 StopSequence *string
173 Usage Usage
174 StartTime *time.Time
175 EndTime *time.Time
176}
177
178func (m *Response) ToMessage() Message {
179 return Message{
180 Role: m.Role,
181 Content: m.Content,
182 }
183}
184
185// Usage represents the billing and rate-limit usage.
186// Most LLM structs do not have JSON tags, to avoid accidental direct use in specific providers.
187// However, the front-end uses this struct, and it relies on its JSON serialization.
188// Do NOT use this struct directly when implementing an llm.Service.
189type Usage struct {
190 InputTokens uint64 `json:"input_tokens"`
191 CacheCreationInputTokens uint64 `json:"cache_creation_input_tokens"`
192 CacheReadInputTokens uint64 `json:"cache_read_input_tokens"`
193 OutputTokens uint64 `json:"output_tokens"`
194 CostUSD float64 `json:"cost_usd"`
195}
196
197func (u *Usage) Add(other Usage) {
198 u.InputTokens += other.InputTokens
199 u.CacheCreationInputTokens += other.CacheCreationInputTokens
200 u.CacheReadInputTokens += other.CacheReadInputTokens
201 u.OutputTokens += other.OutputTokens
202 u.CostUSD += other.CostUSD
203}
204
205func (u *Usage) String() string {
206 return fmt.Sprintf("in: %d, out: %d", u.InputTokens, u.OutputTokens)
207}
208
209func (u *Usage) IsZero() bool {
210 return *u == Usage{}
211}
212
213func (u *Usage) Attr() slog.Attr {
214 return slog.Group("usage",
215 slog.Uint64("input_tokens", u.InputTokens),
216 slog.Uint64("output_tokens", u.OutputTokens),
217 slog.Uint64("cache_creation_input_tokens", u.CacheCreationInputTokens),
218 slog.Uint64("cache_read_input_tokens", u.CacheReadInputTokens),
219 slog.Float64("cost_usd", u.CostUSD),
220 )
221}
222
223// UserStringMessage creates a user message with a single text content item.
224func UserStringMessage(text string) Message {
225 return Message{
226 Role: MessageRoleUser,
227 Content: []Content{StringContent(text)},
228 }
229}