Getting started with Google Gemini with Go
Categories: programming
Tags: golang google-gemini ai
I am curious what it would take to build an AI chat agent in Go. Google’s Gemini is currently free and has a Go API binding. I looked at OpenLlama however I need to experiment with the hardware I have to see if I can get a performant model for it.
I am envisioning a chat bot who can pitch my virtues to potential employers on my resume site. I quickly sketched a WebSocket solution using github.com/gorilla/websocket for the conversation. Now onto the real challenge!
I frankensteined the example at github.com/gorilla/websocket/tree/main/examples/chat to hijack the chat request-response cycle.
I grabbed a Google API key from Google AI Studio.
Integration of the agent
My first instinct is to pull this into a separate struct
:
type agent struct {
client *genai.Client
}
func (a *agent) query(ctx context.Context, input string) (string, error) {
model := a.client.GenerativeModel("gemini-pro")
resp, err := model.GenerateContent(ctx, genai.Text(input))
if err != nil {
return "", err
}
if len(resp.Candidates) == 0 {
return "I am sorry, I did not understand", nil
}
p := resp.Candidates[0].Content.Parts[0]
if txt, ok := p.(genai.Text); ok {
return string(txt), nil
}
return "Returned another type", nil
}
Definitely alpha quality, making a lot of assumptions with zero context. Testing shows a total round trip time around
4-7 seconds for most queries. I was hoping for something sub 2 second but beggars can not be choosers. To accommodate
the slow processing time I double writeWait
to 20 seconds and resolve the occasional timeout.
Context Matters
With a connection to the AI unit, I now need context. I loaded up a bunch of info into a data.txt
file containing
my resume and some commonly asked questions. Normally I would place this in a system
role for the AI. Google’s 1.0
model does not support a system
role, and they are about to charge for 1.5 which does.
In order to give gemini-pro
instructions the data.txt
Part
looked like the following:
You are the best recruiter in the world representing Mark Eschbach to potential clients. Given the resume of Mark,
answer the question below as professionally as possible.
Resume:
<data>
Question:
This provided contextually aware results. It was a fun experiment for an hour to get it up and running. If anyone uses it then I will return to the problem.
Building expert AI systems in Prolog have the same problem as these units: they want closed universes to reason within. Google does offer various forms of pull based context however I am not sure the pricing model on this right now.
TLDR
Checkout the example at github.com/meschbach/xp-resume-agent. Particularly the agent.go file for interacting with the Gemini.