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
2 changes: 1 addition & 1 deletion api/services/message_suggested_questions_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from core.app.entities.app_invoke_entities import CreditUsageCreatedBy, get_credit_usage_app_type
from core.llm_generator.llm_generator import _normalize_completion_params
from core.llm_generator.output_parser.suggested_questions_after_answer import SuggestedQuestionsAfterAnswerOutputParser
from core.model_context import use_credit_usage_metadata, with_credit_usage_created_by
from core.model_manager import ModelInstance, ModelManager
from core.ops.entities.trace_entity import TraceTaskName
Expand All @@ -23,6 +22,7 @@
from graphon.model_runtime.entities.llm_entities import LLMResult
from graphon.model_runtime.entities.message_entities import PromptMessage, UserPromptMessage
from graphon.model_runtime.entities.model_entities import ModelType, ParameterType
from services.message_suggested_questions_parser import SuggestedQuestionsAfterAnswerOutputParser

if TYPE_CHECKING:
from core.memory.token_buffer_memory import PreparedHistory
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Parse model responses for the shared suggested-questions service."""

import json
import logging
import re
from collections.abc import Sequence

from core.llm_generator.prompts import DEFAULT_SUGGESTED_QUESTIONS_AFTER_ANSWER_INSTRUCTION_PROMPT
Expand All @@ -24,11 +25,12 @@ def get_format_instructions(self) -> str:

def parse(self, text: str) -> Sequence[str]:
stripped_text = text.strip()
action_match = re.search(r"\[.*?\]", stripped_text, re.DOTALL)
array_start = stripped_text.find("[")
questions: list[str] = []
if action_match is not None:
if array_start != -1:
try:
json_obj = json.loads(action_match.group(0).strip())
# Let the decoder honor quoted brackets, escapes, and nested values.
json_obj, _ = json.JSONDecoder().raw_decode(stripped_text[array_start:])
except json.JSONDecodeError as exc:
logger.warning("Failed to decode suggested questions payload: %s", exc)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import core.plugin.impl.base as plugin_base
import core.plugin.impl.model_runtime as model_runtime_module
from core.llm_generator.llm_generator import _normalize_completion_params
from core.llm_generator.output_parser.suggested_questions_after_answer import SuggestedQuestionsAfterAnswerOutputParser
from core.llm_generator.prompts import DEFAULT_SUGGESTED_QUESTIONS_AFTER_ANSWER_INSTRUCTION_PROMPT
from core.memory.token_buffer_memory import HistoryPrompt, PreparedHistory
from core.model_context import get_credit_usage_metadata
from core.plugin.entities.plugin_daemon import PluginDaemonInnerError
Expand Down Expand Up @@ -177,34 +175,6 @@ def test_token_transport_failure_propagates_before_best_effort_generation(
assert _get_plugin_daemon_request_timeout() == timeout


@pytest.mark.parametrize(
("text", "expected"),
[
('["Question 1?", "还有吗?"]', ["Question 1?", "还有吗?"]),
('Here are questions:\n["Next?"]', ["Next?"]),
('["Next?", 1, null, true]', ["Next?"]),
("[]", []),
("", []),
("not a question array", []),
('["unfinished]', []),
],
)
def test_response_parser_handles_question_arrays(text: str, expected: list[str]) -> None:
assert SuggestedQuestionsAfterAnswerOutputParser().parse(text) == expected


@pytest.mark.parametrize("instruction", [None, "", " "])
def test_empty_instruction_uses_default_prompt(instruction: str | None) -> None:
parser = SuggestedQuestionsAfterAnswerOutputParser(instruction_prompt=instruction)
assert parser.get_format_instructions() == DEFAULT_SUGGESTED_QUESTIONS_AFTER_ANSWER_INSTRUCTION_PROMPT


def test_custom_instruction_preserves_json_output_requirement() -> None:
instructions = SuggestedQuestionsAfterAnswerOutputParser("Ask a follow-up").get_format_instructions()
assert instructions.startswith("Ask a follow-up\n")
assert 'JSON array like ["question1", "question2", "question3"]' in instructions


@pytest.mark.parametrize("limit", [0, -1, -0.5])
def test_non_positive_token_limits_are_removed_without_mutating_configuration(limit: int | float) -> None:
configured: dict[str, object] = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Suggested-question payload parsing without model/provider I/O."""

import json

import pytest

from core.llm_generator.prompts import DEFAULT_SUGGESTED_QUESTIONS_AFTER_ANSWER_INSTRUCTION_PROMPT
from services.message_suggested_questions_parser import SuggestedQuestionsAfterAnswerOutputParser


@pytest.mark.parametrize(
("text", "expected"),
[
('["How do I use arr[0]?", "Next?"]', ["How do I use arr[0]?", "Next?"]),
('["What does ] mean?", "And [?"]', ["What does ] mean?", "And [?"]),
('Questions:\n```json\n["Can I use [a-z]?", "Next?"]\n```', ["Can I use [a-z]?", "Next?"]),
(json.dumps(['How do I quote "]"?', "Next?"]), ['How do I quote "]"?', "Next?"]),
('["Next?", ["nested"], {"name": "value"}, 1, null, true]', ["Next?"]),
('["Next?"] trailing prose', ["Next?"]),
('["unfinished]', []),
("not a question array", []),
("[]", []),
],
)
def test_parse_suggested_question_payload(text: str, expected: list[str]) -> None:
assert SuggestedQuestionsAfterAnswerOutputParser().parse(text) == expected


@pytest.mark.parametrize(
("text", "expected"),
[
('["Question 1?", "还有吗?"]', ["Question 1?", "还有吗?"]),
('Here are questions:\n["Next?"]', ["Next?"]),
('["Next?", 1, null, true]', ["Next?"]),
("[]", []),
("", []),
("not a question array", []),
('["unfinished]', []),
],
)
def test_response_parser_handles_question_arrays(text: str, expected: list[str]) -> None:
assert SuggestedQuestionsAfterAnswerOutputParser().parse(text) == expected


@pytest.mark.parametrize("instruction", [None, "", " "])
def test_empty_instruction_uses_default_prompt(instruction: str | None) -> None:
parser = SuggestedQuestionsAfterAnswerOutputParser(instruction_prompt=instruction)
assert parser.get_format_instructions() == DEFAULT_SUGGESTED_QUESTIONS_AFTER_ANSWER_INSTRUCTION_PROMPT


def test_custom_instruction_preserves_json_output_requirement() -> None:
instructions = SuggestedQuestionsAfterAnswerOutputParser("Ask a follow-up").get_format_instructions()
assert instructions.startswith("Ask a follow-up\n")
assert 'JSON array like ["question1", "question2", "question3"]' in instructions
2 changes: 1 addition & 1 deletion api/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading