61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/google/generative-ai-go/genai"
|
|
"google.golang.org/api/option"
|
|
)
|
|
|
|
func main() {
|
|
ctx := context.Background()
|
|
|
|
apiKey := os.Getenv("GEMINI_API_KEY")
|
|
if apiKey == "" {
|
|
log.Fatal("GEMINI_API_KEY environment variable not set")
|
|
}
|
|
|
|
client, err := genai.NewClient(ctx, option.WithAPIKey(apiKey))
|
|
if err != nil {
|
|
log.Fatalf("Failed to create genai client: %v", err)
|
|
}
|
|
defer client.Close()
|
|
|
|
model := client.GenerativeModel("gemini-2.5-pro-preview-03-25")
|
|
|
|
// Set system instruction
|
|
model.SystemInstruction = &genai.Content{
|
|
Parts: []genai.Part{
|
|
genai.Text("This is for working with the GO language files at http://go.wit.com/"),
|
|
},
|
|
}
|
|
|
|
// Create user content
|
|
userContent := &genai.Content{
|
|
Parts: []genai.Part{
|
|
genai.Text("what is the current version of forge on the webpage?"),
|
|
},
|
|
}
|
|
|
|
// Stream content generation
|
|
iter := model.GenerateContentStream(ctx, []*genai.Content{userContent}...)
|
|
for {
|
|
resp, err := iter.Next()
|
|
if err != nil {
|
|
break
|
|
}
|
|
for _, part := range resp.Candidates {
|
|
if part.Content != nil {
|
|
for _, p := range part.Content.Parts {
|
|
if text, ok := p.(genai.Text); ok {
|
|
fmt.Print(string(text))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|