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-vertexai): Support Non-Google and Model Garden models in Vertex AI - Anthropic integration #6999

Merged
merged 97 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 88 commits
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
d0cc30f
Use earliest interface in the chain necessary.
afirstenberg Jun 16, 2024
db3cdd7
Add delete method
afirstenberg Jun 16, 2024
27031f4
Provide ways to get additional headers into the request.
afirstenberg Jun 16, 2024
49bb70a
Initial work on Blobs and BlobStores, including initial work on GCS
afirstenberg Jun 16, 2024
951175f
Fix typos and bugs on data fetch
afirstenberg Jun 17, 2024
7860630
Start of refactoring around native JS Blobs
afirstenberg Jun 19, 2024
07c92d6
Refactor how RawConnections are structured and used
afirstenberg Jun 20, 2024
30f228a
Switch to using "blob"
afirstenberg Jun 20, 2024
cfb1081
Refactor how RawConnections are structured and used
afirstenberg Jun 20, 2024
9ca6a31
Refactoring
afirstenberg Jun 20, 2024
d2f4a40
formatting
afirstenberg Jun 20, 2024
7550b51
Initial implementation of the MediaManager
afirstenberg Jun 20, 2024
87f8c13
Add options for store() that can be used to make sure URIs are set in…
afirstenberg Jun 21, 2024
39ffcb3
Add options for store() that can be used to make sure URIs are set in…
afirstenberg Jun 21, 2024
fd54f5e
Default implementation of hasValidPath
afirstenberg Jun 21, 2024
afd2f25
Tests. Bug fixes.
afirstenberg Jun 21, 2024
60589dd
Assorted name and type refactoring.
afirstenberg Jun 22, 2024
48ddbe9
Tests for MediaManager.
afirstenberg Jun 22, 2024
185229b
Fix tests
afirstenberg Jun 23, 2024
d3fcadb
Refactor Gemini API into a single, containable, thing.
afirstenberg Jun 23, 2024
f84cdaf
Remove obsolete comment
afirstenberg Jun 24, 2024
259ec12
Refactor
afirstenberg Jun 24, 2024
708a2fd
Add mediaManager to Gemini functions.
afirstenberg Jun 24, 2024
2467860
Testing MediaManager in chat functions in Vertex AI.
afirstenberg Jun 24, 2024
b394eec
Initial, incomplete, work on BlobStoreAIStudio
afirstenberg Jun 25, 2024
6098ee5
Basic BlobStoreAIStudio implementation and test
afirstenberg Jun 26, 2024
c4089e2
Report more error details in the exception
afirstenberg Jun 27, 2024
ebdf657
Testing MediaManager in chat functions in AI Studio.
afirstenberg Jun 27, 2024
8b7a18b
Merge branch 'main' into media-manager
afirstenberg Jun 27, 2024
5dd7164
Fix bug handling larger files
afirstenberg Jul 1, 2024
94fd6a4
Fix issues with video upload / processing.
afirstenberg Jul 1, 2024
ba5e4b7
Fix issues with the `api` attribute for ChatConnections
afirstenberg Jul 1, 2024
72d7f0c
Formatting
afirstenberg Jul 1, 2024
ebed89c
Leave response on error object.
afirstenberg Jul 2, 2024
1caebf1
Remove commented out stuff.
afirstenberg Jul 2, 2024
bd4c974
Make methods protected.
afirstenberg Jul 2, 2024
ee77ee2
Formatting
afirstenberg Jul 2, 2024
14f74a3
Move media and media_core into experimental status
afirstenberg Jul 2, 2024
da1817c
Formatting
afirstenberg Jul 2, 2024
7cf8e30
Merge branch 'main' into media-manager
afirstenberg Jul 5, 2024
9bd18c5
UUID Version support
afirstenberg Jul 5, 2024
aae6107
Formatting
afirstenberg Jul 5, 2024
c3e3677
Merge branch 'main' into media-manager
afirstenberg Jul 26, 2024
27e427c
Adjust to use a ReadThroughBlobStore instead of putting this logic in…
afirstenberg Jul 29, 2024
fcdba48
Implement MediaBlob serialization.
afirstenberg Jul 29, 2024
076c19c
Formatting
afirstenberg Jul 29, 2024
464bc2b
Merge branch 'main' into media-manager
afirstenberg Aug 1, 2024
e7e00ce
fix test
afirstenberg Aug 1, 2024
51b5eea
Remove unnecessary comments
afirstenberg Aug 1, 2024
f897e72
Add callback handler for request and response
afirstenberg Aug 2, 2024
bf20c32
Implement GooglAIAPI type
afirstenberg Aug 2, 2024
2eb973a
skeleton for Anthropic
afirstenberg Aug 19, 2024
b1e6f6d
Merge
jacoblee93 Aug 22, 2024
986a021
Fix lint, circular deps
jacoblee93 Aug 22, 2024
04d5c74
Use built-in serde properly
jacoblee93 Aug 22, 2024
101af7f
Merge branch 'main' into media-manager
jacoblee93 Aug 23, 2024
3f8a342
Fix build
jacoblee93 Aug 23, 2024
4add032
fix faulty import path
bracesproul Aug 23, 2024
18df7d9
Merge branch 'media-manager' into model-garden
afirstenberg Aug 25, 2024
85ea487
Move tests inside test suite.
afirstenberg Aug 26, 2024
fcc0a9b
Fix handling SAFETY error that contains no content or parts. Addresse…
afirstenberg Aug 26, 2024
4a80410
Improve error message in the event of a bad URI
afirstenberg Sep 11, 2024
29f0b1c
Fix tests
afirstenberg Sep 11, 2024
fde3717
Fix tests
afirstenberg Sep 11, 2024
3012736
Formatting
afirstenberg Sep 11, 2024
9fc8360
Merge branch 'media-manager' into model-garden-old
afirstenberg Sep 16, 2024
34150b1
Merge branch 'model-garden-old' into model-garden
afirstenberg Sep 16, 2024
4d898e2
Merge branch 'main' into model-garden
afirstenberg Sep 29, 2024
e6c2949
Initial API implementation for Anthropic.
afirstenberg Oct 17, 2024
503d8b8
Endpoint and location defaults per-model and better approaches to set…
afirstenberg Oct 23, 2024
cc6fe86
Add SSE streaming classes
afirstenberg Oct 31, 2024
16cf0b1
Add SSE streaming classes
afirstenberg Oct 31, 2024
c269911
Add streaming for Anthropic classes
afirstenberg Oct 31, 2024
f1f96b3
Formatting.
afirstenberg Oct 31, 2024
11702d3
Formatting.
afirstenberg Oct 31, 2024
8eb3ad7
Formatting.
afirstenberg Oct 31, 2024
2f917d7
Testing
afirstenberg Oct 31, 2024
c4023c7
Merge branch 'main' into model-garden
afirstenberg Oct 31, 2024
ab82f7a
Starting work on tool support. Incomplete.
afirstenberg Nov 1, 2024
92544f2
More work on tool support (insufficiently tested).
afirstenberg Nov 2, 2024
724017f
Test with different models.
afirstenberg Nov 2, 2024
22ebd1c
If the content for an AIMessageChunk is all text, then it should be a…
afirstenberg Nov 2, 2024
5023298
Fixes for function call streaming
afirstenberg Nov 2, 2024
00ecc03
Merge branch 'main' into model-garden
afirstenberg Nov 2, 2024
2f923d0
Formatting
afirstenberg Nov 2, 2024
28e6ac7
Documentation
afirstenberg Nov 3, 2024
b5d8225
Add enums for the various Gemini safety settings
afirstenberg Nov 4, 2024
3b1e7e8
Add enums for the various Gemini safety settings
afirstenberg Nov 4, 2024
bc58188
Removed commented out code.
afirstenberg Nov 5, 2024
fcb1b21
Make sure the entire chunk is logged, not just the text.
afirstenberg Nov 5, 2024
d75e3ee
formatting
afirstenberg Nov 5, 2024
4f02f0a
Fix undefined dereference. Issue #7171
afirstenberg Nov 7, 2024
54b08f4
Fix lint and build
jacoblee93 Nov 9, 2024
9a9248f
Merge branch 'main' of github.com:langchain-ai/langchainjs into 6999
jacoblee93 Nov 9, 2024
ff28f04
We can't use an enum for tree shaking reasons, as per @jacoblee93. Us…
afirstenberg Nov 11, 2024
73d4fc6
Update google_vertex_ai.ipynb
jacoblee93 Nov 11, 2024
b55f8cb
Fix lint
jacoblee93 Nov 11, 2024
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
4 changes: 3 additions & 1 deletion docs/core_docs/docs/integrations/chat/google_vertex_ai.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"source": [
"# ChatVertexAI\n",
"\n",
"[Google Vertex](https://cloud.google.com/vertex-ai) is a service that exposes all foundation models available in Google Cloud, like `gemini-1.5-pro`, `gemini-1.5-flash`, etc.\n",
"[Google Vertex](https://cloud.google.com/vertex-ai) is a service that exposes all foundation models available in Google Cloud, like `gemini-1.5-pro`, `gemini-1.5-flash`, etc.",
"As well as non-Google models such as [Anthropic's Claude](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude)."
"\n",
"\n",
"This will help you getting started with `ChatVertexAI` [chat models](/docs/concepts/chat_models). For detailed documentation of all `ChatVertexAI` features and configurations head to the [API reference](https://api.js.langchain.com/classes/langchain_google_vertexai.ChatVertexAI.html).\n",
"\n",
Expand Down
13 changes: 12 additions & 1 deletion docs/core_docs/docs/integrations/platforms/google.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Functionality related to [Google Cloud Platform](https://cloud.google.com/)

### Gemini Models

Access Gemini models such as `gemini-pro` and `gemini-pro-vision` through the [`ChatGoogleGenerativeAI`](/docs/integrations/chat/google_generativeai),
Access Gemini models such as `gemini-1.5-pro` and `gemini-1.5-flex` through the [`ChatGoogleGenerativeAI`](/docs/integrations/chat/google_generativeai),
or if using VertexAI, via the [`ChatVertexAI`](/docs/integrations/chat/google_vertex_ai) class.

import Tabs from "@theme/Tabs";
Expand Down Expand Up @@ -153,6 +153,17 @@ Click [here](/docs/integrations/chat/google_vertex_ai) for the `@langchain/googl

The value of `image_url` must be a base64 encoded image (e.g., `data:image/png;base64,abcd124`).

### Non-Gemini Models

See above for setting up authentication through Vertex AI to use these models.

[Anthropic](/docs/integrations/chat/anthropic) Claude models are also available through
the [Vertex AI](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude)
platform. See [here](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude)
for more information about enabling access to the models and the model names to use.

PaLM models are no longer supported.

## Vector Store

### Vertex AI Vector Search
Expand Down
109 changes: 32 additions & 77 deletions libs/langchain-google-common/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ import {
GoogleAISafetySetting,
GoogleConnectionParams,
GooglePlatformType,
GeminiContent,
GeminiTool,
GoogleAIBaseLanguageModelCallOptions,
GoogleAIAPI,
GoogleAIAPIParams,
} from "./types.js";
import {
convertToGeminiTools,
copyAIModelParams,
copyAndValidateModelParamsInto,
} from "./utils/common.js";
import { AbstractGoogleLLMConnection } from "./connection.js";
import { DefaultGeminiSafetyHandler } from "./utils/gemini.js";
import { DefaultGeminiSafetyHandler, getGeminiAPI } from "./utils/gemini.js";
import { ApiKeyGoogleAuth, GoogleAbstractedClient } from "./auth.js";
import { JsonStream } from "./utils/stream.js";
import { ensureParams } from "./utils/failed_handler.js";
Expand Down Expand Up @@ -96,71 +97,21 @@ export class ChatConnection<AuthOptions> extends AbstractGoogleLLMConnection<
return true;
}

async formatContents(
input: BaseMessage[],
_parameters: GoogleAIModelParams
): Promise<GeminiContent[]> {
const inputPromises: Promise<GeminiContent[]>[] = input.map((msg, i) =>
this.api.baseMessageToContent(
msg,
input[i - 1],
this.useSystemInstruction
)
);
const inputs = await Promise.all(inputPromises);

return inputs.reduce((acc, cur) => {
// Filter out the system content
if (cur.every((content) => content.role === "system")) {
return acc;
}

// Combine adjacent function messages
if (
cur[0]?.role === "function" &&
acc.length > 0 &&
acc[acc.length - 1].role === "function"
) {
acc[acc.length - 1].parts = [
...acc[acc.length - 1].parts,
...cur[0].parts,
];
} else {
acc.push(...cur);
}

return acc;
}, [] as GeminiContent[]);
buildGeminiAPI(): GoogleAIAPI {
const geminiConfig: GeminiAPIConfig = {
useSystemInstruction: this.useSystemInstruction,
...(this.apiConfig as GeminiAPIConfig),
};
return getGeminiAPI(geminiConfig);
}

async formatSystemInstruction(
input: BaseMessage[],
_parameters: GoogleAIModelParams
): Promise<GeminiContent> {
if (!this.useSystemInstruction) {
return {} as GeminiContent;
get api(): GoogleAIAPI {
switch (this.apiName) {
case "google":
return this.buildGeminiAPI();
default:
return super.api;
}

let ret = {} as GeminiContent;
for (let index = 0; index < input.length; index += 1) {
const message = input[index];
if (message._getType() === "system") {
// For system types, we only want it if it is the first message,
// if it appears anywhere else, it should be an error.
if (index === 0) {
// eslint-disable-next-line prefer-destructuring
ret = (
await this.api.baseMessageToContent(message, undefined, true)
)[0];
} else {
throw new Error(
"System messages are only permitted as the first passed message."
);
}
}
}

return ret;
}
}

Expand All @@ -172,7 +123,7 @@ export interface ChatGoogleBaseInput<AuthOptions>
GoogleConnectionParams<AuthOptions>,
GoogleAIModelParams,
GoogleAISafetyParams,
GeminiAPIConfig,
GoogleAIAPIParams,
Pick<GoogleAIBaseLanguageModelCallOptions, "streamUsage"> {}

/**
Expand Down Expand Up @@ -341,12 +292,10 @@ export abstract class ChatGoogleBase<AuthOptions>
const response = await this.connection.request(
messages,
parameters,
options
);
const ret = this.connection.api.safeResponseToChatResult(
response,
this.safetyHandler
options,
runManager
);
const ret = this.connection.api.responseToChatResult(response);
await runManager?.handleLLMNewToken(ret.generations[0].text);
return ret;
}
Expand All @@ -361,7 +310,8 @@ export abstract class ChatGoogleBase<AuthOptions>
const response = await this.streamedConnection.request(
_messages,
parameters,
options
options,
runManager
);

// Get the streaming parser of the response
Expand All @@ -372,6 +322,12 @@ export abstract class ChatGoogleBase<AuthOptions>
// that is either available or added to the queue
while (!stream.streamDone) {
const output = await stream.nextChunk();
await runManager?.handleCustomEvent(
`google-chunk-${this.constructor.name}`,
{
output,
}
);
if (
output &&
output.usageMetadata &&
Expand All @@ -386,10 +342,7 @@ export abstract class ChatGoogleBase<AuthOptions>
}
const chunk =
output !== null
? this.connection.api.safeResponseToChatGeneration(
{ data: output },
this.safetyHandler
)
? this.connection.api.responseToChatGeneration({ data: output })
: new ChatGenerationChunk({
text: "",
generationInfo: { finishReason: "stop" },
Expand All @@ -398,8 +351,10 @@ export abstract class ChatGoogleBase<AuthOptions>
usage_metadata: usageMetadata,
}),
});
yield chunk;
await runManager?.handleLLMNewToken(chunk.text);
if (chunk) {
yield chunk;
await runManager?.handleLLMNewToken(chunk.text);
}
}
}

Expand Down
Loading