feat(model/multiembedding): support async call#57
Conversation
There was a problem hiding this comment.
Summary of Changes
Hello @kevinlin09, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request enhances the multimodal embedding feature by adding asynchronous support. It introduces an AioMultiModalEmbedding class with an async call method, enabling developers to perform embedding requests without blocking the event loop. This improvement is crucial for building more responsive and performant applications that leverage asynchronous programming paradigms.
Highlights
- Asynchronous MultiModal Embedding: Introduced a new
AioMultiModalEmbeddingclass and anasync callmethod to support asynchronous multimodal embedding operations, allowing for non-blocking execution. - API Exposure: The new asynchronous multimodal embedding functionality is now exposed at the top-level
dashscopepackage for convenient access. - Example Usage: A new sample file has been added to demonstrate how to effectively use both the synchronous and the newly introduced asynchronous multimodal embedding APIs.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Code Review
This pull request adds asynchronous support for multi-modal embeddings by introducing the AioMultiModalEmbedding class. The changes are well-structured, including the addition of a new async API class and a corresponding example file. My review focuses on improving code maintainability by reducing duplication in both the new API class and the sample code, and on enhancing code clarity and robustness through docstring corrections, more accurate type hints, and consistent class variable access.
| @classmethod | ||
| async def call(cls, | ||
| model: str, | ||
| input: List[MultiModalEmbeddingItemBase], |
There was a problem hiding this comment.
The type hint for input is List[MultiModalEmbeddingItemBase]. However, the provided test code uses input = [{'image': image}], which is a List[dict]. Furthermore, MultiModalEmbeddingItemBase and its subclasses require a factor parameter during initialization, which is not present in the test data. To avoid confusion and accurately reflect the accepted types, consider changing the type hint to List[dict].
| input: List[MultiModalEmbeddingItemBase], | |
| input: List[dict], |
|
|
||
| Args: | ||
| model (str): The embedding model name. | ||
| input (List[MultiModalEmbeddingElement]): The embedding elements, |
There was a problem hiding this comment.
The type hint for the input parameter in the docstring is List[MultiModalEmbeddingElement], but MultiModalEmbeddingElement is not defined. The method signature uses List[MultiModalEmbeddingItemBase], and as noted in another comment, List[dict] would be even more accurate. Please correct the docstring for consistency and clarity.
| input (List[MultiModalEmbeddingElement]): The embedding elements, | |
| input (List[dict]): The embedding elements, |
| model=model, | ||
| input=embedding_input, | ||
| task_group=task_group, | ||
| task=MultiModalEmbedding.task, |
There was a problem hiding this comment.
In the call to super().call, the task parameter is set using MultiModalEmbedding.task. It is better practice to use cls.task to refer to the task defined in the current class. This improves code consistency and makes it more robust, for instance, if the class is subclassed.
| task=MultiModalEmbedding.task, | |
| task=cls.task, |
| @classmethod | ||
| def _preprocess_message_inputs(cls, model: str, input: List[dict], | ||
| api_key: str): | ||
| """preprocess following inputs | ||
| input = [{'factor': 1, 'text': 'hello'}, | ||
| {'factor': 2, 'audio': ''}, | ||
| {'factor': 3, 'image': ''}] | ||
| """ | ||
| has_upload = False | ||
| for elem in input: | ||
| if not isinstance(elem, (int, float, bool, str, bytes, bytearray)): | ||
| is_upload = preprocess_message_element(model, elem, api_key) | ||
| if is_upload and not has_upload: | ||
| has_upload = True | ||
| return has_upload |
There was a problem hiding this comment.
This method _preprocess_message_inputs is a duplicate of the one in the MultiModalEmbedding class. To improve maintainability and avoid code duplication, you should remove this method and call the existing one from MultiModalEmbedding within the AioMultiModalEmbedding.call method.
For example, in AioMultiModalEmbedding.call, you can change line 149 to:
has_upload = MultiModalEmbedding._preprocess_message_inputs(model, input, api_key)| def test_multimodal_embedding(): | ||
| input = [{'image': image}] | ||
| # 调用模型接口 | ||
| resp = dashscope.MultiModalEmbedding.call( | ||
| model="multimodal-embedding-v1", | ||
| input=input | ||
| ) | ||
|
|
||
| if resp.status_code == HTTPStatus.OK: | ||
| result = { | ||
| "status_code": resp.status_code, | ||
| "request_id": getattr(resp, "request_id", ""), | ||
| "code": getattr(resp, "code", ""), | ||
| "message": getattr(resp, "message", ""), | ||
| "output": resp.output, | ||
| "usage": resp.usage | ||
| } | ||
| print(json.dumps(result, ensure_ascii=False, indent=4)) | ||
|
|
||
| async def test_aio_multimodal_embedding(): | ||
| input = [{'image': image}] | ||
| # 调用模型接口 | ||
| resp = await dashscope.AioMultiModalEmbedding.call( | ||
| model="multimodal-embedding-v1", | ||
| input=input | ||
| ) | ||
|
|
||
| if resp.status_code == HTTPStatus.OK: | ||
| result = { | ||
| "status_code": resp.status_code, | ||
| "request_id": getattr(resp, "request_id", ""), | ||
| "code": getattr(resp, "code", ""), | ||
| "message": getattr(resp, "message", ""), | ||
| "output": resp.output, | ||
| "usage": resp.usage | ||
| } | ||
| print(json.dumps(result, ensure_ascii=False, indent=4)) |
There was a problem hiding this comment.
The response handling logic in test_multimodal_embedding and test_aio_multimodal_embedding is identical. You can refactor this duplicated code into a helper function to improve readability and maintainability. For example:
def _print_response(resp):
if resp.status_code == HTTPStatus.OK:
result = {
"status_code": resp.status_code,
"request_id": getattr(resp, "request_id", ""),
"code": getattr(resp, "code", ""),
"message": getattr(resp, "message", ""),
"output": resp.output,
"usage": resp.usage
}
print(json.dumps(result, ensure_ascii=False, indent=4))
def test_multimodal_embedding():
input = [{'image': image}]
# 调用模型接口
resp = dashscope.MultiModalEmbedding.call(
model="multimodal-embedding-v1",
input=input
)
_print_response(resp)
async def test_aio_multimodal_embedding():
input = [{'image': image}]
# 调用模型接口
resp = await dashscope.AioMultiModalEmbedding.call(
model="multimodal-embedding-v1",
input=input
)
_print_response(resp)
No description provided.