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

我要如何才可以设置client,可以使用代理方式访问gemini的接口 #5

Closed demaxiya1982 closed 6 months ago

demaxiya1982 commented 6 months ago

我要如何才可以设置client,可以使用代理方式访问gemini的接口

因为当前的域名我无法通过直接链接的方式请求到~ 请帮助我完成他

aydinfatih commented 6 months ago

Hi @demaxiya1982 ,

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)
          ->withBaseUrl('test.com')
          ->withHttpClient(client: new GuzzleClient(['timeout' => config('gemini.request_timeout', 30)]));

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

      return $client->make();
  });
demaxiya1982 commented 6 months ago

好的~谢谢你的帮助

demaxiya1982 commented 6 months ago

@aydinfatih 我最终使用下边的代码实现了访问 $yourApiKey = getenv('GEMINI_API_KEY'); $client = null; $client = Gemini::factory() ->withApiKey($yourApiKey) ->withBaseUrl('https://generativelanguage.googleapis.com/v1/') // default: https://generativelanguage.googleapis.com/v1/ ->withHttpHeader('X-My-Header', 'foo') ->withQueryParam('my-param', 'bar') ->withHttpClient(new \GuzzleHttp\Client([ 'proxy' => '127.0.0.1:10809', 'verify' => false ])) // default: HTTP client found using PSR-18 HTTP Client Discovery ->withStreamHandler(fn(RequestInterface $request): ResponseInterface => $client->send($request, [ 'stream' => true // Allows to provide a custom stream handler for the http client. ])) ->make(); $fanhui = $client->geminiPro()->generateContent("你好,非常感谢你的帮助?"); // $fanhui = $client->geminiPro()->generateContent("你好,假如你的一名记者,请用口语化写一篇新闻稿件,字数在500字以上,请用印尼语回复,当前稿件的标题是以下内容:新质生产力,是个什么力?"); return $fanhui->text();