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

google-genai [minor]: Support Gemini system messages #7235

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

Conversation

chrisnathan20
Copy link

@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 Nov 20, 2024 8:25pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchainjs-api-refs ⬜️ Ignored (Inspect) Nov 20, 2024 8:25pm

@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
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!

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 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
4 participants