01. Prompt
PromptTemplate
Copy
from dotenv import load_dotenv
load_dotenv()Copy
# Set up LangSmith tracking: https:
# !pip install -qU langchain-teddynote
from langchain_teddynote import logging
# Enter a project name.
logging.langsmith("CH02-Prompt")Copy
Start tracking LangSmith.
[Project name]
CH02-Prompt Define LLM objects.
Copy
from langchain_openai import ChatOpenAI
llm = ChatOpenAI()Method 1. Create a PromptTemplate object using the from_template() method
Variable to be substituted { variable } Define the template by tying it together.
Copy
Copy
country You can create sentences by putting values in variables.
Copy
Copy
Copy
Copy
Copy
Method 2. Create PromptTemplate objects simultaneously with prompt
For further validation input_variables Please specify
These variables cause exceptions when inconsistent compared to the variables in the template string during instantiation.
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Copy
partial_variables : Fill partial variables
partial The general use of is to partially use the function. This use case Variables you always want to bring in a common way This is the case.
Representative example Date or time is.
Let's say you always have a prompt that you want the current date to be displayed. You can't even hardcode the prompt, and passing it along with other input variables is cumbersome. In this case always present Function to return date It is very convenient if you can partially change the prompt using.
The following code is the Python code that saves the date today.
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Copy
Reading template from file
Copy
Copy
If any of the Window users have an error, run with the code below (encoding settings)
Copy
Copy
Copy
Copy
Copy
ChatPromptTemplate
ChatPromptTemplate You can take advantage of it when you want to inject a silver chat list as a prompt.
The message is organized in tuple format, ( role , message ) Can be configured to create a list.
role - "system" : System setting message. This is mainly a prompt related to global settings. - "human" : User input message. - "ai" : AI's answer message.
Copy
Copy
Copy
Copy
Copy
Copy
You can get the result by injecting the message you generated right away.
Copy
Copy
This time, let's create a chain.
Copy
Copy
Copy
MessagePlaceholder
LangChain also has full control over the messages to be rendered during formatting MessagePlaceholder Gives.
It may be useful if you are not sure which role to use for the message prompt template, or if you want to insert a list of messages during formatting.
Copy
Copy
conversation When I want to add a dialog later MessagesPlaceholder You can use
Copy
Copy
Copy
Copy
Copy
Last updated