你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
使用 Python 工具,可以在提示流中将自定义代码片段创建为自包含可执行节点。 可以轻松创建 Python 工具、编辑代码和验证结果。
Inputs
| Name | 类型 | Description | Required |
|---|---|---|---|
| Code | 字符串 | Python 代码片段 | Yes |
| Inputs | - | 工具函数参数及其赋值列表 | - |
Types
| 类型 | Python example | Description |
|---|---|---|
| int | param: int | Integer type |
| bool | param: bool | Boolean type |
| 字符串 | param: str | String type |
| double | param: float | Double type |
| list | param: list 或 param: List[T] | List type |
| 对象 | param: dict 或 param: Dict[K, V] | Object type |
| Connection | param: CustomConnection | 连接类型为专门处理 |
具有 Connection 类型注释的参数将视为连接输入,这意味着:
- 提示流扩展显示用于选择连接的选择器。
- 在执行期间,提示流尝试从传入的参数值中找到具有相同名称的连接。
Note
The Union[...] type annotation is supported only for the connection type, for example, param: Union[CustomConnection, OpenAIConnection].
Outputs
输出是 Python 工具函数的返回值。
使用 Python 工具写入
使用 Python 工具编写时,请使用以下准则。
Guidelines
Python 工具代码应包含完整的 Python 代码,包括任何必要的模块导入。
Python 工具代码必须包含一个用
@tool修饰的函数(工具函数),该函数用作执行的入口点。 应仅在代码片段中应用@tool修饰器一次。以下示例定义用 Python 工具修饰
@tool的 Python 工具my_python_tool。必须在
Inputs节中分配 Python 工具函数参数。下面的示例定义输入
message并分配它world。Python 工具函数必须具有返回值。
下面的示例返回一个串联字符串。
Code
以下代码片段显示工具函数的基本结构。 提示流读取函数并从函数参数和类型注释中提取输入。
from promptflow import tool
from promptflow.connections import CustomConnection
# 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
@tool
def my_python_tool(message: str, my_conn: CustomConnection) -> str:
my_conn_dict = dict(my_conn)
# Do some function call with my_conn_dict...
return 'hello ' + message
Inputs
| Name | 类型 | 流 YAML 中的示例值 | 传递给函数的值 |
|---|---|---|---|
| 消息 | 字符串 | world |
world |
| my_conn | CustomConnection |
my_conn |
(属于CustomConnection 对象)的父级。 |
提示流尝试查找在执行期间命名 my_conn 的连接。
Outputs
"hello world"
从 Python 工具调用推理模型
如果需要调用 LLM 节点不支持的推理模型,则可以使用 Python 工具直接调用模型。 以下示例演示如何从 Python 工具调用推理模型。
from promptflow import tool
from promptflow.connections import AzureOpenAIConnection
from openai import AzureOpenAI
@tool
def my_python_tool(
OpenAIConnection: AzureOpenAIConnection,
scope_reply: str
):
model_name = "o3-mini"
deployment = "o3-mini"
print(OpenAIConnection['api_base'])
endpoint = OpenAIConnection['api_base'] #"https://<your endpoint>.openai.azure.com/"
model_name = "o3-mini" #your model name
deployment = "o3-mini" #your deployment name
subscription_key = OpenAIConnection['api_key']
api_version = "2024-12-01-preview" #Supply an API version that supports reasoning models.
client = AzureOpenAI(
api_version=api_version,
azure_endpoint=endpoint,
api_key=subscription_key,
)
response = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are a helpful assistant.",
},
{
"role": "user",
"content": "I am going to Paris, what should I see?",
}
],
max_completion_tokens=100000,
model=deployment
)
return response.choices[0].message.content
Python 工具中的自定义连接
如果正在开发需要通过身份验证调用外部服务的 Python 工具,请使用提示流中的自定义连接。 可以使用它来安全存储访问密钥,然后在 Python 代码中检索它。
创建自定义连接
创建自定义连接,用于存储所有大型语言模型 API 密钥或其他所需凭据。
Go to prompt flow in your workspace, and then select the Connections tab.
Select Create>Custom.
在右窗格中,可以定义连接名称。 可以通过选择“添加键值对”来添加多个键值对,以存储凭据和密钥。
Note
To set one key-value pair as secret, select the is secret checkbox. 此选项将加密和存储密钥值。 确保至少将一个键值对设置为机密。 否则,连接不能成功创建。
在 Python 中使用自定义连接
若要在 Python 代码中使用自定义连接:
在 Python 节点的代码部分中,导入自定义连接库
from promptflow.connections import CustomConnection。 在工具函数中定义类型CustomConnection的输入参数。Parse the input to the input section, and then select your target custom connection in the Value dropdown.
For example:
from promptflow import tool
from promptflow.connections import CustomConnection
@tool
def my_python_tool(message: str, myconn: CustomConnection) -> str:
# Get authentication key-values from the custom connection
connection_key1_value = myconn.key1
connection_key2_value = myconn.key2