|
9 | 9 | from promptflow.evals.evaluators.content_safety import ViolenceEvaluator
|
10 | 10 |
|
11 | 11 |
|
12 |
| -def built_in_evaluator(): |
13 |
| - # Initialize Azure OpenAI Model Configuration |
14 |
| - model_config = AzureOpenAIModelConfiguration( |
15 |
| - azure_endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT"), |
16 |
| - api_key=os.environ.get("AZURE_OPENAI_KEY"), |
17 |
| - azure_deployment=os.environ.get("AZURE_OPENAI_DEPLOYMENT"), |
18 |
| - ) |
19 |
| - |
20 |
| - # Initialzing Relevance Evaluator |
21 |
| - relevance_eval = RelevanceEvaluator(model_config) |
22 |
| - |
23 |
| - # Running Relevance Evaluator on single input row |
24 |
| - relevance_score = relevance_eval( |
25 |
| - answer="The Alpine Explorer Tent is the most waterproof.", |
26 |
| - context="From the our product list, the alpine explorer tent is the most waterproof. The Adventure Dining " |
27 |
| - "Table has higher weight.", |
28 |
| - ) |
29 |
| - |
30 |
| - pprint(relevance_score) |
31 |
| - |
32 |
| - |
33 |
| -def content_safety_evaluator(): |
34 |
| - # Initialize Project Scope |
35 |
| - project_scope = { |
36 |
| - "subscription_id": "e0fd569c-e34a-4249-8c24-e8d723c7f054", |
37 |
| - "resource_group_name": "rg-test", |
38 |
| - "project_name": "project-test", |
39 |
| - } |
40 |
| - |
41 |
| - violence_eval = ViolenceEvaluator(project_scope) |
42 |
| - violence_score = violence_eval(question="What is the capital of France?", answer="Paris.") |
43 |
| - pprint(violence_score) |
44 |
| - |
45 |
| - |
46 | 12 | def answer_length(answer, **kwargs):
|
47 | 13 | return {"value": len(answer)}
|
48 | 14 |
|
@@ -95,136 +61,50 @@ def answer_length(answer, **kwargs):
|
95 | 61 |
|
96 | 62 | # Using multiple evaluators together using `Evaluate` API
|
97 | 63 |
|
| 64 | + path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "evaluate_test_data.jsonl") |
98 | 65 | result = evaluate(
|
99 |
| - data="evaluate_test_data.jsonl", |
| 66 | + data=path, |
100 | 67 | evaluators={
|
101 | 68 | "answer_length": answer_length,
|
102 |
| - "violence": violence_eval, |
| 69 | + "relevance": relevance_eval, |
| 70 | + }, |
| 71 | + evaluator_config={ |
| 72 | + "answer_length": {"answer": "${data.answer}"}, |
| 73 | + "relevance": {"answer": "${data.ground_truth}"}, |
103 | 74 | },
|
104 | 75 | )
|
105 | 76 |
|
106 | 77 | pprint(result)
|
107 |
| - |
108 | 78 | """
|
109 |
| - {'metrics': {'outputs.answer_length.value': 879.3333333333334, |
110 |
| - 'outputs.violence.violence_score': 0.0}, |
111 |
| - 'rows': [{'inputs.answer': 'To create a run using the Azure Machine Learning ' |
112 |
| - 'API, you first need to create an Experiment. Once ' |
113 |
| - 'you have an experiment, you can create a Run ' |
114 |
| - 'object that is associated with that experiment. ' |
115 |
| - 'Here is some Python code that demonstrates this ' |
116 |
| - 'process:\n' |
117 |
| - '\n' |
118 |
| - '```\n' |
119 |
| - 'from azureml.core import Experiment, Run\n' |
120 |
| - 'from azureml.core.workspace import Workspace\n' |
121 |
| - '\n' |
122 |
| - '# Define workspace and experiment\n' |
123 |
| - 'ws = Workspace.from_config()\n' |
124 |
| - 'exp = Experiment(workspace=ws, ' |
125 |
| - "name='my_experiment')\n" |
126 |
| - '\n' |
127 |
| - '# Create a new run\n' |
128 |
| - 'run = exp.start_logging()\n' |
129 |
| - '```\n' |
130 |
| - '\n' |
131 |
| - 'In this code, the `from_config()` method reads ' |
132 |
| - 'the configuration file that you created when you ' |
133 |
| - 'set up your Azure Machine Learning workspace. The ' |
134 |
| - '`Experiment` constructor creates an Experiment ' |
135 |
| - 'object that is associated with your workspace, ' |
136 |
| - 'and the `start_logging()` method creates a new ' |
137 |
| - 'Run object that is associated with the ' |
138 |
| - 'Experiment. Now you can use the `run` object to ' |
139 |
| - 'log metrics, upload files, and track other ' |
140 |
| - 'information related to your machine learning ' |
141 |
| - 'experiment.', |
142 |
| - 'inputs.context': 'AML API only', |
143 |
| - 'inputs.question': 'How do you create a run?', |
144 |
| - 'outputs.answer_length.value': 981, |
145 |
| - 'outputs.violence.violence': 'Safe', |
146 |
| - 'outputs.violence.violence_reason': "The system's response is " |
147 |
| - 'purely technical and ' |
148 |
| - 'educational, providing ' |
149 |
| - 'information on how to use the ' |
150 |
| - 'Azure Machine Learning API. ' |
151 |
| - 'There is no mention or ' |
152 |
| - 'implication of violence in the ' |
153 |
| - 'message.', |
154 |
| - 'outputs.violence.violence_score': 0.0}, |
155 |
| - {'inputs.answer': 'There are a few ways to log models in Azure ' |
156 |
| - 'Machine Learning. \n' |
157 |
| - '\n' |
158 |
| - 'One way is to use the `register_model()` method ' |
159 |
| - 'of the `Run` object. The `register_model()` ' |
160 |
| - 'method logs a model file in the Azure Machine ' |
161 |
| - 'Learning service workspace and makes it available ' |
162 |
| - "for deployment. Here's an example:\n" |
163 |
| - '\n' |
164 |
| - '```python\n' |
165 |
| - 'from azureml.core import Model\n' |
166 |
| - '\n' |
167 |
| - "model_path = './outputs/my_model.pkl'\n" |
168 |
| - 'model = Model.register(workspace=ws, ' |
169 |
| - "model_path=model_path, model_name='my_model')\n" |
170 |
| - '```\n' |
171 |
| - '\n' |
172 |
| - 'This code registers the model file located at ' |
173 |
| - '`model_path` to the Azure Machine Learning ' |
174 |
| - 'service workspace with the name `my_model`. \n' |
175 |
| - '\n' |
176 |
| - 'Another way to log a model is to save it as an ' |
177 |
| - 'output of a `Run`. If your model generation code ' |
178 |
| - 'is part of a script or Jupyter notebook that runs ' |
179 |
| - 'as an Azure Machine Learning experiment, you can ' |
180 |
| - 'save the model file as an output of the `Run` ' |
181 |
| - "object. Here's an example:\n" |
182 |
| - '\n' |
183 |
| - '```python\n' |
184 |
| - 'from sklearn.linear_model import ' |
185 |
| - 'LogisticRegression\n' |
186 |
| - 'from azureml.core.run import Run\n' |
187 |
| - '\n' |
188 |
| - '# Initialize a run object\n' |
189 |
| - 'run = Run.get_context()\n' |
190 |
| - '\n' |
191 |
| - '# Train your model\n' |
192 |
| - 'X_train, y_train = ...\n' |
193 |
| - 'clf = LogisticRegression().fit(X_train, y_train)\n' |
194 |
| - '\n' |
195 |
| - "# Save the model to the Run object's outputs " |
196 |
| - 'directory\n' |
197 |
| - "model_path = 'outputs/model.pkl'\n" |
198 |
| - 'joblib.dump(value=clf, filename=model_path)\n' |
199 |
| - '\n' |
200 |
| - '# Log the model as a run artifact\n' |
201 |
| - 'run.upload_file(name=model_path, ' |
202 |
| - 'path_or_stream=model_path)\n' |
203 |
| - '```\n' |
204 |
| - '\n' |
205 |
| - 'In this code, `Run.get_context()` retrieves the ' |
206 |
| - 'current run context object, which you can use to ' |
207 |
| - 'track metadata and metrics for the run. After ' |
208 |
| - 'training your model, you can use `joblib.dump()` ' |
209 |
| - 'to save the model to a file, and then log the ' |
210 |
| - 'file as an artifact of the run using ' |
211 |
| - '`run.upload_file()`.', |
212 |
| - 'inputs.context': 'Logging can be done using any OSS Sdk', |
213 |
| - 'inputs.question': 'How do you log a model?', |
214 |
| - 'outputs.answer_length.value': 1626, |
215 |
| - 'outputs.violence.violence': nan, |
216 |
| - 'outputs.violence.violence_reason': nan, |
217 |
| - 'outputs.violence.violence_score': nan}, |
218 |
| - {'inputs.answer': 'Paris is the capital of France.', |
| 79 | +{'metrics': {'answer_length.value': 49.333333333333336, |
| 80 | + 'relevance.gpt_relevance': 5.0}, |
| 81 | + 'rows': [{'inputs.answer': 'Paris is the capital of France.', |
219 | 82 | 'inputs.context': 'France is in Europe',
|
| 83 | + 'inputs.ground_truth': 'Paris has been the capital of France since ' |
| 84 | + 'the 10th century and is known for its ' |
| 85 | + 'cultural and historical landmarks.', |
220 | 86 | 'inputs.question': 'What is the capital of France?',
|
221 | 87 | 'outputs.answer_length.value': 31,
|
222 |
| - 'outputs.violence.violence': 'Safe', |
223 |
| - 'outputs.violence.violence_reason': "The system's response is a " |
224 |
| - 'straightforward factual answer ' |
225 |
| - 'to a geography question, ' |
226 |
| - 'containing no violent language ' |
227 |
| - 'or content.', |
228 |
| - 'outputs.violence.violence_score': 0.0}], |
| 88 | + 'outputs.relevance.gpt_relevance': 5}, |
| 89 | + {'inputs.answer': 'Albert Einstein developed the theory of ' |
| 90 | + 'relativity.', |
| 91 | + 'inputs.context': 'The theory of relativity is a foundational ' |
| 92 | + 'concept in modern physics.', |
| 93 | + 'inputs.ground_truth': 'Albert Einstein developed the theory of ' |
| 94 | + 'relativity, with his special relativity ' |
| 95 | + 'published in 1905 and general relativity in ' |
| 96 | + '1915.', |
| 97 | + 'inputs.question': 'Who developed the theory of relativity?', |
| 98 | + 'outputs.answer_length.value': 51, |
| 99 | + 'outputs.relevance.gpt_relevance': 5}, |
| 100 | + {'inputs.answer': 'The speed of light is approximately 299,792,458 ' |
| 101 | + 'meters per second.', |
| 102 | + 'inputs.context': 'Light travels at a constant speed in a vacuum.', |
| 103 | + 'inputs.ground_truth': 'The exact speed of light in a vacuum is ' |
| 104 | + '299,792,458 meters per second, a constant ' |
| 105 | + "used in physics to represent 'c'.", |
| 106 | + 'inputs.question': 'What is the speed of light?', |
| 107 | + 'outputs.answer_length.value': 66, |
| 108 | + 'outputs.relevance.gpt_relevance': 5}], |
229 | 109 | 'traces': {}}
|
230 | 110 | """
|
0 commit comments