-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
258 lines (210 loc) · 6.94 KB
/
message.go
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
256
257
258
package anthropic
import (
"context"
"encoding/json"
"errors"
"net/http"
)
const (
MessageRoleAssistant = "assistant"
MessageRoleUser = "user"
)
const messagesSuffix = "/messages"
var (
ErrContentFieldsMisused = errors.New("can't use both Content and ContentBlocks properties simultaneously")
ErrChatCompletionStreamNotSupported = errors.New("streaming is not supported with this method, please use CreateMessageStream") //nolint:lll
ErrModelNotAvailable = errors.New("this model is not available for Anthropic Messages API")
)
const (
TextContentObjectType = "text"
ImageContentObjectType = "image"
ToolUseContentObjectType = "tool_use"
ToolResultContentObjectType = "tool_result"
)
// ContentBlock is used to provide the [InputMessage] with multiple input or input other than a simple string
type ContentBlock struct {
Type string `json:"type"`
// For Text type
Text string `json:"text,omitempty"`
// For Image type
Source ImageSource `json:"source,omitempty"`
// For Tool Use type
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Input map[string]interface{} `json:"input,omitempty"`
// For Tool Result type
ToolUseId string `json:"tool_use_id,omitempty"`
IsError bool `json:"is_error,omitempty"`
ToolResultContent ToolResultContent `json:"content,omitempty"`
}
func (cb ContentBlock) MarshalJSON() ([]byte, error) {
type alias ContentBlock
temp := struct {
alias
Source *ImageSource `json:"source,omitempty"`
ToolResultContent *ToolResultContent `json:"content,omitempty"`
}{
alias: alias(cb),
}
if cb.Source != (ImageSource{}) {
temp.Source = &cb.Source
}
if cb.ToolResultContent != (ToolResultContent{}) {
temp.ToolResultContent = &cb.ToolResultContent
}
return json.Marshal(temp)
}
type ToolResultContent struct {
Type string `json:"type"`
// For Text type
Text string `json:"text,omitempty"`
// For Image type
Source ImageSource `json:"source,omitempty"`
}
func (trs ToolResultContent) MarshalJSON() ([]byte, error) {
type alias ToolResultContent
temp := struct {
alias
Source *ImageSource `json:"source,omitempty"`
}{
alias: alias(trs),
}
if trs.Source != (ImageSource{}) {
temp.Source = &trs.Source
}
return json.Marshal(temp)
}
const ImageSourceType = "base64"
const (
ImageJPEGMediaType = "image/jpeg"
ImagePNGMediaType = "image/png"
ImageGIFMediaType = "image/gif"
ImageWebPMediaType = "image/webp"
)
type ImageSource struct {
Type string `json:"type"`
MediaType string `json:"media_type"`
Data string `json:"data"`
}
// InputMessage stores content of message request. When creating new message with [Client.CreateMessage], Role field is always equal to "user".
// Content field is used to pass just one string of content. ContentBlocks are used to pass multiple input content and/or content other than a string, like an image.
//
// Note that Content and ContentBlocks fields cannot be used simultaneously in one InputMessage.
type InputMessage struct {
Role string `json:"role"`
Content string `json:"content"`
ContentBlocks []ContentBlock
}
func (m InputMessage) MarshalJSON() ([]byte, error) {
if m.Content != "" && m.ContentBlocks != nil {
return nil, ErrContentFieldsMisused
}
if len(m.ContentBlocks) > 0 {
msg := struct {
Role string `json:"role"`
Content string `json:"-"`
ContentBlocks []ContentBlock `json:"content"`
}(m)
return json.Marshal(msg)
}
msg := struct {
Role string `json:"role"`
Content string `json:"content"`
ContentBlocks []ContentBlock `json:"-"`
}(m)
return json.Marshal(msg)
}
func (m *InputMessage) UnmarshalJSON(bs []byte) error {
msg := InputMessage{}
if err := json.Unmarshal(bs, &msg); err == nil {
*m = msg
return nil
}
objectMsg := struct {
Role string `json:"role"`
Content string
ContentBlocks []ContentBlock `json:"content"`
}{}
if err := json.Unmarshal(bs, &objectMsg); err != nil {
return err
}
*m = InputMessage(objectMsg)
return nil
}
const (
Claude35SonnetModel = "claude-3-5-sonnet-20240620"
Claude3OpusModel = "claude-3-opus-20240229"
Claude3SonnetModel = "claude-3-sonnet-20240229"
Claude3HaikuModel = "claude-3-haiku-20240307"
)
type MessageRequestMetadata struct {
UserId string `json:"user_id,omitempty"`
}
// MessageRequest represents a request structure for Anthropic Messages API
type MessageRequest struct {
Model string `json:"model"`
Messages []InputMessage `json:"messages"`
MaxTokens int `json:"max_tokens"`
Temperature int `json:"temperature,omitempty"`
StopSequences []string `json:"stop_sequences,omitempty"`
Metadata *MessageRequestMetadata `json:"metadata,omitempty"`
Stream bool `json:"stream,omitempty"`
System string `json:"system,omitempty"`
TopK int `json:"top_k,omitempty"`
TopP int `json:"top_p,omitempty"`
Tools []Tool `json:"tools,omitempty"`
ToolChoice *ToolChoice `json:"tool_choice,omitempty"`
}
type Tool struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
InputSchema map[string]interface{} `json:"input_schema"`
}
const ObjectToolInputSchemaType = "object"
type ToolInputSchema struct {
Type string `json:"type"`
Properties map[string]interface{} `json:"properties,omitempty"`
}
const (
AutoToolChoiceType = "auto"
AnyToolChoiceType = "any"
ToolToolChoiceType = "tool"
)
type ToolChoice struct {
Type string `json:"type"`
Name string `json:"name,omitempty"`
}
type StopReason string
const (
StopReasonEndTurn StopReason = "end_turn"
StopReasonMaxTokens StopReason = "max_tokens"
StopReasonStopSequence StopReason = "stop_sequence"
StopReasonToolUser StopReason = "tool_use"
)
type MessageResponse struct {
ID string `json:"id"`
Type string `json:"type"`
Content []ContentBlock `json:"content"`
Role string `json:"role"`
Model string `json:"model"`
StopReason StopReason `json:"stop_reason"`
StopSequence string `json:"stop_sequence,omitempty"`
Usage Usage `json:"usage"`
}
type Usage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
}
// CreateMessage - API call to Anthropic Messages API to create a message completion
func (c *Client) CreateMessage(ctx context.Context, request MessageRequest) (response MessageResponse, err error) {
if request.Stream {
err = ErrChatCompletionStreamNotSupported
return
}
req, err := c.newRequest(context.Background(), http.MethodPost, c.fullURL(messagesSuffix), withBody(request))
if err != nil {
return
}
err = c.sendRequest(req, &response)
return
}