-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Add prompt template | ||
Users sometimes need to use prompt template within their tools. To simplify this, we've introduced the `PromptTemplate` feature. | ||
In this guide, we will provide a detailed walkthrough on how to use `PromptTemplate` as a tool input. We will also demonstrate the user experience when utilizing this type of tool within a flow. | ||
|
||
## Prerequisites | ||
Follow the [steps](create-and-use-tool-package.md#Prerequisites) to prepare prerequisites. | ||
|
||
## How to create a tool with prompt template | ||
Here we use [an existing tool package](../../../examples/tools/tool-package-quickstart/my_tool_package) as an example. If you want to create your own tool, please refer to create and use tool package. | ||
|
||
1. Add a `PromptTemplate` input for your tool, such as in [this example](../../../examples/tools/tool-package-quickstart/my_tool_package/tools/tool_with_prompt_template_input.py) | ||
|
||
```python | ||
from jinja2 import Template | ||
from promptflow import tool | ||
from promptflow.connections import CustomConnection | ||
# 1. import the PromptTemplate type | ||
from promptflow.contracts.types import PromptTemplate | ||
|
||
|
||
# 2. add a PromptTemplate input for your tool method | ||
@tool | ||
def my_tool(connection: CustomConnection, prompt: PromptTemplate, **kwargs) -> str: | ||
# 3. customise your own code to handle and use the prompt here | ||
message = Template(prompt, trim_blocks=True, keep_trailing_newline=True).render(**kwargs) | ||
return message | ||
``` | ||
|
||
2. Configure the tool YAML, Note that the `PromptTemplate` input should not be included in the YAML, like in [this example](../../../examples/tools/tool-package-quickstart/my_tool_package/yamls/tool_with_prompt_template_input.yaml) | ||
|
||
```yaml | ||
my_tool_package.tools.tool_with_prompt_template_input.my_tool: | ||
name: Tool with PromptTemplate | ||
description: This is a tool to demonstrate the usage of PromptTemplate | ||
type: custom_llm | ||
module: my_tool_package.tools.tool_with_prompt_template_input | ||
function: my_tool | ||
inputs: | ||
connection: | ||
type: | ||
- CustomConnection | ||
``` | ||
|
||
## Use tool with a prompt template input in VS Code extension | ||
Follow steps to [build and install your tool package](create-and-use-tool-package.md#build-and-share-the-tool-package) and [use your tool from VS Code extension](create-and-use-tool-package.md#use-your-tool-from-vscode-extension). | ||
|
||
Here we use an existing flow to demonstrate the experience, open [this flow](../../../examples/flows/standard/prompt-template-input-tool-showcase/flow.dag.yaml) in VS Code extension | ||
- There is a node named "tool_with_prompt_template" with prompt template file, and the inputs of this node contains the input for the prompt template.. | ||
|
||
![use_prompt_template_in_flow](../../media/how-to-guides/develop-a-tool/use_prompt_template_in_flow.png) |
Binary file added
BIN
+70.3 KB
docs/media/how-to-guides/develop-a-tool/use_prompt_template_in_flow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions
18
examples/flows/standard/prompt-template-input-tool-showcase/flow.dag.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
inputs: | ||
text: | ||
type: string | ||
default: Microsoft | ||
outputs: | ||
output: | ||
type: string | ||
reference: ${tool_with_prompt_template.output} | ||
nodes: | ||
- name: tool_with_prompt_template | ||
type: custom_llm | ||
source: | ||
type: package_with_prompt | ||
tool: my_tool_package.tools.tool_with_prompt_template_input.my_tool | ||
path: prompt_template.jinja2 | ||
inputs: | ||
connection: open_source_llm | ||
text: ${inputs.text} |
1 change: 1 addition & 0 deletions
1
examples/flows/standard/prompt-template-input-tool-showcase/prompt_template.jinja2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello {{text}}. |
3 changes: 3 additions & 0 deletions
3
examples/flows/standard/prompt-template-input-tool-showcase/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
promptflow | ||
promptflow-tools | ||
my-tools-package |
14 changes: 14 additions & 0 deletions
14
...es/tools/tool-package-quickstart/my_tool_package/tools/tool_with_prompt_template_input.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from jinja2 import Template | ||
from promptflow import tool | ||
from promptflow.connections import CustomConnection | ||
from promptflow.contracts.types import PromptTemplate | ||
|
||
|
||
@tool | ||
def my_tool(connection: CustomConnection, prompt: PromptTemplate, **kwargs) -> str: | ||
# Replace with your tool code. | ||
# Usually connection contains configs to connect to an API. | ||
# Use CustomConnection is a dict. You can use it like: connection.api_key, connection.api_base | ||
# Not all tools need a connection. You can remove it if you don't need it. | ||
message = Template(prompt, trim_blocks=True, keep_trailing_newline=True).render(**kwargs) | ||
return message |
10 changes: 10 additions & 0 deletions
10
.../tools/tool-package-quickstart/my_tool_package/yamls/tool_with_prompt_template_input.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
my_tool_package.tools.tool_with_prompt_template_input.my_tool: | ||
name: Tool with PromptTemplate | ||
description: This is a tool to demonstrate the usage of PromptTemplate | ||
type: custom_llm | ||
module: my_tool_package.tools.tool_with_prompt_template_input | ||
function: my_tool | ||
inputs: | ||
connection: | ||
type: | ||
- CustomConnection |