-
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.
Add doc for how to customize an LLM Tool. (#852)
# Description Add doc for how to customize an LLM Tool. # All Promptflow Contribution checklist: - [X] **The pull request does not introduce [breaking changes].** - [ ] **CHANGELOG is updated for new features, bug fixes or other significant changes.** - [X] **I have read the [contribution guidelines](../CONTRIBUTING.md).** - [ ] **Create an issue and link to the pull request to get dedicated review from promptflow team. Learn more: [suggested workflow](../CONTRIBUTING.md#suggested-workflow).** ## General Guidelines and Best Practices - [X] Title of the pull request is clear and informative. - [X] There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, [see this page](https://github.com/Azure/azure-powershell/blob/master/documentation/development-docs/cleaning-up-commits.md). ### Testing Guidelines - [ ] Pull request includes test coverage for the included changes.
- Loading branch information
1 parent
e23381c
commit caa2426
Showing
9 changed files
with
80 additions
and
5 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
60 changes: 60 additions & 0 deletions
60
docs/how-to-guides/develop-a-tool/customize_an_llm_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,60 @@ | ||
# Customize an LLM Tool | ||
In this document, we will guide you through the process of customizing an LLM tool, allowing users to seamlessly connect to a large language model with prompt tuning experience using a `PromptTemplate`. | ||
|
||
## Prerequisites | ||
- Please ensure that your [Prompt flow for VS Code](https://marketplace.visualstudio.com/items?itemName=prompt-flow.prompt-flow) is updated to version 1.2.0 or later. | ||
|
||
## How to customize an LLM tool | ||
Here we use [an existing tool package](https://github.com/microsoft/promptflow/tree/main/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](create-and-use-tool-package.md). | ||
|
||
1. Develop the tool code as in [this example](https://github.com/microsoft/promptflow/blob/main/examples/tools/tool-package-quickstart/my_tool_package/tools/tool_with_custom_llm_type.py). | ||
- Add a `CustomConnection` input to the tool, which is used to authenticate and establish a connection to the large language model. | ||
- Add a `PromptTemplate` input to the tool, which serves as an argument to be passed into the large language model. | ||
|
||
```python | ||
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: | ||
# Customize your own code to use the connection and prompt here. | ||
rendered_prompt = Template(prompt, trim_blocks=True, keep_trailing_newline=True).render(**kwargs) | ||
return rendered_prompt | ||
``` | ||
|
||
2. Generate the custom LLM tool YAML. | ||
Run the command below in your tool project directory to automatically generate your tool YAML, use _-t "custom_llm"_ or _--tool-type "custom_llm"_ to indicate this is a custom LLM tool: | ||
``` | ||
python <path-to-scripts>\tool\generate_package_tool_meta.py -m <tool_module> -o <tool_yaml_path> -t "custom_llm" | ||
``` | ||
Here we use [an existing tool](https://github.com/microsoft/promptflow/blob/main/examples/tools/tool-package-quickstart/my_tool_package/yamls/tool_with_custom_llm_type.yaml) as an example. | ||
``` | ||
cd D:\proj\github\promptflow\examples\tools\tool-package-quickstart | ||
|
||
python D:\proj\github\promptflow\scripts\tool\generate_package_tool_meta.py -m my_tool_package.tools.tool_with_custom_llm_type -o my_tool_package\yamls\tool_with_custom_llm_type.yaml -n "My Custom LLM Tool" -d "This is a tool to demonstrate how to customize an LLM tool with a PromptTemplate." -t "custom_llm" | ||
``` | ||
This command will generate a YAML file as follows: | ||
|
||
```yaml | ||
my_tool_package.tools.tool_with_custom_llm_type.my_tool: | ||
name: My Custom LLM Tool | ||
description: This is a tool to demonstrate how to customize an LLM tool with a PromptTemplate. | ||
# The type is custom_llm. | ||
type: custom_llm | ||
module: my_tool_package.tools.tool_with_custom_llm_type | ||
function: my_tool | ||
inputs: | ||
connection: | ||
type: | ||
- CustomConnection | ||
``` | ||
|
||
## Use the tool in VS Code | ||
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 use an existing flow to demonstrate the experience, open [this flow](https://github.com/microsoft/promptflow/blob/main/examples/tools/use-cases/custom_llm_tool_showcase/flow.dag.yaml) in VS Code extension. | ||
- There is a node named "my_custom_llm_tool" with a prompt template file. You can either use an existing file or create a new one as the prompt template file. | ||
![use_my_custom_llm_tool](../../media/how-to-guides/develop-a-tool/use_my_custom_llm_tool.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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
2 changes: 1 addition & 1 deletion
2
examples/tools/tool-package-quickstart/my_tool_package/yamls/tool_with_custom_llm_type.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
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
7 changes: 6 additions & 1 deletion
7
examples/tools/use-cases/custom_llm_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 |
---|---|---|
@@ -1 +1,6 @@ | ||
This is the prompt for my custom LLM tool. | ||
Welcome to {{ website_name }}! | ||
{% if user_name %} | ||
Hello, {{ user_name }}! | ||
{% else %} | ||
Hello there! | ||
{% endif %} |