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

feat(google-genai): Support Gemini system instructions #7235

Merged

Conversation

chrisnathan20
Copy link
Contributor

@chrisnathan20 chrisnathan20 commented Nov 20, 2024

Fixes #5069

Currently for Google Gen AI, system message (it can only be the first element in list of messages) are prepended to the following human message which is used to steer the model's behavior. The handling of system messages is the same although some newer gen ai models support direct invocation of system messages via the systemInstructions field per https://ai.google.dev/gemini-api/docs/system-instructions?lang=node

This PR provides support for models that allow direct system instructions by providing an alternative method of handling system messages when possible or set via convertSystemMessageToHumanContent. The changes in this PR are modeled after the changes in google-common to support system instructions (#5089)

High Level Outline of Changes and Additions:

  1. added optional convertSystemMessageToHumanContent which overrides dependency on model to use new system message logic or not. This is also done to match the Generative AI package on Python.
  2. added method computeUseSystemInstruction to determine whether model supports system Instructions or not. List of supported and unsupported models taken from google-common's with deprecated models (palm) removed.
  3. Added new optional boolean parameter to convertBaseMessagesToContent which defaults to false. Setting this to true triggers new handling of system messages where system messages are treated as independent messages and not prepended to following user message while maintaining existing restrictions.
  4. Prior to sending prompt for response generation, if prompt's first message is an independent system message, then it is removed from the actual prompt to be sent and set as the system instruction of the model. It is important to note that an independent system message can only be the first message in prompt and can only exist if new system message handling in convertBaseMessagesToContent is triggered which can only be for model that supports system instructions.

Test Cases created:

  1. Given: Input has single system message followed by one user message
    When: convertBaseMessagesToContent is invoked with 3rd parameter set to true
    Then: the System message is removed from prompt and passed as systemInstruction field instead and actualPrompt would only have 1 user message under the role of user

  2. Given: Input has single system message followed by one user message
    When: convertBaseMessagesToContent is invoked with 3rd parameter set to false
    Then: the System message is not removed from prompt as actualPrompt would only have the system message prepended to the user message under the role of user

  3. Given: Input has a system message that is not the first message
    When: convertBaseMessagesToContent is invoked with 3rd parameter set to true
    Then: convertBaseMessagesToContent should raise the error - "System message should be the first one"

  4. Given: Input has a system message that is not the first message
    When: convertBaseMessagesToContent is invoked with 3rd parameter set to false
    Then: convertBaseMessagesToContent should raise the error - "System message should be the first one"

  5. Given: Input has multiple system messages
    When: convertBaseMessagesToContent is invoked with 3rd parameter set to true
    Then: convertBaseMessagesToContent should raise the error - "System message should be the first one"

  6. Given: Input has multiple system messages
    When: convertBaseMessagesToContent is invoked with 3rd parameter set to false
    Then: convertBaseMessagesToContent should raise the error - "System message should be the first one"

  7. Given: Input has no system message and one user message
    When: convertBaseMessagesToContent is invoked with 3rd parameter set to true
    Then: actualPrompt would only have the user message under the role of user

  8. Given: Input has no system message and one user message
    When: convertBaseMessagesToContent is invoked with 3rd parameter set to false
    Then: actualPrompt would only have the user message under the role of user

  9. Given: Input has no system message and multiple user messages
    When: convertBaseMessagesToContent is invoked with 3rd parameter set to true
    Then: actualPrompt would only have the first user message followed by the next user messages as separate messages

  10. Given: Input has no system message and multiple user messages
    When: convertBaseMessagesToContent is invoked with 3rd parameter set to false
    Then: actualPrompt would only have the first user message followed by the next user messages as separate messages

Credits:
Issue selection - @shannon-ab
Implementation - @chrisnathan20 @garychen2002
Testing - @shannon-ab @martinl498

Copy link

vercel bot commented Nov 20, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchainjs-docs ✅ Ready (Inspect) Visit Preview Dec 3, 2024 8:24pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchainjs-api-refs ⬜️ Ignored (Inspect) Dec 3, 2024 8:24pm

@chrisnathan20 chrisnathan20 marked this pull request as ready for review November 21, 2024 18:56
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. auto:improvement Medium size change to existing code to handle new use-cases labels Nov 21, 2024
@chrisnathan20
Copy link
Contributor Author

Hi @jacoblee93 and @afirstenberg - this is our PR to support system instructions for capable Generative AI models based on the implementation plan shared under the issue. If there are any points of concern or improvements, please do not hesitate to let us know. Thank you and have a great rest of your day!

Copy link
Collaborator

@jacoblee93 jacoblee93 left a comment

Choose a reason for hiding this comment

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

Looks good, apologies for the delay in review!

@dosubot dosubot bot added the lgtm PRs that are ready to be merged as-is label Dec 3, 2024
@jacoblee93 jacoblee93 changed the title google-genai [minor]: Support Gemini system messages feat(google-genai): Support Gemini system instructions Dec 3, 2024
@chrisnathan20
Copy link
Contributor Author

chrisnathan20 commented Dec 3, 2024

Thanks for the review @jacoblee93! I also want to let you know that I pushed a new commit to fix formatting issues as I did yarn lint but did not do yarn format which is my mistake, should I re-request a review from you?

@jacoblee93
Copy link
Collaborator

Nope all good, about to pull and try it out now

@jacoblee93 jacoblee93 merged commit 8eadded into langchain-ai:main Dec 3, 2024
25 checks passed
@jacoblee93
Copy link
Collaborator

Thank you!

@chrisnathan20
Copy link
Contributor Author

@jacoblee93 Thanks for the review!

syntaxsec pushed a commit to aks-456/langchainjs that referenced this pull request Dec 13, 2024
…7235)

Co-authored-by: Gary Chen <thegary.chen@mail.utoronto.ca>
Co-authored-by: martinl498 <martinloo498@gmail.com>
Co-authored-by: Shannon Budiman <shannon.budiman@mail.utoronto.ca>
Co-authored-by: Jacob Lee <jacoblee93@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:improvement Medium size change to existing code to handle new use-cases lgtm PRs that are ready to be merged as-is size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

google-genai [feature]: Support Gemini system messages
5 participants