mirror of
https://github.com/QwenLM/Qwen.git
synced 2026-05-21 00:45:48 +08:00
openai_api.py: compatible with both pydantic v1 and v2
This commit is contained in:
@@ -416,6 +416,13 @@ async def create_chat_completion(request: ChatCompletionRequest):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _dump_json(data: BaseModel, *args, **kwargs) -> str:
|
||||||
|
try:
|
||||||
|
return data.model_dump_json(*args, **kwargs)
|
||||||
|
except AttributeError: # pydantic<2.0.0
|
||||||
|
return data.json(*args, **kwargs) # noqa
|
||||||
|
|
||||||
|
|
||||||
async def predict(
|
async def predict(
|
||||||
query: str, history: List[List[str]], model_id: str, stop_words: List[str], gen_kwargs: Dict,
|
query: str, history: List[List[str]], model_id: str, stop_words: List[str], gen_kwargs: Dict,
|
||||||
):
|
):
|
||||||
@@ -426,7 +433,7 @@ async def predict(
|
|||||||
chunk = ChatCompletionResponse(
|
chunk = ChatCompletionResponse(
|
||||||
model=model_id, choices=[choice_data], object="chat.completion.chunk"
|
model=model_id, choices=[choice_data], object="chat.completion.chunk"
|
||||||
)
|
)
|
||||||
yield "{}".format(chunk.model_dump_json(exclude_unset=True))
|
yield "{}".format(_dump_json(chunk, exclude_unset=True))
|
||||||
|
|
||||||
current_length = 0
|
current_length = 0
|
||||||
stop_words_ids = [tokenizer.encode(s) for s in stop_words] if stop_words else None
|
stop_words_ids = [tokenizer.encode(s) for s in stop_words] if stop_words else None
|
||||||
@@ -452,7 +459,7 @@ async def predict(
|
|||||||
chunk = ChatCompletionResponse(
|
chunk = ChatCompletionResponse(
|
||||||
model=model_id, choices=[choice_data], object="chat.completion.chunk"
|
model=model_id, choices=[choice_data], object="chat.completion.chunk"
|
||||||
)
|
)
|
||||||
yield "{}".format(chunk.model_dump_json(exclude_unset=True))
|
yield "{}".format(_dump_json(chunk, exclude_unset=True))
|
||||||
|
|
||||||
choice_data = ChatCompletionResponseStreamChoice(
|
choice_data = ChatCompletionResponseStreamChoice(
|
||||||
index=0, delta=DeltaMessage(), finish_reason="stop"
|
index=0, delta=DeltaMessage(), finish_reason="stop"
|
||||||
@@ -460,7 +467,7 @@ async def predict(
|
|||||||
chunk = ChatCompletionResponse(
|
chunk = ChatCompletionResponse(
|
||||||
model=model_id, choices=[choice_data], object="chat.completion.chunk"
|
model=model_id, choices=[choice_data], object="chat.completion.chunk"
|
||||||
)
|
)
|
||||||
yield "{}".format(chunk.model_dump_json(exclude_unset=True))
|
yield "{}".format(_dump_json(chunk, exclude_unset=True))
|
||||||
yield "[DONE]"
|
yield "[DONE]"
|
||||||
|
|
||||||
_gc()
|
_gc()
|
||||||
|
|||||||
Reference in New Issue
Block a user