google / generative-ai-go

Go SDK for Google Generative AI
Apache License 2.0
582 stars 58 forks source link

Difference between cs.SendMessage() and cs.SendMessageStream() #61

Closed art-korn-39 closed 8 months ago

art-korn-39 commented 8 months ago

Hello I noticed that the output results in the two methods are different. In this case, the normal method returns the full result, and the stream method performs 2-3 iterations and fails with an error (when resp.Candidates[0].Content = nil).

Example of initialization:

`gen_ctx = context.Background()
gen_client, _ = genai.NewClient(gen_ctx, option.WithAPIKey(Cfg.GeminiKey))
gen_TextModel = gen_client.GenerativeModel("gemini-1.0-pro")

text := "напиши первые 3000 сиволов из библии"

// 1 type
cs := gen_TextModel.StartChat()
resp, err := cs.SendMessage(gen_ctx, genai.Text(text))

// 2 type
cs := gen_TextModel.StartChat()
iter := cs.SendMessageStream(gen_ctx, genai.Text(text))`

Please tell me, is this normal behavior or can I somehow fix it so that the capabilities of the streaming method are not inferior to the usual one?

eliben commented 8 months ago

Thanks for the report, but we need more information. Can you please create a minimal reproducible example of the bad path you're observing? Is it something you see consistently for all queries, or just a specific one?

art-korn-39 commented 8 months ago

main.txt

I have prepared an example. I used only one request text. The main thing is that the stream method always receives "resp.Candidates[0].Content = nil" in the last iteration, but if you pause for 1 second inside the loop, the error disappears. At shorter pause intervals it remains. Please check this moment.

jba commented 8 months ago

The underlying problem is that something in your prompt triggered the model to generate some sort of protected (copyrighted) material, and that caused it to stop. Technically, the model returned a new FinishReason that we hadn't accounted for. The fix will prevent the panic, but your prompt will still result in an error.

jba commented 8 months ago

I should add that I couldn't reproduce the change in behavior due to sleeping. I always got the same result.