1
1
---
2
2
title : Azure AI Evaluation client library for Python
3
3
keywords : Azure, python, SDK, API, azure-ai-evaluation, evaluation
4
- ms.date : 10/01 /2024
4
+ ms.date : 10/16 /2024
5
5
ms.topic : reference
6
6
ms.devlang : python
7
7
ms.service : evaluation
8
8
---
9
- # Azure AI Evaluation client library for Python - version 1.0.0b3
9
+ # Azure AI Evaluation client library for Python - version 1.0.0b4
10
10
11
11
12
12
We are excited to introduce the public preview of the Azure AI Evaluation SDK.
@@ -128,11 +128,6 @@ name: ApplicationPrompty
128
128
description : Simulates an application
129
129
model :
130
130
api : chat
131
- configuration :
132
- type : azure_openai
133
- azure_deployment : ${env:AZURE_DEPLOYMENT}
134
- api_key : ${env:AZURE_OPENAI_API_KEY}
135
- azure_endpoint : ${env:AZURE_OPENAI_ENDPOINT}
136
131
parameters :
137
132
temperature : 0.0
138
133
top_p : 1.0
@@ -161,52 +156,55 @@ import asyncio
161
156
from typing import Any, Dict, List, Optional
162
157
from azure.ai.evaluation.simulator import Simulator
163
158
from promptflow.client import load_flow
164
- from azure.identity import DefaultAzureCredential
165
159
import os
160
+ import wikipedia
166
161
167
- azure_ai_project = {
168
- " subscription_id" : os.environ.get(" AZURE_SUBSCRIPTION_ID" ),
169
- " resource_group_name" : os.environ.get(" RESOURCE_GROUP" ),
170
- " project_name" : os.environ.get(" PROJECT_NAME" )
162
+ # Set up the model configuration without api_key, using DefaultAzureCredential
163
+ model_config = {
164
+ " azure_endpoint" : os.environ.get(" AZURE_OPENAI_ENDPOINT" ),
165
+ " azure_deployment" : os.environ.get(" AZURE_DEPLOYMENT" ),
166
+ # not providing key would make the SDK pick up `DefaultAzureCredential`
167
+ # use "api_key": "<your API key>"
171
168
}
172
169
173
- import wikipedia
174
- wiki_search_term = " Leonardo da vinci "
170
+ # Use Wikipedia to get some text for the simulation
171
+ wiki_search_term = " Leonardo da Vinci "
175
172
wiki_title = wikipedia.search(wiki_search_term)[0 ]
176
173
wiki_page = wikipedia.page(wiki_title)
177
174
text = wiki_page.summary[:1000 ]
178
175
179
- def method_to_invoke_application_prompty (query : str ):
176
+ def method_to_invoke_application_prompty (query : str , messages_list : List[Dict], context : Optional[Dict] ):
180
177
try :
181
178
current_dir = os.path.dirname(__file__ )
182
179
prompty_path = os.path.join(current_dir, " application.prompty" )
183
- _flow = load_flow(source = prompty_path, model = {
184
- " configuration" : azure_ai_project
185
- })
180
+ _flow = load_flow(
181
+ source = prompty_path,
182
+ model = model_config,
183
+ credential = DefaultAzureCredential()
184
+ )
186
185
response = _flow(
187
186
query = query,
188
187
context = context,
189
188
conversation_history = messages_list
190
189
)
191
190
return response
192
- except :
193
- print (" Something went wrong invoking the prompty" )
191
+ except Exception as e :
192
+ print (f " Something went wrong invoking the prompty: { e } " )
194
193
return " something went wrong"
195
194
196
195
async def callback (
197
- messages : List[Dict],
196
+ messages : Dict[ str , List[Dict] ],
198
197
stream : bool = False ,
199
198
session_state : Any = None , # noqa: ANN401
200
199
context : Optional[Dict[str , Any]] = None ,
201
200
) -> dict :
202
201
messages_list = messages[" messages" ]
203
- # get last message
202
+ # Get the last message from the user
204
203
latest_message = messages_list[- 1 ]
205
204
query = latest_message[" content" ]
206
- context = None
207
- # call your endpoint or ai application here
208
- response = method_to_invoke_application_prompty(query)
209
- # we are formatting the response to follow the openAI chat protocol format
205
+ # Call your endpoint or AI application here
206
+ response = method_to_invoke_application_prompty(query, messages_list, context)
207
+ # Format the response to follow the OpenAI chat protocol format
210
208
formatted_response = {
211
209
" content" : response,
212
210
" role" : " assistant" ,
@@ -217,10 +215,8 @@ async def callback(
217
215
messages[" messages" ].append(formatted_response)
218
216
return {" messages" : messages[" messages" ], " stream" : stream, " session_state" : session_state, " context" : context}
219
217
220
-
221
-
222
218
async def main ():
223
- simulator = Simulator(azure_ai_project = azure_ai_project, credential = DefaultAzureCredential() )
219
+ simulator = Simulator(model_config = model_config )
224
220
outputs = await simulator(
225
221
target = callback,
226
222
text = text,
@@ -231,17 +227,17 @@ async def main():
231
227
f " I am a teacher and I want to teach my students about { wiki_search_term} "
232
228
],
233
229
)
234
- print (json.dumps(outputs))
230
+ print (json.dumps(outputs, indent = 2 ))
235
231
236
232
if __name__ == " __main__" :
237
- os.environ[" AZURE_SUBSCRIPTION_ID" ] = " "
238
- os.environ[" RESOURCE_GROUP" ] = " "
239
- os.environ[" PROJECT_NAME" ] = " "
240
- os.environ[" AZURE_OPENAI_API_KEY" ] = " "
241
- os.environ[" AZURE_OPENAI_ENDPOINT" ] = " "
242
- os.environ[" AZURE_DEPLOYMENT" ] = " "
233
+ # Ensure that the following environment variables are set in your environment:
234
+ # AZURE_OPENAI_ENDPOINT and AZURE_DEPLOYMENT
235
+ # Example:
236
+ # os.environ["AZURE_OPENAI_ENDPOINT"] = "https://your-endpoint.openai.azure.com/"
237
+ # os.environ["AZURE_DEPLOYMENT"] = "your-deployment-name"
243
238
asyncio.run(main())
244
239
print (" done!" )
240
+
245
241
```
246
242
247
243
#### Adversarial Simulator
@@ -380,18 +376,18 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con
380
376
381
377
<!-- LINKS -->
382
378
383
- [ source_code ] : https://github.com/Azure/azure-sdk-for-python/tree/azure-ai-evaluation_1.0.0b3 /sdk/evaluation/azure-ai-evaluation
379
+ [ source_code ] : https://github.com/Azure/azure-sdk-for-python/tree/azure-ai-evaluation_1.0.0b4 /sdk/evaluation/azure-ai-evaluation
384
380
[ evaluation_pypi ] : https://pypi.org/project/azure-ai-evaluation/
385
381
[ evaluation_ref_docs ] : https://learn.microsoft.com/python/api/azure-ai-evaluation/azure.ai.evaluation?view=azure-python-preview
386
382
[ evaluation_samples ] : https://github.com/Azure-Samples/azureai-samples/tree/main/scenarios
387
383
[ product_documentation ] : https://learn.microsoft.com/azure/ai-studio/how-to/develop/evaluate-sdk
388
384
[ python_logging ] : https://docs.python.org/3/library/logging.html
389
385
[ sdk_logging_docs ] : /azure/developer/python/azure-sdk-logging
390
- [ azure_core_readme ] : https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-evaluation_1.0.0b3 /sdk/core/azure-core/README.md
386
+ [ azure_core_readme ] : https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-evaluation_1.0.0b4 /sdk/core/azure-core/README.md
391
387
[ pip_link ] : https://pypi.org/project/pip/
392
388
[ azure_core_ref_docs ] : https://aka.ms/azsdk-python-core-policies
393
- [ azure_core ] : https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-evaluation_1.0.0b3 /sdk/core/azure-core/README.md
394
- [ azure_identity ] : https://github.com/Azure/azure-sdk-for-python/tree/azure-ai-evaluation_1.0.0b3 /sdk/identity/azure-identity
389
+ [ azure_core ] : https://github.com/Azure/azure-sdk-for-python/blob/azure-ai-evaluation_1.0.0b4 /sdk/core/azure-core/README.md
390
+ [ azure_identity ] : https://github.com/Azure/azure-sdk-for-python/tree/azure-ai-evaluation_1.0.0b4 /sdk/identity/azure-identity
395
391
[ cla ] : https://cla.microsoft.com
396
392
[ code_of_conduct ] : https://opensource.microsoft.com/codeofconduct/
397
393
[ coc_faq ] : https://opensource.microsoft.com/codeofconduct/faq/
0 commit comments