-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.go
87 lines (71 loc) · 2.61 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"log"
"log/slog"
"os"
"github.com/joho/godotenv"
"github.com/resend/resend-go/v2"
"github.com/yomorun/yomo/serverless"
)
// Description outlines the functionality for the LLM Function Calling feature.
func Description() string {
return `Generate and send emails. Please provide the recipient's email address, and you should help generate appropriate subject and content. If no recipient address is provided, You should ask to add one. When you generate the subject and content, you should send it through the email sending function.`
}
// InputSchema defines the argument structure for LLM Function Calling
func InputSchema() any {
return &Parameter{}
}
var client *resend.Client
// Init is an optional function invoked during the initialization phase of the
// sfn instance. It's designed for setup tasks like global variable
// initialization, establishing database connections, or loading models into
// GPU memory. If initialization fails, the sfn instance will halt and
// terminate. This function can be omitted if no initialization tasks are
// needed.
func Init() error {
if _, ok := os.LookupEnv("RESEND_API_KEY"); !ok {
err := godotenv.Load()
if err != nil {
log.Fatal("You have to set RESEND_API_KEY in ENV or .env file")
os.Exit(-1)
}
}
client = resend.NewClient(os.Getenv("RESEND_API_KEY"))
return nil
}
// Parameter defines the arguments for the LLM Function Calling
type Parameter struct {
To string `json:"to" jsonschema:"description=The recipient's email address"`
Subject string `json:"subject" jsonschema:"description=The subject of the email"`
Body string `json:"body" jsonschema:"description=The content of the email"`
}
// Handler orchestrates the core processing logic of this function
func Handler(ctx serverless.Context) {
var args Parameter
ctx.ReadLLMArguments(&args)
result, err := sendEmail(args)
if err != nil {
ctx.WriteLLMResult(fmt.Sprintf("Failed to send email: %v", err))
return
}
ctx.WriteLLMResult(result)
slog.Info("send-email", "to", args.To, "result", result)
}
func sendEmail(args Parameter) (string, error) {
if err := godotenv.Load(); err != nil {
slog.Warn("Error loading .env file", "error", err)
}
slog.Info("send-email", "args", args)
params := &resend.SendEmailRequest{
From: os.Getenv("FROM_EMAIL"),
To: []string{args.To},
Subject: args.Subject,
Html: fmt.Sprintf("<p>%s</p>", args.Body),
}
resp, err := client.Emails.Send(params)
if err != nil {
return "", fmt.Errorf("failed to send email: %w", err)
}
return fmt.Sprintf("Email has been successfully sent to %s with ID: %s", args.To, resp.Id), nil
}