├── .gitignore ├── .env.example ├── README.md └── LangChain_Awesome_Prompt_Templates.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | .env -------------------------------------------------------------------------------- /.env.example: -------------------------------------------------------------------------------- 1 | OPENAI_API_KEY=sk-ABCDEF1234567890 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LangChain Awesome Prompt Templates 2 | -------------------------------------------------------------------------------- /LangChain_Awesome_Prompt_Templates.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "view-in-github" 8 | }, 9 | "source": [ 10 | "\"Open" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 15, 16 | "metadata": { 17 | "id": "Od0lHgdl0TIK" 18 | }, 19 | "outputs": [ 20 | { 21 | "name": "stdout", 22 | "output_type": "stream", 23 | "text": [ 24 | "Requirement already satisfied: openai in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (0.27.2)\n", 25 | "Requirement already satisfied: requests>=2.20 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from openai) (2.28.2)\n", 26 | "Requirement already satisfied: aiohttp in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from openai) (3.8.4)\n", 27 | "Requirement already satisfied: tqdm in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from openai) (4.65.0)\n", 28 | "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from requests>=2.20->openai) (1.26.15)\n", 29 | "Requirement already satisfied: idna<4,>=2.5 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from requests>=2.20->openai) (3.4)\n", 30 | "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from requests>=2.20->openai) (3.1.0)\n", 31 | "Requirement already satisfied: certifi>=2017.4.17 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from requests>=2.20->openai) (2022.12.7)\n", 32 | "Requirement already satisfied: aiosignal>=1.1.2 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from aiohttp->openai) (1.3.1)\n", 33 | "Requirement already satisfied: attrs>=17.3.0 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from aiohttp->openai) (22.2.0)\n", 34 | "Requirement already satisfied: frozenlist>=1.1.1 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from aiohttp->openai) (1.3.3)\n", 35 | "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from aiohttp->openai) (4.0.2)\n", 36 | "Requirement already satisfied: yarl<2.0,>=1.0 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from aiohttp->openai) (1.8.2)\n", 37 | "Requirement already satisfied: multidict<7.0,>=4.5 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from aiohttp->openai) (6.0.4)\n", 38 | "Note: you may need to restart the kernel to use updated packages.\n", 39 | "Requirement already satisfied: langchain in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (0.0.139)\n", 40 | "Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from langchain) (4.0.2)\n", 41 | "Requirement already satisfied: numpy<2,>=1 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from langchain) (1.23.5)\n", 42 | "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from langchain) (8.2.2)\n", 43 | "Requirement already satisfied: gptcache>=0.1.7 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from langchain) (0.1.10)\n", 44 | "Requirement already satisfied: pydantic<2,>=1 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from langchain) (1.10.7)\n", 45 | "Requirement already satisfied: openapi-schema-pydantic<2.0,>=1.2 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from langchain) (1.2.4)\n", 46 | "Requirement already satisfied: SQLAlchemy<2,>=1 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from langchain) (1.4.47)\n", 47 | "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from langchain) (3.8.4)\n", 48 | "Requirement already satisfied: dataclasses-json<0.6.0,>=0.5.7 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from langchain) (0.5.7)\n", 49 | "Requirement already satisfied: requests<3,>=2 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from langchain) (2.28.2)\n", 50 | "Requirement already satisfied: PyYAML>=5.4.1 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from langchain) (5.4.1)\n", 51 | "Requirement already satisfied: yarl<2.0,>=1.0 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.8.2)\n", 52 | "Requirement already satisfied: multidict<7.0,>=4.5 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.4)\n", 53 | "Requirement already satisfied: attrs>=17.3.0 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (22.2.0)\n", 54 | "Requirement already satisfied: charset-normalizer<4.0,>=2.0 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (3.1.0)\n", 55 | "Requirement already satisfied: aiosignal>=1.1.2 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)\n", 56 | "Requirement already satisfied: frozenlist>=1.1.1 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.3)\n", 57 | "Requirement already satisfied: marshmallow<4.0.0,>=3.3.0 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (3.19.0)\n", 58 | "Requirement already satisfied: marshmallow-enum<2.0.0,>=1.5.1 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (1.5.1)\n", 59 | "Requirement already satisfied: typing-inspect>=0.4.0 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (0.8.0)\n", 60 | "Requirement already satisfied: openai in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from gptcache>=0.1.7->langchain) (0.27.2)\n", 61 | "Requirement already satisfied: cachetools in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from gptcache>=0.1.7->langchain) (5.3.0)\n", 62 | "Requirement already satisfied: typing-extensions>=4.2.0 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from pydantic<2,>=1->langchain) (4.5.0)\n", 63 | "Requirement already satisfied: certifi>=2017.4.17 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from requests<3,>=2->langchain) (2022.12.7)\n", 64 | "Requirement already satisfied: idna<4,>=2.5 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from requests<3,>=2->langchain) (3.4)\n", 65 | "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from requests<3,>=2->langchain) (1.26.15)\n", 66 | "Requirement already satisfied: greenlet!=0.4.17 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from SQLAlchemy<2,>=1->langchain) (2.0.2)\n", 67 | "Requirement already satisfied: packaging>=17.0 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from marshmallow<4.0.0,>=3.3.0->dataclasses-json<0.6.0,>=0.5.7->langchain) (23.0)\n", 68 | "Requirement already satisfied: mypy-extensions>=0.3.0 in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from typing-inspect>=0.4.0->dataclasses-json<0.6.0,>=0.5.7->langchain) (1.0.0)\n", 69 | "Requirement already satisfied: tqdm in /Users/wyang14/.pyenv/versions/3.9.16/lib/python3.9/site-packages (from openai->gptcache>=0.1.7->langchain) (4.65.0)\n", 70 | "Note: you may need to restart the kernel to use updated packages.\n" 71 | ] 72 | } 73 | ], 74 | "source": [ 75 | "%pip install openai\n", 76 | "%pip install langchain" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 16, 82 | "metadata": { 83 | "id": "hs6f_anu1UT9" 84 | }, 85 | "outputs": [ 86 | { 87 | "data": { 88 | "text/plain": [ 89 | "'请给我讲个笑话'" 90 | ] 91 | }, 92 | "execution_count": 16, 93 | "metadata": {}, 94 | "output_type": "execute_result" 95 | } 96 | ], 97 | "source": [ 98 | "from langchain import PromptTemplate\n", 99 | "\n", 100 | "no_input_prompt = PromptTemplate(input_variables=[], template=\"请给我讲个笑话\")\n", 101 | "no_input_prompt.format()" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 17, 107 | "metadata": { 108 | "id": "HSnp2k4N1cm5" 109 | }, 110 | "outputs": [ 111 | { 112 | "data": { 113 | "text/plain": [ 114 | "'请给我讲个有趣的笑话.'" 115 | ] 116 | }, 117 | "execution_count": 17, 118 | "metadata": {}, 119 | "output_type": "execute_result" 120 | } 121 | ], 122 | "source": [ 123 | "one_input_prompt = PromptTemplate(input_variables=[\"adjective\"], template=\"请给我讲个{adjective}的笑话.\")\n", 124 | "one_input_prompt.format(adjective=\"有趣\")" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 18, 130 | "metadata": { 131 | "id": "Z-VhLnyG2Ga-" 132 | }, 133 | "outputs": [ 134 | { 135 | "data": { 136 | "text/plain": [ 137 | "'请给我讲个关于猪八戒的有趣的笑话.'" 138 | ] 139 | }, 140 | "execution_count": 18, 141 | "metadata": {}, 142 | "output_type": "execute_result" 143 | } 144 | ], 145 | "source": [ 146 | "multiple_input_prompt = PromptTemplate(\n", 147 | " input_variables=[\"adjective\", \"content\"], \n", 148 | " template=\"请给我讲个关于{content}的{adjective}的笑话.\"\n", 149 | ")\n", 150 | "multiple_input_prompt.format(adjective=\"有趣\", content=\"猪八戒\")" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 19, 156 | "metadata": { 157 | "id": "JKddTSPG2tmn" 158 | }, 159 | "outputs": [], 160 | "source": [ 161 | "import os\n", 162 | "OPENAI_API_KEY=os.environ[\"OPENAI_API_KEY\"]" 163 | ] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "execution_count": 20, 168 | "metadata": { 169 | "id": "ufGsJC5-2nVV" 170 | }, 171 | "outputs": [ 172 | { 173 | "name": "stdout", 174 | "output_type": "stream", 175 | "text": [ 176 | "\n", 177 | "\n", 178 | "猪八戒去买衣服,看到一件灰色的衣服,他问店员:“这件衣服是什么颜色?”店员说:“这是灰色的。”猪八戒说:“那我就买了,这样我就可以把它穿在任何地方,都不会被人发现。”\n" 179 | ] 180 | } 181 | ], 182 | "source": [ 183 | "from langchain.llms import OpenAI\n", 184 | "\n", 185 | "openai = OpenAI(temperature=0, openai_api_key=OPENAI_API_KEY)\n", 186 | "\n", 187 | "print(openai(multiple_input_prompt.format(adjective=\"灰色幽默\", content=\"猪八戒\")))" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 21, 193 | "metadata": { 194 | "id": "9vje4Xu2lTrK" 195 | }, 196 | "outputs": [ 197 | { 198 | "name": "stdout", 199 | "output_type": "stream", 200 | "text": [ 201 | " New York is located in the Northeastern United States, on the coast of the Atlantic Ocean.\n" 202 | ] 203 | } 204 | ], 205 | "source": [ 206 | "prompt = \"\"\"The following are exerpts from conversations with an AI\n", 207 | "assistant. The assistant is speaking like a famous movie character.\n", 208 | "\n", 209 | "User: Where is New York?\n", 210 | "AI: \"\"\"\n", 211 | "\n", 212 | "print(openai(prompt))" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 22, 218 | "metadata": { 219 | "id": "g7r0UyAwfwes" 220 | }, 221 | "outputs": [], 222 | "source": [ 223 | "examples = [\n", 224 | " {\n", 225 | " \"query\": \"What sports do you play?\",\n", 226 | " \"answer\": \"Soccer, I play.\"\n", 227 | " }, {\n", 228 | " \"query\": \"Where is this?\",\n", 229 | " \"answer\": \"My home, this is.\"\n", 230 | " }, {\n", 231 | " \"query\": \"Are you leaving now?\",\n", 232 | " \"answer\": \"Stay and help you, I will.\"\n", 233 | " }, {\n", 234 | " \"query\": \"How are you feeling?\",\n", 235 | " \"answer\": \"Great I feel.\"\n", 236 | " }\n", 237 | "]" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": 23, 243 | "metadata": { 244 | "id": "BbdUw-tZgIOJ" 245 | }, 246 | "outputs": [ 247 | { 248 | "name": "stdout", 249 | "output_type": "stream", 250 | "text": [ 251 | "The following are exerpts from conversations with an AI\n", 252 | "assistant. The assistant is speaking like a famous movie character.\n", 253 | "\n", 254 | "Here are some examples:\n", 255 | "\n", 256 | "\n", 257 | "\n", 258 | "Human: What sports do you play?\n", 259 | "AI: Soccer, I play.\n", 260 | "\n", 261 | "\n", 262 | "\n", 263 | "Human: Where is this?\n", 264 | "AI: My home, this is.\n", 265 | "\n", 266 | "\n", 267 | "\n", 268 | "Human: Are you leaving now?\n", 269 | "AI: Stay and help you, I will.\n", 270 | "\n", 271 | "\n", 272 | "\n", 273 | "Human: How are you feeling?\n", 274 | "AI: Great I feel.\n", 275 | "\n", 276 | "\n", 277 | "\n", 278 | "Human: Where is New York?\n", 279 | "AI: \n" 280 | ] 281 | } 282 | ], 283 | "source": [ 284 | "example_template = \"\"\"\n", 285 | "Human: {query}\n", 286 | "AI: {answer}\n", 287 | "\"\"\"\n", 288 | "\n", 289 | "example_prompt = PromptTemplate(\n", 290 | " input_variables=[\"query\", \"answer\"],\n", 291 | " template=example_template\n", 292 | ")\n", 293 | "\n", 294 | "prefix = \"\"\"The following are exerpts from conversations with an AI\n", 295 | "assistant. The assistant is speaking like a famous movie character.\n", 296 | "\n", 297 | "Here are some examples:\n", 298 | "\"\"\"\n", 299 | "\n", 300 | "suffix = \"\"\"\n", 301 | "Human: {query}\n", 302 | "AI: \"\"\"\n", 303 | "\n", 304 | "from langchain import FewShotPromptTemplate\n", 305 | "few_shot_prompt_template = FewShotPromptTemplate(\n", 306 | " examples=examples,\n", 307 | " example_prompt=example_prompt,\n", 308 | " prefix=prefix,\n", 309 | " suffix=suffix,\n", 310 | " input_variables=[\"query\"],\n", 311 | " example_separator=\"\\n\\n\"\n", 312 | ")\n", 313 | "\n", 314 | "print(few_shot_prompt_template.format(query=\"Where is New York?\"))" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 24, 320 | "metadata": { 321 | "id": "O39-6MO1mjnV" 322 | }, 323 | "outputs": [ 324 | { 325 | "name": "stdout", 326 | "output_type": "stream", 327 | "text": [ 328 | " Far away, New York is.\n" 329 | ] 330 | } 331 | ], 332 | "source": [ 333 | "openai = OpenAI(temperature=0, openai_api_key=OPENAI_API_KEY)\n", 334 | "print(openai(few_shot_prompt_template.format(query=\"Where is New York?\")))" 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "execution_count": 27, 340 | "metadata": { 341 | "id": "0bXN6SU_mwaY" 342 | }, 343 | "outputs": [], 344 | "source": [ 345 | "from langchain.prompts.example_selector import LengthBasedExampleSelector\n", 346 | "\n", 347 | "example_template = \"\"\"\n", 348 | "Human: {query}\n", 349 | "AI: {answer}\n", 350 | "\"\"\"\n", 351 | "\n", 352 | "example_prompt = PromptTemplate(\n", 353 | " input_variables=[\"query\", \"answer\"],\n", 354 | " template=example_template\n", 355 | ")\n", 356 | "\n", 357 | "example_selector = LengthBasedExampleSelector(\n", 358 | " examples=examples,\n", 359 | " example_prompt=example_prompt,\n", 360 | " max_length=30\n", 361 | ")" 362 | ] 363 | }, 364 | { 365 | "cell_type": "code", 366 | "execution_count": 28, 367 | "metadata": { 368 | "id": "ZeqJ1UYYnCdd" 369 | }, 370 | "outputs": [ 371 | { 372 | "name": "stdout", 373 | "output_type": "stream", 374 | "text": [ 375 | "The following are exerpts from conversations with an AI\n", 376 | "assistant. The assistant is speaking like a famous movie character.\n", 377 | "\n", 378 | "Here are some examples:\n", 379 | "\n", 380 | "\n", 381 | "Human: What sports do you play?\n", 382 | "AI: Soccer, I play.\n", 383 | "\n", 384 | "\n", 385 | "Human: Where is this?\n", 386 | "AI: My home, this is.\n", 387 | "\n", 388 | "\n", 389 | "Human: Where is New York?\n", 390 | "AI: \n" 391 | ] 392 | } 393 | ], 394 | "source": [ 395 | "few_shot_prompt_template = FewShotPromptTemplate(\n", 396 | " example_selector=example_selector,\n", 397 | " example_prompt=example_prompt,\n", 398 | " prefix=prefix,\n", 399 | " suffix=suffix,\n", 400 | " input_variables=[\"query\"],\n", 401 | " example_separator=\"\\n\"\n", 402 | ")\n", 403 | "print(few_shot_prompt_template.format(query=\"Where is New York?\"))" 404 | ] 405 | } 406 | ], 407 | "metadata": { 408 | "colab": { 409 | "authorship_tag": "ABX9TyMH5D/e0j5eB5AHW7slj5aa", 410 | "include_colab_link": true, 411 | "provenance": [] 412 | }, 413 | "kernelspec": { 414 | "display_name": "Python 3", 415 | "name": "python3" 416 | }, 417 | "language_info": { 418 | "codemirror_mode": { 419 | "name": "ipython", 420 | "version": 3 421 | }, 422 | "file_extension": ".py", 423 | "mimetype": "text/x-python", 424 | "name": "python", 425 | "nbconvert_exporter": "python", 426 | "pygments_lexer": "ipython3", 427 | "version": "3.9.16" 428 | } 429 | }, 430 | "nbformat": 4, 431 | "nbformat_minor": 0 432 | } 433 | --------------------------------------------------------------------------------