https://github.com/meinside/gemini-things-go
A Golang library for generating things with Gemini APIs
https://github.com/meinside/gemini-things-go
gemini-api golang google
Last synced: about 1 month ago
JSON representation
A Golang library for generating things with Gemini APIs
- Host: GitHub
- URL: https://github.com/meinside/gemini-things-go
- Owner: meinside
- License: other
- Created: 2024-09-13T01:27:43.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2026-05-11T08:18:07.000Z (about 1 month ago)
- Last Synced: 2026-05-11T10:29:00.427Z (about 1 month ago)
- Topics: gemini-api, golang, google
- Language: Go
- Homepage:
- Size: 473 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# gemini-things-go
A helper library for generating things with [Gemini API](https://github.com/googleapis/go-genai).
[](https://pkg.go.dev/github.com/meinside/gemini-things-go)
## Usage
See the code examples in the [generation_test.go](https://github.com/meinside/gemini-things-go/blob/master/generation_test.go) file.
```go
package main
import (
"context"
"fmt"
"time"
gt "github.com/meinside/gemini-things-go"
)
const (
apiKey = `AIabcdefghijklmnopqrstuvwxyz_ABCDEFG-00000000-00` // Your API key here
model = "gemini-2.5-flash"
contentsBuildupTimeoutSeconds = 60
generationTimeoutSeconds = 30
)
func main() {
if client, err := gt.NewClient(
apiKey,
gt.WithModel(model), // Specify the model
gt.WithMaxRetryCount(5), // Configure a maximum of 5 retries on 5xx server errors
); err == nil {
ctxContents, cancelContents := context.WithTimeout(context.TODO(), contentsBuildupTimeoutSeconds*time.Second)
defer cancelContents()
// convert prompts and histories to contents for generation
if contents, err := client.PromptsToContents(
ctxContents,
[]gt.Prompt{
gt.PromptFromText(`What is the answer to life, the universe, and everything?`),
},
nil,
); err == nil {
ctxGenerate, cancelGenerate := context.WithTimeout(context.TODO(), generationTimeoutSeconds*time.Second)
defer cancelGenerate()
// generate with contents
if res, err := client.Generate(
ctxGenerate,
contents,
); err == nil {
// do something with the generated response
fmt.Printf("response: %+v\n", res)
} else {
panic(fmt.Errorf("failed to generate: %w", err))
}
} else {
panic(fmt.Errorf("failed to convert prompts: %w", err))
}
} else {
panic(fmt.Errorf("failed to create client: %w", err))
}
}
```
## Test
Test with Gemini API key:
```bash
$ GEMINI_API_KEY="AIabcdefghijklmnopqrstuvwxyz_ABCDEFG-00000000-00" go test -timeout 0
# for verbose output:
$ GEMINI_API_KEY="AIabcdefghijklmnopqrstuvwxyz_ABCDEFG-00000000-00" VERBOSE=true go test -timeout 0
# for testing free APIs only
$ GEMINI_API_KEY="AIabcdefghijklmnopqrstuvwxyz_ABCDEFG-00000000-00" go test -run="Free"
# for testing paid APIs only
$ GEMINI_API_KEY="AIabcdefghijklmnopqrstuvwxyz_ABCDEFG-00000000-00" go test -run="Paid"
```
or with Google Cloud credential file (for Vertex AI):
```bash
$ CREDENTIALS_FILEPATH="/path/to/credentials.json" go test -timeout 0
```