Skip to content

Commit

Permalink
add prompt template tool and flow
Browse files Browse the repository at this point in the history
update
  • Loading branch information
jiazengcindy committed Oct 23, 2023
1 parent 56ab01a commit 0318594
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 0 deletions.
50 changes: 50 additions & 0 deletions docs/how-to-guides/develop-a-tool/add-prompt-template.md
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)
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_source_llm
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.
# 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

0 comments on commit 0318594

Please sign in to comment.