Immediate Templates are pre-defined recipes for producing prompts for language fashions. A template might embody directions, few-shot examples, and particular context and questions applicable for a given process.
Langchain offers tooling to create and work with immediate templates
Langchain strives to create a mannequin agnostic templates to make it straightforward to reuse present templates throughout completely different language fashions
Usually, language fashions count on the immediate to both be a string or else a listing of chat messages
PromptTemplate:
Use PromptTemplate to create a template for a string immediate.
The template helps any variety of variables, together with no variables
You may create customized immediate templates that format the immediate in any manner you need.
ChatPromptTemplate
The immediate to talk fashions is a listing of chat messages.
Every chat message is related to content material and a further parameter referred to as position.For instance , within the OpenAI Chat Completions API,a chat message may be related to an AI assistant, a human or a system position.
Create a chat immediate template like this:
Piping these formatted messages into Langchain’s ChatOpenAI chat mannequin class is roughly equal with utilizing the OpenAI consumer immediately as given within the code under:
The ChatPromptTemplate.from_messages static methodology accepts quite a lot of message representations and is a handy method to format enter to talk fashions with precisely the messages you need as given under:
This offers you with a variety of flexibility in the way you assemble your chat prompts
Message Prompts
Langchain offers several types of MessagePromptTemplate. Probably the most generally used are AIMessagePromptTemplate,SystemMessagePromptTemplate and HumanMessagePromptTemplate which creates an AI message,system message and human message respectively.
Incases the place the chat mannequin helps taking chat message with arbitrary position, you need to use the ChatMessagePromptTemplate, which permits the consumer to specify the position identify :
MessagesPlaceholder
Langchain additionally offers MessagesPlaceholder, which provides you full management of what messages to be rendered throughout formatting. This may be helpful when you find yourself unsure of what of what position try to be utilizing on your message immediate templates or whenever you want to insert a listing of messages throughout formatting.
LCEL
PromptTemplate and ChatPromptTemplate implement the Runnable interface, the fundamental constructing block of the Langchain Expression Language (LCEL). This implies they assist invoke, ainvoke,stream,astream,batch ,abatch,astream_log calls