-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
157 lines (135 loc) · 3.49 KB
/
model.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"strings"
"github.com/google/generative-ai-go/genai"
"github.com/joho/godotenv"
"github.com/manifoldco/promptui"
"google.golang.org/api/option"
)
func printResponse(resp *genai.GenerateContentResponse) {
for _, cand := range resp.Candidates {
if cand.Content != nil {
for _, part := range cand.Content.Parts {
fmt.Println(part)
}
}
}
fmt.Println("---")
}
type Prompt struct {
Command string `json:"command"`
Description string `json:"description"`
}
func convertRespToPrompts(resp *genai.GenerateContentResponse) []Prompt {
var prompts []Prompt
for _, cand := range resp.Candidates {
if cand.Content != nil {
for _, part := range cand.Content.Parts {
switch v := part.(type) {
case genai.Text:
// check if text start with "{"
// if yes, try to unmarshal it as JSON object
if strings.HasPrefix(string(v), "{") {
// if yes, try to unmarshal it as JSON
var prompt Prompt
err := json.Unmarshal([]byte(v), &prompt)
if err != nil {
log.Fatal(err)
}
prompts = append(prompts, prompt)
} else if strings.HasPrefix(string(v), "[") {
// try to unmarshal it as JSON array
err := json.Unmarshal([]byte(v), &prompts)
if err != nil {
log.Fatal(err)
}
}
default:
fmt.Println("Unknown part type:", v)
}
}
}
}
return prompts
}
func generateContent(prompt string) {
ctx := context.Background()
// load .env file
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file")
}
// Access your API key as an environment variable
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
model := client.GenerativeModel("gemini-1.5-flash")
resp, err := model.GenerateContent(ctx, genai.Text(prompt))
if err != nil {
log.Fatal(err)
}
printResponse(resp)
}
func howto(prompt string) {
ctx := context.Background()
// load .env file
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file")
}
// Access your API key as an environment variable
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
model := client.GenerativeModel("gemini-1.5-flash")
// Ask the model to respond with JSON.
model.ResponseMIMEType = "application/json"
finalPrompt := fmt.Sprintf(`%s. Respond using this JSON schema:
Prompt = {'command': string, description: string}
Return: Array<Prompt>`, prompt)
resp, err := model.GenerateContent(ctx, genai.Text(finalPrompt))
if err != nil {
log.Fatal(err)
}
prompts := convertRespToPrompts(resp)
fmt.Printf("──────────────── %s ────────────────\n", "Command")
p := prompts[0]
fmt.Println(p.Command)
fmt.Printf("\n")
selectAction := promptui.Select{
Label: "Select Action",
Items: []string{"✅ Run this command", "❌ Cancel"},
}
actionIdx, _, err := selectAction.Run()
if err != nil {
fmt.Printf("Prompt cancelled %v\n", err)
return
}
switch actionIdx {
case 0:
// Run the command
fmt.Println("Running command...")
cmd := exec.Command("/bin/bash", "-c", p.Command)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
fmt.Println("Error running command:", err)
}
break
case 1:
// Cancel
fmt.Println("Cancelled")
break
}
}