-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
69 lines (57 loc) · 2.38 KB
/
app.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
package main
import (
"fmt"
"io"
"log/slog"
"net/http"
"os"
"github.com/yomorun/yomo/serverless"
)
// Description defines the Function Calling, this will be combine to prompt automatically. As openweathermap.org does not support request api by `city_name`, we have ask LLM to translate it to Latitude and Longitude.
func Description() string {
return `Get current weather for a giving city. If no city is provided, you should ask to clarify the city.
If the city name is given, you should convert city name to Latitude and Longitude geo coordinates,
keep Latitude and Longitude in decimal format.`
}
// Param defines the input parameters of the Function Calling, this will be combine to prompt automatically
type Param struct {
City string `json:"city" jsonschema:"description=The city name to get the weather for"`
Latitude float64 `json:"latitude" jsonschema:"description=The latitude of the city, in decimal format, range should be in (-90, 90)"`
Longitude float64 `json:"longitude" jsonschema:"description=The longitude of the city, in decimal format, range should be in (-180, 180)"`
}
// InputSchema decalares the type of Function Calling Parameter, required by YoMo
func InputSchema() any {
return &Param{}
}
// DataTags declares the type of data this stateful serverless observe, required by YoMo
func DataTags() []uint32 {
return []uint32{0x30}
}
// Handler is the main entry point of the Function Calling when LLM response with `tool_call`
func Handler(ctx serverless.Context) {
var p Param
// deserilize the parameters from llm tool_call response
ctx.ReadLLMArguments(&p)
// call the openweathermap api and write the result back to LLM
result := requestOpenWeatherMap(p.Latitude, p.Longitude)
ctx.WriteLLMResult(result)
slog.Info("get-weather", "city", p.City, "rag", result)
}
func requestOpenWeatherMap(lat, lon float64) string {
const apiURL = "https://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&appid=%s&units=metric"
apiKey := os.Getenv("OPENWEATHERMAP_API_KEY")
url := fmt.Sprintf(apiURL, lat, lon, apiKey)
// send api request to openweathermap.org
resp, err := http.Get(url)
if err != nil {
fmt.Println(err)
return "can not get the weather information at the moment"
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return "can not get the weather information at the moment"
}
return string(body)
}