diff --git a/CHANGELOG.md b/CHANGELOG.md index bfa7e917b..e6f5294a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - fix: load cached pretrained models in offline mode by @emme1t in #2396 - fix(ci): install matching NVML headers for CUDA wheel builds +- fix: return token logprobs for chat completions that set logprobs without top_logprobs ## [0.3.38] diff --git a/llama_cpp/llama_chat_format.py b/llama_cpp/llama_chat_format.py index 62e236415..1621c2e12 100644 --- a/llama_cpp/llama_chat_format.py +++ b/llama_cpp/llama_chat_format.py @@ -728,7 +728,7 @@ def chat_completion_handler( top_k=top_k, min_p=min_p, typical_p=typical_p, - logprobs=top_logprobs if logprobs else None, + logprobs=(top_logprobs or 0) if logprobs else None, stream=stream, stop=stop, seed=seed, @@ -3096,7 +3096,7 @@ def __call__( top_k=top_k, min_p=min_p, typical_p=typical_p, - logprobs=top_logprobs if logprobs else None, + logprobs=(top_logprobs or 0) if logprobs else None, stream=stream, stop=stop, seed=seed, @@ -3631,7 +3631,7 @@ def raise_exception(message: str): top_k=top_k, min_p=min_p, typical_p=typical_p, - logprobs=top_logprobs if logprobs else None, + logprobs=(top_logprobs or 0) if logprobs else None, stream=stream, stop=stop, seed=seed, @@ -4288,7 +4288,7 @@ def chatml_function_calling( model=model, logits_processor=logits_processor, grammar=grammar, - logprobs=top_logprobs if logprobs else None, + logprobs=(top_logprobs or 0) if logprobs else None, ), stream=stream, ) @@ -4401,7 +4401,7 @@ def chatml_function_calling( typical_p=typical_p, stream=stream, stop=["<|im_end|>"], - logprobs=top_logprobs if logprobs else None, + logprobs=(top_logprobs or 0) if logprobs else None, max_tokens=None, presence_penalty=presence_penalty, frequency_penalty=frequency_penalty, diff --git a/tests/test_llama_chat_format.py b/tests/test_llama_chat_format.py index 18c7279cf..32d244d7e 100644 --- a/tests/test_llama_chat_format.py +++ b/tests/test_llama_chat_format.py @@ -92,3 +92,48 @@ def test_hf_tokenizer_config_str_to_chat_formatter(): ) assert chat_formatter_respoonse.prompt == ("[INST] Hello, world! [/INST]") + + +class _RecordingLlama: + verbose = False + + def __init__(self): + self.kwargs = {} + + def tokenize(self, text, add_bos=True, special=False): + return [1] + + def create_completion(self, **kwargs): + self.kwargs = kwargs + return { + "id": "cmpl-1", + "object": "text_completion", + "created": 0, + "model": "m", + "choices": [ + {"text": "a", "index": 0, "logprobs": None, "finish_reason": "stop"} + ], + "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}, + } + + +def test_chat_logprobs_without_top_logprobs_requests_token_logprobs(): + handler = llama_chat_format.Jinja2ChatFormatter( + template="{% for m in messages %}{{ m['content'] }}{% endfor %}", + eos_token="", + bos_token="", + ).to_chat_handler() + messages = [ChatCompletionRequestUserMessage(role="user", content="hi")] + + llama = _RecordingLlama() + handler(llama=llama, messages=messages, logprobs=True) + assert llama.kwargs["logprobs"] == 0 + + handler(llama=llama, messages=messages, logprobs=True, top_logprobs=3) + assert llama.kwargs["logprobs"] == 3 + + handler(llama=llama, messages=messages, logprobs=False, top_logprobs=3) + assert llama.kwargs["logprobs"] is None + + handler(llama=llama, messages=messages) + assert llama.kwargs["logprobs"] is None