The Python Tool empowers users to offer customized code snippets as self-contained executable nodes in prompt flow. Users can effortlessly create Python tools, edit code, and verify results with ease.
|Code||string||Python code snippet||Yes|
|Inputs||-||List of tool function parameters and its assignments||-|
|int||param: int||Integer type|
|bool||param: bool||Boolean type|
|string||param: str||String type|
|double||param: float||Double type|
|list||param: list or param: List[T]||List type|
|object||param: dict or param: Dict[K, V]||Object type|
|Connection||param: CustomConnection||Connection type will be handled specially|
Connection type annotation will be treated as connection inputs, which means:
- Prompt flow extension will show a selector to select the connection.
- During execution time, prompt flow will try to find the connection with the name same from parameter value passed in.
Union[...] type annotation is supported ONLY for connection type, for example,
param: Union[CustomConnection, OpenAIConnection].
The return of the python tool function.
How to write Python Tool?
Python Tool Code should consist of a complete Python code, including any necessary module imports.
Python Tool Code must contain a function decorated with @tool (tool function), serving as the entry point for execution. The @tool decorator should be applied only once within the snippet.
*The sample in the next section defines python tool "my_python_tool" which decorated with @tool*
Python tool function parameters must be assigned in 'Inputs' section
The sample in the next section defines inputs "message" and assign with "world"
Python tool function shall have return
The sample in the next section returns a concatenated string
This snippet shows the basic structure of a tool function. Prompt flow will read the function and extract inputs from function parameters and type annotations.
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
|Name||Type||Sample Value in Flow Yaml||Value passed to function|
Prompt flow will try to find the connection named 'my_conn' during execution time.
How to consume custom connection in Python Tool?
If you are developing a python tool that requires calling external services with authentication, you can use the custom connection in prompt flow. It allows you to securely store the access key then retrieve it in your python code.
Create a custom connection
Create a custom connection that stores all your LLM API KEY or other required credentials.
- Go to Prompt flow in your workspace, then go to connections tab.
- Select Create and select Custom.
- In the right panel, you can define your connection name, and you can add multiple Key-value pairs to store your credentials and keys by selecting Add key-value pairs.
- You can set one Key-Value pair as secret by is secret checked, which will be encrypted and stored in your key value.
- Make sure at least one key-value pair is set as secret, otherwise the connection will not be created successfully.
Consume custom connection in Python
To consume a custom connection in your python code, follow these steps:
- In the code section in your python node, import custom connection library
from promptflow.connections import CustomConnection, and define an input parameter of type
CustomConnectionin the tool function.
- Parse the input to the input section, then select your target custom connection in the value dropdown.
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