Immediate Templates are pre-defined recipes for producing prompts for language fashions. A template could embody directions, few-shot examples, and particular context and questions acceptable 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
Sometimes, 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 possibly can 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 function.For instance , within the OpenAI Chat Completions API,a chat message might be related to an AI assistant, a human or a system function.
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 shopper immediately as given within the code beneath:
The ChatPromptTemplate.from_messages static technique accepts quite a lot of message representations and is a handy option to format enter to talk fashions with precisely the messages you need as given beneath:
This offers you with a whole lot of flexibility in the way you assemble your chat prompts
Message Prompts
Langchain offers various kinds of MessagePromptTemplate. Essentially 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 function, you should utilize the ChatMessagePromptTemplate, which permits the consumer to specify the function title :
MessagesPlaceholder
Langchain additionally offers MessagesPlaceholder, which provides you full management of what messages to be rendered throughout formatting. This may be helpful if you find yourself unsure of what of what function try to be utilizing in your message immediate templates or once 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