regex/apiExampleCode.go

66 lines
1.7 KiB
Go

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)
}
}
}
}
*/