|
| 1 | +"""Tests for the logprobs request parameter and top_logprobs response data. |
| 2 | +
|
| 3 | +The Together API accepts ``logprobs`` as an integer between 0 and 20: the |
| 4 | +number of top tokens to return log probabilities for at each generation |
| 5 | +step, instead of only the sampled token (see issue #251). When top-k |
| 6 | +logprobs are requested, each choice's ``logprobs`` part carries a |
| 7 | +``top_logprobs`` list with one ``{token: logprob}`` dict per generated |
| 8 | +token. |
| 9 | +""" |
| 10 | + |
| 11 | +import warnings |
| 12 | + |
| 13 | +import pytest |
| 14 | +from pydantic import ValidationError |
| 15 | + |
| 16 | +from together.types import ChatCompletionRequest, CompletionRequest |
| 17 | +from together.types.chat_completions import ChatCompletionResponse |
| 18 | + |
| 19 | + |
| 20 | +MESSAGES = [{"role": "user", "content": "Say hello."}] |
| 21 | +MODEL = "meta-llama/Llama-3.3-70B-Instruct-Turbo" |
| 22 | + |
| 23 | +# Real response shape captured from the chat completions API with |
| 24 | +# ``logprobs=3`` (see issues #251 and #443): ``top_logprobs`` is a list |
| 25 | +# with one dict of the top-k alternatives per generated token. |
| 26 | +TOP_LOGPROBS = [ |
| 27 | + {"Hello": -2.6e-06, "hello": -13.5, " Hello": -13.875}, |
| 28 | + {".": -4.8e-05, "!": -10.0625, ".\n": -11.4375}, |
| 29 | +] |
| 30 | +RESPONSE_PAYLOAD = { |
| 31 | + "id": "889ee12e7b0b3c67", |
| 32 | + "object": "chat.completion", |
| 33 | + "created": 1709240335, |
| 34 | + "model": MODEL, |
| 35 | + "choices": [ |
| 36 | + { |
| 37 | + "index": 0, |
| 38 | + "finish_reason": "eos", |
| 39 | + "logprobs": { |
| 40 | + "tokens": ["Hello", "."], |
| 41 | + "token_logprobs": [-2.6e-06, -4.8e-05], |
| 42 | + "top_logprobs": TOP_LOGPROBS, |
| 43 | + }, |
| 44 | + "message": {"role": "assistant", "content": "Hello."}, |
| 45 | + } |
| 46 | + ], |
| 47 | + "usage": {"prompt_tokens": 4, "completion_tokens": 2, "total_tokens": 6}, |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.parametrize("logprobs", [-1, 21, 100]) |
| 52 | +def test_chat_request_rejects_out_of_range_logprobs(logprobs: int) -> None: |
| 53 | + with pytest.raises(ValidationError, match="between 0 and 20"): |
| 54 | + ChatCompletionRequest(model=MODEL, messages=MESSAGES, logprobs=logprobs) |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.parametrize("logprobs", [-1, 21, 100]) |
| 58 | +def test_completion_request_rejects_out_of_range_logprobs(logprobs: int) -> None: |
| 59 | + with pytest.raises(ValidationError, match="between 0 and 20"): |
| 60 | + CompletionRequest(model=MODEL, prompt="Say hello.", logprobs=logprobs) |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.parametrize("logprobs", [0, 1, 20]) |
| 64 | +def test_chat_request_accepts_in_range_logprobs(logprobs: int) -> None: |
| 65 | + request = ChatCompletionRequest(model=MODEL, messages=MESSAGES, logprobs=logprobs) |
| 66 | + |
| 67 | + # 0 is a valid value and must survive serialization of the payload. |
| 68 | + assert request.model_dump(exclude_none=True)["logprobs"] == logprobs |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.parametrize("logprobs", [0, 1, 20]) |
| 72 | +def test_completion_request_accepts_in_range_logprobs(logprobs: int) -> None: |
| 73 | + request = CompletionRequest(model=MODEL, prompt="Say hello.", logprobs=logprobs) |
| 74 | + |
| 75 | + assert request.model_dump(exclude_none=True)["logprobs"] == logprobs |
| 76 | + |
| 77 | + |
| 78 | +def test_request_logprobs_defaults_to_omitted() -> None: |
| 79 | + request = ChatCompletionRequest(model=MODEL, messages=MESSAGES) |
| 80 | + |
| 81 | + assert "logprobs" not in request.model_dump(exclude_none=True) |
| 82 | + |
| 83 | + |
| 84 | +def test_top_logprobs_survive_parsing_and_model_dump() -> None: |
| 85 | + """Top-k alternatives must round-trip through the response models. |
| 86 | +
|
| 87 | + Guards against the mistyping reported in issue #443, where |
| 88 | + ``top_logprobs`` declared as ``Dict[str, float]`` (instead of a list |
| 89 | + of per-token dicts) made ``model_dump()`` emit |
| 90 | + ``PydanticSerializationUnexpectedValue`` warnings. |
| 91 | + """ |
| 92 | + response = ChatCompletionResponse(**RESPONSE_PAYLOAD) |
| 93 | + |
| 94 | + assert response.choices is not None |
| 95 | + logprobs_part = response.choices[0].logprobs |
| 96 | + assert logprobs_part is not None |
| 97 | + assert logprobs_part.top_logprobs == TOP_LOGPROBS |
| 98 | + |
| 99 | + with warnings.catch_warnings(): |
| 100 | + warnings.simplefilter("error") |
| 101 | + dumped = response.model_dump() |
| 102 | + |
| 103 | + assert dumped["choices"][0]["logprobs"]["top_logprobs"] == TOP_LOGPROBS |
0 commit comments