-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
66 additions
and
15 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
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,40 @@ | ||
# SPDX-FileCopyrightText: 2023-present Massimiliano Pippi <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
from pathlib import Path | ||
from urllib.parse import urlparse | ||
|
||
from banks.types import ContentBlock, ImageUrl | ||
|
||
|
||
def _is_url(string: str) -> bool: | ||
try: | ||
result = urlparse(string) | ||
return all([result.scheme, result.netloc]) | ||
except ValueError: | ||
return False | ||
|
||
|
||
def image(value: str) -> str: | ||
"""Wrap the filtered value into a ContentBlock with the proper cache_control field set. | ||
The resulting ChatMessage will have the field `content` populated with a list of ContentBlock objects. | ||
Example: | ||
```jinja | ||
Describe what you see | ||
{{ "path/to/image/file" | image }} | ||
``` | ||
Important: | ||
this filter marks the content to cache by surrounding it with `<content_block>` and | ||
`</content_block>`, so it's only useful when used within a `{% chat %}` block. | ||
""" | ||
if _is_url(value): | ||
image_url = ImageUrl(url=value) | ||
else: | ||
image_url = ImageUrl.from_path(Path(value)) | ||
|
||
block = ContentBlock.model_validate({"type": "image_url", "image_url": image_url}) | ||
return f"<content_block>{block.model_dump_json()}</content_block>" |
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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
# SPDX-FileCopyrightText: 2023-present Massimiliano Pippi <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
import base64 | ||
from enum import Enum | ||
from inspect import Parameter, getdoc, signature | ||
from pathlib import Path | ||
from typing import Callable | ||
|
||
from pydantic import BaseModel | ||
|
@@ -26,9 +28,14 @@ class ImageUrl(BaseModel): | |
url: str | ||
|
||
@classmethod | ||
def from_base64(cls, media_type: str, base64_str: str): | ||
def from_base64(cls, media_type: str, base64_str: str) -> Self: | ||
return cls(url=f"data:{media_type};base64,{base64_str}") | ||
|
||
@classmethod | ||
def from_path(cls, file_path: Path) -> Self: | ||
with open(file_path, "rb") as image_file: | ||
return cls.from_base64("image/jpeg", base64.b64encode(image_file.read()).decode("utf-8")) | ||
|
||
|
||
class ContentBlock(BaseModel): | ||
type: ContentBlockType | ||
|
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