Harrison/prompt template prefix (#888)

Co-authored-by: Gabriel Simmons <simmons.gabe@gmail.com>
This commit is contained in:
Harrison Chase
2023-02-06 19:09:28 -08:00
committed by GitHub
parent f95cedc443
commit e2b834e427
4 changed files with 253 additions and 3 deletions

View File

@@ -153,7 +153,7 @@
},
{
"cell_type": "markdown",
"id": "72f32ff2",
"id": "cc991ad2",
"metadata": {},
"source": [
"## From Template\n",
@@ -163,7 +163,7 @@
{
"cell_type": "code",
"execution_count": 2,
"id": "2a81f2f8",
"id": "d0a0756c",
"metadata": {},
"outputs": [],
"source": [
@@ -174,7 +174,7 @@
{
"cell_type": "code",
"execution_count": 3,
"id": "d365b144",
"id": "59046640",
"metadata": {},
"outputs": [
{
@@ -332,6 +332,69 @@
"print(prompt_from_string_examples.format(adjective=\"big\"))"
]
},
{
"cell_type": "markdown",
"id": "874b7575",
"metadata": {},
"source": [
"## Few Shot Prompts with Templates\n",
"We can also construct few shot prompt templates where the prefix and suffix themselves are prompt templates"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e710115f",
"metadata": {},
"outputs": [],
"source": [
"from langchain.prompts import FewShotPromptWithTemplates"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "5bf23a65",
"metadata": {},
"outputs": [],
"source": [
"prefix = PromptTemplate(input_variables=[\"content\"], template=\"This is a test about {content}.\")\n",
"suffix = PromptTemplate(input_variables=[\"new_content\"], template=\"Now you try to talk about {new_content}.\")\n",
"\n",
"prompt = FewShotPromptWithTemplates(\n",
" suffix=suffix,\n",
" prefix=prefix,\n",
" input_variables=[\"content\", \"new_content\"],\n",
" examples=examples,\n",
" example_prompt=example_prompt,\n",
" example_separator=\"\\n\",\n",
")\n",
"output = prompt.format(content=\"animals\", new_content=\"party\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "d4036351",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is a test about animals.\n",
"Input: happy\n",
"Output: sad\n",
"Input: tall\n",
"Output: short\n",
"Now you try to talk about party.\n"
]
}
],
"source": [
"print(output)"
]
},
{
"cell_type": "markdown",
"id": "bf038596",