summaryrefslogtreecommitdiff
path: root/doConnect.go
blob: 8eb2674cd6cae6384f3bc4000379cbc0c392e9f3 (plain)
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
package main

import (
	"context"
	"fmt"
	"os"

	"go.wit.com/log"
	"google.golang.org/genai"
)

// doConnect initializes the Gemini client and handles the request flow.
func doConnect() error {
	apiKey := os.Getenv("GEMINI_API_KEY")
	if apiKey == "" {
		return log.Errorf("GEMINI_API_KEY environment variable not set")
	}

	me.ctx = context.Background()
	var err error
	me.client, err = genai.NewClient(me.ctx, &genai.ClientConfig{APIKey: apiKey})
	if err != nil {
		return log.Errorf("failed to create new genai client: %w", err)
	}

	return nil
}

// sampleHello sends a hardcoded prompt to the model and prints the response.
func simpleHello() error {
	log.Info("Sending 'hello, how are you' to the Gemini API...")

	// Create the parts slice
	parts := []*genai.Part{
		{Text: "hello, how are you"},
	}

	content := []*genai.Content{{Parts: parts}}

	resp, err := me.client.Models.GenerateContent(me.ctx, "gemini-2.5-flash", content, nil)
	if err != nil {
		return log.Errorf("error sending message: %v", err)
	}

	log.Info("Response from API:")
	for _, cand := range resp.Candidates {
		if cand.Content != nil {
			for _, part := range cand.Content.Parts {
				fmt.Println(part)
			}
		}
	}
	return nil
}

func submitChat(content []*genai.Content) error {
	resp, err := me.client.Models.GenerateContent(me.ctx, "gemini-2.5-flash", content, nil)
	if err != nil {
		return log.Errorf("error sending message: %v", err)
	}

	log.Info("Response from API:")
	for _, cand := range resp.Candidates {
		if cand.Content != nil {
			for _, part := range cand.Content.Parts {
				fmt.Println(part)
			}
		}
	}
	return nil
}