Hermes SIQ-1-35B on ZeroGPU (llama-cpp-python recipe)
Browse files
app.py
CHANGED
|
@@ -570,6 +570,27 @@ def _tools_md(trace):
|
|
| 570 |
return "\n\n".join(out)
|
| 571 |
|
| 572 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 573 |
def _count_tokens(llm, text):
|
| 574 |
"""Best-effort token count for the live ↑/↓ counter."""
|
| 575 |
try:
|
|
@@ -652,10 +673,9 @@ def _agent_stream(message, history, system_prompt, temperature, max_tokens, use_
|
|
| 652 |
temperature=float(temperature),
|
| 653 |
stream=True,
|
| 654 |
):
|
| 655 |
-
|
| 656 |
-
if
|
| 657 |
-
finish =
|
| 658 |
-
delta = ch["delta"].get("content", "")
|
| 659 |
if not delta:
|
| 660 |
continue
|
| 661 |
out += delta
|
|
|
|
| 570 |
return "\n\n".join(out)
|
| 571 |
|
| 572 |
|
| 573 |
+
def _g(o, key, default=None):
|
| 574 |
+
"""Get a field from a value that may be a dict OR a typed object."""
|
| 575 |
+
if isinstance(o, dict):
|
| 576 |
+
return o.get(key, default)
|
| 577 |
+
return getattr(o, key, default)
|
| 578 |
+
|
| 579 |
+
|
| 580 |
+
def _chunk_fields(chunk):
|
| 581 |
+
"""Extract (content_delta, finish_reason) from a streaming chat chunk.
|
| 582 |
+
llama-cpp-python returns either dict chunks or typed objects (Choice) across
|
| 583 |
+
builds, so access both ways."""
|
| 584 |
+
choices = _g(chunk, "choices") or []
|
| 585 |
+
if not choices:
|
| 586 |
+
return "", None
|
| 587 |
+
ch = choices[0]
|
| 588 |
+
finish = _g(ch, "finish_reason")
|
| 589 |
+
delta = _g(ch, "delta") or {}
|
| 590 |
+
content = _g(delta, "content", "") or ""
|
| 591 |
+
return content, finish
|
| 592 |
+
|
| 593 |
+
|
| 594 |
def _count_tokens(llm, text):
|
| 595 |
"""Best-effort token count for the live ↑/↓ counter."""
|
| 596 |
try:
|
|
|
|
| 673 |
temperature=float(temperature),
|
| 674 |
stream=True,
|
| 675 |
):
|
| 676 |
+
delta, fr = _chunk_fields(chunk)
|
| 677 |
+
if fr:
|
| 678 |
+
finish = fr
|
|
|
|
| 679 |
if not delta:
|
| 680 |
continue
|
| 681 |
out += delta
|