@@ -119,11 +119,6 @@ name: ApplicationPrompty
119
119
description : Simulates an application
120
120
model :
121
121
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}
127
122
parameters :
128
123
temperature : 0.0
129
124
top_p : 1.0
@@ -152,52 +147,55 @@ import asyncio
152
147
from typing import Any, Dict, List, Optional
153
148
from azure.ai.evaluation.simulator import Simulator
154
149
from promptflow.client import load_flow
155
- from azure.identity import DefaultAzureCredential
156
150
import os
151
+ import wikipedia
157
152
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>"
162
159
}
163
160
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 "
166
163
wiki_title = wikipedia.search(wiki_search_term)[0 ]
167
164
wiki_page = wikipedia.page(wiki_title)
168
165
text = wiki_page.summary[:1000 ]
169
166
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] ):
171
168
try :
172
169
current_dir = os.path.dirname(__file__ )
173
170
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
+ )
177
176
response = _flow(
178
177
query = query,
179
178
context = context,
180
179
conversation_history = messages_list
181
180
)
182
181
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 } " )
185
184
return " something went wrong"
186
185
187
186
async def callback (
188
- messages : List[Dict],
187
+ messages : Dict[ str , List[Dict] ],
189
188
stream : bool = False ,
190
189
session_state : Any = None , # noqa: ANN401
191
190
context : Optional[Dict[str , Any]] = None ,
192
191
) -> dict :
193
192
messages_list = messages[" messages" ]
194
- # get last message
193
+ # Get the last message from the user
195
194
latest_message = messages_list[- 1 ]
196
195
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
201
199
formatted_response = {
202
200
" content" : response,
203
201
" role" : " assistant" ,
@@ -208,10 +206,8 @@ async def callback(
208
206
messages[" messages" ].append(formatted_response)
209
207
return {" messages" : messages[" messages" ], " stream" : stream, " session_state" : session_state, " context" : context}
210
208
211
-
212
-
213
209
async def main ():
214
- simulator = Simulator(azure_ai_project = azure_ai_project, credential = DefaultAzureCredential() )
210
+ simulator = Simulator(model_config = model_config )
215
211
outputs = await simulator(
216
212
target = callback,
217
213
text = text,
@@ -222,17 +218,17 @@ async def main():
222
218
f " I am a teacher and I want to teach my students about { wiki_search_term} "
223
219
],
224
220
)
225
- print (json.dumps(outputs))
221
+ print (json.dumps(outputs, indent = 2 ))
226
222
227
223
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"
234
229
asyncio.run(main())
235
230
print (" done!" )
231
+
236
232
```
237
233
238
234
#### Adversarial Simulator
0 commit comments