forked from charmbracelet/fantasy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.go
More file actions
255 lines (224 loc) · 8.6 KB
/
model.go
File metadata and controls
255 lines (224 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package fantasy
import (
"context"
"fmt"
"iter"
)
// Usage represents token usage statistics for a model call.
type Usage struct {
InputTokens int64 `json:"input_tokens"`
OutputTokens int64 `json:"output_tokens"`
TotalTokens int64 `json:"total_tokens"`
ReasoningTokens int64 `json:"reasoning_tokens"`
CacheCreationTokens int64 `json:"cache_creation_tokens"`
CacheReadTokens int64 `json:"cache_read_tokens"`
}
func (u Usage) String() string {
return fmt.Sprintf("Usage{Input: %d, Output: %d, Total: %d, Reasoning: %d, CacheCreation: %d, CacheRead: %d}",
u.InputTokens,
u.OutputTokens,
u.TotalTokens,
u.ReasoningTokens,
u.CacheCreationTokens,
u.CacheReadTokens,
)
}
// ResponseContent represents the content of a model response.
type ResponseContent []Content
// Text returns the text content of the response.
func (r ResponseContent) Text() string {
for _, c := range r {
if c.GetType() == ContentTypeText {
return c.(TextContent).Text
}
}
return ""
}
// Reasoning returns all reasoning content parts.
func (r ResponseContent) Reasoning() []ReasoningContent {
var reasoning []ReasoningContent
for _, c := range r {
if c.GetType() == ContentTypeReasoning {
if reasoningContent, ok := AsContentType[ReasoningContent](c); ok {
reasoning = append(reasoning, reasoningContent)
}
}
}
return reasoning
}
// ReasoningText returns all reasoning content as a concatenated string.
func (r ResponseContent) ReasoningText() string {
var text string
for _, reasoning := range r.Reasoning() {
text += reasoning.Text
}
return text
}
// Files returns all file content parts.
func (r ResponseContent) Files() []FileContent {
var files []FileContent
for _, c := range r {
if c.GetType() == ContentTypeFile {
if fileContent, ok := AsContentType[FileContent](c); ok {
files = append(files, fileContent)
}
}
}
return files
}
// Sources returns all source content parts.
func (r ResponseContent) Sources() []SourceContent {
var sources []SourceContent
for _, c := range r {
if c.GetType() == ContentTypeSource {
if sourceContent, ok := AsContentType[SourceContent](c); ok {
sources = append(sources, sourceContent)
}
}
}
return sources
}
// ToolCalls returns all tool call content parts.
func (r ResponseContent) ToolCalls() []ToolCallContent {
var toolCalls []ToolCallContent
for _, c := range r {
if c.GetType() == ContentTypeToolCall {
if toolCallContent, ok := AsContentType[ToolCallContent](c); ok {
toolCalls = append(toolCalls, toolCallContent)
}
}
}
return toolCalls
}
// ToolResults returns all tool result content parts.
func (r ResponseContent) ToolResults() []ToolResultContent {
var toolResults []ToolResultContent
for _, c := range r {
if c.GetType() == ContentTypeToolResult {
if toolResultContent, ok := AsContentType[ToolResultContent](c); ok {
toolResults = append(toolResults, toolResultContent)
}
}
}
return toolResults
}
// Response represents a response from a language model.
type Response struct {
Content ResponseContent `json:"content"`
FinishReason FinishReason `json:"finish_reason"`
Usage Usage `json:"usage"`
Warnings []CallWarning `json:"warnings"`
// for provider specific response metadata, the key is the provider id
ProviderMetadata ProviderMetadata `json:"provider_metadata"`
}
// StreamPartType represents the type of a stream part.
type StreamPartType string
const (
// StreamPartTypeWarnings represents warnings stream part type.
StreamPartTypeWarnings StreamPartType = "warnings"
// StreamPartTypeTextStart represents text start stream part type.
StreamPartTypeTextStart StreamPartType = "text_start"
// StreamPartTypeTextDelta represents text delta stream part type.
StreamPartTypeTextDelta StreamPartType = "text_delta"
// StreamPartTypeTextEnd represents text end stream part type.
StreamPartTypeTextEnd StreamPartType = "text_end"
// StreamPartTypeReasoningStart represents reasoning start stream part type.
StreamPartTypeReasoningStart StreamPartType = "reasoning_start"
// StreamPartTypeReasoningDelta represents reasoning delta stream part type.
StreamPartTypeReasoningDelta StreamPartType = "reasoning_delta"
// StreamPartTypeReasoningEnd represents reasoning end stream part type.
StreamPartTypeReasoningEnd StreamPartType = "reasoning_end"
// StreamPartTypeToolInputStart represents tool input start stream part type.
StreamPartTypeToolInputStart StreamPartType = "tool_input_start"
// StreamPartTypeToolInputDelta represents tool input delta stream part type.
StreamPartTypeToolInputDelta StreamPartType = "tool_input_delta"
// StreamPartTypeToolInputEnd represents tool input end stream part type.
StreamPartTypeToolInputEnd StreamPartType = "tool_input_end"
// StreamPartTypeToolCall represents tool call stream part type.
StreamPartTypeToolCall StreamPartType = "tool_call"
// StreamPartTypeToolResult represents tool result stream part type.
StreamPartTypeToolResult StreamPartType = "tool_result"
// StreamPartTypeSource represents source stream part type.
StreamPartTypeSource StreamPartType = "source"
// StreamPartTypeFinish represents finish stream part type.
StreamPartTypeFinish StreamPartType = "finish"
// StreamPartTypeError represents error stream part type.
StreamPartTypeError StreamPartType = "error"
)
// StreamPart represents a part of a streaming response.
type StreamPart struct {
Type StreamPartType `json:"type"`
ID string `json:"id"`
ToolCallName string `json:"tool_call_name"`
ToolCallInput string `json:"tool_call_input"`
Delta string `json:"delta"`
ProviderExecuted bool `json:"provider_executed"`
Usage Usage `json:"usage"`
FinishReason FinishReason `json:"finish_reason"`
Error error `json:"error"`
Warnings []CallWarning `json:"warnings"`
// Source-related fields
SourceType SourceType `json:"source_type"`
URL string `json:"url"`
Title string `json:"title"`
ProviderMetadata ProviderMetadata `json:"provider_metadata"`
}
// StreamResponse represents a streaming response sequence.
type StreamResponse = iter.Seq[StreamPart]
// ToolChoice represents the tool choice preference for a model call.
type ToolChoice string
const (
// ToolChoiceNone indicates no tools should be used.
ToolChoiceNone ToolChoice = "none"
// ToolChoiceAuto indicates tools should be used automatically.
ToolChoiceAuto ToolChoice = "auto"
// ToolChoiceRequired indicates tools are required.
ToolChoiceRequired ToolChoice = "required"
)
// SpecificToolChoice creates a tool choice for a specific tool name.
func SpecificToolChoice(name string) ToolChoice {
return ToolChoice(name)
}
// Call represents a call to a language model.
type Call struct {
Prompt Prompt `json:"prompt"`
MaxOutputTokens *int64 `json:"max_output_tokens"`
Temperature *float64 `json:"temperature"`
TopP *float64 `json:"top_p"`
TopK *int64 `json:"top_k"`
PresencePenalty *float64 `json:"presence_penalty"`
FrequencyPenalty *float64 `json:"frequency_penalty"`
Tools []Tool `json:"tools"`
ToolChoice *ToolChoice `json:"tool_choice"`
// for provider specific options, the key is the provider id
ProviderOptions ProviderOptions `json:"provider_options"`
}
// CallWarningType represents the type of call warning.
type CallWarningType string
const (
// CallWarningTypeUnsupportedSetting indicates an unsupported setting.
CallWarningTypeUnsupportedSetting CallWarningType = "unsupported-setting"
// CallWarningTypeUnsupportedTool indicates an unsupported tool.
CallWarningTypeUnsupportedTool CallWarningType = "unsupported-tool"
// CallWarningTypeOther indicates other warnings.
CallWarningTypeOther CallWarningType = "other"
)
// CallWarning represents a warning from the model provider for this call.
// The call will proceed, but e.g. some settings might not be supported,
// which can lead to suboptimal results.
type CallWarning struct {
Type CallWarningType `json:"type"`
Setting string `json:"setting"`
Tool Tool `json:"tool"`
Details string `json:"details"`
Message string `json:"message"`
}
// LanguageModel represents a language model that can generate responses and stream responses.
type LanguageModel interface {
Generate(context.Context, Call) (*Response, error)
Stream(context.Context, Call) (StreamResponse, error)
GenerateObject(context.Context, ObjectCall) (*ObjectResponse, error)
StreamObject(context.Context, ObjectCall) (ObjectStreamResponse, error)
Provider() string
Model() string
}