@@ -17,27 +17,45 @@ package main
17
17
import (
18
18
"context"
19
19
"errors"
20
+ "fmt"
20
21
"log"
22
+ "os"
23
+ "os/signal"
24
+ "syscall"
21
25
22
26
"github.com/firebase/genkit/go/ai"
23
27
"github.com/firebase/genkit/go/genkit"
24
28
"github.com/firebase/genkit/go/plugins/googlegenai"
25
29
)
26
30
27
31
func main () {
28
- ctx := context .Background ()
32
+ ctx , cancel := context .WithCancel (context .Background ())
33
+ defer cancel ()
34
+
35
+ // Handle SIGINT (Ctrl+C) for graceful shutdown
36
+ sigCh := make (chan os.Signal , 1 )
37
+ signal .Notify (sigCh , os .Interrupt , syscall .SIGTERM )
38
+ go func () {
39
+ <- sigCh
40
+ log .Println ("Received interrupt, shutting down..." )
41
+ cancel ()
42
+ }()
29
43
30
44
// Initialize Genkit with the Google AI plugin. When you pass nil for the
31
45
// Config parameter, the Google AI plugin will get the API key from the
32
46
// GEMINI_API_KEY or GOOGLE_API_KEY environment variable, which is the recommended
33
47
// practice.
34
- g , err := genkit .Init (ctx , genkit .WithPlugins (& googlegenai.GoogleAI {}))
48
+ // Initialize Genkit with the Google AI plugin using an invalid API key
49
+ g , err := genkit .Init (ctx , genkit .WithPlugins (& googlegenai.GoogleAI {
50
+ APIKey : "invalid-key-12345" , // Explicitly set invalid key
51
+ }))
35
52
if err != nil {
36
- log .Fatal ( err )
53
+ log .Fatalf ( "Initialization failed: %v" , err )
37
54
}
55
+ log .Println ("Genkit initialized successfully" )
38
56
39
57
// Define a simple flow that generates jokes about a given topic
40
- genkit .DefineFlow (g , "jokesFlow" , func (ctx context.Context , input string ) (string , error ) {
58
+ jokesFlow := genkit .DefineFlow (g , "jokesFlow" , func (ctx context.Context , input string ) (string , error ) {
41
59
m := googlegenai .GoogleAIModel (g , "gemini-2.5-pro-preview-03-25" )
42
60
if m == nil {
43
61
return "" , errors .New ("jokesFlow: failed to find model" )
@@ -50,12 +68,20 @@ func main() {
50
68
}),
51
69
ai .WithPrompt (`Tell silly short jokes about %s` , input ))
52
70
if err != nil {
53
- return "" , err
71
+ // Log detailed error
72
+ log .Printf ("Generate error: %v" , err )
73
+ return "" , fmt .Errorf ("failed to generate: %w" , err )
54
74
}
55
75
56
76
text := resp .Text ()
57
77
return text , nil
58
78
})
59
79
60
- <- ctx .Done ()
80
+ // Execute the flow to trigger the 4xx error
81
+ log .Println ("Running jokesFlow with input 'cats'" )
82
+ result , err := jokesFlow .Run (ctx , "cats" )
83
+ if err != nil {
84
+ log .Fatalf ("Flow failed: %v" , err )
85
+ }
86
+ log .Printf ("Flow result: %s" , result )
61
87
}
0 commit comments