promptflow

Форк
0
/
flex-flow-quickstart-azure.ipynb 
306 строк · 8.9 Кб
1
{
2
 "cells": [
3
  {
4
   "cell_type": "markdown",
5
   "metadata": {},
6
   "source": [
7
    "# Getting started with flex flow in Azure"
8
   ]
9
  },
10
  {
11
   "cell_type": "markdown",
12
   "metadata": {},
13
   "source": [
14
    "**Learning Objectives** - Upon completing this tutorial, you should be able to:\n",
15
    "\n",
16
    "- Write LLM application using notebook and visualize the trace of your application.\n",
17
    "- Convert the application into a flow and batch run against multi lines of data.\n"
18
   ]
19
  },
20
  {
21
   "cell_type": "markdown",
22
   "metadata": {},
23
   "source": [
24
    "## 0. Install dependent packages"
25
   ]
26
  },
27
  {
28
   "cell_type": "code",
29
   "execution_count": null,
30
   "metadata": {},
31
   "outputs": [],
32
   "source": [
33
    "%%capture --no-stderr\n",
34
    "%pip install -r ./requirements-azure.txt"
35
   ]
36
  },
37
  {
38
   "cell_type": "markdown",
39
   "metadata": {},
40
   "source": [
41
    "## 1. Connection to workspace"
42
   ]
43
  },
44
  {
45
   "cell_type": "markdown",
46
   "metadata": {},
47
   "source": [
48
    "### Configure credential\n",
49
    "\n",
50
    "We are using `DefaultAzureCredential` to get access to workspace. \n",
51
    "`DefaultAzureCredential` should be capable of handling most Azure SDK authentication scenarios. \n",
52
    "\n",
53
    "Reference for more available credentials if it does not work for you: [configure credential example](https://github.com/microsoft/promptflow/blob/main/examples/configuration.ipynb), [azure-identity reference doc](https://docs.microsoft.com/en-us/python/api/azure-identity/azure.identity?view=azure-python)."
54
   ]
55
  },
56
  {
57
   "cell_type": "code",
58
   "execution_count": null,
59
   "metadata": {},
60
   "outputs": [],
61
   "source": [
62
    "from azure.identity import DefaultAzureCredential, InteractiveBrowserCredential\n",
63
    "\n",
64
    "try:\n",
65
    "    credential = DefaultAzureCredential()\n",
66
    "    # Check if given credential can get token successfully.\n",
67
    "    credential.get_token(\"https://management.azure.com/.default\")\n",
68
    "except Exception as ex:\n",
69
    "    # Fall back to InteractiveBrowserCredential in case DefaultAzureCredential not work\n",
70
    "    credential = InteractiveBrowserCredential()"
71
   ]
72
  },
73
  {
74
   "cell_type": "markdown",
75
   "metadata": {},
76
   "source": [
77
    "### Get a handle to the workspace\n",
78
    "\n",
79
    "We use config file to connect to a workspace. The Azure ML workspace should be configured with computer cluster. [Check this notebook for configure a workspace](https://github.com/microsoft/promptflow/blob/main/examples/configuration.ipynb)"
80
   ]
81
  },
82
  {
83
   "cell_type": "code",
84
   "execution_count": null,
85
   "metadata": {},
86
   "outputs": [],
87
   "source": [
88
    "from promptflow.azure import PFClient\n",
89
    "\n",
90
    "# Get a handle to workspace\n",
91
    "pf = PFClient.from_config(credential=credential)"
92
   ]
93
  },
94
  {
95
   "cell_type": "markdown",
96
   "metadata": {},
97
   "source": [
98
    "### Create necessary connections\n",
99
    "Connection helps securely store and manage secret keys or other sensitive credentials required for interacting with LLM and other external tools for example Azure Content Safety.\n",
100
    "\n",
101
    "In this notebook, we will use flow `basic` & `eval-code-quality` flex flow which uses connection `open_ai_connection` inside, we need to set up the connection if we haven't added it before.\n",
102
    "\n",
103
    "Prepare your Azure OpenAI resource follow this [instruction](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal) and get your `api_key` if you don't have one.\n",
104
    "\n",
105
    "Please go to [workspace portal](https://ml.azure.com/), click `Prompt flow` -> `Connections` -> `Create`, then follow the instruction to create your own connections. \n",
106
    "Learn more on [connections](https://learn.microsoft.com/en-us/azure/machine-learning/prompt-flow/concept-connections?view=azureml-api-2)."
107
   ]
108
  },
109
  {
110
   "cell_type": "markdown",
111
   "metadata": {},
112
   "source": [
113
    "## 2. Batch run the function as flow with multi-line data\n",
114
    "\n",
115
    "Create a `flow.flex.yaml` file to define a flow which entry pointing to the python function we defined.\n"
116
   ]
117
  },
118
  {
119
   "cell_type": "code",
120
   "execution_count": null,
121
   "metadata": {},
122
   "outputs": [],
123
   "source": [
124
    "# show the flow.flex.yaml content\n",
125
    "with open(\"flow.flex.yaml\") as fin:\n",
126
    "    print(fin.read())"
127
   ]
128
  },
129
  {
130
   "cell_type": "markdown",
131
   "metadata": {},
132
   "source": [
133
    "### Batch run with a data file (with multiple lines of test data)\n"
134
   ]
135
  },
136
  {
137
   "cell_type": "code",
138
   "execution_count": null,
139
   "metadata": {},
140
   "outputs": [],
141
   "source": [
142
    "flow = \".\"  # path to the flow directory\n",
143
    "data = \"./data.jsonl\"  # path to the data file\n",
144
    "\n",
145
    "# create run with the flow and data\n",
146
    "base_run = pf.run(\n",
147
    "    flow=flow,\n",
148
    "    data=data,\n",
149
    "    column_mapping={\n",
150
    "        \"text\": \"${data.text}\",\n",
151
    "    },\n",
152
    "    environment_variables={\n",
153
    "        \"AZURE_OPENAI_API_KEY\": \"${open_ai_connection.api_key}\",\n",
154
    "        \"AZURE_OPENAI_ENDPOINT\": \"${open_ai_connection.api_base}\",\n",
155
    "    },\n",
156
    "    stream=True,\n",
157
    ")"
158
   ]
159
  },
160
  {
161
   "cell_type": "code",
162
   "execution_count": null,
163
   "metadata": {},
164
   "outputs": [],
165
   "source": [
166
    "details = pf.get_details(base_run)\n",
167
    "details.head(10)"
168
   ]
169
  },
170
  {
171
   "cell_type": "markdown",
172
   "metadata": {},
173
   "source": [
174
    "## 3. Evaluate your flow\n",
175
    "Then you can use an evaluation method to evaluate your flow. The evaluation methods are also flows which usually using LLM assert the produced output matches certain expectation. "
176
   ]
177
  },
178
  {
179
   "cell_type": "markdown",
180
   "metadata": {},
181
   "source": [
182
    "### Setup model configuration with connection\n",
183
    "\n",
184
    "When used in cloud, create a model configuration object with connection name. \n",
185
    "The model config will be resolved from connection in runtime."
186
   ]
187
  },
188
  {
189
   "cell_type": "code",
190
   "execution_count": null,
191
   "metadata": {},
192
   "outputs": [],
193
   "source": [
194
    "from promptflow.core import AzureOpenAIModelConfiguration\n",
195
    "\n",
196
    "model_config = AzureOpenAIModelConfiguration(\n",
197
    "    connection=\"open_ai_connection\",\n",
198
    "    azure_deployment=\"gpt-35-turbo\",\n",
199
    ")"
200
   ]
201
  },
202
  {
203
   "cell_type": "markdown",
204
   "metadata": {},
205
   "source": [
206
    "### Run evaluation on the previous batch run\n",
207
    "The **base_run** is the batch run we completed in step 2 above, for web-classification flow with \"data.jsonl\" as input."
208
   ]
209
  },
210
  {
211
   "cell_type": "code",
212
   "execution_count": null,
213
   "metadata": {},
214
   "outputs": [],
215
   "source": [
216
    "eval_flow = \"../eval-code-quality/flow.flex.yaml\"\n",
217
    "\n",
218
    "eval_run = pf.run(\n",
219
    "    flow=eval_flow,\n",
220
    "    init={\"model_config\": model_config},\n",
221
    "    data=\"./data.jsonl\",  # path to the data file\n",
222
    "    run=base_run,  # specify base_run as the run you want to evaluate\n",
223
    "    column_mapping={\n",
224
    "        \"code\": \"${run.outputs.output}\",\n",
225
    "    },\n",
226
    "    stream=True,\n",
227
    ")"
228
   ]
229
  },
230
  {
231
   "cell_type": "code",
232
   "execution_count": null,
233
   "metadata": {},
234
   "outputs": [],
235
   "source": [
236
    "details = pf.get_details(eval_run)\n",
237
    "details.head(10)"
238
   ]
239
  },
240
  {
241
   "cell_type": "code",
242
   "execution_count": null,
243
   "metadata": {},
244
   "outputs": [],
245
   "source": [
246
    "import json\n",
247
    "\n",
248
    "metrics = pf.get_metrics(eval_run)\n",
249
    "print(json.dumps(metrics, indent=4))"
250
   ]
251
  },
252
  {
253
   "cell_type": "code",
254
   "execution_count": null,
255
   "metadata": {},
256
   "outputs": [],
257
   "source": [
258
    "pf.visualize([base_run, eval_run])"
259
   ]
260
  },
261
  {
262
   "cell_type": "markdown",
263
   "metadata": {},
264
   "source": [
265
    "## Next steps\n",
266
    "\n",
267
    "By now you've successfully run your first prompt flow and even did evaluation on it. That's great!\n",
268
    "\n",
269
    "You can check out more examples:\n",
270
    "- [Basic Chat](https://github.com/microsoft/promptflow/tree/main/examples/flex-flows/chat-basic): demonstrates how to create a chatbot that can remember previous interactions and use the conversation history to generate next message."
271
   ]
272
  }
273
 ],
274
 "metadata": {
275
  "build_doc": {
276
   "author": [
277
    "D-W-@github.com",
278
    "wangchao1230@github.com"
279
   ],
280
   "category": "azure",
281
   "section": "Flow",
282
   "weight": 10
283
  },
284
  "description": "A quickstart tutorial to run a flex flow and evaluate it in azure.",
285
  "kernelspec": {
286
   "display_name": "prompt_flow",
287
   "language": "python",
288
   "name": "python3"
289
  },
290
  "language_info": {
291
   "codemirror_mode": {
292
    "name": "ipython",
293
    "version": 3
294
   },
295
   "file_extension": ".py",
296
   "mimetype": "text/x-python",
297
   "name": "python",
298
   "nbconvert_exporter": "python",
299
   "pygments_lexer": "ipython3",
300
   "version": "3.9.18"
301
  },
302
  "resources": "examples/requirements-azure.txt, examples/flex-flows/basic, examples/flex-flows/eval-code-quality"
303
 },
304
 "nbformat": 4,
305
 "nbformat_minor": 2
306
}
307

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.