chore: import upstream snapshot with attribution
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
Flake8 Lint / flake8 (push) Has been cancelled
Spell check CI / Spell_Check (push) Has been cancelled
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
Flake8 Lint / flake8 (push) Has been cancelled
Spell check CI / Spell_Check (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
AZURE_OPENAI_API_KEY=<your_AOAI_key>
|
||||
AZURE_OPENAI_API_BASE=<your_AOAI_endpoint>
|
||||
AZURE_OPENAI_API_TYPE=azure
|
||||
@@ -0,0 +1,134 @@
|
||||
# Basic standard flow
|
||||
A basic standard flow using custom python tool that calls Azure OpenAI with connection info stored in environment variables.
|
||||
|
||||
Tools used in this flow:
|
||||
- `prompt` tool
|
||||
- custom `python` Tool
|
||||
|
||||
Connections used in this flow:
|
||||
- None
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Install promptflow sdk and other dependencies:
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
## Run flow
|
||||
|
||||
- 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.
|
||||
|
||||
- Setup environment variables
|
||||
|
||||
Ensure you have put your azure OpenAI endpoint key in [.env](.env) file. You can create one refer to this [example file](.env.example).
|
||||
|
||||
```bash
|
||||
cat .env
|
||||
```
|
||||
|
||||
- Test flow/node
|
||||
```bash
|
||||
# test with default input value in flow.dag.yaml
|
||||
pf flow test --flow .
|
||||
|
||||
# test with flow inputs
|
||||
pf flow test --flow . --inputs text="Java Hello World!"
|
||||
|
||||
# test node with inputs
|
||||
pf flow test --flow . --node llm --inputs prompt="Write a simple Hello World program that displays the greeting message."
|
||||
```
|
||||
|
||||
- Create run with multiple lines data
|
||||
```bash
|
||||
# using environment from .env file (loaded in user code: hello.py)
|
||||
pf run create --flow . --data ./data.jsonl --column-mapping text='${data.text}' --stream
|
||||
```
|
||||
|
||||
You can also skip providing `column-mapping` if provided data has same column name as the flow.
|
||||
Reference [here](https://aka.ms/pf/column-mapping) for default behavior when `column-mapping` not provided in CLI.
|
||||
|
||||
- List and show run meta
|
||||
```bash
|
||||
# list created run
|
||||
pf run list
|
||||
|
||||
# get a sample run name
|
||||
name=$(pf run list -r 10 | jq '.[] | select(.name | contains("basic_variant_0")) | .name'| head -n 1 | tr -d '"')
|
||||
|
||||
# show specific run detail
|
||||
pf run show --name $name
|
||||
|
||||
# show output
|
||||
pf run show-details --name $name
|
||||
|
||||
# visualize run in browser
|
||||
pf run visualize --name $name
|
||||
```
|
||||
|
||||
## Run flow with connection
|
||||
Storing connection info in .env with plaintext is not safe. We recommend to use `pf connection` to guard secrets like `api_key` from leak.
|
||||
|
||||
- Show or create `open_ai_connection`
|
||||
```bash
|
||||
# create connection from `azure_openai.yml` file
|
||||
# Override keys with --set to avoid yaml file changes
|
||||
pf connection create --file ../../../connections/azure_openai.yml --set api_key=<your_api_key> api_base=<your_api_base>
|
||||
|
||||
# check if connection exists
|
||||
pf connection show -n open_ai_connection
|
||||
```
|
||||
|
||||
- Test using connection secret specified in environment variables
|
||||
**Note**: we used `'` to wrap value since it supports raw value without escape in powershell & bash. For windows command prompt, you may remove the `'` to avoid it become part of the value.
|
||||
|
||||
```bash
|
||||
# test with default input value in flow.dag.yaml
|
||||
pf flow test --flow . --environment-variables AZURE_OPENAI_API_KEY='${open_ai_connection.api_key}' AZURE_OPENAI_API_BASE='${open_ai_connection.api_base}'
|
||||
```
|
||||
|
||||
- Create run using connection secret binding specified in environment variables, see [run.yml](run.yml)
|
||||
```bash
|
||||
# create run
|
||||
pf run create --flow . --data ./data.jsonl --stream --environment-variables AZURE_OPENAI_API_KEY='${open_ai_connection.api_key}' AZURE_OPENAI_API_BASE='${open_ai_connection.api_base}' --column-mapping text='${data.text}'
|
||||
# create run using yaml file
|
||||
pf run create --file run.yml --stream
|
||||
|
||||
# show outputs
|
||||
name=$(pf run list -r 10 | jq '.[] | select(.name | contains("basic_variant_0")) | .name'| head -n 1 | tr -d '"')
|
||||
pf run show-details --name $name
|
||||
```
|
||||
|
||||
## Run flow in cloud with connection
|
||||
- Assume we already have a connection named `open_ai_connection` in workspace.
|
||||
```bash
|
||||
# set default workspace
|
||||
az account set -s <your_subscription_id>
|
||||
az configure --defaults group=<your_resource_group_name> workspace=<your_workspace_name>
|
||||
```
|
||||
|
||||
- Create run
|
||||
```bash
|
||||
# run with environment variable reference connection in azureml workspace
|
||||
pfazure run create --flow . --data ./data.jsonl --environment-variables AZURE_OPENAI_API_KEY='${open_ai_connection.api_key}' AZURE_OPENAI_API_BASE='${open_ai_connection.api_base}' --column-mapping text='${data.text}' --stream
|
||||
# run using yaml file
|
||||
pfazure run create --file run.yml --stream
|
||||
```
|
||||
|
||||
- List and show run meta
|
||||
```bash
|
||||
# list created run
|
||||
pfazure run list -r 3
|
||||
|
||||
# get a sample run name
|
||||
name=$(pfazure run list -r 100 | jq '.[] | select(.name | contains("basic_variant_0")) | .name'| head -n 1 | tr -d '"')
|
||||
|
||||
# show specific run detail
|
||||
pfazure run show --name $name
|
||||
|
||||
# show output
|
||||
pfazure run show-details --name $name
|
||||
|
||||
# visualize run in browser
|
||||
pfazure run visualize --name $name
|
||||
```
|
||||
@@ -0,0 +1,3 @@
|
||||
{"text": "Python Hello World!"}
|
||||
{"text": "C Hello World!"}
|
||||
{"text": "C# Hello World!"}
|
||||
@@ -0,0 +1,28 @@
|
||||
$schema: https://azuremlschemas.azureedge.net/promptflow/latest/Flow.schema.json
|
||||
environment:
|
||||
python_requirements_txt: requirements.txt
|
||||
inputs:
|
||||
text:
|
||||
type: string
|
||||
default: Hello World!
|
||||
outputs:
|
||||
output:
|
||||
type: string
|
||||
reference: ${llm.output}
|
||||
nodes:
|
||||
- name: hello_prompt
|
||||
type: prompt
|
||||
source:
|
||||
type: code
|
||||
path: hello.jinja2
|
||||
inputs:
|
||||
text: ${inputs.text}
|
||||
- name: llm
|
||||
type: python
|
||||
source:
|
||||
type: code
|
||||
path: hello.py
|
||||
inputs:
|
||||
prompt: ${hello_prompt.output}
|
||||
deployment_name: gpt-35-turbo-instruct
|
||||
max_tokens: "120"
|
||||
@@ -0,0 +1,2 @@
|
||||
{# Please replace the template with your own prompt. #}
|
||||
Write a simple {{text}} program that displays the greeting message.
|
||||
@@ -0,0 +1,88 @@
|
||||
import os
|
||||
from openai.version import VERSION as OPENAI_VERSION
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from promptflow.core import tool
|
||||
|
||||
# The inputs section will change based on the arguments of the tool function, after you save the code
|
||||
# Adding type to arguments and return value will help the system show the types properly
|
||||
# Please update the function name/signature per need
|
||||
|
||||
|
||||
def to_bool(value) -> bool:
|
||||
return str(value).lower() == "true"
|
||||
|
||||
|
||||
def get_client():
|
||||
if OPENAI_VERSION.startswith("0."):
|
||||
raise Exception(
|
||||
"Please upgrade your OpenAI package to version >= 1.0.0 or using the command: pip install --upgrade openai."
|
||||
)
|
||||
api_key = os.environ["AZURE_OPENAI_API_KEY"]
|
||||
conn = dict(
|
||||
api_key=os.environ["AZURE_OPENAI_API_KEY"],
|
||||
)
|
||||
if api_key.startswith("sk-"):
|
||||
from openai import OpenAI as Client
|
||||
else:
|
||||
from openai import AzureOpenAI as Client
|
||||
conn.update(
|
||||
azure_endpoint=os.environ.get("AZURE_OPENAI_API_BASE", "azure"),
|
||||
api_version=os.environ.get("OPENAI_API_VERSION", "2023-07-01-preview"),
|
||||
)
|
||||
return Client(**conn)
|
||||
|
||||
|
||||
@tool
|
||||
def my_python_tool(
|
||||
prompt: str,
|
||||
# for AOAI, deployment name is customized by user, not model name.
|
||||
deployment_name: str,
|
||||
suffix: str = None,
|
||||
max_tokens: int = 120,
|
||||
temperature: float = 1.0,
|
||||
top_p: float = 1.0,
|
||||
n: int = 1,
|
||||
logprobs: int = None,
|
||||
echo: bool = False,
|
||||
stop: list = None,
|
||||
presence_penalty: float = 0,
|
||||
frequency_penalty: float = 0,
|
||||
best_of: int = 1,
|
||||
logit_bias: dict = {},
|
||||
user: str = "",
|
||||
**kwargs,
|
||||
) -> str:
|
||||
if "AZURE_OPENAI_API_KEY" not in os.environ or "AZURE_OPENAI_API_BASE" not in os.environ:
|
||||
# load environment variables from .env file
|
||||
load_dotenv()
|
||||
|
||||
if "AZURE_OPENAI_API_KEY" not in os.environ:
|
||||
raise Exception("Please specify environment variables: AZURE_OPENAI_API_KEY")
|
||||
|
||||
# TODO: remove below type conversion after client can pass json rather than string.
|
||||
echo = to_bool(echo)
|
||||
|
||||
response = get_client().completions.create(
|
||||
prompt=prompt,
|
||||
model=deployment_name,
|
||||
# empty string suffix should be treated as None.
|
||||
suffix=suffix if suffix else None,
|
||||
max_tokens=int(max_tokens),
|
||||
temperature=float(temperature),
|
||||
top_p=float(top_p),
|
||||
n=int(n),
|
||||
logprobs=int(logprobs) if logprobs else None,
|
||||
echo=echo,
|
||||
# fix bug "[] is not valid under any of the given schemas-'stop'"
|
||||
stop=stop if stop else None,
|
||||
presence_penalty=float(presence_penalty),
|
||||
frequency_penalty=float(frequency_penalty),
|
||||
best_of=int(best_of),
|
||||
# Logit bias must be a dict if we passed it to openai api.
|
||||
logit_bias=logit_bias if logit_bias else {},
|
||||
user=user,
|
||||
)
|
||||
|
||||
# get first element because prompt is single.
|
||||
return response.choices[0].text
|
||||
@@ -0,0 +1,3 @@
|
||||
promptflow[azure]
|
||||
promptflow-tools
|
||||
python-dotenv
|
||||
@@ -0,0 +1,10 @@
|
||||
$schema: https://azuremlschemas.azureedge.net/promptflow/latest/Run.schema.json
|
||||
flow: .
|
||||
data: data.jsonl
|
||||
environment_variables:
|
||||
# environment variables from connection
|
||||
AZURE_OPENAI_API_KEY: ${open_ai_connection.api_key}
|
||||
AZURE_OPENAI_API_BASE: ${open_ai_connection.api_base}
|
||||
AZURE_OPENAI_API_TYPE: azure
|
||||
column_mapping:
|
||||
text: ${data.text}
|
||||
Reference in New Issue
Block a user