Skip to content

Commit

Permalink
fix: openai_web metadata parsing uncompleted
Browse files Browse the repository at this point in the history
  • Loading branch information
moeakwak committed Dec 1, 2023
1 parent 2c333e8 commit dfee2d1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
4 changes: 2 additions & 2 deletions backend/api/routers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from api.schemas import AskRequest, AskResponse, AskResponseType, UserReadAdmin, \
BaseConversationSchema
from api.schemas.openai_schemas import OpenaiChatPlugin, OpenaiChatPluginUserSettings, OpenaiChatPluginListResponse
from api.sources import OpenaiWebChatManager, convert_revchatgpt_message, OpenaiApiChatManager
from api.sources import OpenaiWebChatManager, convert_openai_web_message, OpenaiApiChatManager
from api.users import websocket_auth, current_active_user, current_super_user
from utils.common import desensitize
from utils.logger import get_logger
Expand Down Expand Up @@ -335,7 +335,7 @@ async def reply(response: AskResponse):

try:
if ask_request.source == ChatSourceTypes.openai_web:
message = convert_revchatgpt_message(data)
message = convert_openai_web_message(data)
if conversation_id is None:
conversation_id = data["conversation_id"]
else:
Expand Down
30 changes: 16 additions & 14 deletions backend/api/sources/openai_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
logger = get_logger(__name__)


def convert_revchatgpt_message(item: dict, message_id: str = None) -> OpenaiWebChatMessage | None:
def convert_openai_web_message(item: dict, message_id: str = None) -> OpenaiWebChatMessage | None:
if not item.get("message"):
return None
if not item["message"].get("author"):
Expand Down Expand Up @@ -71,22 +71,24 @@ def convert_revchatgpt_message(item: dict, message_id: str = None) -> OpenaiWebC
create_time=item["message"].get("create_time"),
parent=item.get("parent"),
children=item.get("children", []),
content=content,
metadata=OpenaiWebChatMessageMetadata(
source="openai_web",
weight=item["message"].get("weight"),
end_turn=item["message"].get("end_turn"),
recipient=item["message"].get("recipient"),
message_status=item["message"].get("status"),
fallback_content=fallback_content,
)
content=content
)
metadata_dict = OpenaiWebChatMessageMetadata(
source="openai_web",
weight=item["message"].get("weight"),
end_turn=item["message"].get("end_turn"),
recipient=item["message"].get("recipient"),
message_status=item["message"].get("status"),
fallback_content=fallback_content,
).model_dump(exclude_unset=True, exclude_none=True)
if "metadata" in item["message"] and item["message"]["metadata"] != {}:
result.metadata = result.metadata.model_copy(
update=item["message"]["metadata"]
)
metadata_dict.update(item["message"]["metadata"])
metadata = OpenaiWebChatMessageMetadata.model_validate(metadata_dict)
result.metadata = metadata
model_code = item["message"]["metadata"].get("model_slug")
result.model = OpenaiWebChatModels.from_code(model_code) or model_code
else:
result.metadata = OpenaiWebChatMessageMetadata.model_validate(metadata_dict)
return result


Expand All @@ -95,7 +97,7 @@ def convert_mapping(mapping: dict[uuid.UUID, dict]) -> dict[str, OpenaiWebChatMe
if not mapping:
return result
for key, item in mapping.items():
message = convert_revchatgpt_message(item, str(key))
message = convert_openai_web_message(item, str(key))
if message:
result[key] = message
return {str(key): value for key, value in result.items()}
Expand Down

0 comments on commit dfee2d1

Please sign in to comment.