Skip to content

Commit ff4227b

Browse files
feat: #729 - images support in chat message, image gallery (#731)
1 parent bcfa5f6 commit ff4227b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+799
-354
lines changed

examples/chat/chat.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#
2121

2222
import asyncio
23+
import uuid
2324
from collections.abc import AsyncGenerator
2425
from typing import Literal
2526

@@ -117,6 +118,11 @@ async def chat(
117118
{"example_state_key": "example_state_value", "example_state_key_2": "example_state_value_2"}
118119
)
119120

121+
yield self.create_image_response(
122+
str(uuid.uuid4()),
123+
"https://media.istockphoto.com/id/1145618475/photo/villefranche-on-sea-in-evening.jpg?s=612x612&w=0&k=20&c=vQGj6uK7UUVt0vQhZc9yhRO_oYBEf8IeeDxGyJKbLKI=",
124+
)
125+
120126
example_live_updates = [
121127
self.create_live_update("0", LiveUpdateType.START, "[EXAMPLE] Searching for examples in the web..."),
122128
self.create_live_update(

packages/ragbits-chat/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# CHANGELOG
22

33
## Unreleased
4+
- Add images support in chat message, images gallery (#731)
45
- Add persistent user settings (#719)
56
- Send chat options under `user_settings` key (#721)
67
- Add feedback indicator to messages, allow `extensions` in chat messages (#722)

packages/ragbits-chat/src/ragbits/chat/api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ async def chat_response() -> AsyncGenerator[str, None]:
234234
outputs.message_id = content
235235
case ChatResponseType.CONVERSATION_ID:
236236
outputs.conversation_id = content
237+
case ChatResponseType.IMAGE:
238+
outputs.image_url = content
237239

238240
yield chunk
239241

packages/ragbits-chat/src/ragbits/chat/interface/_interface.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
ChatContext,
2323
ChatResponse,
2424
ChatResponseType,
25+
Image,
2526
LiveUpdate,
2627
LiveUpdateContent,
2728
LiveUpdateType,
@@ -228,6 +229,11 @@ def create_followup_messages(messages: list[str]) -> ChatResponse:
228229
"""Helper method to create a live update response."""
229230
return ChatResponse(type=ChatResponseType.FOLLOWUP_MESSAGES, content=messages)
230231

232+
@staticmethod
233+
def create_image_response(image_id: str, image_url: str) -> ChatResponse:
234+
"""Helper method to create an image response."""
235+
return ChatResponse(type=ChatResponseType.IMAGE, content=Image(id=image_id, url=image_url))
236+
231237
@staticmethod
232238
def _sign_state(state: dict[str, Any]) -> str:
233239
"""

packages/ragbits-chat/src/ragbits/chat/interface/types.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ class LiveUpdate(BaseModel):
5656
content: LiveUpdateContent
5757

5858

59+
class Image(BaseModel):
60+
"""Represents an image in the conversation."""
61+
62+
id: str
63+
url: str
64+
65+
5966
class ChatResponseType(str, Enum):
6067
"""Types of responses that can be returned by the chat interface."""
6168

@@ -66,13 +73,14 @@ class ChatResponseType(str, Enum):
6673
CONVERSATION_ID = "conversation_id"
6774
LIVE_UPDATE = "live_update"
6875
FOLLOWUP_MESSAGES = "followup_messages"
76+
IMAGE = "image"
6977

7078

7179
class ChatResponse(BaseModel):
7280
"""Container for different types of chat responses."""
7381

7482
type: ChatResponseType
75-
content: str | Reference | StateUpdate | LiveUpdate | list[str]
83+
content: str | Reference | StateUpdate | LiveUpdate | list[str] | Image
7684

7785
def as_text(self) -> str | None:
7886
"""
@@ -130,6 +138,12 @@ def as_followup_messages(self) -> list[str] | None:
130138
"""
131139
return cast(list[str], self.content) if self.type == ChatResponseType.FOLLOWUP_MESSAGES else None
132140

141+
def as_image(self) -> Image | None:
142+
"""
143+
Return the content as Image if this is an image response, else None.
144+
"""
145+
return cast(Image, self.content) if self.type == ChatResponseType.IMAGE else None
146+
133147

134148
class ChatContext(BaseModel):
135149
"""Represents the context of a chat conversation."""

packages/ragbits-chat/src/ragbits/chat/ui-build/assets/ChatOptionsForm-BKEhD5H-.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/ragbits-chat/src/ragbits/chat/ui-build/assets/ChatOptionsForm-D07-tiY8.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/ragbits-chat/src/ragbits/chat/ui-build/assets/FeedbackForm--TbyuyTt.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/ragbits-chat/src/ragbits/chat/ui-build/assets/FeedbackForm-CircDKTC.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)