Add Gemini integration
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)tocomposer.jsonto support the new Gemini API integration.
New Classes:
- Introduced
GeminiChatclass to handle chat interactions with the Gemini API, including methods for generating text, streaming responses, and handling function calls. - Added
GeminiProChatclass extendingGeminiChatfor using the Gemini Pro model. - Created
GeminiEmbeddingGeneratorclass for generating embeddings using the Gemini API, including methods for embedding text and documents.
Configuration:
- Added
GeminiConfigclass 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
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.
Hi @MaximeThoonsen. Yes, sure. That's an initial PR to get the feedback.
Hi @MaximeThoonsen the composer.json updated.
Best Vlad
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.
Failing test should be fixed by https://github.com/theodo-group/LLPhant/pull/265 Problems with formats should be solved instead
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.
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.
When can we expect this implemented?
What is the status here?
We are waiting for this bug fix in OpenAI client: https://github.com/openai-php/client/issues/568
Why is an open-ai fix necessary for the Gemini provider?
Ah, it's a Gemini related issue. I understand.
Hey @josecanciani @gabrielius914 @jtsternberg @volodymyrdovhan , what do you think about this solution? https://github.com/LLPhant/LLPhant/pull/359
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).
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?
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!
We decided to integrate Gemini using OpenAI API