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

update version for release #79

Merged
merged 3 commits into from
Mar 29, 2024
Merged
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: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "fastapi_poe"
version = "0.0.36"
version = "0.0.37"
authors = [
{ name="Lida Li", email="[email protected]" },
{ name="Jelle Zijlstra", email="[email protected]" },
Expand Down
54 changes: 54 additions & 0 deletions src/fastapi_poe/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
from starlette.types import Message
from typing_extensions import deprecated, overload

from fastapi_poe.templates import (
IMAGE_VISION_ATTACHMENT_TEMPLATE,
TEXT_ATTACHMENT_TEMPLATE,
URL_ATTACHMENT_TEMPLATE,
)
from fastapi_poe.types import (
AttachmentUploadResponse,
ContentType,
Expand Down Expand Up @@ -102,6 +107,9 @@ async def http_exception_handler(request: Request, ex: Exception) -> Response:
class PoeBot:
path: str = "/" # Path where this bot will be exposed
access_key: Optional[str] = None # Access key for this bot
concat_attachments_to_message: bool = (
True # attachment content will be concatenated to message
)

# Override these for your bot

Expand Down Expand Up @@ -302,6 +310,48 @@ async def _process_pending_attachment_requests(
logger.error("Error processing pending attachment requests")
raise

def concat_attachment_content_to_message_body(
self, query_request: QueryRequest
) -> QueryRequest:
"""Concat received attachment file content into message content body."""
last_message = query_request.query[-1]
concatenated_content = last_message.content
for attachment in last_message.attachments:
if attachment.parsed_content:
if attachment.content_type == "text/html":
url_attachment_content = URL_ATTACHMENT_TEMPLATE.format(
attachment_name=attachment.name,
content=attachment.parsed_content,
)
concatenated_content = (
f"{concatenated_content}\n\n{url_attachment_content}"
)
elif "text" in attachment.content_type:
text_attachment_content = TEXT_ATTACHMENT_TEMPLATE.format(
attachment_name=attachment.name,
attachment_parsed_content=attachment.parsed_content,
)
concatenated_content = (
f"{concatenated_content}\n\n{text_attachment_content}"
)
elif "image" in attachment.content_type:
parsed_content_filename = attachment.parsed_content.split("***")[0]
parsed_content_text = attachment.parsed_content.split("***")[1]
image_attachment_content = IMAGE_VISION_ATTACHMENT_TEMPLATE.format(
filename=parsed_content_filename,
parsed_image_description=parsed_content_text,
)
concatenated_content = (
f"{concatenated_content}\n\n{image_attachment_content}"
)
modified_last_message = last_message.model_copy(
update={"content": concatenated_content}
)
modified_query = query_request.model_copy(
update={"query": query_request.query[:-1] + [modified_last_message]}
)
return modified_query

@staticmethod
def text_event(text: str) -> ServerSentEvent:
return ServerSentEvent(data=json.dumps({"text": text}), event="text")
Expand Down Expand Up @@ -378,6 +428,10 @@ async def handle_query(
self, request: QueryRequest, context: RequestContext
) -> AsyncIterable[ServerSentEvent]:
try:
if self.concat_attachments_to_message:
request = self.concat_attachment_content_to_message_body(
query_request=request
)
async for event in self.get_response_with_context(request, context):
if isinstance(event, ServerSentEvent):
yield event
Expand Down
25 changes: 25 additions & 0 deletions src/fastapi_poe/templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""

This module contains a collection of string templates designed to streamline the integration
of features like attachments into the LLM request.

"""

TEXT_ATTACHMENT_TEMPLATE = (
"""Your response must be in the language of the relevant queries related to the document.\n"""
"""Below is the content of {attachment_name}:\n\n{attachment_parsed_content}"""
)
URL_ATTACHMENT_TEMPLATE = (
"""Assume you can access the external URL {attachment_name}. """
"""Your response must be in the language of the relevant queries related to the URL.\n"""
"""Use the URL's content below to respond to the queries:\n\n{content}"""
)
IMAGE_VISION_ATTACHMENT_TEMPLATE = (
"""I have uploaded an image ({filename}). """
"""Assume that you can see the attached image. """
"""First, read the image analysis:\n\n"""
"""<image_analysis>{parsed_image_description}</image_analysis>\n\n"""
"""Use any relevant parts to inform your response. """
"""Do NOT reference the image analysis in your response. """
"""Respond in the same language as my next message. """
)
4 changes: 4 additions & 0 deletions src/fastapi_poe/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Attachment(BaseModel):
url: str
content_type: str
name: str
parsed_content: Optional[str] = None


class ProtocolMessage(BaseModel):
Expand Down Expand Up @@ -93,6 +94,9 @@ class SettingsResponse(BaseModel):
server_bot_dependencies: Dict[str, int] = Field(default_factory=dict)
allow_attachments: bool = False
introduction_message: str = ""
expand_text_attachments: bool = True
enable_image_comprehension: bool = False
enforce_author_role_alternation: bool = False


class AttachmentUploadResponse(BaseModel):
Expand Down
Loading