Skip to content
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

add data_event type #144

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/fastapi_poe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"PartialResponse",
"ErrorResponse",
"MetaResponse",
"DataResponse",
"RequestContext",
"ToolDefinition",
"ToolCallDefinition",
Expand All @@ -41,6 +42,7 @@
from .types import (
Attachment,
CostItem,
DataResponse,
ErrorResponse,
MessageFeedback,
MetaResponse,
Expand Down
7 changes: 7 additions & 0 deletions src/fastapi_poe/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
AttachmentUploadResponse,
ContentType,
CostItem,
DataResponse,
ErrorResponse,
Identifier,
MetaResponse,
Expand Down Expand Up @@ -772,6 +773,10 @@ async def _cost_requests_inner(
def text_event(text: str) -> ServerSentEvent:
return ServerSentEvent(data=json.dumps({"text": text}), event="text")

@staticmethod
def data_event(metadata: str) -> ServerSentEvent:
return ServerSentEvent(data=json.dumps({"metadata": metadata}), event="data")

@staticmethod
def replace_response_event(text: str) -> ServerSentEvent:
return ServerSentEvent(
Expand Down Expand Up @@ -882,6 +887,8 @@ async def handle_query(
linkify=event.linkify,
suggested_replies=event.suggested_replies,
)
elif isinstance(event, DataResponse):
yield self.data_event(event.metadata)
elif event.is_suggested_reply:
yield self.suggested_reply_event(event.text)
elif event.is_replace_response:
Expand Down
18 changes: 18 additions & 0 deletions src/fastapi_poe/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class ProtocolMessage(BaseModel):
message_id: str = ""
feedback: list[MessageFeedback] = Field(default_factory=list)
attachments: list[Attachment] = Field(default_factory=list)
metadata: Optional[str] = None


class RequestContext(BaseModel):
Expand Down Expand Up @@ -237,6 +238,23 @@ class AttachmentUploadResponse(BaseModel):
attachment_url: Optional[str]


class DataResponse(BaseModel):
"""

A response that contains arbitrary data to attach to the bot response.
This data can be retrieved in later requests to the bot within the same chat.
Note that only the final DataResponse object in the stream will be attached to the bot response.

#### Fields:
- `metadata` (`str`): String of data to attach to the bot response.

"""

model_config = ConfigDict(extra="forbid")

metadata: str


class PartialResponse(BaseModel):
"""

Expand Down