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

TST: fix lint error #2911

Merged
merged 5 commits into from
Feb 21, 2025
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import ABC, abstractmethod
from typing import Dict, Optional, Tuple, Type, Union

from ....types import ChatCompletionChunkDelta, CompletionChoice, CompletionChunk
from ....types import ChatCompletionChunkDelta, CompletionChoice


class ReasoningParser(ABC):
Expand All @@ -26,7 +26,7 @@ def extract_reasoning_content_streaming(
self,
previous_text: str,
current_text: str,
delta: Union[str, CompletionChunk],
delta: ChatCompletionChunkDelta,
) -> ChatCompletionChunkDelta:
"""Extract reasoning content from model output in a streaming fashion.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def extract_reasoning_content_streaming(
previous_text: str,
current_text: str,
delta: ChatCompletionChunkDelta,
) -> Optional[ChatCompletionChunkDelta]:
) -> ChatCompletionChunkDelta:
"""Extract reasoning content from DeepSeek-R1 model output in a streaming fashion.

Args:
Expand Down Expand Up @@ -122,7 +122,7 @@ def extract_reasoning_content(
# Thus we assume the reasoning content is always at the start.
# Ref https://huggingface.co/deepseek-ai/DeepSeek-R1/commit/8a58a132790c9935686eb97f042afa8013451c9f
if self.reasoning_end_tag not in model_output:
return model_output, None
return model_output, ""
else:
# Add a start token if it's missing to keep compatibility.
if self.reasoning_start_tag not in model_output:
Expand All @@ -136,5 +136,5 @@ def extract_reasoning_content(
final_output = model_output[end_index:]

if len(final_output) == 0:
return reasoning_content, None
return reasoning_content, ""
return reasoning_content, final_output
2 changes: 1 addition & 1 deletion xinference/model/llm/tests/test_llm_family.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def test_serialize_llm_family_v1():
stop=["hello", "world"],
)

expected = """{"version": 1, "context_length": 2048, "model_name": "TestModel", "model_lang": ["en"], "model_ability": ["embed", "generate"], "model_description": null, "model_family": null, "model_specs": [{"model_format": "ggufv2", "model_hub": "huggingface", "model_size_in_billions": 2, "quantizations": ["q4_0", "q4_1"], "quantization_parts": {"q4_2": ["a", "b"]}, "model_id": "example/TestModel", "model_revision": "123", "model_file_name_template": "TestModel.{quantization}.bin", "model_file_name_split_template": "TestModel.{quantization}.bin.{part}", "model_uri": null}, {"model_format": "pytorch", "model_hub": "huggingface", "model_size_in_billions": 3, "quantizations": ["int8", "int4", "none"], "model_id": "example/TestModel", "model_revision": "456", "model_uri": null}], "chat_template": "xyz", "stop_token_ids": [1, 2, 3], "stop": ["hello", "world"]}"""
expected = """{"version": 1, "context_length": 2048, "model_name": "TestModel", "model_lang": ["en"], "model_ability": ["embed", "generate"], "model_description": null, "model_family": null, "model_specs": [{"model_format": "ggufv2", "model_hub": "huggingface", "model_size_in_billions": 2, "quantizations": ["q4_0", "q4_1"], "quantization_parts": {"q4_2": ["a", "b"]}, "model_id": "example/TestModel", "model_revision": "123", "model_file_name_template": "TestModel.{quantization}.bin", "model_file_name_split_template": "TestModel.{quantization}.bin.{part}", "model_uri": null}, {"model_format": "pytorch", "model_hub": "huggingface", "model_size_in_billions": 3, "quantizations": ["int8", "int4", "none"], "model_id": "example/TestModel", "model_revision": "456", "model_uri": null}], "chat_template": "xyz", "stop_token_ids": [1, 2, 3], "stop": ["hello", "world"], "reasoning_start_tag":null, "reasoning_end_tag":null}"""
assert json.loads(llm_family.json()) == json.loads(expected)

llm_family_context_length = LLMFamilyV1(
Expand Down
19 changes: 11 additions & 8 deletions xinference/model/llm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,19 +374,21 @@ async def _async_to_chat_completion_chunks(
current_text = ""
async for chunk in chunks:
if i == 0:
chunk = cls._get_first_chat_completion_chunk(chunk)
chat_chunk = cls._get_first_chat_completion_chunk(chunk)
elif not chunk.get("choices"):
# usage
chunk = cls._get_final_chat_completion_chunk(chunk)
chat_chunk = cls._get_final_chat_completion_chunk(chunk)
else:
chunk = cls._to_chat_completion_chunk(chunk)
chat_chunk = cls._to_chat_completion_chunk(chunk)
if reasoning_parser is not None:
choices = chunk.get("choices")
choices = chat_chunk.get("choices")
if choices is None:
continue
for choice in choices:
delta = choice.get("delta")
if not delta:
continue
current_text = previous_text + delta.get("content")
current_text = previous_text + delta.get("content", "")
choice[
"delta"
] = reasoning_parser.extract_reasoning_content_streaming(
Expand All @@ -395,7 +397,7 @@ async def _async_to_chat_completion_chunks(
delta=delta,
)
previous_text = current_text
yield chunk
yield chat_chunk
i += 1

@staticmethod
Expand All @@ -408,7 +410,7 @@ def _to_chat_completion(
reasoning_content = None

if reasoning_parser is not None:
reasoning_content, content = reasoning_parser.extract_reasoning_content(
reasoning_content, content = reasoning_parser.extract_reasoning_content( # type: ignore
choice
)

Expand All @@ -429,7 +431,8 @@ def _to_chat_completion(
"id": "chat" + completion["id"],
"object": "chat.completion",
"created": completion["created"],
"model": choices,
"model": completion["model"],
"choices": choices, # type: ignore
"usage": completion["usage"],
}

Expand Down
Loading