-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(adapters): add GCP VertexAI adapter
Signed-off-by: Akihiko Kuroda <akihikokuroda2020@gmail.com>
- Loading branch information
1 parent
b333594
commit 98ca4a9
Showing
11 changed files
with
610 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/** | ||
* Copyright 2024 IBM Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { | ||
AsyncStream, | ||
BaseLLMTokenizeOutput, | ||
ExecutionOptions, | ||
GenerateCallbacks, | ||
GenerateOptions, | ||
LLMCache, | ||
LLMMeta, | ||
} from "@/llms/base.js"; | ||
import { shallowCopy } from "@/serializer/utils.js"; | ||
import type { GetRunContext } from "@/context.js"; | ||
import { Emitter } from "@/emitter/emitter.js"; | ||
import { VertexAI, GenerativeModel } from "@google-cloud/vertexai"; | ||
import { ChatLLM, ChatLLMOutput } from "@/llms/chat.js"; | ||
import { BaseMessage, Role } from "@/llms/primitives/message.js"; | ||
import { signalRace } from "@/internals/helpers/promise.js"; | ||
import { processContentResponse, registerGenerativeModel } from "./utils.js"; | ||
|
||
export class VertexAIChatLLMOutput extends ChatLLMOutput { | ||
public readonly chunks: BaseMessage[] = []; | ||
|
||
constructor(chunk: BaseMessage) { | ||
super(); | ||
this.chunks.push(chunk); | ||
} | ||
|
||
get messages() { | ||
return this.chunks; | ||
} | ||
|
||
merge(other: VertexAIChatLLMOutput): void { | ||
this.chunks.push(...other.chunks); | ||
} | ||
|
||
getTextContent(): string { | ||
return this.chunks.map((result) => result.text).join(""); | ||
} | ||
|
||
toString() { | ||
return this.getTextContent(); | ||
} | ||
|
||
createSnapshot() { | ||
return { chunks: shallowCopy(this.chunks) }; | ||
} | ||
|
||
loadSnapshot(snapshot: typeof this.createSnapshot): void { | ||
Object.assign(this, snapshot); | ||
} | ||
} | ||
|
||
type VertexAIGenerateOptions = GenerateOptions; | ||
|
||
export interface VertexAIChatLLMInput { | ||
modelId: string; | ||
project: string; | ||
location: string; | ||
client?: GenerativeModel; | ||
executionOptions?: ExecutionOptions; | ||
cache?: LLMCache<VertexAIChatLLMOutput>; | ||
parameters?: Record<string, any>; | ||
} | ||
|
||
export class VertexAIChatLLM extends ChatLLM<VertexAIChatLLMOutput> { | ||
public readonly emitter: Emitter<GenerateCallbacks> = Emitter.root.child({ | ||
namespace: ["vertexai", "llm"], | ||
creator: this, | ||
}); | ||
|
||
protected client: GenerativeModel; | ||
|
||
constructor(protected readonly input: VertexAIChatLLMInput) { | ||
super(input.modelId, input.executionOptions, input.cache); | ||
const vertexAI = new VertexAI({ project: input.project, location: input.location }); | ||
this.client = | ||
input.client ?? | ||
vertexAI.getGenerativeModel({ | ||
model: input.modelId, | ||
}); | ||
} | ||
|
||
static { | ||
this.register(); | ||
registerGenerativeModel(); | ||
} | ||
|
||
async meta(): Promise<LLMMeta> { | ||
return { tokenLimit: Infinity }; | ||
} | ||
|
||
async tokenize(input: BaseMessage[]): Promise<BaseLLMTokenizeOutput> { | ||
const response = await this.client.countTokens({ | ||
contents: input.map((msg) => ({ parts: [{ text: msg.text }], role: msg.role })), | ||
}); | ||
return { | ||
tokensCount: response.totalTokens, | ||
}; | ||
} | ||
|
||
protected async _generate( | ||
input: BaseMessage[], | ||
options: VertexAIGenerateOptions, | ||
run: GetRunContext<this>, | ||
): Promise<VertexAIChatLLMOutput> { | ||
const response = await signalRace( | ||
() => | ||
this.client.generateContent({ | ||
contents: input.map((msg) => ({ parts: [{ text: msg.text }], role: msg.role })), | ||
}), | ||
run.signal, | ||
); | ||
const result = BaseMessage.of({ | ||
role: Role.ASSISTANT, | ||
text: processContentResponse(response.response), | ||
}); | ||
return new VertexAIChatLLMOutput(result); | ||
} | ||
|
||
protected async *_stream( | ||
input: BaseMessage[], | ||
options: VertexAIGenerateOptions | undefined, | ||
run: GetRunContext<this>, | ||
): AsyncStream<VertexAIChatLLMOutput, void> { | ||
const chat = this.client.startChat(); | ||
const response = await chat.sendMessageStream(input.map((msg) => msg.text)); | ||
for await (const chunk of await response.stream) { | ||
if (options?.signal?.aborted) { | ||
break; | ||
} | ||
const result = BaseMessage.of({ | ||
role: Role.ASSISTANT, | ||
text: processContentResponse(chunk), | ||
}); | ||
yield new VertexAIChatLLMOutput(result); | ||
} | ||
run.signal.throwIfAborted(); | ||
} | ||
|
||
createSnapshot() { | ||
return { | ||
...super.createSnapshot(), | ||
input: shallowCopy(this.input), | ||
client: this.client, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Copyright 2024 IBM Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { verifyDeserialization } from "@tests/e2e/utils.js"; | ||
import { VertexAILLM } from "@/adapters/vertexai/llm.js"; | ||
import { VertexAIChatLLM } from "@/adapters/vertexai/chat.js"; | ||
|
||
describe("VertexAI LLM", () => { | ||
const getInstance = () => { | ||
return new VertexAILLM({ | ||
modelId: "gemini-1.5-flash-001", | ||
location: "us-central1", | ||
project: "systemInstruction", | ||
}); | ||
}; | ||
|
||
it("Serializes", async () => { | ||
const instance = getInstance(); | ||
const serialized = instance.serialize(); | ||
const deserialized = VertexAILLM.fromSerialized(serialized); | ||
verifyDeserialization(instance, deserialized); | ||
}); | ||
}); | ||
|
||
describe("VertexAI ChatLLM", () => { | ||
const getInstance = () => { | ||
return new VertexAIChatLLM({ | ||
modelId: "gemini-1.5-flash-001", | ||
location: "us-central1", | ||
project: "systemInstruction", | ||
}); | ||
}; | ||
|
||
it("Serializes", async () => { | ||
const instance = getInstance(); | ||
const serialized = instance.serialize(); | ||
const deserialized = VertexAIChatLLM.fromSerialized(serialized); | ||
verifyDeserialization(instance, deserialized); | ||
}); | ||
}); |
Oops, something went wrong.