mscraftsman / generative-ai

Gemini AI SDK for .NET and ASP.NET Core enables developers to use Google's state-of-the-art generative AI models to build AI-powered features and applications.
https://mscraftsman.github.io/generative-ai/
Apache License 2.0
55 stars 11 forks source link

Check for more FinishReason #12

Closed doggy8088 closed 7 months ago

doggy8088 commented 7 months ago

In the current version, the code only check for FinishReason.Safety.

Due to docs said, I think there are others needs to be mentioned.

There are only STOP can be treated as "NORMAL" response.

jochenkirstaetter commented 7 months ago

Thanks! New extension method CheckResponse has been implemented already and is currently used in ChatSession.SendMessage[Stream] methods. Going to extend the use to more methods soon. Need to cross-check with the Python sources for appropriate locations.

        public static void CheckResponse(this GenerateContentResponse response, bool stream = false)
        {
            if (response.PromptFeedback.BlockReason is not BlockedReason.BlockedReasonUnspecified)
            {
                throw new BlockedPromptException(response.PromptFeedback);
            }

            if (!stream)
            {
                if (response.Candidates[0].FinishReason is
                    FinishReason.Safety or
                    FinishReason.Recitation or
                    FinishReason.Other)
                {
                    throw new StopCandidateException(response.Candidates[0]);
                }
            }
        }

Apart from STOP there is also UNSPECIFIED and MAX_TOKENS as acceptable reason that the response has been concluded. That's according to the Python sources.

doggy8088 commented 7 months ago

Apart from STOP there is also UNSPECIFIED and MAX_TOKENS as acceptable reason that the response has been concluded. That's according to the Python sources.

Can you share the Python source link? I want to check it as well. Thanks.

jochenkirstaetter commented 7 months ago

Hello @doggy8088

Yes, sure. See here: https://github.com/google/generative-ai-python/blob/ae8f7ccaee2629152072ff1302da912eba458daf/google/generativeai/generative_models.py#L468-L478

jochenkirstaetter commented 7 months ago

Hi @doggy8088

This has been improved in v1.1.2.