-
Notifications
You must be signed in to change notification settings - Fork 0
/
gemini.go
52 lines (42 loc) · 1.13 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
// Package gemini provides communication with Gemini.
package gemini
import (
"context"
)
// Question is to send a message or image.
//
// ex)
func Question(apiKey, text string, images []string) (string, error) {
ctx := context.Background()
client, err := getClient(ctx, apiKey)
if err != nil {
return "", err
}
defer client.Close()
model := getModel(client, images)
if parts, err := makeParts(text, images); err != nil {
return "", err
} else if response, err := model.GenerateContent(ctx, parts...); err != nil {
return "", err
} else {
return responseToAnswer(response), nil
}
}
// QuestionStream is to send a message or image and receive it as a stream.
//
// ex)
func QuestionStream(apiKey, text string, images []string) (chan answerForStream, error) {
ctx := context.Background()
client, err := getClient(ctx, apiKey)
if err != nil {
return nil, err
}
defer client.Close()
model := getModel(client, images)
if parts, err := makeParts(text, images); err != nil {
return nil, err
} else {
responseIterator := model.GenerateContentStream(ctx, parts...)
return getStreamChannel(responseIterator), nil
}
}