-
Notifications
You must be signed in to change notification settings - Fork 228
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 Google VertexAI support (#469)
Ref #468
- Loading branch information
Showing
9 changed files
with
198 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright 2025 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. | ||
|
||
|
15 changes: 15 additions & 0 deletions
15
python/beeai_framework/adapters/vertexai/backend/__init__.py
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,15 @@ | ||
# Copyright 2025 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. | ||
|
||
|
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,50 @@ | ||
# Copyright 2025 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 os | ||
|
||
from beeai_framework.adapters.litellm.chat import LiteLLMChatModel | ||
from beeai_framework.backend.constants import ProviderName | ||
from beeai_framework.utils.custom_logger import BeeLogger | ||
|
||
logger = BeeLogger(__name__) | ||
|
||
|
||
class VertexAIChatModel(LiteLLMChatModel): | ||
@property | ||
def provider_id(self) -> ProviderName: | ||
return "vertexai" | ||
|
||
def __init__(self, model_id: str | None = None, settings: dict | None = None) -> None: | ||
_settings = settings.copy() if settings is not None else {} | ||
|
||
vertexai_project = _settings.get("vertexai_project", os.getenv("VERTEXAI_PROJECT")) | ||
if not vertexai_project: | ||
raise ValueError( | ||
"Project ID is required for Vertex AI model. Specify *vertexai_project* " | ||
+ "or set VERTEXAI_PROJECT environment variable" | ||
) | ||
|
||
# Ensure standard google auth credentials are available | ||
# Set GOOGLE_APPLICATION_CREDENTIALS / GOOGLE_CREDENTIALS / GOOGLE_APPLICATION_CREDENTIALS_JSON | ||
|
||
super().__init__( | ||
model_id if model_id else os.getenv("VERTEXAI_CHAT_MODEL", "geminid-2.0-flash-lite-001"), | ||
provider_id="vertex_ai", | ||
settings=_settings | ||
| { | ||
"vertex_project": vertexai_project, | ||
}, | ||
) |
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,98 @@ | ||
import asyncio | ||
from typing import Any | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from beeai_framework.adapters.vertexai.backend.chat import VertexAIChatModel | ||
from beeai_framework.backend.chat import ChatModel | ||
from beeai_framework.backend.message import UserMessage | ||
from beeai_framework.cancellation import AbortSignal | ||
from beeai_framework.emitter import EventMeta | ||
from beeai_framework.errors import AbortError | ||
from beeai_framework.parsers.field import ParserField | ||
from beeai_framework.parsers.line_prefix import LinePrefixParser, LinePrefixParserNode | ||
|
||
|
||
async def vertexai_from_name() -> None: | ||
llm = ChatModel.from_name("vertexai:gemini-2.0-flash-lite-001") | ||
user_message = UserMessage("what states are part of New England?") | ||
response = await llm.create(messages=[user_message]) | ||
print(response.get_text_content()) | ||
|
||
|
||
async def vertexai_sync() -> None: | ||
llm = VertexAIChatModel("gemini-2.0-flash-lite-001") | ||
user_message = UserMessage("what is the capital of Massachusetts?") | ||
response = await llm.create(messages=[user_message]) | ||
print(response.get_text_content()) | ||
|
||
|
||
async def vertexai_stream() -> None: | ||
llm = VertexAIChatModel("gemini-2.0-flash-lite-001") | ||
user_message = UserMessage("How many islands make up the country of Cape Verde?") | ||
response = await llm.create(messages=[user_message], stream=True) | ||
print(response.get_text_content()) | ||
|
||
|
||
async def vertexai_stream_abort() -> None: | ||
llm = VertexAIChatModel("gemini-2.0-flash-lite-001") | ||
user_message = UserMessage("What is the smallest of the Cape Verde islands?") | ||
|
||
try: | ||
response = await llm.create(messages=[user_message], stream=True, abort_signal=AbortSignal.timeout(0.5)) | ||
|
||
if response is not None: | ||
print(response.get_text_content()) | ||
else: | ||
print("No response returned.") | ||
except AbortError as err: | ||
print(f"Aborted: {err}") | ||
|
||
|
||
async def vertexai_structure() -> None: | ||
class TestSchema(BaseModel): | ||
answer: str = Field(description="your final answer") | ||
|
||
llm = VertexAIChatModel("gemini-2.0-flash-lite-001") | ||
user_message = UserMessage("How many islands make up the country of Cape Verde?") | ||
response = await llm.create_structure(schema=TestSchema, messages=[user_message]) | ||
print(response.object) | ||
|
||
|
||
async def vertexai_stream_parser() -> None: | ||
llm = VertexAIChatModel("gemini-2.0-flash-lite-001") | ||
|
||
parser = LinePrefixParser( | ||
nodes={ | ||
"test": LinePrefixParserNode( | ||
prefix="Prefix: ", field=ParserField.from_type(str), is_start=True, is_end=True | ||
) | ||
} | ||
) | ||
|
||
async def on_new_token(data: dict[str, Any], event: EventMeta) -> None: | ||
await parser.add(data["value"].get_text_content()) | ||
|
||
user_message = UserMessage("Produce 3 lines each starting with 'Prefix: ' followed by a sentence and a new line.") | ||
await llm.create(messages=[user_message], stream=True).observe(lambda emitter: emitter.on("newToken", on_new_token)) | ||
result = await parser.end() | ||
print(result) | ||
|
||
|
||
async def main() -> None: | ||
print("*" * 10, "vertexai_from_name") | ||
await vertexai_from_name() | ||
print("*" * 10, "vertexai_sync") | ||
await vertexai_sync() | ||
print("*" * 10, "vertexai_stream") | ||
await vertexai_stream() | ||
print("*" * 10, "vertexai_stream_abort") | ||
await vertexai_stream_abort() | ||
print("*" * 10, "vertexai_structure") | ||
await vertexai_structure() | ||
print("*" * 10, "vertexai_stream_parser") | ||
await vertexai_stream_parser() | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
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