Skip to content

Commit 7a08411

Browse files
nagkumar91Nagkumar Arkalgud
authored andcommitted
Remove usage of keys from prompty (Azure#37708)
* Update task_query_response.prompty remove required keys * Update task_simulate.prompty * Update task_query_response.prompty * Update task_simulate.prompty * Keys are not needed for the simulator --------- Co-authored-by: Nagkumar Arkalgud <[email protected]>
1 parent 1e34504 commit 7a08411

File tree

6 files changed

+432
-206
lines changed

6 files changed

+432
-206
lines changed

sdk/evaluation/azure-ai-evaluation/CHANGELOG.md

+20
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ evaluate(
6767
)
6868
```
6969

70+
- Simulator now requires a model configuration to call the prompty instead of an Azure AI project scope. This enables the usage of simulator with Entra ID based auth.
71+
Before:
72+
```python
73+
azure_ai_project = {
74+
"subscription_id": os.environ.get("AZURE_SUBSCRIPTION_ID"),
75+
"resource_group_name": os.environ.get("RESOURCE_GROUP"),
76+
"project_name": os.environ.get("PROJECT_NAME"),
77+
}
78+
sim = Simulator(azure_ai_project=azure_ai_project, credentails=DefaultAzureCredentials())
79+
```
80+
After:
81+
```python
82+
model_config = {
83+
"azure_endpoint": os.environ.get("AZURE_OPENAI_ENDPOINT"),
84+
"azure_deployment": os.environ.get("AZURE_DEPLOYMENT"),
85+
}
86+
sim = Simulator(model_config=model_config)
87+
```
88+
If `api_key` is not included in the `model_config`, the prompty runtime in `promptflow-core` will pick up `DefaultAzureCredential`.
89+
7090
### Bugs Fixed
7191

7292
- Fixed issue where Entra ID authentication was not working with `AzureOpenAIModelConfiguration`

sdk/evaluation/azure-ai-evaluation/README.md

+30-34
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,6 @@ name: ApplicationPrompty
119119
description: Simulates an application
120120
model:
121121
api: chat
122-
configuration:
123-
type: azure_openai
124-
azure_deployment: ${env:AZURE_DEPLOYMENT}
125-
api_key: ${env:AZURE_OPENAI_API_KEY}
126-
azure_endpoint: ${env:AZURE_OPENAI_ENDPOINT}
127122
parameters:
128123
temperature: 0.0
129124
top_p: 1.0
@@ -152,52 +147,55 @@ import asyncio
152147
from typing import Any, Dict, List, Optional
153148
from azure.ai.evaluation.simulator import Simulator
154149
from promptflow.client import load_flow
155-
from azure.identity import DefaultAzureCredential
156150
import os
151+
import wikipedia
157152

158-
azure_ai_project = {
159-
"subscription_id": os.environ.get("AZURE_SUBSCRIPTION_ID"),
160-
"resource_group_name": os.environ.get("RESOURCE_GROUP"),
161-
"project_name": os.environ.get("PROJECT_NAME")
153+
# Set up the model configuration without api_key, using DefaultAzureCredential
154+
model_config = {
155+
"azure_endpoint": os.environ.get("AZURE_OPENAI_ENDPOINT"),
156+
"azure_deployment": os.environ.get("AZURE_DEPLOYMENT"),
157+
# not providing key would make the SDK pick up `DefaultAzureCredential`
158+
# use "api_key": "<your API key>"
162159
}
163160

164-
import wikipedia
165-
wiki_search_term = "Leonardo da vinci"
161+
# Use Wikipedia to get some text for the simulation
162+
wiki_search_term = "Leonardo da Vinci"
166163
wiki_title = wikipedia.search(wiki_search_term)[0]
167164
wiki_page = wikipedia.page(wiki_title)
168165
text = wiki_page.summary[:1000]
169166

170-
def method_to_invoke_application_prompty(query: str):
167+
def method_to_invoke_application_prompty(query: str, messages_list: List[Dict], context: Optional[Dict]):
171168
try:
172169
current_dir = os.path.dirname(__file__)
173170
prompty_path = os.path.join(current_dir, "application.prompty")
174-
_flow = load_flow(source=prompty_path, model={
175-
"configuration": azure_ai_project
176-
})
171+
_flow = load_flow(
172+
source=prompty_path,
173+
model=model_config,
174+
credential=DefaultAzureCredential()
175+
)
177176
response = _flow(
178177
query=query,
179178
context=context,
180179
conversation_history=messages_list
181180
)
182181
return response
183-
except:
184-
print("Something went wrong invoking the prompty")
182+
except Exception as e:
183+
print(f"Something went wrong invoking the prompty: {e}")
185184
return "something went wrong"
186185

187186
async def callback(
188-
messages: List[Dict],
187+
messages: Dict[str, List[Dict]],
189188
stream: bool = False,
190189
session_state: Any = None, # noqa: ANN401
191190
context: Optional[Dict[str, Any]] = None,
192191
) -> dict:
193192
messages_list = messages["messages"]
194-
# get last message
193+
# Get the last message from the user
195194
latest_message = messages_list[-1]
196195
query = latest_message["content"]
197-
context = None
198-
# call your endpoint or ai application here
199-
response = method_to_invoke_application_prompty(query)
200-
# we are formatting the response to follow the openAI chat protocol format
196+
# Call your endpoint or AI application here
197+
response = method_to_invoke_application_prompty(query, messages_list, context)
198+
# Format the response to follow the OpenAI chat protocol format
201199
formatted_response = {
202200
"content": response,
203201
"role": "assistant",
@@ -208,10 +206,8 @@ async def callback(
208206
messages["messages"].append(formatted_response)
209207
return {"messages": messages["messages"], "stream": stream, "session_state": session_state, "context": context}
210208

211-
212-
213209
async def main():
214-
simulator = Simulator(azure_ai_project=azure_ai_project, credential=DefaultAzureCredential())
210+
simulator = Simulator(model_config=model_config)
215211
outputs = await simulator(
216212
target=callback,
217213
text=text,
@@ -222,17 +218,17 @@ async def main():
222218
f"I am a teacher and I want to teach my students about {wiki_search_term}"
223219
],
224220
)
225-
print(json.dumps(outputs))
221+
print(json.dumps(outputs, indent=2))
226222

227223
if __name__ == "__main__":
228-
os.environ["AZURE_SUBSCRIPTION_ID"] = ""
229-
os.environ["RESOURCE_GROUP"] = ""
230-
os.environ["PROJECT_NAME"] = ""
231-
os.environ["AZURE_OPENAI_API_KEY"] = ""
232-
os.environ["AZURE_OPENAI_ENDPOINT"] = ""
233-
os.environ["AZURE_DEPLOYMENT"] = ""
224+
# Ensure that the following environment variables are set in your environment:
225+
# AZURE_OPENAI_ENDPOINT and AZURE_DEPLOYMENT
226+
# Example:
227+
# os.environ["AZURE_OPENAI_ENDPOINT"] = "https://your-endpoint.openai.azure.com/"
228+
# os.environ["AZURE_DEPLOYMENT"] = "your-deployment-name"
234229
asyncio.run(main())
235230
print("done!")
231+
236232
```
237233

238234
#### Adversarial Simulator

sdk/evaluation/azure-ai-evaluation/azure/ai/evaluation/simulator/_prompty/task_query_response.prompty

-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ name: TaskSimulatorQueryResponse
33
description: Gets queries and responses from a blob of text
44
model:
55
api: chat
6-
configuration:
7-
type: azure_openai
8-
azure_deployment: ${env:AZURE_DEPLOYMENT}
9-
azure_endpoint: ${env:AZURE_OPENAI_ENDPOINT}
106
parameters:
117
temperature: 0.0
128
top_p: 1.0

sdk/evaluation/azure-ai-evaluation/azure/ai/evaluation/simulator/_prompty/task_simulate.prompty

-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ name: TaskSimulatorWithPersona
33
description: Simulates a user to complete a conversation
44
model:
55
api: chat
6-
configuration:
7-
type: azure_openai
8-
azure_deployment: ${env:AZURE_DEPLOYMENT}
9-
azure_endpoint: ${env:AZURE_OPENAI_ENDPOINT}
106
parameters:
117
temperature: 0.0
128
top_p: 1.0

0 commit comments

Comments
 (0)