Skip to content

Commit

Permalink
fix content block models
Browse files Browse the repository at this point in the history
  • Loading branch information
masci committed Nov 10, 2024
1 parent ea57bea commit 0b240e6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
25 changes: 7 additions & 18 deletions src/banks/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,26 @@

class ContentBlockType(str, Enum):
text = "text"
image = "image"


class MediaTypeBlockType(str, Enum):
image_jpeg = "image/jpeg"
image_png = "image/png"
image_gif = "image/gif"
image_webp = "image/webp"
image_url = "image_url"


class CacheControl(BaseModel):
type: str = "ephemeral"


class Source(BaseModel):
type: str = "base64"
media_type: MediaTypeBlockType
data: str
class ImageUrl(BaseModel):
url: str

class Config:
use_enum_values = True
@classmethod
def from_base64(cls, media_type: str, base64_str: str):
return cls(url=f"data:{media_type};base64,{base64_str}")


class ContentBlock(BaseModel):
type: ContentBlockType
cache_control: CacheControl | None = None
text: str | None = None
source: Source | None = None

class Config:
use_enum_values = True
image_url: ImageUrl | None = None


ChatMessageContent = list[ContentBlock] | str
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cache_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ def test_cache_control():
res = cache_control("foo", "ephemeral")
res = res.replace("<content_block_txt>", "")
res = res.replace("</content_block_txt>", "")
assert res == '{"type":"text","cache_control":{"type":"ephemeral"},"text":"foo","source":null}'
assert res == '{"type":"text","cache_control":{"type":"ephemeral"},"text":"foo","image_url":null}'

0 comments on commit 0b240e6

Please sign in to comment.