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
317 stars 32 forks source link

Support for API Responses in JSON Format #22

Closed mr-vijaychauhan closed 3 weeks ago

mr-vijaychauhan commented 3 weeks ago

Currently, the API response I am receiving is in a text format that resembles JSON but includes newline characters (\n) and other formatting issues that make it difficult to parse as structured data.

image

 $model = "gemini-1.5-flash";
    $response = Gemini::generativeModel($model)
        ->generateContent($prompt)
        ->text();

    dd($response);

Official Doc (aistudio.google.com)

image

aydinfatih commented 3 weeks ago

Hi @mr-vijaychauhan This feature does not work in the stable version. You can update the package version to 1.0.0-beta in Composer.json. Beta features are available in the beta version.

https://github.com/google-gemini-php/laravel/releases/tag/1.0.0-beta

chrisbeaver commented 3 weeks ago

Following the documented example from the release, I cannot see where the classes are coming from being referenced. Do you have namespaces for these other types in the config? Will this work with the startChat() method?

Specifically: ResponseMimeType, Schema, and DataType

image

aydinfatih commented 3 weeks ago

@chrisbeaver Yes, you are right, that part is missing. I will add it. I am sharing the file paths here to make it faster.

use Gemini\Data\GenerationConfig;
use Gemini\Data\Schema;
use Gemini\Enums\DataType;
use Gemini\Data\Content;
use Gemini\Enums\HarmBlockThreshold;
use Gemini\Data\SafetySetting;
use Gemini\Enums\HarmCategory;
use Gemini\Data\Blob;
use Gemini\Enums\ModelType;
use Gemini\Enums\ModelVariation;
use Gemini\Enums\ResponseMimeType;
use Gemini\Enums\MimeType;
use Gemini\Enums\Role;
chrisbeaver commented 3 weeks ago

Thanks for the response. It's not working for me however. I noticed that when I update the package to pull in v1.0.0-beta, that package is requiring google-gemini-php/client:^1.0.0-beta but composer is pulling down google-gemini-php/client:1.0.10. So without the beta, I don't think I'm getting the new classes. I don't know if this is my end, I did clear cache though... Or maybe composer pulls the 1.0.10 since it is referenced with the carat as ^1.0.0.

mr-vijaychauhan commented 3 weeks ago

@chrisbeaver yes I faced same issue. I thought I was doing wrong.. thanks for raised this issue

chrisbeaver commented 3 weeks ago

The fix for the versions is easy. Just manually install : composer require google-gemini-php/client=v1.0.0-beta This will downgrade the package that is installed. It will add this line to your requirements in your composer.json. Just go in your composer.json, remove the requirement for google-gemini-php/client=v1.0.0-beta. Clean up your lock file then with:

composer update nothing

Now you're locked on both packages at v1.0.0-beta, and the classes are available. Now I just need to figure out how to get it to work with chat.

mr-vijaychauhan commented 3 weeks ago

@chrisbeaver okk thanks ! I will try later

mr-vijaychauhan commented 3 weeks ago

Thanks @chrisbeaver its working now


    $response = Gemini::generativeModel($model)
        ->withGenerationConfig(
            generationConfig: new GenerationConfig(
                responseMimeType: ResponseMimeType::APPLICATION_JSON,
            )
        )
        ->generateContent($prompt)
        ->json();

        dd($response);