Skip to content

Commit

Permalink
Merge branch 'zhrua/update_tool_howto_doc' of https://github.com/micr…
Browse files Browse the repository at this point in the history
…osoft/promptflow into tool_doc
  • Loading branch information
lalala123123 committed Nov 28, 2023
2 parents f10fe4e + 8a21e8c commit 342fbbe
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ pip install promptflow

### Create custom tool package
You can use [pf tool init](../../reference/pf-command-reference.md#pf-tool-init) to initialize a package tool in current folder:
```

```bash
pf tool init --package <your-package-name> --tool <your-tool-name>

```
For example:
```
```bash
pf tool init --package hello_world --tool hello_world_tool
```
This auto-generated script will create one tool for you. The parameters _destination_ and _package-name_ are mandatory. The parameters _tool-name_ and _function-name_ are optional. If left unfilled, the _tool-name_ will default to _hello_world_tool_, and the _function-name_ will default to _tool-name_.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
Cascading input settings are useful when the value of one input field determines which subsequent inputs are shown. This makes the input process more streamlined, user-friendly, and error-free. This guide will walk through how to create cascading inputs for your tools.

## Prerequisites
Please make sure you have the latest version of [Prompt flow for VS Code](https://marketplace.visualstudio.com/items?itemName=prompt-flow.prompt-flow) installed (v1.2.0+).

- Please make sure you have the latest version of [Prompt flow for VS Code](https://marketplace.visualstudio.com/items?itemName=prompt-flow.prompt-flow) installed (v1.2.0+).
- Please install promptflow package and ensure that its version is 1.0.0 or later.
```
pip install promptflow>=1.0.0
```

## Create a tool with cascading inputs
We'll build out an example tool to show how cascading inputs work. The `student_id` and `teacher_id` inputs will be controlled by the value selected for the `user_type` input. Here's how to configure this in the tool code.
Expand All @@ -18,7 +21,6 @@ Develop the tool function, following the [cascading inputs example](https://gith
* The `enabled_by_value` attribute defines the accepted enum values from the `enabled_by` field that will make this dependent input field visible.
> Note: `enabled_by_value` takes a list, allowing multiple values to enable an input.

```python
from enum import Enum

Expand All @@ -41,7 +43,6 @@ class UserType(str, Enum):
)
def my_tool(user_type: UserType, student_id: str = "", teacher_id: str = "") -> str:
"""This is a dummy function to support enabled by feature.
:param user_type: user type, student or teacher.
:param student_id: student id.
:param teacher_id: teacher id.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def my_list_func(prefix: str = "", size: int = 10, **kwargs) -> List[Dict[str, U

In `input_settings` section of tool, add following properties to the input that you want to make dynamic:

- `dynamic_list`:
- `DynamicList`:
- `function`: Path to the list function (module_name.function_name).
- `input_mapping`: Parameters to pass to the function, can reference other input values.
- `allow_manual_entry`: Allow user to enter input value manually. Default to false.
Expand Down Expand Up @@ -93,7 +93,6 @@ input_settings = {
def my_tool(input_text: list, input_prefix: str) -> str:
return f"Hello {input_prefix} {','.join(input_text)}"
```

## Use the tool in VS Code

Once you package and share your tool, you can use it in VS Code per the [tool package guide](create-and-use-tool-package.md#use-your-tool-from-vscode-extension). You could try `my-tools-package` for a quick test.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ For other connections types, please refer to [Connections](https://microsoft.git
- Please ensure that your [Prompt flow for VS Code](https://marketplace.visualstudio.com/items?itemName=prompt-flow.prompt-flow) is updated to at least version 1.2.1.
- Please install promptflow package and ensure that its version is 0.1.0b8 or later.
```
pip install promptflow>=0.1.0b8
pip install promptflow>=1.0.0
```

## Create a custom strong type connection
Expand Down
31 changes: 17 additions & 14 deletions docs/how-to-guides/develop-a-tool/use-file-path-as-tool-input.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,24 @@ Here we use [an existing tool package](https://github.com/microsoft/promptflow/t

Add a `FilePath` input for your tool, like in [this example](https://github.com/microsoft/promptflow/blob/main/examples/tools/tool-package-quickstart/my_tool_package/tools/tool_with_file_path_input.py).

```python
import importlib
from pathlib import Path
from promptflow import tool
# 1. import the FilePath type
from promptflow.contracts.types import FilePath
```python
import importlib
from pathlib import Path
from promptflow import tool
# 1. import the FilePath type
from promptflow.contracts.types import FilePath

# 2. add a FilePath input for your tool method
@tool()
def my_tool(input_file: FilePath, input_text: str) -> str:
# 3. customise your own code to handle and use the input_file here
new_module = importlib.import_module(Path(input_file).stem)

return new_module.hello(input_text)
```

> [!Note] tool yaml file can be generated using a python script. For further details, please refer to [create custom tool package](create-and-use-tool-package.md#create-custom-tool-package).
# 2. add a FilePath input for your tool method
@tool
def my_tool(input_file: FilePath, input_text: str) -> str:
# 3. customise your own code to handle and use the input_file here
new_module = importlib.import_module(Path(input_file).stem)

return new_module.hello(input_text)
```

### Use tool with a file path input in VS Code extension

Expand Down

0 comments on commit 342fbbe

Please sign in to comment.