forked from Archives/langchain
Harrison/prompt constructor methods (#4616)
This commit is contained in:
parent
6265cbfb11
commit
daa3e6dedb
@ -36,6 +36,15 @@ This is where output parsers come in.
|
||||
Output Parsers are responsible for (1) instructing the model how output should be formatted,
|
||||
(2) parsing output into the desired formatting (including retrying if necessary).
|
||||
|
||||
Getting Started
|
||||
---------------
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
|
||||
./prompts/getting_started.ipynb
|
||||
|
||||
|
||||
|
||||
Go Deeper
|
||||
---------
|
||||
|
218
docs/modules/prompts/getting_started.ipynb
Normal file
218
docs/modules/prompts/getting_started.ipynb
Normal file
@ -0,0 +1,218 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3651e424",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Getting Started\n",
|
||||
"\n",
|
||||
"This section contains everything related to prompts. A prompt is the value passed into the Language Model. This value can either be a string (for LLMs) or a list of messages (for Chat Models).\n",
|
||||
"\n",
|
||||
"The data types of these prompts are rather simple, but their construction is anything but. Value props of LangChain here include:\n",
|
||||
"\n",
|
||||
"- A standard interface for string prompts and message prompts\n",
|
||||
"- A standard (to get started) interface for string prompt templates and message prompt templates\n",
|
||||
"- Example Selectors: methods for inserting examples into the prompt for the language model to follow\n",
|
||||
"- OutputParsers: methods for inserting instructions into the prompt as the format in which the language model should output information, as well as methods for then parsing that string output into a format.\n",
|
||||
"\n",
|
||||
"We have in depth documentation for specific types of string prompts, specific types of chat prompts, example selectors, and output parsers.\n",
|
||||
"\n",
|
||||
"Here, we cover a quick-start for a standard interface for getting started with simple prompts."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ff34414d",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## PromptTemplates\n",
|
||||
"\n",
|
||||
"PromptTemplates are responsible for constructing a prompt value. These PromptTemplates can do things like formatting, example selection, and more. At a high level, these are basically objects that expose a `format_prompt` method for constructing a prompt. Under the hood, ANYTHING can happen."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"id": "7ce42639",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.prompts import PromptTemplate, ChatPromptTemplate"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"id": "5a178697",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"string_prompt = PromptTemplate.from_template(\"tell me a joke about {subject}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "f4ef6d6b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"chat_prompt = ChatPromptTemplate.from_template(\"tell me a joke about {subject}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"id": "5f16c8f1",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"string_prompt_value = string_prompt.format_prompt(subject=\"soccer\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"id": "863755ea",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"chat_prompt_value = chat_prompt.format_prompt(subject=\"soccer\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "8b3d8511",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## `to_string`\n",
|
||||
"\n",
|
||||
"This is what is called when passing to an LLM (which expects raw text)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"id": "1964a8a0",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'tell me a joke about soccer'"
|
||||
]
|
||||
},
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"string_prompt_value.to_string()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"id": "bf6c94e9",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'Human: tell me a joke about soccer'"
|
||||
]
|
||||
},
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"chat_prompt_value.to_string()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c0825af8",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## `to_messages`\n",
|
||||
"\n",
|
||||
"This is what is called when passing to ChatModel (which expects a list of messages)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"id": "e4da46f3",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[HumanMessage(content='tell me a joke about soccer', additional_kwargs={}, example=False)]"
|
||||
]
|
||||
},
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"string_prompt_value.to_messages()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"id": "eae84b88",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[HumanMessage(content='tell me a joke about soccer', additional_kwargs={}, example=False)]"
|
||||
]
|
||||
},
|
||||
"execution_count": 21,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"chat_prompt_value.to_messages()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a34fa440",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.1"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
@ -143,13 +143,19 @@ class ChatPromptTemplate(BaseChatPromptTemplate, ABC):
|
||||
input_variables: List[str]
|
||||
messages: List[Union[BaseMessagePromptTemplate, BaseMessage]]
|
||||
|
||||
@classmethod
|
||||
def from_template(cls, template: str, **kwargs: Any) -> ChatPromptTemplate:
|
||||
prompt_template = PromptTemplate.from_template(template, **kwargs)
|
||||
message = HumanMessagePromptTemplate(prompt=prompt_template)
|
||||
return cls.from_messages([message])
|
||||
|
||||
@classmethod
|
||||
def from_role_strings(
|
||||
cls, string_messages: List[Tuple[str, str]]
|
||||
) -> ChatPromptTemplate:
|
||||
messages = [
|
||||
ChatMessagePromptTemplate(
|
||||
content=PromptTemplate.from_template(template), role=role
|
||||
prompt=PromptTemplate.from_template(template), role=role
|
||||
)
|
||||
for role, template in string_messages
|
||||
]
|
||||
@ -160,7 +166,7 @@ class ChatPromptTemplate(BaseChatPromptTemplate, ABC):
|
||||
cls, string_messages: List[Tuple[Type[BaseMessagePromptTemplate], str]]
|
||||
) -> ChatPromptTemplate:
|
||||
messages = [
|
||||
role(content=PromptTemplate.from_template(template))
|
||||
role(prompt=PromptTemplate.from_template(template))
|
||||
for role, template in string_messages
|
||||
]
|
||||
return cls.from_messages(messages)
|
||||
|
Loading…
Reference in New Issue
Block a user