Skip to content
Merged
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
54 changes: 40 additions & 14 deletions fastchat/serve/openai_api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,28 @@ async def create_chat_completion(request: ChatCompletionRequest):
for i, content in enumerate(all_tasks):
if content["error_code"] != 0:
return create_error_response(content["error_code"], content["text"])
choices.append(
ChatCompletionResponseChoice(
index=i,
message=ChatMessage(role="assistant", content=content["text"]),
finish_reason=content.get("finish_reason", "stop"),
if isinstance(content["text"], list):
for t in content["text"]:
choices.append(
ChatCompletionResponseChoice(
index=i,
message=ChatMessage(role="assistant", content=t),
finish_reason=content.get("finish_reason", "stop"),
)
)
else:
choices.append(
ChatCompletionResponseChoice(
index=i,
message=ChatMessage(role="assistant", content=content["text"]),
finish_reason=content.get("finish_reason", "stop"),
)
Comment on lines +402 to +417
Copy link
Member

@infwinston infwinston Sep 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we do this to reduce LoC and repeated codes

if isinstance(content["text"], str):
    contents = [context["text"]]
for t in contents:
    choices.append(
        ChatCompletionResponseChoice(
            index=i,
            message=ChatMessage(role="assistant", content=t),
            finish_reason=content.get("finish_reason", "stop"),
        )
    )

)
)
if "usage" in content:
task_usage = UsageInfo.parse_obj(content["usage"])
if isinstance(content["usage"], list):
task_usage = UsageInfo.parse_obj(content["usage"][0])
else:
task_usage = UsageInfo.parse_obj(content["usage"])
for usage_key, usage_value in task_usage.dict().items():
setattr(usage, usage_key, getattr(usage, usage_key) + usage_value)

Expand Down Expand Up @@ -775,14 +788,27 @@ async def create_chat_completion(request: APIChatCompletionRequest):
for i, content in enumerate(all_tasks):
if content["error_code"] != 0:
return create_error_response(content["error_code"], content["text"])
choices.append(
ChatCompletionResponseChoice(
index=i,
message=ChatMessage(role="assistant", content=content["text"]),
finish_reason=content.get("finish_reason", "stop"),
if isinstance(content["text"], list):
for t in content["text"]:
choices.append(
ChatCompletionResponseChoice(
index=i,
message=ChatMessage(role="assistant", content=t),
finish_reason=content.get("finish_reason", "stop"),
)
)
else:
choices.append(
ChatCompletionResponseChoice(
index=i,
message=ChatMessage(role="assistant", content=content["text"]),
finish_reason=content.get("finish_reason", "stop"),
)
)
)
task_usage = UsageInfo.parse_obj(content["usage"])
if isinstance(content["usage"], list):
task_usage = UsageInfo.parse_obj(content["usage"][0])
else:
task_usage = UsageInfo.parse_obj(content["usage"])
for usage_key, usage_value in task_usage.dict().items():
setattr(usage, usage_key, getattr(usage, usage_key) + usage_value)

Expand Down