original issue: streaming requests with tools may return a 500 error under certain prompts · Issue #118 · google/generative-ai-go · GitHub .
package main
import (
"context"
"fmt"
"log"
"github.com/google/generative-ai-go/genai"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey("your-api-key"))
if err != nil {
log.Fatal(err)
}
defer client.Close()
schema := &genai.Schema{
Type: genai.TypeObject,
Properties: map[string]*genai.Schema{
"location": {
Type: genai.TypeString,
Description: "The city and state, e.g. San Francisco, CA",
},
"unit": {
Type: genai.TypeString,
Enum: []string{"celsius", "fahrenheit"},
},
},
Required: []string{"location"},
}
weatherTool := &genai.Tool{
FunctionDeclarations: []*genai.FunctionDeclaration{{
Name: "CurrentWeather",
Description: "Get the current weather in a given location",
Parameters: schema,
}},
}
model := client.GenerativeModel("gemini-1.5-flash-latest")
model.Tools = []*genai.Tool{weatherTool}
session := model.StartChat()
iter := session.SendMessageStream(ctx, genai.Text("write a python function that takes a list of integers and returns the sum of the list")) // googleapi: Error 500:
//iter := session.SendMessageStream(ctx, genai.Text("hello")) // ok
if err != nil {
log.Fatal(err)
}
for {
res, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatal(err)
}
printResponse(res)
}
}
func printResponse(resp *genai.GenerateContentResponse) {
for _, cand := range resp.Candidates {
if cand.Content != nil {
for _, part := range cand.Content.Parts {
fmt.Println(part)
}
}
}
fmt.Println("---")
}
when use iter := session.SendMessageStream(ctx, genai.Text("write a python function that takes a list of integers and returns the sum of the list"))
:
2024/05/15 15:09:45 googleapi: Error 500:
exit status 1
if use iter := session.SendMessageStream(ctx, genai.Text("hello"))
:
Hello
---
! How can I help you today? 😊
---