package main
import (
"encoding/json"
"fmt"
"log"
"github.com/edgee-cloud/go-sdk/edgee"
)
func stringPtr(s string) *string {
return &s
}
func getWeather(location string, unit string) map[string]interface{} {
return map[string]interface{}{
"location": location,
"temperature": 15,
"unit": unit,
"condition": "sunny",
}
}
func main() {
client, err := edgee.NewClient("your-api-key")
if err != nil {
log.Fatal(err)
}
// Define the weather function
function := edgee.FunctionDefinition{
Name: "get_weather",
Description: stringPtr("Get the current weather for a location"),
Parameters: map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"location": map[string]interface{}{
"type": "string",
"description": "The city name",
},
"unit": map[string]interface{}{
"type": "string",
"enum": []string{"celsius", "fahrenheit"},
"description": "Temperature unit",
},
},
"required": []string{"location"},
},
}
// Step 1: Initial request with tools
input := edgee.InputObject{
Messages: []edgee.Message{
{Role: "user", Content: "What is the weather in Paris and Tokyo?"},
},
Tools: []edgee.Tool{
{Type: "function", Function: function},
},
ToolChoice: "auto",
}
response1, err := client.Send("gpt-4o", input)
if err != nil {
log.Fatal(err)
}
// Step 2: Execute all tool calls
messages := []edgee.Message{
{Role: "user", Content: "What is the weather in Paris and Tokyo?"},
}
// Add assistant's message
if msg := response1.MessageContent(); msg != nil {
messages = append(messages, *msg)
}
if toolCalls := response1.ToolCalls(); len(toolCalls) > 0 {
for _, toolCall := range toolCalls {
var args map[string]interface{}
if err := json.Unmarshal([]byte(toolCall.Function.Arguments), &args); err != nil {
log.Fatal(err)
}
location := args["location"].(string)
unit := "celsius"
if u, ok := args["unit"].(string); ok {
unit = u
}
result := getWeather(location, unit)
resultJSON, _ := json.Marshal(result)
toolCallID := toolCall.ID
messages = append(messages, edgee.Message{
Role: "tool",
ToolCallID: &toolCallID,
Content: string(resultJSON),
})
}
}
// Step 3: Send results back
input2 := edgee.InputObject{
Messages: messages,
Tools: []edgee.Tool{
// Keep tools available for follow-up
{Type: "function", Function: function},
},
}
response2, err := client.Send("gpt-4o", input2)
if err != nil {
log.Fatal(err)
}
fmt.Println(response2.Text())
}