tryParseEvaluationResponseWithTags method

bool tryParseEvaluationResponseWithTags(
  1. ChatResponse response,
  2. Duration duration
)

Parses a tagged evaluation response (XML <S0>, <S1>, <S2> format).

Returns true if a score was successfully parsed.

Implementation

bool tryParseEvaluationResponseWithTags(
    ChatResponse response, Duration duration) {
  final text = response.text;
  if (text.isEmpty) return false;

  final scoreMatch =
      RegExp(r'<S2>\s*(\d+(?:\.\d+)?)\s*</S2>').firstMatch(text);
  if (scoreMatch == null) return false;

  final score = double.tryParse(scoreMatch.group(1) ?? '');
  if (score == null) return false;
  value = score;

  final explanationMatch =
      RegExp(r'<S1>(.*?)</S1>', dotAll: true).firstMatch(text);
  if (explanationMatch != null) {
    reason = explanationMatch.group(1)?.trim();
  }

  addOrUpdateChatMetadata(response, duration: duration);
  return true;
}