-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (70 loc) · 1.64 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
package main
import (
"context"
"fmt"
"html/template"
"net/http"
"os"
"cloud.google.com/go/compute/metadata"
"cloud.google.com/go/vertexai/genai"
)
type AnimalForm struct {
Animal string
}
func main() {
ctx := context.Background()
var projectId string
var err error
projectId = os.Getenv("GOOGLE_CLOUD_PROJECT")
if projectId == "" {
projectId, err = metadata.ProjectIDWithContext(ctx)
if err != nil {
return
}
}
var client *genai.Client
client, err = genai.NewClient(ctx, projectId, "us-central1")
if err != nil {
return
}
defer client.Close()
model := client.GenerativeModel("gemini-1.5-flash-001")
// Load the HTML template
tmpl, err := template.ParseFiles("templates/form.html")
if err != nil {
panic(err)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
// Handle form submission
r.ParseForm()
animal := r.FormValue("animal")
resp, err := model.GenerateContent(
ctx,
genai.Text(
fmt.Sprintf("Give me 10 fun facts about %s. Return the results as HTML without markdown backticks.", animal)),
)
if err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
if len(resp.Candidates) > 0 && len(resp.Candidates[0].Content.Parts) > 0 {
htmlContent := resp.Candidates[0].Content.Parts[0]
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprint(w, htmlContent)
}
} else {
// Display the form
form := AnimalForm{}
err := tmpl.Execute(w, form)
if err != nil {
panic(err)
}
}
})
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
http.ListenAndServe(":"+port, nil)
}