Skip to content

fix: chat API logprobs format #1788

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

Merged
merged 2 commits into from
Dec 6, 2024
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ docker:
docker build -t llama-cpp-python:latest -f docker/simple/Dockerfile .

run-server:
uvicorn --factory llama.server:app --host ${HOST} --port ${PORT}
python llama_cpp/server --model ${MODEL}
Copy link
Contributor

@lukestanley lukestanley Oct 14, 2024

Choose a reason for hiding this comment

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

Why this change? It is unrelated to logprobs? @domdomegg

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't believe the current instructions work, and this is how I got it working.

Happy to split this out to a separate PR, or for challenge that the uvicorn command does work.

Copy link
Owner

Choose a reason for hiding this comment

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

Err yes @domdomegg that's a typo, it should be uvicorn --factory llama.server.app:create_app --host ${HOST} --port ${PORT}, I'll merge this and fix the Makefile after.


clean:
- cd vendor/llama.cpp && make clean
Expand Down
53 changes: 38 additions & 15 deletions llama_cpp/llama_chat_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,31 @@ def to_chat_handler(self) -> LlamaChatCompletionHandler:
return chat_formatter_to_chat_completion_handler(self)


def _convert_text_completion_logprobs_to_chat(
logprobs: Optional[llama_types.CompletionLogprobs],
) -> llama_types.ChatCompletionLogprobs:
if logprobs is None:
return None

return {
"content": [
{
"token": token,
"bytes": None,
"logprob": logprob,
"top_logprobs": [
{
"token": top_token,
"logprob": top_logprob,
"bytes": None,
}
for top_token, top_logprob in top_logprobs.items()
],
} for (token, logprob, top_logprobs) in zip(logprobs["tokens"], logprobs["token_logprobs"], logprobs["top_logprobs"])
],
"refusal": None,
}

def _convert_text_completion_to_chat(
completion: llama_types.Completion,
) -> llama_types.ChatCompletion:
Expand All @@ -275,7 +300,7 @@ def _convert_text_completion_to_chat(
"role": "assistant",
"content": completion["choices"][0]["text"],
},
"logprobs": completion["choices"][0]["logprobs"],
"logprobs": _convert_text_completion_logprobs_to_chat(completion["choices"][0]["logprobs"]),
"finish_reason": completion["choices"][0]["finish_reason"],
}
],
Expand Down Expand Up @@ -319,7 +344,7 @@ def _convert_text_completion_chunks_to_chat(
if chunk["choices"][0]["finish_reason"] is None
else {}
),
"logprobs": chunk["choices"][0]["logprobs"],
"logprobs": _convert_text_completion_logprobs_to_chat(chunk["choices"][0]["logprobs"]),
"finish_reason": chunk["choices"][0]["finish_reason"],
}
],
Expand Down Expand Up @@ -382,7 +407,7 @@ def _convert_completion_to_chat_function(
}
],
},
"logprobs": completion["choices"][0]["logprobs"],
"logprobs": _convert_text_completion_logprobs_to_chat(completion["choices"][0]["logprobs"]),
"finish_reason": "tool_calls",
}
],
Expand Down Expand Up @@ -435,7 +460,7 @@ def _stream_response_to_function_stream(
{
"index": 0,
"finish_reason": None,
"logprobs": chunk["choices"][0]["logprobs"],
"logprobs": _convert_text_completion_logprobs_to_chat(chunk["choices"][0]["logprobs"]),
"delta": {
"role": None,
"content": None,
Expand Down Expand Up @@ -472,7 +497,7 @@ def _stream_response_to_function_stream(
{
"index": 0,
"finish_reason": None,
"logprobs": chunk["choices"][0]["logprobs"],
"logprobs": _convert_text_completion_logprobs_to_chat(chunk["choices"][0]["logprobs"]),
"delta": {
"role": None,
"content": None,
Expand Down Expand Up @@ -1716,7 +1741,7 @@ def message_to_str(msg: llama_types.ChatCompletionRequestMessage):
}
],
},
"logprobs": completion["choices"][0]["logprobs"],
"logprobs": _convert_text_completion_logprobs_to_chat(completion["choices"][0]["logprobs"]),
"finish_reason": "tool_calls",
}
],
Expand Down Expand Up @@ -2128,7 +2153,7 @@ def generate_streaming(tools, functions, function_call, prompt):
choices=[
{
"index": 0,
"logprobs": chunk["choices"][0]["logprobs"],
"logprobs": _convert_text_completion_logprobs_to_chat(chunk["choices"][0]["logprobs"]),
"delta": {
"role": None,
"content": None,
Expand Down Expand Up @@ -2230,7 +2255,7 @@ def generate_streaming(tools, functions, function_call, prompt):
choices=[
{
"index": 0,
"logprobs": chunk["choices"][0]["logprobs"],
"logprobs": _convert_text_completion_logprobs_to_chat(chunk["choices"][0]["logprobs"]),
"delta": {
"role": "assistant",
"content": None,
Expand Down Expand Up @@ -2268,9 +2293,7 @@ def generate_streaming(tools, functions, function_call, prompt):
choices=[
{
"index": 0,
"logprobs": chunk["choices"][0][
"logprobs"
],
"logprobs": _convert_text_completion_logprobs_to_chat(chunk["choices"][0]["logprobs"]),
"delta": {
"role": "assistant",
"content": buffer.pop(0),
Expand All @@ -2293,7 +2316,7 @@ def generate_streaming(tools, functions, function_call, prompt):
choices=[
{
"index": 0,
"logprobs": chunk["choices"][0]["logprobs"],
"logprobs": _convert_text_completion_logprobs_to_chat(chunk["choices"][0]["logprobs"]),
"delta": {
"role": "assistant",
"content": (
Expand Down Expand Up @@ -2379,7 +2402,7 @@ def generate_streaming(tools, functions, function_call, prompt):
choices=[
{
"index": 0,
"logprobs": chunk["choices"][0]["logprobs"],
"logprobs": _convert_text_completion_logprobs_to_chat(chunk["choices"][0]["logprobs"]),
"delta": {
"role": None,
"content": None,
Expand Down Expand Up @@ -2613,7 +2636,7 @@ def generate_streaming(tools, functions, function_call, prompt):
choices=[
{
"index": 0,
"logprobs": completion["choices"][0]["logprobs"],
"logprobs": _convert_text_completion_logprobs_to_chat(completion["choices"][0]["logprobs"]),
"message": {
"role": "assistant",
"content": None if content == "" else content,
Expand Down Expand Up @@ -3745,7 +3768,7 @@ def chatml_function_calling(
{
"finish_reason": "tool_calls",
"index": 0,
"logprobs": completion["choices"][0]["logprobs"],
"logprobs": _convert_text_completion_logprobs_to_chat(completion["choices"][0]["logprobs"]),
"message": {
"role": "assistant",
"content": None,
Expand Down
22 changes: 20 additions & 2 deletions llama_cpp/llama_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,28 @@ class ChatCompletionFunction(TypedDict):
parameters: Dict[str, JsonType] # TODO: make this more specific


class ChatCompletionTopLogprobToken(TypedDict):
token: str
logprob: float
bytes: Optional[List[int]]


class ChatCompletionLogprobToken(ChatCompletionTopLogprobToken):
token: str
logprob: float
bytes: Optional[List[int]]
top_logprobs: List[ChatCompletionTopLogprobToken]


class ChatCompletionLogprobs(TypedDict):
content: Optional[List[ChatCompletionLogprobToken]]
refusal: Optional[List[ChatCompletionLogprobToken]]


class ChatCompletionResponseChoice(TypedDict):
index: int
message: "ChatCompletionResponseMessage"
logprobs: Optional[CompletionLogprobs]
logprobs: Optional[ChatCompletionLogprobs]
finish_reason: Optional[str]


Expand Down Expand Up @@ -134,7 +152,7 @@ class ChatCompletionStreamResponseChoice(TypedDict):
ChatCompletionStreamResponseDelta, ChatCompletionStreamResponseDeltaEmpty
]
finish_reason: Optional[Literal["stop", "length", "tool_calls", "function_call"]]
logprobs: NotRequired[Optional[CompletionLogprobs]]
logprobs: NotRequired[Optional[ChatCompletionLogprobs]]


class CreateChatCompletionStreamResponse(TypedDict):
Expand Down