Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Document & Expose StringPromptTemplate as a custom-prompt-template. #1753

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions docs/modules/prompts/examples/custom_prompt_template.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@
"id": "5d56ce86",
"metadata": {},
"source": [
"## Create a custom prompt template\n",
"## Creating a Custom Prompt Template\n",
"\n",
"The only two requirements for all prompt templates are:\n",
"There are essentially two distinct prompt templates available - string prompt templates and chat prompt templates. String prompt templates provides a simple prompt in string format, while chat prompt templates produces a more structured prompt to be used with a chat API.\n",
"\n",
"1. They have a input_variables attribute that exposes what input variables this prompt template expects.\n",
"2. They expose a format method which takes in keyword arguments corresponding to the expected input_variables and returns the formatted prompt.\n",
"In this guide, we will create a custom prompt using a string prompt template. \n",
"\n",
"Let's create a custom prompt template that takes in the function name as input, and formats the prompt template to provide the source code of the function.\n",
"To create a custom string prompt template, there are two requirements:\n",
"1. It has an input_variables attribute that exposes what input variables the prompt template expects.\n",
"2. It exposes a format method that takes in keyword arguments corresponding to the expected input_variables and returns the formatted prompt.\n",
"\n",
"First, let's create a function that will return the source code of a function given its name."
"We will create a custom prompt template that takes in the function name as input and formats the prompt to provide the source code of the function. To achieve this, let's first create a function that will return the source code of a function given its name."
]
},
{
Expand Down Expand Up @@ -62,11 +63,11 @@
"metadata": {},
"outputs": [],
"source": [
"from langchain.prompts import BasePromptTemplate\n",
"from langchain.prompts import StringPromptTemplate\n",
"from pydantic import BaseModel, validator\n",
"\n",
"\n",
"class FunctionExplainerPromptTemplate(BasePromptTemplate, BaseModel):\n",
"class FunctionExplainerPromptTemplate(StringPromptTemplate, BaseModel):\n",
" \"\"\" A custom prompt template that takes in the function name as input, and formats the prompt template to provide the source code of the function. \"\"\"\n",
"\n",
" @validator(\"input_variables\")\n",
Expand Down
3 changes: 2 additions & 1 deletion langchain/prompts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Prompt template classes."""
from langchain.prompts.base import BasePromptTemplate
from langchain.prompts.base import BasePromptTemplate, StringPromptTemplate
from langchain.prompts.chat import (
AIMessagePromptTemplate,
ChatMessagePromptTemplate,
Expand All @@ -15,6 +15,7 @@

__all__ = [
"BasePromptTemplate",
"StringPromptTemplate",
"load_prompt",
"PromptTemplate",
"FewShotPromptTemplate",
Expand Down