Every call that no later request continues (call_llm_direct, call_llm_streaming, the warm-up and the v1 intent judge) now sends "strata_checkpoint": false on the OpenAI-compatible path. Strata's Qwen3.6 engine then gives that call's session away first, so with two or more requests at once a reply's tool turn keeps its session through the short calls made between its steps. The reply loop's chat_with_messages is not marked (its next step continues it); the Ollama API gets no new field. With one request at once nothing changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
272 lines
12 KiB
Python
272 lines
12 KiB
Python
"""The brain talks to an OpenAI-compatible server (Strata, llama.cpp) as well as Ollama.
|
|
|
|
A base URL that ends in ``/v1`` selects the OpenAI-compatible API
|
|
(``/chat/completions``, ``/embeddings``). The callers keep the Ollama-shaped
|
|
messages and responses they already use, so these tests check what goes over
|
|
the wire and what the caller gets back:
|
|
|
|
- requests go to ``/chat/completions`` with OpenAI fields (``max_tokens``,
|
|
thinking switch, JSON-string tool arguments, no private ``_`` keys),
|
|
- responses come back in the Ollama shape the reply engine reads
|
|
(``message.content``, ``message.tool_calls[*].function.arguments`` as a dict,
|
|
``message.thinking``),
|
|
- streaming, embeddings, the warm-up and the intent judge use the same API.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from jarvis import llm
|
|
from jarvis.llm import (
|
|
ToolsNotSupportedError,
|
|
call_llm_direct,
|
|
call_llm_streaming,
|
|
chat_with_messages,
|
|
is_openai_compatible,
|
|
)
|
|
|
|
V1 = "http://127.0.0.1:8080/v1"
|
|
|
|
|
|
def _response(body, status=200):
|
|
resp = MagicMock()
|
|
resp.status_code = status
|
|
resp.json.return_value = body
|
|
resp.__enter__ = lambda s: s
|
|
resp.__exit__ = lambda s, *a: False
|
|
if status >= 400:
|
|
import requests
|
|
|
|
err = requests.exceptions.HTTPError(f"{status}")
|
|
err.response = resp
|
|
resp.raise_for_status.side_effect = err
|
|
else:
|
|
resp.raise_for_status.return_value = None
|
|
return resp
|
|
|
|
|
|
def _completion(content="", tool_calls=None, reasoning=None, finish="stop"):
|
|
message = {"role": "assistant", "content": content}
|
|
if tool_calls is not None:
|
|
message["tool_calls"] = tool_calls
|
|
if reasoning is not None:
|
|
message["reasoning_content"] = reasoning
|
|
return {
|
|
"choices": [{"index": 0, "message": message, "finish_reason": finish}],
|
|
"usage": {"prompt_tokens": 12, "completion_tokens": 3},
|
|
}
|
|
|
|
|
|
def test_v1_suffix_selects_the_openai_api():
|
|
assert is_openai_compatible("http://127.0.0.1:8080/v1")
|
|
assert is_openai_compatible("http://127.0.0.1:8080/v1/")
|
|
assert not is_openai_compatible("http://127.0.0.1:11434")
|
|
assert not is_openai_compatible("")
|
|
|
|
|
|
def test_chat_with_messages_returns_tool_calls_in_the_shape_the_engine_reads():
|
|
tc = [{"id": "call_7", "type": "function",
|
|
"function": {"name": "getWeather", "arguments": "{\"location\": \"서울\"}"}}]
|
|
with patch("jarvis.llm.requests.post", return_value=_response(_completion("", tc, reasoning="hmm"))) as post:
|
|
data = chat_with_messages(V1, "qwen", [{"role": "user", "content": "서울 날씨"}],
|
|
tools=[{"type": "function", "function": {"name": "getWeather"}}])
|
|
|
|
assert post.call_args.args[0] == f"{V1}/chat/completions"
|
|
msg = data["message"]
|
|
assert msg["content"] == ""
|
|
assert msg["thinking"] == "hmm"
|
|
call = msg["tool_calls"][0]
|
|
assert call["id"] == "call_7"
|
|
assert call["function"]["name"] == "getWeather"
|
|
assert call["function"]["arguments"] == {"location": "서울"}
|
|
|
|
|
|
def test_tool_call_without_id_gets_one_so_the_tool_result_can_refer_to_it():
|
|
tc = [{"type": "function", "function": {"name": "stop", "arguments": "{}"}}]
|
|
with patch("jarvis.llm.requests.post", return_value=_response(_completion("", tc))):
|
|
data = chat_with_messages(V1, "qwen", [{"role": "user", "content": "x"}], tools=[{"type": "function"}])
|
|
assert data["message"]["tool_calls"][0]["id"]
|
|
|
|
|
|
def test_history_is_sent_as_openai_messages():
|
|
history = [
|
|
{"role": "system", "content": "sys", "_is_tool_guidance": True},
|
|
{"role": "user", "content": "서울 날씨"},
|
|
{"role": "assistant", "content": "", "thinking": "plan",
|
|
"tool_calls": [{"id": "call_1", "function": {"name": "getWeather", "arguments": {"location": "서울"}}}]},
|
|
{"role": "tool", "tool_call_id": "call_1", "tool_name": "getWeather", "tool_failed": False,
|
|
"content": "맑음"},
|
|
]
|
|
with patch("jarvis.llm.requests.post", return_value=_response(_completion("맑아요"))) as post:
|
|
chat_with_messages(V1, "qwen", history, extra_options={"num_predict": 512, "temperature": 0.2})
|
|
|
|
body = post.call_args.kwargs["json"]
|
|
sent = body["messages"]
|
|
assert sent[0] == {"role": "system", "content": "sys"}
|
|
assistant = sent[2]
|
|
assert "thinking" not in assistant
|
|
call = assistant["tool_calls"][0]
|
|
assert call["type"] == "function"
|
|
assert json.loads(call["function"]["arguments"]) == {"location": "서울"}
|
|
assert sent[3] == {"role": "tool", "tool_call_id": "call_1", "content": "맑음"}
|
|
assert body["max_tokens"] == 512
|
|
assert body["temperature"] == 0.2
|
|
assert body["stream"] is False
|
|
for ollama_only in ("options", "keep_alive", "think"):
|
|
assert ollama_only not in body
|
|
# The history the caller keeps is not changed by the conversion.
|
|
assert history[2]["tool_calls"][0]["function"]["arguments"] == {"location": "서울"}
|
|
|
|
|
|
@pytest.mark.parametrize("thinking", [False, True])
|
|
def test_thinking_switch_reaches_the_chat_template(thinking):
|
|
with patch("jarvis.llm.requests.post", return_value=_response(_completion("ok"))) as post:
|
|
chat_with_messages(V1, "qwen", [{"role": "user", "content": "x"}], thinking=thinking)
|
|
assert post.call_args.kwargs["json"]["chat_template_kwargs"] == {"enable_thinking": thinking}
|
|
|
|
|
|
def test_rejected_tools_raise_tools_not_supported():
|
|
with patch("jarvis.llm.requests.post", return_value=_response({"error": "tools"}, status=400)):
|
|
with pytest.raises(ToolsNotSupportedError):
|
|
chat_with_messages(V1, "qwen", [{"role": "user", "content": "x"}], tools=[{"type": "function"}])
|
|
|
|
|
|
def test_call_llm_direct_returns_the_answer_text():
|
|
with patch("jarvis.llm.requests.post", return_value=_response(_completion("{\"a\": 1}"))) as post:
|
|
out = call_llm_direct(V1, "qwen", "system", "user", temperature=0.0)
|
|
assert out == "{\"a\": 1}"
|
|
body = post.call_args.kwargs["json"]
|
|
assert post.call_args.args[0] == f"{V1}/chat/completions"
|
|
assert body["messages"] == [{"role": "system", "content": "system"}, {"role": "user", "content": "user"}]
|
|
assert body["temperature"] == 0.0
|
|
|
|
|
|
def test_call_llm_direct_returns_none_on_server_error():
|
|
with patch("jarvis.llm.requests.post", return_value=_response({"error": "x"}, status=500)):
|
|
assert call_llm_direct(V1, "qwen", "s", "u") is None
|
|
|
|
|
|
def _sse(*chunks):
|
|
lines = [f"data: {json.dumps(c)}".encode() for c in chunks] + [b"data: [DONE]"]
|
|
resp = _response(None)
|
|
resp.iter_lines.return_value = lines
|
|
return resp
|
|
|
|
|
|
def test_streaming_passes_each_piece_of_text_on():
|
|
resp = _sse(
|
|
{"choices": [{"delta": {"role": "assistant"}}]},
|
|
{"choices": [{"delta": {"reasoning_content": "think"}}]},
|
|
{"choices": [{"delta": {"content": "안녕"}}]},
|
|
{"choices": [{"delta": {"content": "하세요."}}]},
|
|
{"choices": [], "usage": {"completion_tokens": 2}},
|
|
)
|
|
got = []
|
|
with patch("jarvis.llm.requests.post", return_value=resp) as post:
|
|
out = call_llm_streaming(V1, "qwen", "s", "u", on_token=got.append)
|
|
assert out == "안녕하세요."
|
|
assert got == ["안녕", "하세요."]
|
|
body = post.call_args.kwargs["json"]
|
|
assert body["stream"] is True
|
|
assert post.call_args.kwargs["stream"] is True
|
|
|
|
|
|
def test_embeddings_use_the_openai_endpoint():
|
|
from jarvis.memory.embeddings import get_embedding
|
|
|
|
with patch("jarvis.memory.embeddings.requests") as req:
|
|
req.post.return_value = _response({"data": [{"embedding": [0.5, 0.25]}]})
|
|
vec = get_embedding("hello", V1, "embed-model")
|
|
assert vec == [0.5, 0.25]
|
|
assert req.post.call_args.args[0] == f"{V1}/embeddings"
|
|
assert req.post.call_args.kwargs["json"] == {"model": "embed-model", "input": "hello"}
|
|
|
|
|
|
def test_embeddings_fail_open_when_the_server_has_none():
|
|
from jarvis.memory.embeddings import get_embedding
|
|
|
|
with patch("jarvis.memory.embeddings.requests") as req:
|
|
req.post.return_value = _response({"error": "not found"}, status=404)
|
|
assert get_embedding("hello", V1, "embed-model") is None
|
|
|
|
|
|
def test_warm_up_asks_for_one_token_through_chat_completions():
|
|
from jarvis.listening.intent_judge import warm_up_ollama_model
|
|
|
|
with patch("jarvis.llm.requests.post", return_value=_response(_completion("x"))) as post:
|
|
assert warm_up_ollama_model(V1, "qwen", timeout=5) is True
|
|
assert post.call_args.args[0] == f"{V1}/chat/completions"
|
|
assert post.call_args.kwargs["json"]["max_tokens"] == 1
|
|
|
|
|
|
def test_intent_judge_reads_an_openai_answer():
|
|
from jarvis.listening.intent_judge import IntentJudge, IntentJudgeConfig
|
|
from jarvis.listening.transcript_buffer import TranscriptSegment
|
|
|
|
answer = json.dumps({"directed": True, "query": "what time is it", "stop": False,
|
|
"confidence": "high", "reasoning": "wake word"})
|
|
judge = IntentJudge(IntentJudgeConfig(ollama_base_url=V1, model="qwen"))
|
|
seg = TranscriptSegment(text="jarvis what time is it", start_time=1.0, end_time=2.0)
|
|
with patch("jarvis.llm.requests.post", return_value=_response(_completion(answer))) as post:
|
|
judgment = judge.judge([seg], wake_timestamp=1.0)
|
|
assert post.call_args.args[0] == f"{V1}/chat/completions"
|
|
assert judgment is not None and judgment.directed and judgment.query == "what time is it"
|
|
|
|
|
|
def test_ollama_urls_still_use_the_ollama_api():
|
|
with patch("jarvis.llm.requests.post", return_value=_response({"message": {"content": "ok"}})) as post:
|
|
assert call_llm_direct("http://127.0.0.1:11434", "m", "s", "u") == "ok"
|
|
assert post.call_args.args[0].endswith("/api/chat")
|
|
assert llm.OLLAMA_NUM_CTX == post.call_args.kwargs["json"]["options"]["num_ctx"]
|
|
|
|
|
|
def test_dictation_clean_up_uses_chat_completions():
|
|
from jarvis.dictation.dictation_engine import _llm_clean_dictation
|
|
|
|
with patch("jarvis.llm.requests.post", return_value=_response(_completion("hello there"))) as post:
|
|
out = _llm_clean_dictation("um hello there", V1, model="qwen")
|
|
assert out == "hello there"
|
|
assert post.call_args.args[0] == f"{V1}/chat/completions"
|
|
|
|
|
|
# One-shot calls (docs/llm_contexts.md, transport note): a call no later request continues says so
|
|
# ("strata_checkpoint": false), so Strata gives its session away first and a reply's tool turn keeps
|
|
# its own session through the short calls made between its steps.
|
|
|
|
|
|
def test_short_calls_are_marked_one_shot():
|
|
from jarvis.listening.intent_judge import IntentJudge, IntentJudgeConfig, warm_up_ollama_model
|
|
from jarvis.listening.transcript_buffer import TranscriptSegment
|
|
|
|
answer = json.dumps({"directed": True, "query": "q", "stop": False, "confidence": "high", "reasoning": "r"})
|
|
seg = TranscriptSegment(text="jarvis q", start_time=1.0, end_time=2.0)
|
|
calls = {
|
|
"direct": lambda: call_llm_direct(V1, "qwen", "s", "u"),
|
|
"streaming": lambda: call_llm_streaming(V1, "qwen", "s", "u"),
|
|
"warm-up": lambda: warm_up_ollama_model(V1, "qwen", timeout=5),
|
|
"intent judge": lambda: IntentJudge(IntentJudgeConfig(ollama_base_url=V1, model="qwen")).judge([seg], wake_timestamp=1.0),
|
|
}
|
|
for name, call in calls.items():
|
|
resp = _sse({"choices": [{"delta": {"content": "x"}}]}) if name == "streaming" else _response(_completion(answer))
|
|
with patch("jarvis.llm.requests.post", return_value=resp) as post:
|
|
call()
|
|
assert post.call_args.kwargs["json"].get("strata_checkpoint") is False, name
|
|
|
|
|
|
def test_the_reply_loop_is_not_marked_one_shot():
|
|
# its next step continues this one, so its session must be kept
|
|
with patch("jarvis.llm.requests.post", return_value=_response(_completion("ok"))) as post:
|
|
chat_with_messages(V1, "qwen", [{"role": "user", "content": "hi"}],
|
|
tools=[{"type": "function", "function": {"name": "getWeather"}}])
|
|
assert "strata_checkpoint" not in post.call_args.kwargs["json"]
|
|
|
|
|
|
def test_ollama_api_gets_no_one_shot_field():
|
|
with patch("jarvis.llm.requests.post", return_value=_response({"message": {"content": "ok"}})) as post:
|
|
call_llm_direct("http://127.0.0.1:11434", "m", "s", "u")
|
|
assert "strata_checkpoint" not in post.call_args.kwargs["json"]
|