Skip to content

Commit

Permalink
fix linter and change enforce author role alternation default to false
Browse files Browse the repository at this point in the history
  • Loading branch information
JohntheLi committed Mar 29, 2024
1 parent a152557 commit de50de9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
39 changes: 26 additions & 13 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 All @@ -44,12 +49,6 @@
SettingsResponse,
)

from fastapi_poe.templates import (
IMAGE_VISION_ATTACHMENT_TEMPLATE,
TEXT_ATTACHMENT_TEMPLATE,
URL_ATTACHMENT_TEMPLATE
)

logger = logging.getLogger("uvicorn.default")


Expand Down Expand Up @@ -108,7 +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
concat_attachments_to_message: bool = (
True # attachment content will be concatenated to message
)

# Override these for your bot

Expand Down Expand Up @@ -322,23 +323,33 @@ def concat_attachment_content_to_message_body(
attachment_name=attachment.name,
content=attachment.parsed_content,
)
concatenated_content = f"{concatenated_content}\n\n{url_attachment_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}"
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]})
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
Expand Down Expand Up @@ -418,7 +429,9 @@ async def handle_query(
) -> AsyncIterable[ServerSentEvent]:
try:
if self.concat_attachments_to_message:
request = self.concat_attachment_content_to_message_body(query_request=request)
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
2 changes: 1 addition & 1 deletion src/fastapi_poe/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
"""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. """
)
)
3 changes: 1 addition & 2 deletions src/fastapi_poe/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ class SettingsResponse(BaseModel):
introduction_message: str = ""
expand_text_attachments: bool = True
enable_image_comprehension: bool = False
enforce_author_role_alternation: bool = True

enforce_author_role_alternation: bool = False


class AttachmentUploadResponse(BaseModel):
Expand Down

0 comments on commit de50de9

Please sign in to comment.