Skip to content

Commit bfc7842

Browse files
authored
Fix quality sample (#168)
* update promptflow-eval dependencies to azure-ai-evaluation * clear local variables * fix errors and remove 'question' col from data * small fix in evaluator config * Fix AI as Judge Quality Evaluators sample
1 parent be83486 commit bfc7842

File tree

2 files changed

+61
-63
lines changed

2 files changed

+61
-63
lines changed

scenarios/evaluate/Supported_Evaluation_Metrics/AI_Judge_Evaluators_Quality/AI_Judge_Evaluators_Quality.ipynb

Lines changed: 16 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
"source": [
4040
"%pip install azure-ai-evaluation\n",
4141
"%pip install promptflow-azure\n",
42-
"%pip install azure-identity"
42+
"%pip install azure-identity\n",
43+
"%pip install --upgrade openai"
4344
]
4445
},
4546
{
@@ -56,9 +57,8 @@
5657
"outputs": [],
5758
"source": [
5859
"from pprint import pprint\n",
59-
"from openai import AzureOpenAI\n",
6060
"import pandas as pd\n",
61-
"from azure.identity import DefaultAzureCredential, get_bearer_token_provider"
61+
"from azure.identity import DefaultAzureCredential"
6262
]
6363
},
6464
{
@@ -89,9 +89,9 @@
8989
"outputs": [],
9090
"source": [
9191
"azure_ai_project = {\n",
92-
" \"subscription_id\": \"<your-subscription-id>\",\n",
93-
" \"resource_group_name\": \"<your-resource-group-name>\",\n",
94-
" \"project_name\": \"<your-project-name>\",\n",
92+
" \"subscription_id\": \"<subscription_id>\",\n",
93+
" \"resource_group_name\": \"<resource_group_name>\",\n",
94+
" \"project_name\": \"<project_name>\",\n",
9595
"}"
9696
]
9797
},
@@ -103,11 +103,10 @@
103103
"source": [
104104
"import os\n",
105105
"\n",
106-
"# Use the following code to set the environment variables if not already set. If set, you can skip this step.\n",
106+
"# Use the following code to set the environment variables if not already set. If set, you can skip this step. In addition, you should also set \"AZURE_OPENAI_ENDPOINT\" to the endpoint of your AzureOpenAI service.\n",
107107
"\n",
108-
"os.environ[\"AZURE_OPENAI_API_VERSION\"] = \"<api version>\"\n",
109-
"os.environ[\"AZURE_OPENAI_DEPLOYMENT\"] = \"<your-deployment>\"\n",
110-
"os.environ[\"AZURE_OPENAI_ENDPOINT\"] = \"<your-endpoint>\""
108+
"os.environ[\"AZURE_OPENAI_API_VERSION\"] = \"<openai_api_version>\"\n",
109+
"os.environ[\"AZURE_OPENAI_DEPLOYMENT\"] = \"<openai_deployment>\""
111110
]
112111
},
113112
{
@@ -143,63 +142,14 @@
143142
"metadata": {},
144143
"outputs": [],
145144
"source": [
145+
"import os\n",
146+
"\n",
146147
"model_config = {\n",
147148
" \"azure_endpoint\": os.environ.get(\"AZURE_OPENAI_ENDPOINT\"),\n",
148149
" \"azure_deployment\": os.environ.get(\"AZURE_OPENAI_DEPLOYMENT\"),\n",
149150
"}"
150151
]
151152
},
152-
{
153-
"cell_type": "code",
154-
"execution_count": null,
155-
"metadata": {},
156-
"outputs": [],
157-
"source": [
158-
"from typing_extensions import Self\n",
159-
"from typing import TypedDict\n",
160-
"from promptflow.tracing import trace\n",
161-
"\n",
162-
"\n",
163-
"class ModelEndpoint:\n",
164-
" def __init__(self: Self, env: dict) -> str:\n",
165-
" self.env = env\n",
166-
"\n",
167-
" class Response(TypedDict):\n",
168-
" query: str\n",
169-
" response: str\n",
170-
"\n",
171-
" @trace\n",
172-
" def __call__(self: Self, query: str) -> Response:\n",
173-
" token_provider = get_bearer_token_provider(\n",
174-
" DefaultAzureCredential(), \"https://cognitiveservices.azure.com/.default\"\n",
175-
" )\n",
176-
"\n",
177-
" client = AzureOpenAI(\n",
178-
" azure_endpoint=self.env[\"azure_endpoint\"],\n",
179-
" api_version=\"2024-06-01\",\n",
180-
" azure_ad_token_provider=token_provider,\n",
181-
" )\n",
182-
" # Call the model\n",
183-
" completion = client.chat.completions.create(\n",
184-
" model=self.env[\"azure_deployment\"],\n",
185-
" messages=[\n",
186-
" {\n",
187-
" \"role\": \"user\",\n",
188-
" \"content\": query,\n",
189-
" }\n",
190-
" ],\n",
191-
" max_tokens=800,\n",
192-
" temperature=0.7,\n",
193-
" top_p=0.95,\n",
194-
" frequency_penalty=0,\n",
195-
" presence_penalty=0,\n",
196-
" stop=None,\n",
197-
" stream=False,\n",
198-
" )\n",
199-
" output = completion.to_dict()\n",
200-
" return {\"query\": query, \"response\": output[\"choices\"][0][\"message\"][\"content\"]}"
201-
]
202-
},
203153
{
204154
"cell_type": "markdown",
205155
"metadata": {},
@@ -236,9 +186,12 @@
236186
" FluencyEvaluator,\n",
237187
" SimilarityEvaluator,\n",
238188
")\n",
189+
"from model_endpoint import ModelEndpoint\n",
239190
"\n",
240191
"\n",
241-
"content_safety_evaluator = ContentSafetyEvaluator(azure_ai_project)\n",
192+
"content_safety_evaluator = ContentSafetyEvaluator(\n",
193+
" azure_ai_project=azure_ai_project, credential=DefaultAzureCredential()\n",
194+
")\n",
242195
"relevance_evaluator = RelevanceEvaluator(model_config)\n",
243196
"coherence_evaluator = CoherenceEvaluator(model_config)\n",
244197
"groundedness_evaluator = GroundednessEvaluator(model_config)\n",
@@ -310,7 +263,7 @@
310263
],
311264
"metadata": {
312265
"kernelspec": {
313-
"display_name": "venv-azureai-samples",
266+
"display_name": ".venv",
314267
"language": "python",
315268
"name": "python3"
316269
},
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from typing_extensions import Self
2+
from typing import TypedDict
3+
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
4+
from openai import AzureOpenAI
5+
6+
7+
class ModelEndpoint:
8+
def __init__(self: Self, env: dict) -> None:
9+
self.env = env
10+
print(self.env)
11+
12+
class Response(TypedDict):
13+
query: str
14+
response: str
15+
16+
# @trace
17+
def __call__(self: Self, query: str) -> Response:
18+
token_provider = get_bearer_token_provider(
19+
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
20+
)
21+
22+
client = AzureOpenAI(
23+
azure_endpoint=self.env["azure_endpoint"],
24+
api_version="2024-06-01",
25+
azure_ad_token_provider=token_provider,
26+
)
27+
# Call the model
28+
completion = client.chat.completions.create(
29+
model=self.env["azure_deployment"],
30+
messages=[
31+
{
32+
"role": "user",
33+
"content": query,
34+
}
35+
],
36+
max_tokens=800,
37+
temperature=0.7,
38+
top_p=0.95,
39+
frequency_penalty=0,
40+
presence_penalty=0,
41+
stop=None,
42+
stream=False,
43+
)
44+
output = completion.to_dict()
45+
return {"query": query, "response": output["choices"][0]["message"]["content"]}

0 commit comments

Comments
 (0)