Add custom prompt template example

pull/140/head
Kacper Łukawski 1 year ago
parent 98b639df22
commit fa9fded505

@ -41,8 +41,8 @@
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2023-02-16T17:06:25.477937Z",
"start_time": "2023-02-16T17:06:24.739247Z"
"end_time": "2023-03-03T16:17:00.430505Z",
"start_time": "2023-03-03T16:16:58.488129Z"
}
},
"outputs": [
@ -50,7 +50,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"qdrant_qdrant_1 is up-to-date\r\n"
"Starting qdrant_qdrant_1 ... \n",
"\u001B[1Bting qdrant_qdrant_1 ... \u001B[32mdone\u001B[0m"
]
}
],
@ -70,8 +71,8 @@
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2023-02-16T17:06:25.621326Z",
"start_time": "2023-02-16T17:06:25.483947Z"
"end_time": "2023-03-03T16:17:00.650341Z",
"start_time": "2023-03-03T16:17:00.438332Z"
}
},
"outputs": [
@ -101,8 +102,8 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2023-02-16T17:06:29.193187Z",
"start_time": "2023-02-16T17:06:25.624497Z"
"end_time": "2023-03-03T16:17:15.121014Z",
"start_time": "2023-03-03T16:17:00.653356Z"
},
"scrolled": true
},
@ -129,8 +130,8 @@
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2023-02-16T17:06:29.206718Z",
"start_time": "2023-02-16T17:06:29.202344Z"
"end_time": "2023-03-03T16:17:15.140434Z",
"start_time": "2023-03-03T16:17:15.130446Z"
}
},
"outputs": [
@ -170,8 +171,8 @@
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2023-02-16T17:06:30.144430Z",
"start_time": "2023-02-16T17:06:29.212729Z"
"end_time": "2023-03-03T16:17:16.184482Z",
"start_time": "2023-03-03T16:17:15.146386Z"
}
},
"outputs": [
@ -179,39 +180,37 @@
"name": "stdout",
"output_type": "stream",
"text": [
"--2023-02-16 18:06:29-- https://storage.googleapis.com/dataset-natural-questions/questions.json\n",
"Resolving storage.googleapis.com (storage.googleapis.com)... 142.250.203.208, 216.58.208.208, 142.250.75.16, ...\n",
"Connecting to storage.googleapis.com (storage.googleapis.com)|142.250.203.208|:443... connected.\n",
"HTTP request sent, awaiting response... 416 Requested range not satisfiable\n",
"\n",
" The file is already fully retrieved; nothing to do.\n",
"\n",
"--2023-02-16 18:06:29-- https://storage.googleapis.com/dataset-natural-questions/answers.json\n",
"Resolving storage.googleapis.com (storage.googleapis.com)... 216.58.208.208, 142.250.186.208, 216.58.215.112, ...\n",
"Connecting to storage.googleapis.com (storage.googleapis.com)|216.58.208.208|:443... connected.\n",
"HTTP request sent, awaiting response... 416 Requested range not satisfiable\n",
"\n",
" The file is already fully retrieved; nothing to do.\n",
"\n"
"100% [..............................................................................] 95372 / 95372"
]
},
{
"data": {
"text/plain": [
"'answers.json'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import wget\n",
"\n",
"# All the examples come from https://ai.google.com/research/NaturalQuestions\n",
"# This is a sample of the training set that we download and extract for some\n",
"# futher processing.\n",
"\n",
"!wget -c https://storage.googleapis.com/dataset-natural-questions/questions.json\n",
"!wget -c https://storage.googleapis.com/dataset-natural-questions/answers.json"
"wget.download(\"https://storage.googleapis.com/dataset-natural-questions/questions.json\")\n",
"wget.download(\"https://storage.googleapis.com/dataset-natural-questions/answers.json\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2023-02-16T17:09:31.352487Z",
"start_time": "2023-02-16T17:09:31.343483Z"
"end_time": "2023-03-03T16:17:16.202633Z",
"start_time": "2023-03-03T16:17:16.189718Z"
},
"code_folding": []
},
@ -228,11 +227,11 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2023-02-16T17:09:31.915856Z",
"start_time": "2023-02-16T17:09:31.908270Z"
"end_time": "2023-03-03T16:17:16.280960Z",
"start_time": "2023-03-03T16:17:16.209442Z"
}
},
"outputs": [
@ -250,11 +249,11 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2023-02-16T17:09:32.777080Z",
"start_time": "2023-02-16T17:09:32.768859Z"
"end_time": "2023-03-03T16:17:16.286628Z",
"start_time": "2023-03-03T16:17:16.283546Z"
}
},
"outputs": [
@ -281,11 +280,11 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2023-02-16T17:10:06.470388Z",
"start_time": "2023-02-16T17:09:34.000011Z"
"end_time": "2023-03-03T16:17:22.922735Z",
"start_time": "2023-03-03T16:17:16.288631Z"
}
},
"outputs": [],
@ -309,11 +308,11 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2023-02-16T17:10:06.482311Z",
"start_time": "2023-02-16T17:10:06.476817Z"
"end_time": "2023-03-03T16:17:22.941289Z",
"start_time": "2023-03-03T16:17:22.931412Z"
}
},
"outputs": [],
@ -340,11 +339,11 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2023-02-16T17:34:03.816845Z",
"start_time": "2023-02-16T17:34:03.808928Z"
"end_time": "2023-03-03T16:17:22.960403Z",
"start_time": "2023-03-03T16:17:22.946040Z"
}
},
"outputs": [],
@ -357,11 +356,11 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 12,
"metadata": {
"ExecuteTime": {
"end_time": "2023-02-16T17:34:31.439642Z",
"start_time": "2023-02-16T17:34:04.170150Z"
"end_time": "2023-03-03T16:17:42.330331Z",
"start_time": "2023-03-03T16:17:22.962450Z"
}
},
"outputs": [
@ -370,13 +369,13 @@
"output_type": "stream",
"text": [
"> where do frankenstein and the monster first meet\n",
" Victor retreats into the mountains and the Creature finds him and pleads for Victor to hear his tale, so they first meet in the mountains.\n",
" Victor and the Creature first meet in the mountains.\n",
"\n",
"> who are the actors in fast and furious\n",
" The actors in Fast and Furious are Vin Diesel as Dominic Toretto, Paul Walker as Brian O'Conner, Michelle Rodriguez as Letty Ortiz, Jordana Brewster as Mia Toretto, Tyrese Gibson as Roman Pearce, Ludacris as Tej Parker, Lucas Black as Sean Boswell, Sung Kang as Han Lue, Gal Gadot as Gisele Yashar, Dwayne Johnson as Luke Hobbs, Matt Schulze as Vince, Chad Lindberg as Jesse, Johnny Strong as Leon, Eva Mendes as Monica Fuentes, Devon Aoki as Suki, Nathalie Kelley as Neela, Bow Wow as Twinkie, Tego Calderón as Tego Leo, Don Omar as Rico Santos, Elsa Pataky as Elena Neves, and Kurt Russell as Mr. Nobody.\n",
" The actors in the Fast and Furious films are Vin Diesel, Paul Walker, Michelle Rodriguez, Jordana Brewster, Tyrese Gibson, Ludacris, Lucas Black, Sung Kang, Gal Gadot, Dwayne Johnson, Matt Schulze, Chad Lindberg, Johnny Strong, Eva Mendes, Devon Aoki, Nathalie Kelley, Bow Wow, Tego Calderón, Don Omar, Elsa Pataky, Kurt Russell, Nathalie Emmanuel, Scott Eastwood, Noel Gugliemi, Ja Rule, Thom Barry, Ted Levine, Minka Kelly, James Remar, Amaury Nolasco, Michael Ealy, MC Jin, Brian Goodman, Lynda Boyd, Jason Tobin, Neela, Liza Lapira, Alimi Ballard, Yorgo Constantine, Geoff Meed, Jeimy Osorio, Max William Crane, Charlie & Miller Kimsey, Eden Estrella, Romeo Santos, John Brotherton, Helen Mirren, Celestino Cornielle, Janmarco Santiago, Carlos De La Hoz, James Ayoub, Rick Yune, Cole Hauser, Brian Tee, John Ortiz, Luke Evans, Jason Statham, Charlize Theron, Reggie Lee, Mo Gallini, Roberto Sanchez, Leonardo\n",
"\n",
"> properties of red black tree in data structure\n",
" Red black trees have the following properties: each node is either red or black, the root is black, all leaves (NIL) are black, if a node is red, then both its children are black, and every path from a given node to any of its descendant NIL nodes contains the same number of black nodes.\n",
" Red black trees are a type of binary tree with a special set of properties. Each node is either red or black, the root is black, and if a node is red, then both its children are black. Every path from a given node to any of its descendant NIL nodes contains the same number of black nodes. The number of black nodes from the root to a node is the node's black depth, and the uniform number of black nodes in all paths from root to the leaves is called the black-height of the red-black tree.\n",
"\n",
"> who designed the national coat of arms of south africa\n",
" Iaan Bekker\n",
@ -394,11 +393,139 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"source": [
"### Custom prompt templates\n",
"\n",
"The `stuff` chain type in Langchain uses a specific prompt with question and context documents incorporated. This is what the default prompt looks like:\n",
"\n",
"```text\n",
"Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.\n",
"{context}\n",
"Question: {question}\n",
"Helpful Answer:\n",
"```\n",
"\n",
"We can, however, provide our prompt template and change the behaviour of the OpenAI LLM, while still using the `stuff` chain type. It is important to keep `{context}` and `{question}` as placeholders.\n",
"\n",
"#### Experimenting with custom prompts\n",
"\n",
"We can try using a different prompt template, so the model:\n",
"1. Responds with a single-sentence answer if it knows it.\n",
"2. Suggests a random song title if it doesn't know the answer to our question."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"ExecuteTime": {
"end_time": "2023-03-03T16:28:04.907231Z",
"start_time": "2023-03-03T16:28:04.898528Z"
}
},
"outputs": [],
"source": [
"from langchain.prompts import PromptTemplate"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"ExecuteTime": {
"end_time": "2023-03-03T16:45:25.130450Z",
"start_time": "2023-03-03T16:45:25.121744Z"
}
},
"outputs": [],
"source": [
"custom_prompt = \"\"\"\n",
"Use the following pieces of context to answer the question at the end. Please provide\n",
"a short single-sentence summary answer only. If you don't know the answer or if it's \n",
"not present in given context, don't try to make up an answer, but suggest me a random \n",
"unrelated song title I could listen to. \n",
"Context: {context}\n",
"Question: {question}\n",
"Helpful Answer:\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"ExecuteTime": {
"end_time": "2023-03-03T16:45:25.563255Z",
"start_time": "2023-03-03T16:45:25.559014Z"
}
},
"outputs": [],
"source": []
"source": [
"custom_prompt_template = PromptTemplate(\n",
" template=custom_prompt, input_variables=[\"context\", \"question\"]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"ExecuteTime": {
"end_time": "2023-03-03T16:45:25.850729Z",
"start_time": "2023-03-03T16:45:25.845721Z"
}
},
"outputs": [],
"source": [
"custom_qa = VectorDBQA.from_chain_type(\n",
" llm=llm, \n",
" chain_type=\"stuff\", \n",
" vectorstore=doc_store,\n",
" return_source_documents=False,\n",
" chain_type_kwargs={\"prompt\": custom_prompt_template},\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"ExecuteTime": {
"end_time": "2023-03-03T16:46:49.553646Z",
"start_time": "2023-03-03T16:46:40.710304Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"> what was uncle jesse's original last name on full house\n",
"Uncle Jesse's original last name on Full House was Cochran.\n",
"\n",
"> when did the volcano erupt in indonesia 2018\n",
"No volcanic eruption is mentioned in the given context. Suggested Song: \"Ring of Fire\" by Johnny Cash.\n",
"\n",
"> what does a dualist way of thinking mean\n",
"Dualist way of thinking means that the mind and body are separate entities, with the mind being a non-physical substance.\n",
"\n",
"> the first civil service commission in india was set up on the basis of recommendation of\n",
"The first Civil Service Commission in India was not set up on the basis of a recommendation.\n",
"\n",
"> how old do you have to be to get a tattoo in utah\n",
"In Utah, you must be at least 18 years old to get a tattoo.\n",
"\n"
]
}
],
"source": [
"random.seed(41)\n",
"for question in random.choices(questions, k=5):\n",
" print(\">\", question)\n",
" print(custom_qa.run(question), end=\"\\n\\n\")"
]
}
],
"metadata": {

Loading…
Cancel
Save