-
Notifications
You must be signed in to change notification settings - Fork 904
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
docs/how-to-guides/develop-a-tool/add-prompt-template-for-tool.md
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,51 @@ | ||
# Add prompt template for tool | ||
Users sometimes need to use a 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 | ||
- Create a tool package as described in [Create and Use Tool Package](create-and-use-tool-package.md). | ||
- Ensure that the tool's type is `custom_llm`. | ||
|
||
## 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. | ||
|
||
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. Please note that the `PromptTemplate` input should not be included in the YAML. You can check out an example in [this location](../../../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 a tool with a prompt template input in VS Code extension | ||
To use your tool with a prompt template input, follow the 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 will 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 a prompt template file, and the inputs of this node contain the input for the prompt template. | ||
|
||
![use_prompt_template_in_flow](../../media/how-to-guides/develop-a-tool/use_prompt_template_in_flow.png) |
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
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
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_ai_connection | ||
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, customise your own code to handle and use the prompt here. | ||
# 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 |
28 changes: 28 additions & 0 deletions
28
examples/tools/tool-package-quickstart/tests/test_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,28 @@ | ||
import pytest | ||
import unittest | ||
|
||
from promptflow.connections import CustomConnection | ||
from my_tool_package.tools.tool_with_prompt_template_input import my_tool | ||
|
||
|
||
@pytest.fixture | ||
def my_custom_connection() -> CustomConnection: | ||
my_custom_connection = CustomConnection( | ||
{ | ||
"api-key" : "my-api-key", | ||
"api-secret" : "my-api-secret", | ||
"api-url" : "my-api-url" | ||
} | ||
) | ||
return my_custom_connection | ||
|
||
|
||
class TestToolWithPromptTemplateInput: | ||
def test_tool_with_prompt_template_input(self, my_custom_connection): | ||
result = my_tool(my_custom_connection, "Hello {{text}}", text="Microsoft") | ||
assert result == "Hello Microsoft" | ||
|
||
|
||
# Run the unit tests | ||
if __name__ == "__main__": | ||
unittest.main() |