-
Notifications
You must be signed in to change notification settings - Fork 9
/
gemini.go
199 lines (177 loc) · 5.04 KB
/
gemini.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"github.com/google/generative-ai-go/genai"
"google.golang.org/api/option"
)
func GeminiImage(imgData []byte, prompt string) (string, error) {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(geminiKey))
if err != nil {
log.Fatal(err)
}
defer client.Close()
model := client.GenerativeModel("gemini-pro-vision")
value := float32(0.8)
model.Temperature = &value
data := []genai.Part{
genai.ImageData("png", imgData),
genai.Text(prompt),
}
log.Println("Begin processing image...")
resp, err := model.GenerateContent(ctx, data...)
log.Println("Finished processing image...", resp)
if err != nil {
log.Fatal(err)
return "", err
}
return printResponse(resp), nil
}
// Gemini Chat Complete: Iput a prompt and get the response string.
func GeminiChatComplete(req string) string {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(geminiKey))
if err != nil {
log.Fatal(err)
}
defer client.Close()
model := client.GenerativeModel("gemini-pro")
value := float32(0.8)
model.Temperature = &value
cs := model.StartChat()
send := func(msg string) *genai.GenerateContentResponse {
fmt.Printf("== Me: %s\n== Model:\n", msg)
res, err := cs.SendMessage(ctx, genai.Text(msg))
if err != nil {
log.Fatal(err)
}
return res
}
res := send(req)
return printResponse(res)
}
func printResponse(resp *genai.GenerateContentResponse) string {
var ret string
for _, cand := range resp.Candidates {
for _, part := range cand.Content.Parts {
ret = ret + fmt.Sprintf("%v", part)
fmt.Println(part)
}
}
return ret
}
const api_url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent"
type GenerateContentRequest struct {
Contents struct {
Role string `json:"role"`
Parts struct {
Text string `json:"text"`
} `json:"parts"`
} `json:"contents"`
Tools []struct {
FunctionDeclarations []struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters struct {
Type string `json:"type"`
Properties struct {
Location struct {
Type string `json:"type"`
Description string `json:"description"`
} `json:"location"`
Description struct {
Type string `json:"type"`
Description string `json:"description"`
} `json:"description"`
Movie struct {
Type string `json:"type"`
Description string `json:"description"`
} `json:"movie"`
Theater struct {
Type string `json:"type"`
Description string `json:"description"`
} `json:"theater"`
Date struct {
Type string `json:"type"`
Description string `json:"description"`
} `json:"date"`
} `json:"properties"`
Required []string `json:"required"`
} `json:"parameters"`
} `json:"function_declarations"`
} `json:"tools"`
}
func newGenerateContentRequest(text string) GenerateContentRequest {
request := GenerateContentRequest{}
request.Contents.Role = "user"
request.Contents.Parts.Text = text
// Add any specific function declarations or configurations here
return request
}
func generateContent(contentRequest GenerateContentRequest) error {
jsonData, err := json.Marshal(contentRequest)
if err != nil {
return fmt.Errorf("error marshalling request data: %w", err)
}
req, err := http.NewRequest("POST", api_url, bytes.NewBuffer(jsonData))
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}
query := req.URL.Query()
query.Add("key", geminiKey)
req.URL.RawQuery = query.Encode()
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error making request: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error reading response body: %w", err)
}
fmt.Println("Response status:", resp.Status)
fmt.Println("Response body:", string(body))
return nil
}
type ResponseData []struct {
Candidates []struct {
Content struct {
Role string `json:"role"`
Parts []struct {
FunctionCall struct {
Name string `json:"name"`
Args struct {
Movie interface{} `json:"movie"`
Location string `json:"location"`
} `json:"args"`
} `json:"functionCall"`
} `json:"parts"`
} `json:"content"`
FinishReason string `json:"finishReason"`
SafetyRatings []struct {
Category string `json:"category"`
Probability string `json:"probability"`
} `json:"safetyRatings"`
} `json:"candidates"`
UsageMetadata struct {
PromptTokenCount int `json:"promptTokenCount"`
TotalTokenCount int `json:"totalTokenCount"`
} `json:"usageMetadata"`
}
func processResponseData(jsonData []byte) (ResponseData, error) {
var responseData ResponseData
err := json.Unmarshal(jsonData, &responseData)
if err != nil {
return nil, fmt.Errorf("error unmarshalling response data: %w", err)
}
// Return the parsed response data
return responseData, nil
}