google-gemini-php / laravel

⚡️ Gemini PHP for Laravel is a community-maintained PHP API client that allows you to interact with the Gemini AI API.
MIT License
253 stars 29 forks source link

Undefined array key "safetyRatings" #7

Closed koossaayy closed 2 months ago

koossaayy commented 3 months ago

Hello, Running the following code

    $chat = Gemini::chat()
        ->startChat(history: [
            Content::parse(part: "You have the following transcript.  Hello, guys. Today,....", role: Role::USER),
            Content::parse(part: 'I understand', role: Role::MODEL),
            Content::parse(part: "Summarize it", role: Role::USER),
            Content::parse(part: 'In this video, the speaker discusses three ways to return JSON from API controllers or routes in Laravel: 1. **Directly returning an Eloquent collection or model:** Laravel will automatically convert this to JSON. 2.....', role: Role::MODEL),
            Content::parse(part: "What is the best way to send api data ? according to the transcript", role: Role::USER),
            Content::parse(part: 'According to the transcript, the best way to send API data is to use Eloquent API resources....', role: Role::MODEL),
            Content::parse(part: 'Thank You', role: Role::USER),
            Content::parse(part: 'You\'re welcome! I\'m glad I could be of assistance.', role: Role::MODEL),

        ]);

    $response = $chat->sendMessage('Show me an example for that ');
    echo $response->text();

Will return the mentioned error. Here is the full error stack : https://flareapp.io/share/xmN9W6p5

Update:
Commenting the last entries in the history, won't trigger that error. I guess that's something I am doing on my end, with the prompts. I will try more, and see.

    $chat = Gemini::chat()
        ->startChat(history: [
            Content::parse(part: "You have the following transcript.  Hello, guys....", role: Role::USER),
            Content::parse(part: 'I understand', role: Role::MODEL),
            Content::parse(part: "Summarize it", role: Role::USER),
            Content::parse(part: 'In this video, the speaker discusses three ways to return JSON from API controllers or routes in Laravel: 1. **Directly returning an Eloquent collection or model:*....', role: Role::MODEL),
            Content::parse(part: "What is the best way to send api data ? according to the transcript", role: Role::USER),
            Content::parse(part: 'According to the transcript, the best way to send API data is to use Eloquent API resources. ....', role: Role::MODEL),
            // Content::parse(part: 'Thank You', role: Role::USER),
            // Content::parse(part: 'You\'re welcome! I\'m glad I could be of assistance.', role: Role::MODEL),

        ]);

    $response = $chat->sendMessage('Thank you ');
    echo $response->text();
aydinfatih commented 3 months ago

Hello @koossaayy, Thanks for your feedback. I was able to reproduce the problem locally. As far as I understand, it seems to be a general problem with gemini ai. Gemini returns the following response in some requests other than the normal api response. I am continuing to investigate the problem and will let you know when I find the most appropriate solution.

{ 
    "finishReason":  "RECITATION",
    "index" :  0,
} 

https://github.com/google/generative-ai-docs/issues/257

aydinfatih commented 2 months ago

Hello @koossaayy We have a new fix for this issue. Can you try again after composer update? https://github.com/google-gemini-php/client/releases/tag/1.0.4