|
| 1 | +# --------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# --------------------------------------------------------- |
| 4 | + |
| 5 | +import logging |
| 6 | +import uuid |
| 7 | +import os |
| 8 | +import json |
| 9 | +import traceback |
| 10 | +import asyncio |
| 11 | +import re |
| 12 | +from typing import Dict, Optional, Any, Tuple, List |
| 13 | +from azure.ai.evaluation._common.rai_service import evaluate_with_rai_service |
| 14 | +from azure.ai.evaluation.simulator._model_tools._generated_rai_client import GeneratedRAIClient |
| 15 | +from pyrit.models import PromptRequestResponse, construct_response_from_request |
| 16 | +from pyrit.prompt_target import PromptChatTarget |
| 17 | +from .constants import USER_AGENT |
| 18 | +from .metric_mapping import get_metric_from_risk_category |
| 19 | +from .._attack_objective_generator import RiskCategory |
| 20 | + |
| 21 | + |
| 22 | +class RAIServiceEvalChatTarget(PromptChatTarget): |
| 23 | + """A class to handle chat-based interactions with the RAI service for evaluation purposes.""" |
| 24 | + |
| 25 | + def __init__( |
| 26 | + self, |
| 27 | + credential: Any, |
| 28 | + azure_ai_project, |
| 29 | + risk_category: RiskCategory, |
| 30 | + logger: Optional[logging.Logger] = None, |
| 31 | + evaluator_name: Optional[str] = None, |
| 32 | + ) -> None: |
| 33 | + """Initialize the RAIServiceEvalChatTarget. |
| 34 | +
|
| 35 | + :param endpoint: The endpoint URL for the RAI service. |
| 36 | + :type endpoint: str |
| 37 | + :param credential: The credential object for authentication. |
| 38 | + :type credential: Any |
| 39 | + """ |
| 40 | + super().__init__() |
| 41 | + self.logger = logger |
| 42 | + self.risk_category = risk_category |
| 43 | + self.evaluator_name = evaluator_name |
| 44 | + self.credential = credential |
| 45 | + self.azure_ai_project = azure_ai_project |
| 46 | + |
| 47 | + async def send_prompt_async(self, *, prompt_request: PromptRequestResponse, objective: str = "") -> PromptRequestResponse: |
| 48 | + self.logger.info("Starting send_prompt_async operation") |
| 49 | + self._validate_request(prompt_request=prompt_request) |
| 50 | + |
| 51 | + thing_to_eval = prompt_request.request_pieces[0].to_dict()['original_value'] |
| 52 | + |
| 53 | + thing_to_eval_qr = { |
| 54 | + "query":"", |
| 55 | + "response": thing_to_eval |
| 56 | + } |
| 57 | + |
| 58 | + metric_name = get_metric_from_risk_category(self.risk_category) |
| 59 | + |
| 60 | + evaluation_result = await evaluate_with_rai_service(thing_to_eval_qr, |
| 61 | + metric_name=metric_name, |
| 62 | + project_scope=self.azure_ai_project, |
| 63 | + credential=self.credential) |
| 64 | + self.logger.info(f"Evaluation result: {evaluation_result}") |
| 65 | + |
| 66 | + # Extract the relevant data from evaluation_result |
| 67 | + score = evaluation_result.get(f"{metric_name}_score", 0) |
| 68 | + reason = evaluation_result.get(f"{metric_name}_reason", "") |
| 69 | + threshold = evaluation_result.get(f"{metric_name}_threshold", 3) |
| 70 | + result = evaluation_result.get(metric_name, "") |
| 71 | + |
| 72 | + # Calculate pass/fail using the same logic as in _evaluate_conversation |
| 73 | + # Convert to boolean then to string for PyRIT's true/false score |
| 74 | + # Ensure we're working with numeric values for comparison |
| 75 | + boolean_result = float(score) > float(threshold) |
| 76 | + |
| 77 | + # Format the response in a way that PyRIT's scorer can parse |
| 78 | + # Use "true"/"false" string for score_value which is required for true_false score type |
| 79 | + scorer_compatible_response = { |
| 80 | + "score_value": boolean_result, # PyRIT requires lowercase "true" or "false" for true_false scores |
| 81 | + "description": f"Threshold: {threshold}, Result: {result}", |
| 82 | + "rationale": reason, |
| 83 | + "metadata": { |
| 84 | + "raw_score": score, |
| 85 | + "threshold": threshold, |
| 86 | + "result": result, |
| 87 | + "risk_category": self.risk_category |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + # Convert to JSON string |
| 92 | + response_json = json.dumps(scorer_compatible_response) |
| 93 | + |
| 94 | + # Construct the response |
| 95 | + response = construct_response_from_request( |
| 96 | + request=prompt_request.request_pieces[0], |
| 97 | + response_text_pieces=[response_json], |
| 98 | + ) |
| 99 | + self.logger.info(f"Constructed response: {response}") |
| 100 | + return response |
| 101 | + |
| 102 | + |
| 103 | + def is_json_response_supported(self) -> bool: |
| 104 | + """Check if JSON response is supported. |
| 105 | + |
| 106 | + :return: True if JSON response is supported, False otherwise |
| 107 | + """ |
| 108 | + # This target supports JSON responses |
| 109 | + return True |
| 110 | + |
| 111 | + def _validate_request(self, *, prompt_request: PromptRequestResponse) -> None: |
| 112 | + """Validate the request. |
| 113 | + |
| 114 | + :param prompt_request: The prompt request |
| 115 | + """ |
| 116 | + if len(prompt_request.request_pieces) != 1: |
| 117 | + raise ValueError("This target only supports a single prompt request piece.") |
| 118 | + |
| 119 | + if prompt_request.request_pieces[0].converted_value_data_type != "text": |
| 120 | + raise ValueError("This target only supports text prompt input.") |
| 121 | + |
0 commit comments