-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(py/vertexai): Enchance VertexAI plugin
- Loading branch information
Showing
10 changed files
with
139 additions
and
27 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
35 changes: 35 additions & 0 deletions
35
py/plugins/vertex-ai/src/genkit/plugins/vertex_ai/mixins.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,35 @@ | ||
# Copyright 2025 Google LLC | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import logging | ||
|
||
from genkit.core.typing import GenerateRequest, TextPart | ||
from vertexai.generative_models import Content, Part | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
class VertexAIMixin: | ||
@staticmethod | ||
def build_prompt(request: GenerateRequest) -> str: | ||
prompt = [] | ||
for message in request.messages: | ||
for text_part in message.content: | ||
if isinstance(text_part.root, TextPart): | ||
prompt.append(text_part.root.text) | ||
else: | ||
LOG.error('Non-text messages are not supported') | ||
return ' '.join(prompt) | ||
|
||
@staticmethod | ||
def build_messages(request: GenerateRequest) -> list[Content]: | ||
messages: list[Content] = [] | ||
for message in request.messages: | ||
parts: list[Part] = [] | ||
for text_part in message.content: | ||
if isinstance(text_part.root, TextPart): | ||
parts.append(Part.from_text(text_part.root.text)) | ||
else: | ||
LOG.error('Non-text messages are not supported') | ||
messages.append(Content(role=message.role.value, parts=parts)) | ||
return messages |
13 changes: 13 additions & 0 deletions
13
py/plugins/vertex-ai/src/genkit/plugins/vertex_ai/options.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,13 @@ | ||
# Copyright 2025 Google LLC | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class ImagenOptions(BaseModel): | ||
prompt: str | ||
number_of_images: int | ||
language: str = ('en',) | ||
aspect_ratio: str = ('1:1',) | ||
safety_filter_level: str = ('block_some',) | ||
person_generation: str = ('allow_adult',) |
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,62 @@ | ||
# Copyright 2025 Google LLC | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import pytest | ||
from genkit.core.typing import GenerateRequest, Message, Role, TextPart | ||
from genkit.plugins.vertex_ai.mixins import VertexAIMixin | ||
|
||
MULTILINE_CONTENT = [ | ||
'Hi!', | ||
'I have a question for you.', | ||
'Where can I read a Genkit documentation?', | ||
] | ||
|
||
|
||
@pytest.fixture | ||
def setup_request(): | ||
request = GenerateRequest( | ||
messages=[ | ||
Message( | ||
role=Role.USER, | ||
content=[TextPart(text=x) for x in MULTILINE_CONTENT], | ||
), | ||
], | ||
) | ||
|
||
return request | ||
|
||
|
||
def test_create_prompt(setup_request): | ||
request = setup_request | ||
result = VertexAIMixin.build_prompt(request) | ||
expected = ' '.join(MULTILINE_CONTENT) | ||
assert isinstance(result, str) | ||
assert result == expected | ||
|
||
|
||
def test_built_gemini_message_multiple_parts(setup_request): | ||
request = setup_request | ||
result = VertexAIMixin.build_messages(request) | ||
assert isinstance(result, list) | ||
assert isinstance(result[0].parts, list) | ||
assert len(result[0].parts) == len(MULTILINE_CONTENT) | ||
|
||
for part, text in zip(result[0].parts, MULTILINE_CONTENT): | ||
assert part.text == text | ||
|
||
|
||
def test_built_gemini_message_multiple_messages(): | ||
request = GenerateRequest( | ||
messages=[ | ||
Message( | ||
role=Role.USER, | ||
content=[TextPart(text=text)], | ||
) | ||
for text in MULTILINE_CONTENT | ||
], | ||
) | ||
result = VertexAIMixin.build_messages(request) | ||
assert isinstance(result, list) | ||
assert len(result) == len(MULTILINE_CONTENT) | ||
for message, text in zip(result, MULTILINE_CONTENT): | ||
assert message.parts[0].text == text |
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