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,108 @@
|
||||
# Basic flow with custom connection
|
||||
A basic standard flow that using custom python tool calls Azure OpenAI with connection info stored in custom connection.
|
||||
|
||||
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
|
||||
```
|
||||
|
||||
## Setup connection
|
||||
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.
|
||||
|
||||
Create connection if you haven't done that.
|
||||
```bash
|
||||
# Override keys with --set to avoid yaml file changes
|
||||
pf connection create -f custom.yml --set secrets.api_key=<your_api_key> configs.api_base=<your_api_base>
|
||||
```
|
||||
|
||||
Ensure you have created `basic_custom_connection` connection.
|
||||
```bash
|
||||
pf connection show -n basic_custom_connection
|
||||
```
|
||||
|
||||
## Run flow
|
||||
|
||||
### Run with single line input
|
||||
|
||||
```bash
|
||||
# test with default input value in flow.dag.yaml
|
||||
pf flow test --flow .
|
||||
|
||||
# test with flow inputs
|
||||
pf flow test --flow . --inputs text="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."
|
||||
|
||||
```
|
||||
|
||||
### Run with multiple lines data
|
||||
|
||||
- create run
|
||||
```bash
|
||||
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 -r 3
|
||||
|
||||
# get a sample run name
|
||||
name=$(pf run list -r 10 | jq '.[] | select(.name | contains("basic_with_connection")) | .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 with connection override
|
||||
|
||||
Ensure you have created `open_ai_connection` connection before.
|
||||
|
||||
```bash
|
||||
pf connection show -n open_ai_connection
|
||||
```
|
||||
|
||||
Create connection if you haven't done that.
|
||||
```bash
|
||||
# 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>
|
||||
```
|
||||
|
||||
Run flow with newly created connection.
|
||||
|
||||
```bash
|
||||
pf run create --flow . --data ./data.jsonl --connections llm.connection=open_ai_connection --column-mapping text='${data.text}' --stream
|
||||
```
|
||||
|
||||
### Run in cloud with connection override
|
||||
|
||||
Ensure you have created `open_ai_connection` connection in cloud. Reference [this notebook](../../../tutorials/get-started/quickstart-azure.ipynb) on how to create connections in cloud with UI.
|
||||
|
||||
Run flow with connection `open_ai_connection`.
|
||||
|
||||
```bash
|
||||
# set default workspace
|
||||
az account set -s <your_subscription_id>
|
||||
az configure --defaults group=<your_resource_group_name> workspace=<your_workspace_name>
|
||||
|
||||
pfazure run create --flow . --data ./data.jsonl --connections llm.connection=open_ai_connection --column-mapping text='${data.text}' --stream
|
||||
```
|
||||
@@ -0,0 +1,9 @@
|
||||
$schema: https://azuremlschemas.azureedge.net/promptflow/latest/CustomConnection.schema.json
|
||||
name: basic_custom_connection
|
||||
type: custom
|
||||
configs:
|
||||
api_type: azure
|
||||
api_version: 2023-03-15-preview
|
||||
api_base: https://<to-be-replaced>.openai.azure.com/
|
||||
secrets: # must-have
|
||||
api_key: <to-be-replaced>
|
||||
@@ -0,0 +1,3 @@
|
||||
{"text": "Python Hello World!"}
|
||||
{"text": "C Hello World!"}
|
||||
{"text": "C# Hello World!"}
|
||||
@@ -0,0 +1,29 @@
|
||||
$schema: https://azuremlschemas.azureedge.net/promptflow/latest/Flow.schema.json
|
||||
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:
|
||||
connection: basic_custom_connection
|
||||
deployment_name: gpt-35-turbo-instruct
|
||||
max_tokens: "120"
|
||||
prompt: ${hello_prompt.output}
|
||||
environment:
|
||||
python_requirements_txt: requirements.txt
|
||||
@@ -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,85 @@
|
||||
from typing import Union
|
||||
from openai.version import VERSION as OPENAI_VERSION
|
||||
|
||||
from promptflow.core import tool
|
||||
from promptflow.connections import CustomConnection, AzureOpenAIConnection
|
||||
|
||||
# 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(connection: Union[CustomConnection, AzureOpenAIConnection]):
|
||||
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."
|
||||
)
|
||||
# connection can be extract as a dict object contains the configs and secrets
|
||||
connection_dict = dict(connection)
|
||||
api_key = connection_dict.get("api_key")
|
||||
conn = dict(
|
||||
api_key=api_key,
|
||||
)
|
||||
if api_key.startswith("sk-"):
|
||||
from openai import OpenAI as Client
|
||||
else:
|
||||
from openai import AzureOpenAI as Client
|
||||
conn.update(
|
||||
azure_endpoint=connection_dict.get("api_base"),
|
||||
api_version=connection_dict.get("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 = "",
|
||||
connection: Union[CustomConnection, AzureOpenAIConnection] = None,
|
||||
**kwargs,
|
||||
) -> str:
|
||||
|
||||
# TODO: remove below type conversion after client can pass json rather than string.
|
||||
echo = to_bool(echo)
|
||||
|
||||
response = get_client(connection).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
|
||||
Reference in New Issue
Block a user