Skip to content

Commit

Permalink
add prompt template tool and flow
Browse files Browse the repository at this point in the history
update

update

update
  • Loading branch information
jiazengcindy committed Oct 24, 2023
1 parent b599796 commit e98bd44
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 1 deletion.
51 changes: 51 additions & 0 deletions docs/how-to-guides/develop-a-tool/add-prompt-template-for-tool.md
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)
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,5 @@ Alternatively, you can test your tool package using the script below to ensure t
## Advanced features
[Customize your tool icon](add-a-tool-icon.md)
[Add category and tags for tool](add-category-and-tags-for-tool.md)
[Add category and tags for tool](add-category-and-tags-for-tool.md)
[Add Prompt Template for Custom LLM Tool](add-prompt-template-for-tool.md)
1 change: 1 addition & 0 deletions docs/how-to-guides/develop-a-tool/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ We provide guides on how to develop a tool and use it.
create-and-use-tool-package
add-a-tool-icon
add-category-and-tags-for-tool
add-prompt-template-for-tool
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello {{text}}.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
promptflow
promptflow-tools
my-tools-package
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
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
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()

0 comments on commit e98bd44

Please sign in to comment.