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-common[patch]: adding client-info #5103

Merged
merged 11 commits into from
May 1, 2024
22 changes: 15 additions & 7 deletions libs/langchain-google-common/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,30 @@ export abstract class GoogleConnection<
abstract buildMethod(): GoogleAbstractedClientOpsMethod;

Copy link

Choose a reason for hiding this comment

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

Hey team, just wanted to flag the recent change in the PR for your review. The added _getclientInfo function calls getRuntimeEnvironment, which is expected to access or read environment variables or configuration related to the runtime environment. Please take a look and ensure everything is handled correctly. Thanks!

async _clientInfoHeaders(): Promise<Record<string, string>> {
const clientLibraryVersion = await this._clientLibraryVersion();
const { userAgent, clientLibraryVersion } = await this._getClientInfo();
return {
"User-Agent": clientLibraryVersion,
"User-Agent": userAgent,
"Client-Info": clientLibraryVersion,
};
}

async _clientLibraryVersion(): Promise<string> {
async _getClientInfo(): Promise<{
userAgent: string;
clientLibraryVersion: string;
}> {
const env = await getRuntimeEnvironment();
const langchain = env?.library ?? "langchain-js";
const langchainVersion = env?.libraryVersion ?? "0";
// TODO: Add an API for getting the current LangChain version
const langchainVersion = "0";
const moduleName = await this._moduleName();
let ret = `${langchain}/${langchainVersion}`;
let clientLibraryVersion = `${langchain}/${langchainVersion}`;
if (moduleName && moduleName.length) {
ret = `${ret}-${moduleName}`;
clientLibraryVersion = `${clientLibraryVersion}-${moduleName}`;
}
return ret;
return {
userAgent: clientLibraryVersion,
clientLibraryVersion: `${langchainVersion}-${moduleName}`,
};
}

async _moduleName(): Promise<string> {
Expand Down
4 changes: 4 additions & 0 deletions libs/langchain-google-common/src/tests/chat_models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,13 @@ describe("Mock ChatGoogle", () => {
await model.invoke(messages);

expect(record?.opts?.headers).toHaveProperty("User-Agent");
expect(record?.opts?.headers).toHaveProperty("Client-Info");
expect(record.opts.headers["User-Agent"]).toMatch(
/langchain-js\/[0-9.]+-ChatConnection/
);
expect(record.opts.headers["Client-Info"]).toMatch(
/\d+(\.\d+)?-ChatConnection/ // Since we are not getting libraryVersion from env right now, it will always be 0
);
});

test("platform default", async () => {
Expand Down
Loading