File size: 3,492 Bytes
f730521
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""Has the caller finished? Asked of the baseline itself.

The small controller could not tell a thinking pause from a finished turn: that needs the
meaning of the words, and the 33B model has it. So at a silence the audio heard so far is
sent to the baseline with one question, the reply is opened past its reasoning block, and
only the first token is read: "yes" or "no". One prefill and one token, so the answer
comes back in about 0.15 s. No training is involved.

The answer is the model's single most likely word. Its probability would be better (a
threshold could be tuned), but asking this vLLM image for logprobs kills its engine on a
this GPU (see serving/launch_vllm.sh).
"""
from serving.baseline_client import human_turn

NL = chr(10)
PROMPTS = {
    # v1: the first wording tried (measured: 49% of pauses held, 82% of turn ends released)
    "v1": ("You are listening to one side of a phone call. The caller has just gone quiet. Decide whether the caller has "
           "finished what they wanted to say and is now waiting for a reply, or has only paused and will go on speaking. "
           "Answer with one word: yes if the caller has finished, no if the caller will go on."),
    # v2: says what "finished" means, so that a complete sentence that asks for nothing is not an invitation to reply
    "v2": ("You are the listener on a phone call. The caller has just gone quiet for a moment. Should you start replying now? "
           "Answer no if the caller's last words stop in the middle of a sentence (for example they end on a word like "
           "and, but, to, the, my, on, or a half-said phrase), or if the caller is still setting out information and has "
           "not yet asked their question or made their request. Answer yes only if the caller has finished a question, "
           "a request, or a remark that calls for your reply. Answer with one word: yes or no."),
}
EMPTY_THOUGHT = "<think>" + NL + NL + "</think>" + NL      # an empty reasoning block: the next token is the answer


async def _post(http, api_url, payload):
    async with http.post(api_url, json=payload) as r:
        if r.status != 200:
            raise RuntimeError(f"vLLM answered {r.status}: {(await r.text())[:300]}")
        return (await r.json())["choices"][0]["message"].get("content") or ""


async def judge_finished(http, api_url, model, pcm16, prompt="v2", think_tokens=0):
    """pcm16: the caller's audio so far (int16, 16 kHz). Returns True (finished), False (will go on) or None.

    With think_tokens > 0 the model may reason for up to that many tokens before it answers,
    which costs think_tokens / (tokens per second) of extra time."""
    base = {"model": model, "stream": False, "skip_special_tokens": False, "temperature": 0.0,
            "continue_final_message": True, "add_generation_prompt": False}
    head = [{"role": "system", "content": PROMPTS[prompt]}, human_turn(pcm16)]
    prefix = EMPTY_THOUGHT
    if think_tokens:
        thought = await _post(http, api_url, dict(base, max_tokens=think_tokens, stop=["</think>"],
                                                  messages=head + [{"role": "assistant", "content": "<think>" + NL}]))
        prefix = "<think>" + NL + thought.rstrip() + NL + "</think>" + NL
    word = (await _post(http, api_url, dict(base, max_tokens=1, messages=head + [{"role": "assistant", "content": prefix}]))).strip().lower()
    return True if word.startswith("yes") else False if word.startswith("no") else None