Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/utils/llm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ def call_llm_for_system_prompt(
temperature=0.3,
top_p=0.95,
)
# The evaluator consumes the response as a stream. Remove any
# construction-time stream value before forcing the call-level
# streaming mode, otherwise Python receives duplicate keywords.
completion_kwargs.pop("stream", None)
current_request = llm.client.chat.completions.create(stream=True, **completion_kwargs)
token_join: List[str] = []
is_thinking = False
Expand Down
29 changes: 29 additions & 0 deletions test/backend/utils/test_llm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,35 @@ def test_call_llm_for_system_prompt_success(self, mocker: MockFixture):
timeout_seconds=None,
)

def test_call_llm_for_system_prompt_removes_prepared_stream(self, mocker: MockFixture):
mock_get_model_by_id = mocker.patch('backend.utils.llm_utils.get_model_by_model_id')
mock_adapter = mocker.patch('backend.utils.llm_utils.get_llm_adapter_from_config')

mock_get_model_by_id.return_value = {
"base_url": "http://example.com",
"api_key": "fake-key",
"model_factory": "qwen",
}

mock_llm_instance = mock_adapter.return_value
mock_chunk = MagicMock()
mock_chunk.choices = [MagicMock()]
mock_chunk.choices[0].delta.content = "Generated prompt"
mock_llm_instance.client = MagicMock()
mock_llm_instance.client.chat.completions.create.return_value = [mock_chunk]
mock_llm_instance._prepare_completion_kwargs.return_value = {
"stream": False,
"temperature": 0.3,
}

result = call_llm_for_system_prompt(1, "user prompt", "system prompt")

assert result == "Generated prompt"
mock_llm_instance.client.chat.completions.create.assert_called_once_with(
stream=True,
temperature=0.3,
)

def test_call_llm_for_system_prompt_exception(self, mocker: MockFixture):
from consts.error_code import ErrorCode
from consts.exceptions import AppException
Expand Down
Loading