Hi! I just tried to implement Gemini API for my Golang Gin project, I just following the instruction on this page:
But, when I try to run my apps, it return the unknown field “usageMetadata” error when it try to do the “cs.SendMessage()”.
Btw this is how I implement my code:
func (srv *GeminiService) Chat(prompt *request.GeminiText) (error, []string) {
ctx := context.TODO()
client, err := genai.NewClient(ctx, option.WithAPIKey(config.EnvGeminiApiKey()))
if err != nil {
return err, nil
}
defer func(client *genai.Client) {
err := client.Close()
if err != nil {
log.Fatalln(err)
}
}(client)
model := client.GenerativeModel("gemini-pro")
cs := model.StartChat()
resp, err := cs.SendMessage(ctx, genai.Text(prompt.Text))
if err != nil {
return err, nil
}
var results []string
for _, candidate := range resp.Candidates {
if candidate.Content != nil {
for _, part := range candidate.Content.Parts {
if text, ok := part.(genai.Text); ok {
results = append(results, string(text))
}
}
}
}
return nil, results
}
Am I do anything wrong?
*screenshot when I comment the error handling and let the program to print the error: