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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
# word, so the apostrophes in "don't ... it's" do not mask the text between them.
_FORMULA_SPAN_RE = re.compile(r"`+[^`]*`+|\{[^{}]*\}|\"[^\"\n]*\"|(?<!\w)'[^'\n]*'(?!\w)")

# Where one sentence ends and the next starts, for asks_in_text.
_SENTENCE_END_RE = re.compile(r"(?<=[.?!])\s+")
# Checked after _CONFIRMATION_RE, so "please confirm" stays a confirmation.
_REQUEST_RE = re.compile(
r"\bplease\s+(?:select|choose|pick|specify|provide|tell\s+me|let\s+me\s+know|clarify|share)\b"
Expand Down Expand Up @@ -106,6 +108,20 @@ def classify_reply(chat_result: ChatResult, rendered_text: str) -> ReplyKind:
return "no_action"


def asks_in_text(chat_result: ChatResult, rendered_text: str) -> bool:
"""Whether a reply without the expected output asks the user something, read for the clarification checks.

``classify_reply`` reads "Did you mean A or B? Please confirm." as a confirmation, which is right for
what the simulated user says back. Here it is a question: a sentence that ends in a question mark and
is not itself a confirmation phrase asks, whatever confirmation follows it.
"""
kind = classify_reply(chat_result, rendered_text)
if kind != "confirmation" or chat_result.alert_proposals:
return kind == "question"
sentences = _SENTENCE_END_RE.split(rendered_text.strip())
return any(s.rstrip().endswith("?") and not _CONFIRMATION_RE.search(s) for s in sentences)


@dataclass
class TranscriptEntry:
role: Literal["user", "assistant"]
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ class _SseAccumulator:
alert_proposals: list[dict[str, Any]] = field(default_factory=list)
search_results: list[dict[str, Any]] = field(default_factory=list)
unhandled_parts: list[dict[str, Any]] = field(default_factory=list)
clarifying_questions_item_id: str | None = None
tool_call_events: list[dict[str, Any]] = field(default_factory=list)
call_id_to_event_index: dict[str, int] = field(default_factory=dict)
reasoning_steps: list[dict[str, Any]] = field(default_factory=list)
Expand All @@ -226,7 +227,8 @@ def _handle_text(content: dict[str, Any], acc: _SseAccumulator) -> None:
acc.text_parts.append(text)


def _handle_multipart(content: dict[str, Any], acc: _SseAccumulator) -> None:
def _handle_multipart(content: dict[str, Any], acc: _SseAccumulator, item_id: str | None = None) -> None:
"""Sort the item's parts; a ``clarifyingQuestions`` part keeps ``item_id``, since answers number questions per item."""
for part in content.get("parts", []):
ptype = part.get("type")
if ptype == "text":
Expand All @@ -247,7 +249,8 @@ def _handle_multipart(content: dict[str, Any], acc: _SseAccumulator) -> None:
else:
if ptype not in _KNOWN_PART_TYPES:
_log.warning("unknown multipart part type %r; captured as an unhandled part", ptype)
acc.unhandled_parts.append(part)
tagged = ptype == "clarifyingQuestions" and item_id
acc.unhandled_parts.append({**part, "itemId": item_id} if tagged else part)


def _handle_reasoning(content: dict[str, Any], acc: _SseAccumulator) -> None:
Expand Down Expand Up @@ -300,6 +303,7 @@ def _build_chat_result(acc: _SseAccumulator) -> ChatResult:
"alertProposals": acc.alert_proposals,
"searchResults": acc.search_results,
"unhandledParts": acc.unhandled_parts,
"clarifyingQuestionsItemId": acc.clarifying_questions_item_id,
"toolCallEvents": acc.tool_call_events,
"reasoningStepCount": len(acc.reasoning_steps),
"reasoningSteps": [step["summary"] for step in acc.reasoning_steps],
Expand Down Expand Up @@ -439,7 +443,9 @@ def parse_sse_lines(lines: Iterable[str]) -> ChatResult:
if ctype == "text":
_handle_text(content, acc)
elif ctype == "multipart":
_handle_multipart(content, acc)
_handle_multipart(content, acc, item_id=item.get("itemId"))
if any(p.get("type") == "clarifyingQuestions" for p in content.get("parts", [])):
acc.clarifying_questions_item_id = item.get("itemId") or acc.clarifying_questions_item_id
elif ctype == "reasoning":
_handle_reasoning(content, acc)
elif ctype == "toolCall":
Expand Down Expand Up @@ -540,11 +546,31 @@ def delete_conversation(self, conversation_id: str) -> None:

def send_message(
self, conversation_id: str, question: str, *, user_context: dict[str, Any] | None = None
) -> ChatResult:
return self._send(conversation_id, {"type": "text", "text": question}, user_context=user_context)

def send_clarifying_answers(
self,
conversation_id: str,
questions_item_id: str,
answers: list[dict[str, Any]],
*,
user_context: dict[str, Any] | None = None,
) -> ChatResult:
"""Answer a questionnaire the way the chat panel does: positions plus the texts shown.

A question left out of ``answers`` is skipped; an empty list skips them all.
"""
content = {"type": "clarifyingAnswers", "questionsItemId": questions_item_id, "answers": answers}
return self._send(conversation_id, content, user_context=user_context)

def _send(
self, conversation_id: str, content: dict[str, Any], *, user_context: dict[str, Any] | None = None
) -> ChatResult:
url = f"{self._base}/{conversation_id}/messages"
join_headers, joined = self._join_item_trace(conversation_id)
headers = {**self._auth, "Accept": "text/event-stream", "Content-Type": "application/json", **join_headers}
body: dict[str, Any] = {"item": {"role": "user", "content": {"type": "text", "text": question}}}
body: dict[str, Any] = {"item": {"role": "user", "content": content}}
options: dict[str, str] = {}
if self._reasoning_effort is not None:
options["reasoningEffort"] = self._reasoning_effort
Expand Down
3 changes: 3 additions & 0 deletions packages/gooddata-eval/src/gooddata_eval/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ class ChatResult(BaseModel):
alert_proposals: list[dict] = Field(default_factory=list, alias="alertProposals")
search_results: list[dict] = Field(default_factory=list, alias="searchResults")
unhandled_parts: list[dict] = Field(default_factory=list, alias="unhandledParts")
# The itemId of the assistant item holding a clarifyingQuestions part: a clarifyingAnswers
# message names it as questionsItemId.
clarifying_questions_item_id: str | None = Field(default=None, alias="clarifyingQuestionsItemId")
tool_call_events: list[ToolCallEvent] = Field(default_factory=list, alias="toolCallEvents")
reasoning_step_count: int = Field(default=0, alias="reasoningStepCount")
reasoning_steps: list[str] = Field(default_factory=list, alias="reasoningSteps")
Expand Down
Loading
Loading