e768098d0e
Flake8 Lint / flake8 (push) Waiting to run
Spell check CI / Spell_Check (push) Waiting to run
tools_continuous_delivery / Private PyPI non-main branch release (push) Has been skipped
tools_continuous_delivery / Private PyPI main branch release (push) Failing after 2m42s
Publish Promptflow Doc / Build (push) Has been cancelled
Publish Promptflow Doc / Deploy (push) Has been cancelled
386 lines
9.8 KiB
Plaintext
386 lines
9.8 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Getting started with flex flow"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"**Learning Objectives** - Upon completing this tutorial, you should be able to:\n",
|
|
"\n",
|
|
"- Write LLM application using notebook and visualize the trace of your application.\n",
|
|
"- Convert the application into a flow and batch run against multi lines of data.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 0. Install dependent packages"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"%%capture --no-stderr\n",
|
|
"%pip install -r ./requirements.txt"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 1. Trace your application with promptflow\n",
|
|
"\n",
|
|
"Assume we already have a python function that calls OpenAI API. "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"with open(\"llm.py\") as fin:\n",
|
|
" print(fin.read())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Note: before running below cell, please configure required environment variable `AZURE_OPENAI_API_KEY`, `AZURE_OPENAI_ENDPOINT` by create an `.env` file. Please refer to `../.env.example` as an template."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# control the AOAI deployment (model) used in this example\n",
|
|
"deployment_name = \"gpt-4o\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from llm import my_llm_tool\n",
|
|
"\n",
|
|
"# pls configure `AZURE_OPENAI_API_KEY`, `AZURE_OPENAI_ENDPOINT` environment variables first\n",
|
|
"result = my_llm_tool(\n",
|
|
" prompt=\"Write a simple Hello, world! program that displays the greeting message when executed. Output code only.\",\n",
|
|
" deployment_name=deployment_name,\n",
|
|
")\n",
|
|
"result"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Visualize trace by using start_trace\n",
|
|
"\n",
|
|
"Note we add `@trace` in the `my_llm_tool` function, re-run below cell will collect a trace in trace UI."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from promptflow.tracing import start_trace\n",
|
|
"\n",
|
|
"# start a trace session, and print a url for user to check trace\n",
|
|
"start_trace()\n",
|
|
"# rerun the function, which will be recorded in the trace\n",
|
|
"result = my_llm_tool(\n",
|
|
" prompt=\"Write a simple Hello, world! program that displays the greeting message when executed. Output code only.\",\n",
|
|
" deployment_name=deployment_name,\n",
|
|
")\n",
|
|
"result"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Now, let's add another layer of function call. In `programmer.py` there is a function called `write_simple_program`, which calls a new function called `load_prompt` and previous `my_llm_tool` function."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# show the programmer.py content\n",
|
|
"with open(\"programmer.py\") as fin:\n",
|
|
" print(fin.read())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# call the flow entry function\n",
|
|
"from programmer import write_simple_program\n",
|
|
"\n",
|
|
"result = write_simple_program(\"Java Hello, world!\")\n",
|
|
"result"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Setup model configuration with environment variables\n",
|
|
"\n",
|
|
"When used in local, create a model configuration object with environment variables."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import os\n",
|
|
"from dotenv import load_dotenv\n",
|
|
"\n",
|
|
"from promptflow.core import AzureOpenAIModelConfiguration\n",
|
|
"\n",
|
|
"if \"AZURE_OPENAI_API_KEY\" not in os.environ:\n",
|
|
" # load environment variables from .env file\n",
|
|
" load_dotenv()\n",
|
|
"\n",
|
|
"if \"AZURE_OPENAI_API_KEY\" not in os.environ:\n",
|
|
" raise Exception(\"Please specify environment variables: AZURE_OPENAI_API_KEY\")\n",
|
|
"model_config = AzureOpenAIModelConfiguration(\n",
|
|
" azure_endpoint=os.environ[\"AZURE_OPENAI_ENDPOINT\"],\n",
|
|
" api_key=os.environ[\"AZURE_OPENAI_API_KEY\"],\n",
|
|
" azure_deployment=deployment_name,\n",
|
|
" api_version=\"2023-07-01-preview\",\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Eval the result "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"%load_ext autoreload\n",
|
|
"%autoreload 2\n",
|
|
"\n",
|
|
"import paths # add the code_quality module to the path\n",
|
|
"from code_quality import CodeEvaluator\n",
|
|
"\n",
|
|
"evaluator = CodeEvaluator(model_config=model_config)\n",
|
|
"eval_result = evaluator(result)\n",
|
|
"eval_result"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 2. Batch run the function as flow with multi-line data\n",
|
|
"\n",
|
|
"Create a [flow.flex.yaml](https://github.com/microsoft/promptflow/blob/main/examples/flex-flows/basic/flow.flex.yaml) file to define a flow which entry pointing to the python function we defined.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# show the flow.flex.yaml content\n",
|
|
"with open(\"flow.flex.yaml\") as fin:\n",
|
|
" print(fin.read())"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Batch run with a data file (with multiple lines of test data)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from promptflow.client import PFClient\n",
|
|
"\n",
|
|
"pf = PFClient()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"data = \"./data.jsonl\" # path to the data file\n",
|
|
"# create run with the flow function and data\n",
|
|
"base_run = pf.run(\n",
|
|
" flow=write_simple_program,\n",
|
|
" data=data,\n",
|
|
" column_mapping={\n",
|
|
" \"text\": \"${data.text}\",\n",
|
|
" },\n",
|
|
" stream=True,\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"details = pf.get_details(base_run)\n",
|
|
"details.head(10)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 3. Evaluate your flow\n",
|
|
"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. "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Run evaluation on the previous batch run\n",
|
|
"The **base_run** is the batch run we completed in step 2 above, for web-classification flow with \"data.jsonl\" as input."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# we can also run flow pointing to yaml file\n",
|
|
"eval_flow = \"../eval-code-quality/flow.flex.yaml\"\n",
|
|
"\n",
|
|
"eval_run = pf.run(\n",
|
|
" flow=eval_flow,\n",
|
|
" init={\"model_config\": model_config},\n",
|
|
" data=\"./data.jsonl\", # path to the data file\n",
|
|
" run=base_run, # specify base_run as the run you want to evaluate\n",
|
|
" column_mapping={\n",
|
|
" \"code\": \"${run.outputs.output}\",\n",
|
|
" },\n",
|
|
" stream=True,\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"details = pf.get_details(eval_run)\n",
|
|
"details.head(10)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import json\n",
|
|
"\n",
|
|
"metrics = pf.get_metrics(eval_run)\n",
|
|
"print(json.dumps(metrics, indent=4))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"pf.visualize([base_run, eval_run])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Next steps\n",
|
|
"\n",
|
|
"By now you've successfully run your first prompt flow and even did evaluation on it. That's great!\n",
|
|
"\n",
|
|
"You can check out more examples:\n",
|
|
"- [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."
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"build_doc": {
|
|
"author": [
|
|
"D-W-@github.com",
|
|
"wangchao1230@github.com"
|
|
],
|
|
"category": "local",
|
|
"section": "Flow",
|
|
"weight": 10
|
|
},
|
|
"description": "A quickstart tutorial to run a flex flow and evaluate it.",
|
|
"kernelspec": {
|
|
"display_name": "prompt_flow",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.9.18"
|
|
},
|
|
"resources": "examples/requirements.txt, examples/flex-flows/basic"
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|