Skip to content

Commit

Permalink
more finishReason fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
justinthelaw committed Sep 16, 2024
1 parent 1cdbe69 commit 625f7b3
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/leapfrogai_api/backend/grpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async def completion(model: Model, request: lfai.CompletionRequest):
CompletionChoice(
index=0,
text=response.choices[0].text,
finish_reason=str(response.choices[0].finish_reason),
finish_reason=response.choices[0].finish_reason,
logprobs=None,
)
],
Expand Down
6 changes: 3 additions & 3 deletions src/leapfrogai_api/backend/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ class CompletionChoice(BaseModel):
None,
description="Log probabilities for the generated tokens. Only returned if requested.",
)
finish_reason: str = Field(
"", description="The reason why the completion finished.", example="length"
finish_reason: str | None = Field(
None, description="The reason why the completion finished.", example="length"
)


Expand Down Expand Up @@ -252,7 +252,7 @@ class ChatChoice(BaseModel):
default=ChatMessage(), description="The message content for this choice."
)
finish_reason: str | None = Field(
default="",
default=None,
description="The reason why the model stopped generating tokens.",
examples=["stop", "length"],
)
Expand Down
2 changes: 1 addition & 1 deletion src/leapfrogai_sdk/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


class FinishReason(Enum):
NONE = ""
NONE = None
STOP = "stop"
LENGTH = "length"

Expand Down
6 changes: 3 additions & 3 deletions tests/pytest/leapfrogai_api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def test_chat_completion(dummy_auth_middleware):

# parse finish reason
assert "finish_reason" in response_choices[0]
assert "STOP" == response_choices[0].get("finish_reason")
assert "stop" == response_choices[0].get("finish_reason")

# parse usage data
response_usage = response_obj.get("usage")
Expand Down Expand Up @@ -324,9 +324,9 @@ def test_stream_chat_completion(dummy_auth_middleware):
assert "finish_reason" in choices[0]
# in streaming responses, the stop reason is not STOP until the last iteration (token) is sent back
if iter_length == input_length:
assert "STOP" == choices[0].get("finish_reason")
assert "stop" == choices[0].get("finish_reason")
else:
assert "NONE" == choices[0].get("finish_reason")
assert None is choices[0].get("finish_reason")
# parse usage data
response_usage = stream_response.get("usage")
prompt_tokens = response_usage.get("prompt_tokens")
Expand Down

0 comments on commit 625f7b3

Please sign in to comment.