simpleflash

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2024 License: Apache-2.0 Imports: 12 Imported by: 1

README

SimpleFlash

A simple way to use Gemini and the gemini-1.5-flash model (or other Gemini-models).

Example use:

package main

import (
    "fmt"
    "os"
    "time"

    "github.com/xyproto/env/v2"
    "github.com/xyproto/simpleflash"
)

func main() {
    const (
        textModel       = "gemini-1.5-flash-001"
        multiModalModel = "gemini-1.0-pro-vision-001"
    )

    var (
        projectLocation = env.Str("PROJECT_LOCATION", "europe-west4") // europe-west4 is just the default
        projectID       = env.Str("PROJECT_ID")
    )

    if projectID == "" {
        fmt.Fprintln(os.Stderr, "Error: PROJECT_ID environment variable is not set.")
        return
    }

    sf, err := simpleflash.New(textModel, multiModalModel, projectLocation, projectID, true)
    if err != nil {
        fmt.Fprintln(os.Stderr, "Error:", err)
        return
    }

    sf.Timeout = 10 * time.Second

    const prompt = "Write a haiku about the color of cows."

    // Query Gemini with the prompt, nothing multimodal
    output, err := sf.QueryGemini(prompt, nil, nil, nil)
    if err != nil {
        fmt.Fprintln(os.Stderr, "Error:", err)
        return
    }

    fmt.Println(output)
}

Building and running the example (in cmd/simple):

cd cmd/simple

# Use your own Google Cloud Project ID here, and also log in with `gcloud auth application-default login` **or** deploy and run it within Google Cloud:
export PROJECT_ID=44444444444

# Build the executable
go build

# Run the program and observe the output
./simple
Black and white patches,
Sun-kissed brown, a gentle breeze,
Grazing in the field.
General info
  • Version: 1.0.1
  • License: Apache 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SimpleFlash

type SimpleFlash struct {
	ModelName           string
	MultiModalModelName string
	ProjectLocation     string
	ProjectID           string
	Client              *genai.Client
	Cache               *bigcache.BigCache
	Timeout             time.Duration
}

func New

func New(modelName, multiModalModelName, projectLocation, projectID string, cache bool) (*SimpleFlash, error)

func (*SimpleFlash) CountTextTokens

func (sf *SimpleFlash) CountTextTokens(prompt string) (int, error)

CountTextTokens tries to count the number of tokens in the given prompt, using the VertexAI API

func (*SimpleFlash) InitCache

func (sf *SimpleFlash) InitCache() error

InitCache initializes the BigCache cache

func (*SimpleFlash) QueryGemini

func (sf *SimpleFlash) QueryGemini(prompt string, temperature *float64, base64Data, dataMimeType *string) (string, error)

QueryGemini processes a prompt with optional temperature, base64-encoded data, and MIME type for the data.

Directories

Path Synopsis
cmd
simple command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL