-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Update Friendly Evaluator #46076
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
w-javed
wants to merge
9
commits into
feature/azure-ai-projects/2.0.2
Choose a base branch
from
waqasjaved02/friendly-evaluator-properties-output
base: feature/azure-ai-projects/2.0.2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Update Friendly Evaluator #46076
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
15c38a2
fix(samples): restructure friendly evaluator output with properties
w-javed aca5524
refactor: switch FriendlyEvaluator to OpenAI Responses API
w-javed ccd1529
docs: add required/optional field comments per PR review
w-javed 805a66c
fix: update sample init_parameters to pass api_key and model
w-javed 35fd916
feat: combine standalone + upload into single friendly evaluator sample
w-javed b9ad133
fix
w-javed ada4f01
update answ
w-javed 9540f22
docs: mark label and reason as optional fields in evaluator output
w-javed 4ed7d84
fix
w-javed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 18 additions & 42 deletions
60
...i-projects/samples/evaluations/custom_evaluators/friendly_evaluator/friendly_evaluator.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,66 +1,42 @@ | ||
| """Custom evaluator that uses an LLM to assess the friendliness of a response.""" | ||
|
|
||
| from openai import AzureOpenAI | ||
| from common_util.util import build_evaluation_messages, parse_evaluation_result | ||
| from openai import OpenAI | ||
| from common_util.util import build_evaluation_instructions, build_evaluation_input, parse_evaluation_result | ||
|
|
||
|
|
||
| class FriendlyEvaluator: | ||
| """Evaluates how friendly and approachable a response is using an LLM judge. | ||
|
|
||
| This evaluator sends the query and response to an LLM, which returns a | ||
| friendliness score (1-5), a pass/fail label, a reason, and a detailed explanation. | ||
| This evaluator sends the query and response to an LLM via the OpenAI Responses | ||
| API, which returns a friendliness score (1-5), a pass/fail label, a reason, | ||
| and a detailed explanation. | ||
|
|
||
| :param model_config: A dict containing Azure OpenAI connection info. Expected keys: | ||
| - azure_endpoint: The Azure OpenAI endpoint URL. | ||
| - azure_deployment: The deployment/model name. | ||
| - api_version: The API version (default: "2024-06-01"). | ||
| - api_key: (Optional) The API key. If not provided, DefaultAzureCredential is used. | ||
| :param api_key: The OpenAI API key. | ||
| :param model_name: The model_name to use for evaluation (e.g. "gpt-4o"). | ||
| :param threshold: The minimum score (1-5) to be considered "Pass" (default: 3). | ||
| """ | ||
|
|
||
| def __init__(self, *, model_config: dict, threshold: int = 3, **kwargs): | ||
| self.model_config = model_config | ||
| def __init__(self, *, api_key: str, model_name: str, threshold: int = 3, **kwargs): | ||
| self.client = OpenAI(api_key=api_key) | ||
| self.model_name = model_name | ||
| self.threshold = threshold | ||
| api_key = model_config.get("api_key") | ||
|
|
||
| if api_key: | ||
| self.client = AzureOpenAI( | ||
| azure_endpoint=model_config["azure_endpoint"], | ||
| api_key=api_key, | ||
| api_version=model_config.get("api_version", "2024-06-01"), | ||
| ) | ||
| else: | ||
| from azure.identity import DefaultAzureCredential, get_bearer_token_provider | ||
|
|
||
| token_provider = get_bearer_token_provider( | ||
| DefaultAzureCredential(), | ||
| "https://cognitiveservices.azure.com/.default", | ||
| ) | ||
| self.client = AzureOpenAI( | ||
| azure_endpoint=model_config["azure_endpoint"], | ||
| azure_ad_token_provider=token_provider, | ||
| api_version=model_config.get("api_version", "2024-06-01"), | ||
| ) | ||
|
|
||
| self.deployment = model_config["azure_deployment"] | ||
|
|
||
| def __call__(self, *, query: str, response: str, **kwargs) -> dict: | ||
| """Evaluate the friendliness of a response. | ||
|
|
||
| :param query: The original user query. | ||
| :param response: The response to evaluate. | ||
| :return: A dict with score, label, reason, and explanation. | ||
| :return: A dict with score, label, reason, threshold, passed, and properties. | ||
| """ | ||
| messages = build_evaluation_messages(query, response) | ||
|
|
||
| completion = self.client.chat.completions.create( | ||
| model=self.deployment, | ||
| messages=messages, | ||
| result = self.client.responses.create( | ||
| model=self.model_name, | ||
| instructions=build_evaluation_instructions(), | ||
| input=build_evaluation_input(query, response), | ||
| temperature=0.0, | ||
| max_tokens=500, | ||
| max_output_tokens=500, | ||
| ) | ||
|
|
||
| raw_result = completion.choices[0].message.content | ||
| raw_result = result.output_text | ||
| if raw_result is None: | ||
| raise ValueError("No content in completion response") | ||
| raise ValueError("No content in response") | ||
| return parse_evaluation_result(raw_result, self.threshold) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.