Skip to content

feat(google): consolidate Google packages into unified @langchain/google package #8393

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions libs/langchain-google-common/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export abstract class GoogleAbstractedFetchClient

abstract request(opts: GoogleAbstractedClientOps): unknown;

_fetch: typeof fetch = fetch;
abstract fetch(...args: Parameters<typeof fetch>): Promise<Response>;

async _buildData(res: Response, opts: GoogleAbstractedClientOps) {
switch (opts.responseType) {
Expand Down Expand Up @@ -67,7 +67,7 @@ export abstract class GoogleAbstractedFetchClient
}
}

const res = await this._fetch(url, fetchOptions);
const res = await this.fetch(url, fetchOptions);

if (!res.ok) {
const resText = await res.text();
Expand Down Expand Up @@ -106,6 +106,10 @@ export class ApiKeyGoogleAuth extends GoogleAbstractedFetchClient {
this.apiKey = apiKey;
}

async fetch(...args: Parameters<typeof fetch>) {
return fetch(...args);
}

get clientType(): string {
return "apiKey";
}
Expand Down
5 changes: 2 additions & 3 deletions libs/langchain-google-gauth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,15 @@
"author": "LangChain",
"license": "MIT",
"dependencies": {
"@langchain/google-common": "^0.2.13",
"google-auth-library": "^10.1.0"
"@langchain/google": "workspace:*"
},
"peerDependencies": {
"@langchain/core": ">=0.3.58 <0.4.0"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
"@langchain/core": "workspace:*",
"@langchain/scripts": ">=0.1.0 <0.2.0",
"@langchain/scripts": "workspace:*",
"@swc/core": "^1.3.90",
"@swc/jest": "^0.2.29",
"@tsconfig/recommended": "^1.0.3",
Expand Down
109 changes: 11 additions & 98 deletions libs/langchain-google-gauth/src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,98 +1,11 @@
import { Readable } from "stream";
import {
AbstractStream,
ensureAuthOptionScopes,
GoogleAbstractedClientOps,
GoogleAbstractedFetchClient,
GoogleConnectionParams,
JsonStream,
SseJsonStream,
SseStream,
} from "@langchain/google-common";
import { GoogleAuth, GoogleAuthOptions } from "google-auth-library";

export class NodeAbstractStream implements AbstractStream {
private baseStream: AbstractStream;

constructor(baseStream: AbstractStream, data: Readable) {
this.baseStream = baseStream;
const decoder = new TextDecoder("utf-8");
data.on("data", (data) => {
const text = decoder.decode(data, { stream: true });
this.appendBuffer(text);
});
data.on("end", () => {
const rest = decoder.decode();
this.appendBuffer(rest);
this.closeBuffer();
});
}

appendBuffer(data: string): void {
return this.baseStream.appendBuffer(data);
}

closeBuffer(): void {
return this.baseStream.closeBuffer();
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
nextChunk(): Promise<any> {
return this.baseStream.nextChunk();
}

get streamDone(): boolean {
return this.baseStream.streamDone;
}
}

export class NodeJsonStream extends NodeAbstractStream {
constructor(data: Readable) {
super(new JsonStream(), data);
}
}

export class NodeSseStream extends NodeAbstractStream {
constructor(data: Readable) {
super(new SseStream(), data);
}
}

export class NodeSseJsonStream extends NodeAbstractStream {
constructor(data: Readable) {
super(new SseJsonStream(), data);
}
}

export class GAuthClient extends GoogleAbstractedFetchClient {
gauth: GoogleAuth;

constructor(fields?: GoogleConnectionParams<GoogleAuthOptions>) {
super();
const options = ensureAuthOptionScopes<GoogleAuthOptions>(
fields?.authOptions,
"scopes",
fields?.platformType
);
this.gauth = new GoogleAuth(options);
this._fetch = async (...args) => {
const url = args[0];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const opts: any = args[1] ?? {};
opts.responseType = "stream";
return await this.gauth.fetch(url, opts);
};
}

get clientType(): string {
return "gauth";
}

async getProjectId(): Promise<string> {
return this.gauth.getProjectId();
}

async request(opts: GoogleAbstractedClientOps): Promise<unknown> {
return this._request(opts?.url, opts, {});
}
}
/**
* The implementation of this file was consolidated into the @langchain/google package.
* This file is kept here for backwards compatibility.
*/
export {
NodeAbstractStream,
NodeJsonStream,
NodeSseStream,
NodeSseJsonStream,
GAuthClient,
} from "@langchain/google";
38 changes: 3 additions & 35 deletions libs/langchain-google-gauth/src/chat_models.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,5 @@
import {
ChatGoogleBase,
ChatGoogleBaseInput,
GoogleAbstractedClient,
GoogleBaseLLMInput,
} from "@langchain/google-common";
import { GoogleAuthOptions } from "google-auth-library";
import { GAuthClient } from "./auth.js";

/**
* Input to chat model class.
* The implementation of this file was consolidated into the @langchain/google package.
* This file is kept here for backwards compatibility.
*/
export interface ChatGoogleInput
extends ChatGoogleBaseInput<GoogleAuthOptions> {}

/**
* Integration with a Google chat model.
*/
export class ChatGoogle
extends ChatGoogleBase<GoogleAuthOptions>
implements ChatGoogleInput
{
// Used for tracing, replace with the same name as your class
static lc_name() {
return "ChatGoogle";
}

constructor(fields?: ChatGoogleInput) {
super(fields);
}

buildAbstractedClient(
fields: GoogleBaseLLMInput<GoogleAuthOptions> | undefined
): GoogleAbstractedClient {
return new GAuthClient(fields);
}
}
export { ChatGoogleInput, ChatGoogle } from "@langchain/google";
40 changes: 3 additions & 37 deletions libs/langchain-google-gauth/src/embeddings.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,5 @@
import {
GoogleAbstractedClient,
GoogleConnectionParams,
BaseGoogleEmbeddings,
BaseGoogleEmbeddingsParams,
} from "@langchain/google-common";
import { GoogleAuthOptions } from "google-auth-library";
import { GAuthClient } from "./auth.js";

/**
* Input to LLM class.
* The implementation of this file was consolidated into the @langchain/google package.
* This file is kept here for backwards compatibility.
*/
export interface GoogleEmbeddingsInput
extends BaseGoogleEmbeddingsParams<GoogleAuthOptions> {}

/**
* Integration with an Google embeddings model.
*/
export class GoogleEmbeddings
extends BaseGoogleEmbeddings<GoogleAuthOptions>
implements GoogleEmbeddingsInput
{
// Used for tracing, replace with the same name as your class
static lc_name() {
return "GoogleEmbeddings";
}

lc_serializable = true;

constructor(fields: GoogleEmbeddingsInput) {
super(fields);
}

buildAbstractedClient(
fields?: GoogleConnectionParams<GoogleAuthOptions>
): GoogleAbstractedClient {
return new GAuthClient(fields);
}
}
export { GoogleEmbeddingsInput, GoogleEmbeddings } from "@langchain/google";
38 changes: 3 additions & 35 deletions libs/langchain-google-gauth/src/llms.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,5 @@
import {
GoogleAbstractedClient,
GoogleBaseLLM,
GoogleBaseLLMInput,
} from "@langchain/google-common";
import { GoogleAuthOptions } from "google-auth-library";
import { GAuthClient } from "./auth.js";

/**
* Input to LLM class.
* The implementation of this file was consolidated into the @langchain/google package.
* This file is kept here for backwards compatibility.
*/
export interface GoogleLLMInput extends GoogleBaseLLMInput<GoogleAuthOptions> {}

/**
* Integration with a Google LLM.
*/
export class GoogleLLM
extends GoogleBaseLLM<GoogleAuthOptions>
implements GoogleLLMInput
{
// Used for tracing, replace with the same name as your class
static lc_name() {
return "GoogleLLM";
}

lc_serializable = true;

constructor(fields?: GoogleLLMInput) {
super(fields);
}

buildAbstractedClient(
fields: GoogleBaseLLMInput<GoogleAuthOptions> | undefined
): GoogleAbstractedClient {
return new GAuthClient(fields);
}
}
export { GoogleLLMInput, GoogleLLM } from "@langchain/google";
41 changes: 10 additions & 31 deletions libs/langchain-google-gauth/src/media.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,10 @@
import { GoogleAbstractedClient } from "@langchain/google-common";
import {
BlobStoreGoogleCloudStorageBase,
BlobStoreGoogleCloudStorageBaseParams,
BlobStoreAIStudioFileBase,
BlobStoreAIStudioFileBaseParams,
} from "@langchain/google-common/experimental/media";
import { GoogleAuthOptions } from "google-auth-library";
import { GAuthClient } from "./auth.js";

export interface BlobStoreGoogleCloudStorageParams
extends BlobStoreGoogleCloudStorageBaseParams<GoogleAuthOptions> {}

export class BlobStoreGoogleCloudStorage extends BlobStoreGoogleCloudStorageBase<GoogleAuthOptions> {
buildClient(
fields?: BlobStoreGoogleCloudStorageParams
): GoogleAbstractedClient {
return new GAuthClient(fields);
}
}

export interface BlobStoreAIStudioFileParams
extends BlobStoreAIStudioFileBaseParams<GoogleAuthOptions> {}

export class BlobStoreAIStudioFile extends BlobStoreAIStudioFileBase<GoogleAuthOptions> {
buildAbstractedClient(
fields?: BlobStoreAIStudioFileParams
): GoogleAbstractedClient {
return new GAuthClient(fields);
}
}
/**
* The implementation of this file was consolidated into the @langchain/google package.
* This file is kept here for backwards compatibility.
*/
export {
BlobStoreGoogleCloudStorageParams,
BlobStoreGoogleCloudStorage,
BlobStoreAIStudioFileParams,
BlobStoreAIStudioFile,
} from "@langchain/google";
2 changes: 1 addition & 1 deletion libs/langchain-google-gauth/src/types.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "@langchain/google-common/types";
export * from "@langchain/google/types";
2 changes: 1 addition & 1 deletion libs/langchain-google-gauth/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "@langchain/google-common/utils";
export * from "@langchain/google/utils";
8 changes: 4 additions & 4 deletions libs/langchain-google-vertexai-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@
"author": "LangChain",
"license": "MIT",
"dependencies": {
"@langchain/google-webauth": "^0.2.13"
"@langchain/google": "workspace:*"
},
"peerDependencies": {
"@langchain/core": ">=0.3.58 <0.4.0"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
"@langchain/core": "workspace:*",
"@langchain/google-common": "^0.2.13",
"@langchain/scripts": ">=0.1.0 <0.2.0",
"@langchain/standard-tests": "0.0.0",
"@langchain/google-common": "workspace:*",
"@langchain/scripts": "workspace:*",
"@langchain/standard-tests": "workspace:*",
"@swc/core": "^1.3.90",
"@swc/jest": "^0.2.29",
"@tsconfig/recommended": "^1.0.3",
Expand Down
Loading