Gemini Example with Go

English 简体中文 Tiếng Việt
Summary

To interact with Google's Gemini LLM using Go, developers can leverage the official Go SDK. The process involves initializing a genai.Client with an API key, then selecting a specific generative model such as "gemini-1.5-flash-latest". After optionally setting parameters like Temperature to control output randomness, content generation is initiated by calling model.GenerateContent with the desired text prompt. The response, containing potential outputs in resp.Candidates, can then be iterated and cast to genai.Text to retrieve the model's generated content. This workflow facilitates basic chat functionalities, like translation, with the Gemini model.

To connect and use Gemini with Go, Google's LLM, one can use their official Go SDK for doing this. In this post, we will just show a simple chat example to demonstrate how to make it work with Go.

The example is just to ask the model to translate some English to Chinese and get its output. The code actually looks like:

var client *genai.Client
// geminiOnce.Do(func() {
client, err = genai.NewClient(ctx, option.WithAPIKey(string(apiKey)))
if err != nil {
	log.Fatal(err)
}

model := client.GenerativeModel("gemini-1.5-flash-latest")
model.SetTemperature(0.1)
resp, err := model.GenerateContent(ctx, genai.Text("Translate 'Hello world' to Chinese"))
if err != nil {
	log.Printf("Error generating content: %v", err)
	return "", err
}

for _, candidate := range resp.Candidates {
	if candidate != nil {
		if candidate.Content.Parts != nil {
		     log.Printf("Output: %s", string(candidate.Content.Parts[0].(genai.Text)))
		}
	}
}

Here’s a step-by-step explanation of the provided code snippet:

1. Declare a Client Variable

var client *genai.Client
  • A pointer variable client of type *genai.Client is declared.
  • This variable will hold the initialized client instance for interacting with the genai service.

2. Initialize the Client

client, err = genai.NewClient(ctx, option.WithAPIKey(string(apiKey)))
if err != nil {
	log.Fatal(err)
}
  • genai.NewClient(ctx, option.WithAPIKey(string(apiKey))):
    • Creates a new client instance using the genai library.
    • ctx: A context.Context is passed, enabling request cancellation or timeout control.
    • option.WithAPIKey(string(apiKey)): Specifies the API key for authenticating the client. The API key can be generated on Google AI Studio by creating a new project.
  • Error Handling:
    • If err is not nil, the program logs the error and exits using log.Fatal(err).

3. Choose a Generative Model

model := client.GenerativeModel("gemini-1.5-flash-latest")
  • The GenerativeModel method of the client selects a specific model version to work with.
  • Here, the model is "gemini-1.5-flash-latest", which presumably is a generative AI model. There are other models available where 2.0 is the latest one in experiment phase.

4. Set Model Parameters

model.SetTemperature(0.1)
  • Sets the temperature of the model to 0.1.
  • Temperature controls the randomness of the output.
  • A lower temperature (e.g., 0.1) produces more deterministic results, while a higher temperature introduces more variation.

There are other parameters can be set as well by calling model.SetXXX(), such as output token length, 

5. Generate Content

resp, err := model.GenerateContent(ctx, genai.Text("Translate 'Hello world' to Chinese"))
if err != nil {
	log.Printf("Error generating content: %v", err)
	return "", err
}
  • GenerateContent(ctx, genai.Text(...)):
    • Sends a generation request to the model with the input "Translate 'Hello world' to Chinese".
    • The input is wrapped in genai.Text. A kind of general text handling, it also supports other types like Blob(Image), FunctionCall,

6. Process the Response

for _, candidate := range resp.Candidates {
	if candidate != nil {
		if candidate.Content.Parts != nil {
		     log.Printf("Output: %s", string(candidate.Content.Parts[0].(genai.Text)))
		}
	}
}
  • Iterate Through Candidates:
    • resp.Candidates contains possible outputs from the model. The number of candidates can be set at step 4 with model.SetCandidateCount(). The default is 1 and currently it only supports 1 as well.
    • The loop processes each candidate in the response.
  • Since the context was a text chat, hence here the output is also kind of genai.Text. We need to convert the Part to a genai.Text type as originally it is an interface.

With above, you should be able to dive into the vast other features of the SDK and the Gemini model offers.

EXAMPLE GO TRANSLATION GOLANG GEMINI

  RELATED

  COMMENTS

2
Anonymous
Dec 23, 2024 at 5:14 am

Where the ctx comming from? (cwl)

Ke Pi
Dec 23, 2024 at 6:09 am

you can just create one or pass from your request if you have one already.

To create one, can just do:

ctx := context.Background()