LLPhant icon indicating copy to clipboard operation
LLPhant copied to clipboard

Add Gemini integration

Open volodymyrdovhan opened this issue 1 year ago • 7 comments

This pull request introduces support for the Gemini API within the LLPhant library, adding new chat and embedding generation functionality. The changes include updates to dependencies, new classes for interacting with the Gemini API, and configuration options for the Gemini models.

Dependency Updates:

  • Added [google-gemini-php/client](https://github.com/google-gemini-php/client) to composer.json to support the new Gemini API integration.

New Classes:

  • Introduced GeminiChat class to handle chat interactions with the Gemini API, including methods for generating text, streaming responses, and handling function calls.
  • Added GeminiProChat class extending GeminiChat for using the Gemini Pro model.
  • Created GeminiEmbeddingGenerator class for generating embeddings using the Gemini API, including methods for embedding text and documents.

Configuration:

  • Added GeminiConfig class to manage configuration options for the Gemini API, including API key, URL, model, and model options.

TODO:

  • Implement Image and Audio models
  • Add instructions to README
  • Implement functions/tools mechanism

volodymyrdovhan avatar Nov 11 '24 16:11 volodymyrdovhan

hey @volodymyrdovhan ! Thanks for this PR, can you move the dependencies to the suggest section? We don't want to force to install all external lib. We will move out openai also.

MaximeThoonsen avatar Nov 11 '24 22:11 MaximeThoonsen

Hi @MaximeThoonsen. Yes, sure. That's an initial PR to get the feedback.

volodymyrdovhan avatar Nov 11 '24 22:11 volodymyrdovhan

Hi @MaximeThoonsen the composer.json updated.

Best Vlad

volodymyrdovhan avatar Nov 12 '24 09:11 volodymyrdovhan

Hi @volodymyrdovhan , first of all thank you for your PR. A couple of things. First of all in your opinion what are the main advantages of using a dedicated library for Gemini, since Google announced its support for OpenAI interfaces? https://developers.googleblog.com/en/gemini-is-now-accessible-from-the-openai-library/ I'd like to know also @MaximeThoonsen 's opinion on that Then, maybe it would be useful to add unit and integration tests to the PR.

f-lombardo avatar Nov 15 '24 08:11 f-lombardo

Failing test should be fixed by https://github.com/theodo-group/LLPhant/pull/265 Problems with formats should be solved instead

f-lombardo avatar Nov 15 '24 10:11 f-lombardo

Hello, adding Gemini would be great! Thanks for your efforts!

@volodymyrdovhan apart from the previous questions, can we modify the Readme.md and docusaurus/docs/features.md with instructions and supported features?

@f-lombardo using the OpenAI-like API seems interesting, I'm trying to test Gemini with it but I can't get it to work (probably I can't find the proper URL?). Does anyone have luck with it? To give my opinion on your question, using Google's composer modules could make it easy to integrate specific features that could not be available through OpenAI. But if we want to keep OpenAI API compatibility, that may not be a problem (unless Google decides not to put enough love on this endpoint). The composer module should always get the latest features, I suppose.

josecanciani avatar Feb 24 '25 14:02 josecanciani

Replying myself, I got it working with Google's OpenAI-compatible endpoint, here's how:

use LLPhant\Chat\OpenAIChat;
use LLPhant\OpenAIConfig;

$config = new OpenAIConfig();
$config->apiKey = $geminiApiKey; // YOUR API KEY HERE, generated via Google Studio
$config->model = 'gemini-2.0-flash'; // A supported model
$config->url = 'https://generativelanguage.googleapis.com/v1beta/openai';
$chat = new OpenAIChat($config);

There's only one thing breaking:

Fatal error: Uncaught TypeError: OpenAI\Responses\Chat\CreateResponse::__construct(): 
  Argument #1 ($id) must be of type string, null given, 
  called in vendor/openai-php/client/src/Responses/Chat/CreateResponse.php on line 53
  and defined in vendor/openai-php/client/src/Responses/Chat/CreateResponse.php:30

A quick if is modifying vendor/openai-php/client/src/Responses/Chat/CreateResponse.php:

-            $attributes['id'],
+            $attributes['id'] ?? 'unknown',

but since that's a file outside LLPHant I guess we should find a better way. I tried setting an "id" in the request ($config->modelOptions['id'] = 1234;) but the call fails because it does not understand "id".

If anyone knows how to make Google API to return it, please let us know. Apart from that, the chat seems to work fine, although I'll be testing more.

josecanciani avatar Feb 24 '25 16:02 josecanciani

When can we expect this implemented?

gabrielius914 avatar May 07 '25 10:05 gabrielius914

What is the status here?

jtsternberg avatar Jun 23 '25 20:06 jtsternberg

We are waiting for this bug fix in OpenAI client: https://github.com/openai-php/client/issues/568

f-lombardo avatar Jun 25 '25 11:06 f-lombardo

Why is an open-ai fix necessary for the Gemini provider?

jtsternberg avatar Jun 25 '25 14:06 jtsternberg

Ah, it's a Gemini related issue. I understand.

jtsternberg avatar Jun 25 '25 14:06 jtsternberg

Hey @josecanciani @gabrielius914 @jtsternberg @volodymyrdovhan , what do you think about this solution? https://github.com/LLPhant/LLPhant/pull/359

f-lombardo avatar Jun 25 '25 19:06 f-lombardo

Hey @josecanciani @gabrielius914 @jtsternberg @volodymyrdovhan , what do you think about this solution? #359

Looks great, but it will have the problem I described here: https://github.com/LLPhant/LLPhant/pull/264#issuecomment-2678923023 regarding the "id" parameter. I suggested a fix, but it's in an OpenAI's PHP package file, so I'm not sure how usable it is (unless we can upgrade the package and wait for Google to "fix" it on the server).

josecanciani avatar Jun 25 '25 20:06 josecanciani

Hey @josecanciani @gabrielius914 @jtsternberg @volodymyrdovhan , what do you think about this solution? #359

Looks great, but it will have the problem I described here: #264 (comment) regarding the "id" parameter. I suggested a fix, but it's in an OpenAI's PHP package file, so I'm not sure how usable it is (unless we can upgrade the package and wait for Google to "fix" it on the server).

I think they solved this issue in one of the latest releases of OpenAI PHP client, since this integration test works: https://github.com/LLPhant/LLPhant/pull/359/files#diff-6f9f1185c359b06c39403c64c5921d5670a1737b8fb7e830ed77a065fe2e6d56

Do you have any failing integration test to suggest?

f-lombardo avatar Jun 25 '25 20:06 f-lombardo

Hey @josecanciani @gabrielius914 @jtsternberg @volodymyrdovhan , what do you think about this solution? #359

Looks great, but it will have the problem I described here: #264 (comment) regarding the "id" parameter. I suggested a fix, but it's in an OpenAI's PHP package file, so I'm not sure how usable it is (unless we can upgrade the package and wait for Google to "fix" it on the server).

I think they solved this issue in one of the latest releases of OpenAI PHP client, since this integration test works: https://github.com/LLPhant/LLPhant/pull/359/files#diff-6f9f1185c359b06c39403c64c5921d5670a1737b8fb7e830ed77a065fe2e6d56

Do you have any failing integration test to suggest?

No, it just failed in my box months ago, and I haven't tried again. If you got it working, then great!

josecanciani avatar Jun 25 '25 21:06 josecanciani

We decided to integrate Gemini using OpenAI API

f-lombardo avatar Aug 20 '25 07:08 f-lombardo