Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Spring Boot Project Starter for Google Gemini API model - ChatLangauge, Streaming model and Embedding Model #74

Open
wants to merge 47 commits into
base: main
Choose a base branch
from

Conversation

Suhas-Koheda
Copy link

@Suhas-Koheda Suhas-Koheda commented Nov 13, 2024

@Suhas-Koheda
Copy link
Author

@langchain4j hey can you please have a look at this

Copy link
Owner

@langchain4j langchain4j left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Suhas-Koheda thanks a lot!

langchain4j-google-ai-gemini/pom.xml Outdated Show resolved Hide resolved
langchain4j-google-ai-gemini/pom.xml Outdated Show resolved Hide resolved
.topP(chatModelProperties.getTopP())
.topK(chatModelProperties.getTopK())
.maxOutputTokens(chatModelProperties.getMaxOutputTokens())
.responseFormat(ResponseFormat.JSON)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it json?

Copy link

@franglopez franglopez Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. Related to your question. AutoConfig it's not injecting logging properties and json format from the properties -> https://docs.langchain4j.dev/tutorials/logging/ is not working when this AutoConfig is used. I will wait until this PR is merged to create a PR to avoid conflicts or If you want to add this configuration in this PR. Whatever you consider better.
Thank you!

Copy link
Author

@Suhas-Koheda Suhas-Koheda Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! can you provide me with some references
As i did not find the logging used in other starters
Or maybe i overlooked
Thank you!
@franglopez

@Suhas-Koheda
Copy link
Author

@langchain4j also in the timeout values i had doubt
Will it be set by the user? Or should i keep the condition if its not set then set to 0

At present i have hardcoded used 60 secs

@langchain4j
Copy link
Owner

@Suhas-Koheda you can see how timeouts are handled in OpenAI starter here, the same for .logRequests(chatModelProperties.logRequests()).

@ddobrin
Copy link

ddobrin commented Nov 14, 2024

@Suhas-Koheda
when you are looking at logging requests and responses, please be aware that the GogleAiGeminiChatModel in the module langchain4j-google-ai-gemini does not make a distinction in the constructor between logRequests() and log Responses() and uses a single Boolean to enable/disable the logging functionality link

@Suhas-Koheda
Copy link
Author

Suhas-Koheda commented Nov 14, 2024

Yeah sure I'll take care of it!
Thank you!
@ddobrin

@Suhas-Koheda
Copy link
Author

@ddobrin hey for the response format i have kept the type of prop as ResponseFormat only which is user defined - not a primitive type?
is this ok ? or does it cause any problem?

@Suhas-Koheda
Copy link
Author

@langchain4j
Hey! Hi!
I've completed writing tests and both of the tests passed!
Also review the code once and let me know if any changes are to be made :)

@ddobrin
Copy link

ddobrin commented Nov 16, 2024

@Suhas-Koheda on a 📱 let me come back to it at the beginning of the week

@ddobrin
Copy link

ddobrin commented Nov 27, 2024

@langchain4j - yes, Friday. Task overload until then

@ddobrin
Copy link

ddobrin commented Nov 29, 2024

Hi @Suhas-Koheda
I have looked at the code and can run your tests.

Re: streaming chat model not having a builder for safety settings

I have revisited streaming, as well as the Vertex AI Gemini model. While that is missing, rather than making the starter config different between chat and streaming chat, can I suggest that we add this small missing bit in the /langchain4j/langchain4j-google-ai-gemini module first, and the starter would then come after.

What do you think?

@Suhas-Koheda
Copy link
Author

Suhas-Koheda commented Nov 29, 2024

Hi @Suhas-Koheda
I have looked at the code and can run your tests.

Yeah! Happy to hear that!

Re: streaming chat model not having a builder for safety settings

I have revisited streaming, as well as the Vertex AI Gemini model. While that is missing, rather than making the starter config different between chat and streaming chat, can I suggest that we add this small missing bit in the /langchain4j/langchain4j-google-ai-gemini module first, and the starter would then come after.

What do you think?

Yeah sure! It works!
Actually i can work on that if you are okay with it!

@ddobrin
Copy link

ddobrin commented Nov 30, 2024

If you like to add it and send a PR, that would be great

@Suhas-Koheda
Copy link
Author

@ddobrin hey i have opened a pr for the above in the main repo and also i have chnaged the tests here also to support the same !
langchain4j/langchain4j#2217

@ddobrin
Copy link

ddobrin commented Dec 2, 2024

Hi @Suhas-Koheda
I noticed that #2217 has been merged.

Right now, your tests are failing due to Bean instantiation issues.
If you like, you can change L66-69 to

                        "langchain4j.google-ai-gemini.streamingChatModel.safetySetting.gemini-harm-category=HARM_CATEGORY_SEXUALLY_EXPLICIT",
                        "langchain4j.google-ai-gemini.streamingChatModel.safetySetting.gemini-harm-block-threshold=HARM_BLOCK_THRESHOLD_UNSPECIFIED",
                        "langchain4j.google-ai-gemini.streamingChatModel.functionCallingConfig.gemini-mode=ANY",
                        "langchain4j.google-ai-gemini.streamingChatModel.functionCallingConfig.allowed-function-names=allowCodeExecution,includeCodeExecutionOutput"

and your tests would succeed.
It is a change from langchain4j.google-ai-gemini.chatModel to langchain4j.google-ai-gemini.streamingChatModel

@Suhas-Koheda
Copy link
Author

Yeah okay!
I'll look into it tomorrow mrng! :-)
Thank you!

@Suhas-Koheda
Copy link
Author

@ddobrin

completed! 🎉

@ddobrin
Copy link

ddobrin commented Dec 3, 2024

All good @Suhas-Koheda
I can run the tests as well

cc/ @langchain4j

Copy link
Owner

@langchain4j langchain4j left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Suhas-Koheda thank you!

@ddobrin could you please take a look at my comments as well?

@Suhas-Koheda
Copy link
Author

@langchain4j done!

@ddobrin
Copy link

ddobrin commented Dec 7, 2024

@langchain4j done!

@Suhas-Koheda
Could you please replace

  @Bean
    @ConditionalOnProperty(name = PREFIX + ".chatModel.enabled", havingValue = "true")

with

@ConditionalOnProperty(name = PREFIX + ".chatModel.api-key")
@ConditionalOnProperty(name = PREFIX + ".chatModel.model-name")

Thanks

@Suhas-Koheda
Copy link
Author

@ddobrin hey i have completed it!
out of curiosity, the above changes allow us defining beans with different models and such sort right?
Is that the motive?
Thank you!

@ddobrin
Copy link

ddobrin commented Dec 9, 2024

Hi @Suhas-Koheda
this last change you have made will help us specify the fact that "auto config is set conditional on values a developer needs to set in order to establish a chat model object and interact with the LLM: the API Key and the model name"

The change for "supporting multiple models in the same config" is separate but it's good that it surfaced in the context of AutoConfig.

@Suhas-Koheda
Copy link
Author

Yeah!
Now i get it!
Thank you!

Copy link
Owner

@langchain4j langchain4j left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Suhas-Koheda thanks a lot!🙏
I've applied a few cosmetic changes, I hope you don't mind.

I've also noticed that when safetySettings or toolConfig is not specified explicitly in the properties, the whole thing fails with NPE, could you please fix this? We can merge after this is fixed. This PR will be a great addition to the next relesase which is planned for this week (on Thursday)!

.responseFormat(chatModelProperties.responseFormat())
.logRequestsAndResponses(chatModelProperties.logRequestsAndResponses())
.safetySettings(Map.of(
// TODO NPE?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please rewrite it to avoid NPE

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@langchain4j hey how can we avoid NPE like i am writing a function that checks if it is empty or not
if it is not empty the map is returned or else i wanted to return a defautl value
but what can the default value be
@ddobrin if you can help me with this :-)
Thank you

chatModelProperties.safetySetting().geminiHarmBlockThreshold()
))
.toolConfig(
// TODO NPE?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, please check all other model beans as well

@langchain4j
Copy link
Owner

@Suhas-Koheda I would love to include your PR in this release (today), but there are still some issues left (please see my comments above). Since this is an independent module, we can release it a bit later separately, once this PR is ready.

@Suhas-Koheda
Copy link
Author

@langchain4j
Yeah yeha sure!
I'll be working on it
I was a bit busy with my exams
Now I'll be completely on it!

@Suhas-Koheda
Copy link
Author

@langchain4j hey i have written the NPE checks can you please have a look over this
Thank you!

return defaultMap;
}
return Map.of(
safetySetting.geminiHarmCategory(),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these settings supposed to have only a single key-value pair?

Copy link
Author

@Suhas-Koheda Suhas-Koheda Dec 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The safety settings builder takes the map of geminiharmcategory as key and geminiharmblockthreshold as value

No it can take any number of values
It changes them into list

But for a single model thre would be only one harmcategory and blockthreshold right?

If that's not the case we can write such that the harmcategory and blockthreshold takes comma separated values and we manually convert then into map in the autoconfig?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@langchain4j

private Map<GeminiHarmCategory,GeminiHarmBlockThreshold> checkSafetySettingForNull(GeminiSafetySetting safetySetting) {
        if(safetySetting==null){
            Map<GeminiHarmCategory,GeminiHarmBlockThreshold> defaultMap= new HashMap<>();
            defaultMap.put(HARM_CATEGORY_CIVIC_INTEGRITY,HARM_BLOCK_THRESHOLD_UNSPECIFIED);
            return defaultMap;
        }
        Map<GeminiHarmCategory,GeminiHarmBlockThreshold> userMap= new HashMap<>();
        safetySetting.geminiHarmCategory().forEach(category -> userMap.put(category,safetySetting.geminiHarmBlockThreshold().get(safetySetting.geminiHarmCategory().indexOf(category))));
        return userMap;
    }

this can be done if there are multiple GeminiHarmCategory and GeminiHarmBlockThreshold

and the test cases run properly too

the code is ready to be commited
thank you!

@Suhas-Koheda
Copy link
Author

@langchain4j hey! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Google AI Gemini: implement Spring Boot starter
4 participants