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

User location is not supported for the API use #8

Closed foremtehan closed 2 months ago

foremtehan commented 2 months ago

image

Looks like gemini isn't available in some regions (in my case: germany). How can I specify a proxy? it would be great if we could do that, as it may take a while for them to fix it.

aydinfatih commented 2 months ago

Hello @foremtehan ,

First of all, thank you for your feedback.

Currently there is no way to define a separate proxy server in the package. But you can customize the guzzle client with the code below. Can you try this and share the results?

https://www.zenrows.com/blog/guzzle-proxy#request-option

You can configure your own http client by overriding the gemini client in Laravel. You have to register the Gemini client in your ServiceProvider:

 $this->app->extend(ClientContract::class, static function (): Client {
      $apiKey = config('gemini.api_key');

      if (!is_string($apiKey)) {
          throw MissingApiKey::create();
      }

      $baseURL = config('gemini.base_url');
      if (isset($baseURL) && !is_string($baseURL)) {
          throw new InvalidArgumentException('Invalid Gemini API base URL.');
      }

      $client = Gemini::factory()
          ->withApiKey(apiKey: $apiKey)
          ->withHttpClient(client: new GuzzleClient([
              'timeout' => config('gemini.request_timeout', 30),
                  RequestOptions::PROXY => [
                        ...yourProxies
                         'http'  => 'http://155.155.155.155:999',
                         'https' => 'http://156.156.156.156:8080',
                  ],
                  RequestOptions::VERIFY => false, # disable SSL certificate validation
                  RequestOptions::TIMEOUT => config('gemini.request_timeout', 30)
          ]));

      if (!empty($baseURL)) {
          $client->withBaseUrl(baseUrl: $baseURL);
      }

      return $client->make();
  });
foremtehan commented 2 months ago

Didn't work for me. I used the proxy from https://proxyscrape.com/free-proxy-list, but they were ignored.

aydinfatih commented 2 months ago

I was able to connect via proxy with the code below. Can you try with this code? If the problem occurs, you can try different proxies.

use Gemini;
use Gemini\Client;
use Gemini\Contracts\ClientContract;
use Gemini\Laravel\Exceptions\MissingApiKey;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\RequestOptions;
use http\Exception\InvalidArgumentException;

$this->app->extend(ClientContract::class, static function (): Client {
    $apiKey = config('gemini.api_key');

    if (!is_string($apiKey)) {
        throw MissingApiKey::create();
    }

    $baseURL = config('gemini.base_url');
    if (isset($baseURL) && !is_string($baseURL)) {
        throw new InvalidArgumentException('Invalid Gemini API base URL.');
    }

    $client = Gemini::factory()
        ->withApiKey(apiKey: $apiKey)
        ->withHttpClient(client: new GuzzleClient([
            RequestOptions::PROXY   => 'http://yourproxyserver:port',
            RequestOptions::VERIFY  => false, // disable SSL certificate validation
            RequestOptions::TIMEOUT => 60,
        ]));

    if (!empty($baseURL)) {
        $client->withBaseUrl(baseUrl: $baseURL);
    }

    return $client->make();
});
foremtehan commented 2 months ago

It worked... thanks!