summaryrefslogtreecommitdiff
path: root/apiExampleCode.go
blob: d444f4ed2d892ec8c185ea9c6ff203820371b551 (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
package main

// this is just example code the GO API's wrapper for handling statelessness
// it doesn't really compile and is just junk Gemini AI sent back but I saved it here anyway

/*
func statelessnessExample() {
	ctx := context.Background()
	// Get the API key from an environment variable
	apiKey := os.Getenv("GEMINI_API_KEY")
	if apiKey == "" {
		log.Fatal("GEMINI_API_KEY environment variable not set")
	}

	// Create a new client
	client, err := genai.NewClient(ctx, option.WithAPIKey(apiKey))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	// Choose the model
	model := client.GenerativeModel("gemini-1.5-flash")

	// ---- Start a new chat session ----
	cs := model.StartChat()
	cs.History = []*genai.Content{} // Start with a clean history

	// --- First message ---
	fmt.Println("User: My brother's name is Paul.")
	resp, err := cs.SendMessage(ctx, genai.Text("My brother's name is Paul."))
	if err != nil {
		log.Fatal(err)
	}
	printResponse(resp)

	// --- Second message ---
	// The ChatSession now remembers the previous exchange.
	fmt.Println("\nUser: What is my brother's name?")
	resp, err = cs.SendMessage(ctx, genai.Text("What is my brother's name?"))
	if err != nil {
		log.Fatal(err)
	}
	printResponse(resp)

	// You can inspect the history at any time
	// fmt.Println("\n--- Full Chat History ---")
	// for _, content := range cs.History {
	//     for _, part := range content.Parts {
	//         fmt.Printf("Role: %s, Text: %v\n", content.Role, part)
	//     }
	// }
}

// Helper function to print the response
func printResponse(resp *genai.GenerateContentResponse) {
	for _, cand := range resp.Candidates {
		if cand.Content != nil {
			for _, part := range cand.Content.Parts {
				fmt.Printf("Gemini: %v\n", part)
			}
		}
	}
}
*/