-
Notifications
You must be signed in to change notification settings - Fork 767
Support vision input for Planner #472
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
958283a
add image reader
liqul 27baa03
refactor for clear
liqul ebe185c
Merge branch 'main' of https://github.com/microsoft/TaskWeaver into u…
liqul 4cc25cb
image message display
liqul 1f87ca4
remove progagate
liqul 634f4b6
rename
liqul f1f305e
rename
liqul 06e2c80
change default roles
liqul 50f3232
reset default roles
liqul 586324a
add doc
liqul fccf2fa
ujpdate doc
liqul e27a9ca
update readme
liqul 26f2848
Update taskweaver/ext_role/image_reader/image_reader.py
liqul b5b8d12
Update taskweaver/ext_role/image_reader/image_reader.py
liqul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Empty file.
This file contains hidden or 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,113 @@ | ||
| import base64 | ||
| import json | ||
| import os.path | ||
| from mimetypes import guess_type | ||
|
|
||
| from injector import inject | ||
|
|
||
| from taskweaver.llm import LLMApi, format_chat_message | ||
| from taskweaver.logging import TelemetryLogger | ||
| from taskweaver.memory import Memory, Post | ||
| from taskweaver.memory.attachment import AttachmentType | ||
| from taskweaver.module.event_emitter import SessionEventEmitter | ||
| from taskweaver.module.tracing import Tracing | ||
| from taskweaver.role import Role | ||
| from taskweaver.role.role import RoleConfig, RoleEntry | ||
| from taskweaver.session import SessionMetadata | ||
|
|
||
|
|
||
| # Function to encode a local image into data URL | ||
| def local_image_to_data_url(image_path): | ||
| # Guess the MIME type of the image based on the file extension | ||
| mime_type, _ = guess_type(image_path) | ||
| if mime_type is None: | ||
| mime_type = "application/octet-stream" # Default MIME type if none is found | ||
|
|
||
| # Read and encode the image file | ||
| with open(image_path, "rb") as image_file: | ||
| base64_encoded_data = base64.b64encode(image_file.read()).decode("utf-8") | ||
|
|
||
| # Construct the data URL | ||
| return f"data:{mime_type};base64,{base64_encoded_data}" | ||
|
|
||
|
|
||
| class ImageReaderConfig(RoleConfig): | ||
| def _configure(self): | ||
| pass | ||
|
|
||
|
|
||
| class ImageReader(Role): | ||
| @inject | ||
| def __init__( | ||
| self, | ||
| config: ImageReaderConfig, | ||
| logger: TelemetryLogger, | ||
| tracing: Tracing, | ||
| event_emitter: SessionEventEmitter, | ||
| role_entry: RoleEntry, | ||
| llm_api: LLMApi, | ||
| session_metadata: SessionMetadata, | ||
| ): | ||
| super().__init__(config, logger, tracing, event_emitter, role_entry) | ||
|
|
||
| self.llm_api = llm_api | ||
| self.session_metadata = session_metadata | ||
|
|
||
| def reply(self, memory: Memory, **kwargs: ...) -> Post: | ||
| rounds = memory.get_role_rounds( | ||
| role=self.alias, | ||
| include_failure_rounds=False, | ||
| ) | ||
|
|
||
| # obtain the query from the last round | ||
| last_post = rounds[-1].post_list[-1] | ||
|
|
||
| post_proxy = self.event_emitter.create_post_proxy(self.alias) | ||
|
|
||
| post_proxy.update_send_to(last_post.send_from) | ||
|
|
||
| input_message = last_post.message | ||
| prompt = ( | ||
| f"Input message: {input_message}.\n" | ||
| "\n" | ||
| "Your response should be a JSON object with the key 'image_url' and the value as the image path. " | ||
| "For example, {'image_url': 'c:/images/image.jpg'} or {'image_url': 'http://example.com/image.jpg'}. " | ||
| "Do not add any additional information in the response or wrap the JSON with ```json and ```." | ||
| ) | ||
|
|
||
| response = self.llm_api.chat_completion( | ||
| messages=[ | ||
| format_chat_message( | ||
| role="system", | ||
| message="Your task is to read the image path from the message.", | ||
| ), | ||
| format_chat_message( | ||
| role="user", | ||
| message=prompt, | ||
| ), | ||
| ], | ||
| ) | ||
|
|
||
| image_url = json.loads(response["content"])["image_url"] | ||
| if image_url.startswith("http"): | ||
| image_content = image_url | ||
| attachment_message = f"Image from {image_url}." | ||
| else: | ||
| if os.path.isabs(image_url): | ||
| image_content = local_image_to_data_url(image_url) | ||
| else: | ||
| image_content = local_image_to_data_url(os.path.join(self.session_metadata.execution_cwd, image_url)) | ||
| attachment_message = f"Image from {image_url} encoded as a Base64 data URL." | ||
|
|
||
| post_proxy.update_attachment( | ||
| message=attachment_message, | ||
| type=AttachmentType.image_url, | ||
| extra={"image_url": image_content}, | ||
| is_end=True, | ||
| ) | ||
|
|
||
| post_proxy.update_message( | ||
| "I have read the image path from the message. The image is attached below.", | ||
| ) | ||
|
|
||
| return post_proxy.end() | ||
This file contains hidden or 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,5 @@ | ||
| alias: ImageReader | ||
| module: taskweaver.ext_role.image_reader.image_reader.ImageReader | ||
| intro : |- | ||
| - ImageReader is responsible for helping the Planner to read images. | ||
| - The input message must contain the image path, either local or remote. |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,11 @@ | ||
| liqli: | ||
| name: Liqun Li | ||
| url: https://liqul.github.io | ||
| title: Principal Researcher | ||
| image_url: https://liqul.github.io/assets/logo_small_bw.png | ||
|
|
||
| xu: | ||
| name: Xu Zhang | ||
| url: https://scholar.google.com/citations?user=bqXdMMMAAAAJ&hl=zh-CN | ||
| title: Senior Researcher | ||
| image_url: https://scholar.googleusercontent.com/citations?view_op=view_photo&user=bqXdMMMAAAAJ&citpid=3 |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.