-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain.go
187 lines (162 loc) · 4.45 KB
/
main.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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/anthropics/anthropic-sdk-go"
)
func main() {
client := anthropic.NewClient()
content := "What is the weather in San Francisco, CA?"
println(color("[user]: ") + content)
messages := []anthropic.MessageParam{
anthropic.NewUserMessage(anthropic.NewTextBlock(content)),
}
tools := []anthropic.ToolParam{
{
Name: anthropic.F("get_coordinates"),
Description: anthropic.F("Accepts a place as an address, then returns the latitude and longitude coordinates."),
InputSchema: anthropic.F[interface{}](map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"location": map[string]interface{}{
"type": "string",
"description": "The location to look up.",
},
},
}),
},
{
Name: anthropic.F("get_temperature_unit"),
InputSchema: anthropic.F[interface{}](map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"country": map[string]interface{}{
"type": "string",
"description": "The country",
},
},
}),
},
{
Name: anthropic.F("get_weather"),
Description: anthropic.F("Get the weather at a specific location"),
InputSchema: anthropic.F[interface{}](map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"lat": map[string]interface{}{
"type": "number",
"description": "The lattitude of the location to check weather.",
},
"long": map[string]interface{}{
"type": "number",
"description": "The longitude of the location to check weather.",
},
"unit": map[string]interface{}{
"type": "string",
"enum": []string{"celsius", "fahrenheit"},
"description": "Unit for the output",
},
},
}),
},
}
for {
message, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{
Model: anthropic.F(anthropic.ModelClaude3_5SonnetLatest),
MaxTokens: anthropic.Int(1024),
Messages: anthropic.F(messages),
Tools: anthropic.F(tools),
})
if err != nil {
panic(err)
}
print(color("[assistant]: "))
for _, block := range message.Content {
switch block := block.AsUnion().(type) {
case anthropic.TextBlock:
println(block.Text)
println()
case anthropic.ToolUseBlock:
println(block.Name + ": " + string(block.Input))
println()
}
}
println()
messages = append(messages, message.ToParam())
toolResults := []anthropic.ContentBlockParamUnion{}
for _, block := range message.Content {
if block.Type == anthropic.ContentBlockTypeToolUse {
print(color("[user (" + block.Name + ")]: "))
var response interface{}
switch block.Name {
case "get_coordinates":
var input struct {
Location string `json:"location"`
}
err := json.Unmarshal(block.Input, &input)
if err != nil {
panic(err)
}
response = GetCoordinates(input.Location)
case "get_temperature_unit":
var input struct {
Country string `json:"country"`
}
err := json.Unmarshal(block.Input, &input)
if err != nil {
panic(err)
}
response = GetTemperatureUnit(input.Country)
case "get_weather":
var input struct {
Lat float64 `json:"lat"`
Long float64 `json:"long"`
Unit string `json:"unit"`
}
err := json.Unmarshal(block.Input, &input)
if err != nil {
panic(err)
}
response = GetWeather(input.Lat, input.Long, input.Unit)
}
b, err := json.Marshal(response)
if err != nil {
panic(err)
}
println(string(b))
toolResults = append(toolResults, anthropic.NewToolResultBlock(block.ID, string(b), false))
}
}
if len(toolResults) == 0 {
break
}
messages = append(messages, anthropic.NewUserMessage(toolResults...))
}
}
type CoordinateResponse struct {
Long float64 `json:"long"`
Lat float64 `json:"lat"`
}
func GetCoordinates(location string) CoordinateResponse {
return CoordinateResponse{
Long: -122.4194,
Lat: 37.7749,
}
}
func GetTemperatureUnit(country string) string {
return "farenheit"
}
type WeatherResponse struct {
Unit string `json:"unit"`
Temperature float64 `json:"temperature"`
}
func GetWeather(lat, long float64, unit string) WeatherResponse {
return WeatherResponse{
Unit: "farenheit",
Temperature: 122,
}
}
func color(s string) string {
return fmt.Sprintf("\033[1;%sm%s\033[0m", "33", s)
}