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[minor]: Move Vertex embeddings to integration package #12

Open
wants to merge 2 commits into
base: cloned_main_de3a4
Choose a base branch
from

Conversation

local-dev-korbit-ai-mentor[bot]
Copy link

@local-dev-korbit-ai-mentor local-dev-korbit-ai-mentor bot commented Aug 15, 2024

Will need to break this up into different PRs to make it pass CI

CC @afirstenberg

Description by Korbit AI

What change is being made?

Move Vertex embeddings to the integration package and deprecate the old imports.

Why are these changes being made?

This change is part of a refactoring effort to better organize the codebase by moving the Vertex embeddings functionality to a dedicated integration package. This improves modularity and maintainability. The old imports are deprecated to guide users towards the new structure.

Summary by CodeRabbit

  • New Features

    • Introduced Google Vertex AI embeddings integration, enhancing support for embedding generation within the Langchain framework.
    • Added classes and interfaces for managing Google embeddings and authentication processes.
  • Deprecation Notices

    • Added deprecation notices for existing interfaces related to Google Vertex AI embeddings to guide users towards new import paths.
  • Improvements

    • Enhanced logging for URL generation in Google AI connection methods, aiding in debugging and monitoring.
  • Tests

    • Added unit tests for the Google Vertex AI embeddings class, ensuring functionality of key methods.

Copy link
Author

Clone of the PR langchain-ai/langchainjs#6459

Copy link

korbit-ai bot commented Aug 15, 2024

My review is in progress 📖 - I will have feedback for you in a few minutes!

Copy link

coderabbitai bot commented Aug 15, 2024

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The recent updates enhance the Langchain library by reorganizing Google Vertex AI embeddings and authentication features, improving code maintainability and usability. Deprecation notices guide users toward new import paths, while new classes and interfaces facilitate seamless integration with Google’s AI capabilities. Logging enhancements and expanded test coverage further bolster functionality, ensuring that the library remains robust and user-friendly.

Changes

Files Changed Change Summary
libs/langchain-community/src/embeddings/googlevertexai.ts
libs/langchain-google-common/src/connection.ts
libs/langchain-google-common/src/embeddings.ts
libs/langchain-google-gauth/src/auth.ts
libs/langchain-google-gauth/src/embeddings.ts
libs/langchain-google-vertexai/src/embeddings.ts
libs/langchain-google-webauth/src/embeddings.ts
Added deprecation notices for several interfaces, restructured type parameters, and introduced new classes for embeddings.
libs/langchain-google-common/src/index.ts
libs/langchain-google-gauth/src/index.ts
libs/langchain-google-vertexai-web/src/index.ts
libs/langchain-google-vertexai/src/index.ts
libs/langchain-google-webauth/src/index.ts
Added export statements to include new embeddings functionalities in the module's index files.
libs/langchain-google-vertexai-web/src/tests/embeddings.int.test.ts Introduced unit tests for the GoogleVertexAIEmbeddings class to validate core methods.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant GoogleAI
    participant Embeddings

    User->>Embeddings: Request embeddings for input data
    Embeddings->>GoogleAI: Format request and authenticate
    GoogleAI-->>Embeddings: Return embeddings
    Embeddings-->>User: Send back embeddings
Loading

🐰 In the meadow, I hop and play,
With code that dances, bright as day.
New imports spring, like flowers bloom,
In every corner, there's joy and room!
So let us cheer, in code we trust,
With every change, our dreams adjust! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@korbit-ai korbit-ai bot left a comment

Choose a reason for hiding this comment

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

I have reviewed your code and found 5 potential issues.

Comment on lines +11 to +14
/**
* Integration with a chat model.
*/
export class GoogleVertexAIEmbeddings extends GoogleEmbeddings {
Copy link

Choose a reason for hiding this comment

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

category Functionality severity potentially major

The class description for GoogleVertexAIEmbeddings is incorrect. It's currently described as 'Integration with a chat model', but this class is actually for embeddings. Please update the comment to accurately describe the class, for example: 'Integration with Google Vertex AI for embeddings.'

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

Comment on lines +198 to +201
async embedQuery(document: string): Promise<number[]> {
const data = await this.embedDocuments([document]);
return data[0];
}
Copy link

Choose a reason for hiding this comment

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

category Functionality severity potentially major

In the embedQuery method, there's no check to ensure that the data array returned by embedDocuments is not empty before accessing its first element. This could lead to a runtime error if embedDocuments returns an empty array. Consider adding a check and handling the case where no embeddings are returned.

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

Comment on lines 54 to 58
/**
* @deprecated Import and use from @langchain/google-vertexai or @langchain/google-vertexai-web
* Enables calls to the Google Cloud's Vertex AI API to access
* the embeddings generated by Large Language Models.
*
Copy link

Choose a reason for hiding this comment

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

category Functionality

The class GoogleVertexAIEmbeddings has been marked as deprecated, which is good. However, to ensure smooth transition for users, it would be helpful to provide more detailed information in the deprecation notice. Consider adding a brief explanation of why the class is being deprecated and provide a code example of how to use the new import location. This will help users understand the change and quickly update their code.

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

Comment on lines +215 to +217
console.log(
`https://${this.endpoint}/v1/projects/${projectId}/locations/${this.location}/publishers/google/models/${this.model}:${method}`
);
Copy link

Choose a reason for hiding this comment

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

category Functionality

I noticed that a console.log statement has been added to print the URL method. While this might be helpful for debugging, it's generally not recommended to leave console.log statements in production code. Additionally, this log might expose sensitive information about the project ID, location, and model. Consider removing this console.log statement or replacing it with a more appropriate logging mechanism that respects different environments (development vs. production) and doesn't expose sensitive data.

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

Comment on lines +4 to +10
test("Test GoogleVertexAIEmbeddings.embedQuery", async () => {
const embeddings = new GoogleVertexAIEmbeddings({
model: "textembedding-gecko",
});
const res = await embeddings.embedQuery("Hello world");
expect(typeof res[0]).toBe("number");
});
Copy link

Choose a reason for hiding this comment

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

category Functionality

The current test cases for GoogleVertexAIEmbeddings are a good start, but they could be more comprehensive. Consider adding checks for the following:

  1. The length of each embedding vector (should be consistent and match the expected dimensionality for the chosen model).
  2. The range of values in the embeddings (typically between -1 and 1 for normalized embeddings).
  3. Ensure that different input texts produce different embeddings.
  4. Test edge cases like empty strings or very long texts.

These additional checks will provide more confidence in the correct functionality of the embeddings.

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

@kavitaganeshan
Copy link

/review

@kavitaganeshan
Copy link

@coderabbitai review

Copy link

coderabbitai bot commented Aug 15, 2024

Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

PR Reviewer Guide 🔍

⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Key issues to review

Debugging Code
A console.log statement has been added, which may have been intended for debugging purposes and should be removed before merging.

Type Changes
The types for CallOptions and InputType have been changed, which may affect other parts of the codebase that depend on these types.

New File
A new file has been added with significant new functionality. This requires careful review to ensure it integrates well with existing code and follows project standards.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

Outside diff range, codebase verification and nitpick comments (2)
libs/langchain-google-vertexai-web/src/tests/embeddings.int.test.ts (2)

4-10: Enhance test coverage for embedQuery.

The test case for embedQuery is basic. Consider adding checks for:

  1. The length of the embedding vector.
  2. The range of values.
  3. Edge cases like empty strings or very long texts.

12-29: Enhance test coverage for embedDocuments.

The test case for embedDocuments is a good start but can be improved by:

  1. Checking the length of each embedding vector.
  2. Verifying the range of values.
  3. Testing with edge cases like empty or very long documents.
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between de3a485 and e413dcc.

Files selected for processing (15)
  • libs/langchain-community/src/embeddings/googlevertexai.ts (4 hunks)
  • libs/langchain-community/src/utils/googlevertexai-connection.ts (1 hunks)
  • libs/langchain-google-common/src/connection.ts (2 hunks)
  • libs/langchain-google-common/src/embeddings.ts (1 hunks)
  • libs/langchain-google-common/src/index.ts (1 hunks)
  • libs/langchain-google-gauth/src/auth.ts (2 hunks)
  • libs/langchain-google-gauth/src/embeddings.ts (1 hunks)
  • libs/langchain-google-gauth/src/index.ts (1 hunks)
  • libs/langchain-google-vertexai-web/src/embeddings.ts (1 hunks)
  • libs/langchain-google-vertexai-web/src/index.ts (1 hunks)
  • libs/langchain-google-vertexai-web/src/tests/embeddings.int.test.ts (1 hunks)
  • libs/langchain-google-vertexai/src/embeddings.ts (1 hunks)
  • libs/langchain-google-vertexai/src/index.ts (1 hunks)
  • libs/langchain-google-webauth/src/embeddings.ts (1 hunks)
  • libs/langchain-google-webauth/src/index.ts (1 hunks)
Files skipped from review due to trivial changes (1)
  • libs/langchain-google-gauth/src/index.ts
Additional context used
Biome
libs/langchain-google-webauth/src/embeddings.ts

[error] 29-31: This constructor is unnecessary.

Unsafe fix: Remove the unnecessary constructor.

(lint/complexity/noUselessConstructor)

libs/langchain-google-gauth/src/embeddings.ts

[error] 30-32: This constructor is unnecessary.

Unsafe fix: Remove the unnecessary constructor.

(lint/complexity/noUselessConstructor)

libs/langchain-google-common/src/embeddings.ts

[error] 22-29: This constructor is unnecessary.

Unsafe fix: Remove the unnecessary constructor.

(lint/complexity/noUselessConstructor)

Additional comments not posted (12)
libs/langchain-google-vertexai-web/src/index.ts (1)

3-3: LGTM! Export addition for embeddings.js.

The addition of export * from "./embeddings.js"; successfully extends the module's interface to include embeddings functionality, aligning with the refactoring goals.

libs/langchain-google-vertexai/src/index.ts (1)

3-3: LGTM! Export addition for embeddings.js.

The addition of export * from "./embeddings.js"; successfully extends the module's interface to include embeddings functionality, aligning with the refactoring goals.

libs/langchain-google-webauth/src/index.ts (1)

3-3: LGTM! Export addition for embeddings.js.

The addition of export * from "./embeddings.js"; successfully extends the module's interface to include embeddings functionality, aligning with the refactoring goals.

libs/langchain-google-common/src/index.ts (1)

3-3: Export addition looks good.

The addition of export * from "./embeddings.js"; enhances the modularity of the codebase by making embeddings-related functionality accessible through the index file.

libs/langchain-google-vertexai/src/embeddings.ts (1)

11-14: Update the class description.

The class description inaccurately states "Integration with a chat model." It should be updated to reflect that this class is for embeddings, such as "Integration with Google Vertex AI for embeddings."

libs/langchain-google-gauth/src/auth.ts (1)

Line range hint 30-34:
Verify the impact of changing the constructor parameter type.

The constructor now accepts GoogleConnectionParams<GoogleAuthOptions> instead of GoogleBaseLLMInput<GoogleAuthOptions>. Ensure that all instances of GAuthClient are updated to use the new parameter type and that this change does not introduce any breaking changes in the codebase.

libs/langchain-community/src/embeddings/googlevertexai.ts (2)

13-16: Ensure deprecation notices are clear and informative.

The deprecation notices guide users to import from @langchain/google-vertexai or @langchain/google-vertexai-web. Ensure that these notices provide sufficient context, such as reasons for deprecation and examples of how to update the code.

Also applies to: 23-26, 30-33, 39-42


55-58: Enhance deprecation notice with examples.

The deprecation notice for GoogleVertexAIEmbeddings should include a brief explanation and code examples to help users transition to the new import paths.

libs/langchain-google-common/src/embeddings.ts (1)

198-201: Add a check for empty data in embedQuery.

The embedQuery method does not currently check if the data array returned by embedDocuments is empty, which could lead to runtime errors. Consider adding a check to handle this case.

libs/langchain-google-common/src/connection.ts (3)

169-170: Update type parameters for flexibility.

The change from MessageType to InputType broadens the scope of input data, enhancing the flexibility of the GoogleAIConnection class.


235-236: Update method signature for formatData.

The method now accepts InputType, aligning with the updated type parameters for broader input support.


240-243: Update method signature for request.

The method now uses InputType and AsyncCallerCallOptions, which enhances the asynchronous capabilities of the class.

Comment on lines +215 to +217
console.log(
`https://${this.endpoint}/v1/projects/${projectId}/locations/${this.location}/publishers/google/models/${this.model}:${method}`
);
Copy link

Choose a reason for hiding this comment

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

Remove or replace console.log for sensitive information.

The console.log statement exposes sensitive information, such as the project ID and model. Consider using a logging mechanism that respects different environments and does not expose sensitive data.

- console.log(
-   `https://${this.endpoint}/v1/projects/${projectId}/locations/${this.location}/publishers/google/models/${this.model}:${method}`
- );
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
console.log(
`https://${this.endpoint}/v1/projects/${projectId}/locations/${this.location}/publishers/google/models/${this.model}:${method}`
);

Comment on lines +29 to +31
constructor(fields: GoogleEmbeddingsInput) {
super(fields);
}
Copy link

Choose a reason for hiding this comment

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

Remove the unnecessary constructor.

The constructor in GoogleEmbeddings is redundant as it only calls the superclass constructor. Removing it will simplify the code.

-  constructor(fields: GoogleEmbeddingsInput) {
-    super(fields);
-  }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
constructor(fields: GoogleEmbeddingsInput) {
super(fields);
}
Tools
Biome

[error] 29-31: This constructor is unnecessary.

Unsafe fix: Remove the unnecessary constructor.

(lint/complexity/noUselessConstructor)

Comment on lines +30 to +32
constructor(fields: GoogleEmbeddingsInput) {
super(fields);
}
Copy link

Choose a reason for hiding this comment

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

Remove the unnecessary constructor.

The constructor in GoogleEmbeddings is redundant as it only calls the superclass constructor. Removing it will simplify the code.

-  constructor(fields: GoogleEmbeddingsInput) {
-    super(fields);
-  }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
constructor(fields: GoogleEmbeddingsInput) {
super(fields);
}
Tools
Biome

[error] 30-32: This constructor is unnecessary.

Unsafe fix: Remove the unnecessary constructor.

(lint/complexity/noUselessConstructor)

Comment on lines +11 to +14
/**
* Integration with a chat model.
*/
export class GoogleVertexAIEmbeddings extends GoogleEmbeddings {
Copy link

Choose a reason for hiding this comment

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

Update the class description.

The class description inaccurately states "Integration with a chat model." It should be updated to reflect that this class is for embeddings, such as "Integration with Google Vertex AI for embeddings."

Comment on lines +22 to +29
constructor(
fields: GoogleConnectionParams<AuthOptions> | undefined,
caller: AsyncCaller,
client: GoogleAbstractedClient,
streaming: boolean
) {
super(fields, caller, client, streaming);
}
Copy link

Choose a reason for hiding this comment

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

Remove unnecessary constructor.

The constructor in EmbeddingsConnection does not perform any additional logic and can be safely removed to simplify the code.

-  constructor(
-    fields: GoogleConnectionParams<AuthOptions> | undefined,
-    caller: AsyncCaller,
-    client: GoogleAbstractedClient,
-    streaming: boolean
-  ) {
-    super(fields, caller, client, streaming);
-  }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
constructor(
fields: GoogleConnectionParams<AuthOptions> | undefined,
caller: AsyncCaller,
client: GoogleAbstractedClient,
streaming: boolean
) {
super(fields, caller, client, streaming);
}
Tools
Biome

[error] 22-29: This constructor is unnecessary.

Unsafe fix: Remove the unnecessary constructor.

(lint/complexity/noUselessConstructor)

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

Successfully merging this pull request may close these issues.

2 participants