$git clone https://github.com/ju-bezdek/langchain-decoratorsLangChain Decorators is a lightweight layer built on top of LangChain that provides syntactic sugar 🍭 for writing custom prompts and chains.
| 1 | # LangChain Decorators ✨ |
| 2 | |
| 3 | LangChain Decorators is a lightweight layer built on top of LangChain that provides syntactic sugar 🍭 for writing custom prompts and chains. |
| 4 | |
| 5 | > Note: This is an unofficial add-on to the LangChain library. It is not trying to compete—just to make using it easier. Many ideas here are opinionated. |
| 6 | |
| 7 | Here is a simple example using LangChain Decorators ✨: |
| 8 | |
| 9 | ```python |
| 10 | @llm_prompt |
| 11 | def write_me_short_post(topic: str, platform: str = "twitter", audience: str = "developers") -> str: |
| 12 | """ |
| 13 | Write a short header for my post about {topic} for the {platform} platform. |
| 14 | It should be for a {audience} audience. |
| 15 | (Max 15 words) |
| 16 | """ |
| 17 | return |
| 18 | |
| 19 | # Run it naturally |
| 20 | write_me_short_post(topic="starwars") |
| 21 | # or |
| 22 | write_me_short_post(topic="starwars", platform="reddit") |
| 23 | ``` |
| 24 | |
| 25 | Main principles and benefits: |
| 26 | - A more Pythonic way to write prompts |
| 27 | - Write multi-line prompts without breaking your code’s indentation |
| 28 | - Leverage IDE support for hints, type checking, and doc popups to quickly see prompts and parameters |
| 29 | - Keep the power of the 🦜🔗 LangChain ecosystem |
| 30 | - Add support for optional parameters |
| 31 | - Easily share parameters between prompts by binding them to a class |
| 32 | |
| 33 | [Quick start](#quick-start) |
| 34 | |
| 35 | - [Installation](#installation) |
| 36 | - [Examples](#examples) |
| 37 | |
| 38 | [Prompt declarations](#prompt-declarations) |
| 39 | |
| 40 | - [Documenting your prompt](#documenting-your-prompt) |
| 41 | - [Chat messages in prompt](#chat-messages-in-prompt) |
| 42 | - [Optional sections](#optional-sections) |
| 43 | - [Output parsers](#output-parsers) |
| 44 | |
| 45 | [Chat sessions / threads](#chat-sessions--threads) |
| 46 | |
| 47 | [Tool calling](#tool-calling) |
| 48 | |
| 49 | - [Enum arguments](#enum-arguments) |
| 50 | |
| 51 | [Simplified streaming](#simplified-streaming) |
| 52 | |
| 53 | [Structured output](#structured-output) |
| 54 | |
| 55 | [Binding the prompt to an object](#prompts-as-object-methods) |
| 56 | |
| 57 | [Defining custom settings](#defining-custom-settings) |
| 58 | |
| 59 | [Debugging](#debugging) |
| 60 | |
| 61 | ## Quick start |
| 62 | |
| 63 | ### Installation |
| 64 | |
| 65 | ```bash |
| 66 | pip install langchain_decorators |
| 67 | ``` |
| 68 | |
| 69 | ### Examples |
| 70 | |
| 71 | A good way to start is to review the examples here: |
| 72 | |
| 73 | - [Jupyter notebook](example_notebook.ipynb) |
| 74 | |
| 75 | ## Prompt declarations |
| 76 | |
| 77 | Define a prompt by creating a function with arguments as inputs and the function docstring as the prompt template: |
| 78 | |
| 79 | ```python |
| 80 | @llm_prompt |
| 81 | def write_me_short_post(topic: str, platform: str = "twitter", audience: str = "developers"): |
| 82 | """ |
| 83 | Write a short header for my post about {topic} for the {platform} platform. |
| 84 | It should be for a {audience} audience. |
| 85 | (Max 15 words) |
| 86 | """ |
| 87 | ``` |
| 88 | |
| 89 | This default declaration is translated into a chat with a single user message. |
| 90 | |
| 91 | If you want to define a prompt with multiple messages (common for chat models), add special code blocks inside the function docstring: |
| 92 | |
| 93 | ```python |
| 94 | @llm_prompt |
| 95 | def write_me_short_post(topic: str, platform: str = "twitter", audience: str = "developers"): |
| 96 | """ |
| 97 | ```<prompt:system> |
| 98 | You are a social media manager. |
| 99 | ``` |
| 100 | ```<prompt:user> |
| 101 | Write a short header for my post about {topic} for the {platform} platform. |
| 102 | It should be for a {audience} audience. |
| 103 | (Max 15 words) |
| 104 | ``` |
| 105 | ```<prompt:assistant> |
| 106 | I need to think about it. |
| 107 | ``` |
| 108 | """ |
| 109 | ``` |
| 110 | |
| 111 | The pattern is a series of consecutive code blocks with a “language” tag in this format: `<prompt:[message-role]>`. |
| 112 | |
| 113 | ### Documenting your prompt |
| 114 | |
| 115 | You can specify which part of your docstring is the prompt by using a code block with the `<prompt>` tag: |
| 116 | |
| 117 | ```python |
| 118 | @llm_prompt |
| 119 | def write_me_short_post(topic: str, platform: str = "twitter", audience: str = "developers"): |
| 120 | """ |
| 121 | Here is a good way to write a prompt as part of a function docstring, with additional documentation for developers. |
| 122 | |
| 123 | It needs to be a code block marked with `<prompt>`. |
| 124 | ```<prompt:user> |
| 125 | Write a short header for my post about {topic} for the {platform} platform. |
| 126 | It should be for a {audience} audience. |
| 127 | (Max 15 words) |
| 128 | ``` |
| 129 | |
| 130 | Only the code block above will be used as a prompt; the rest of the docstring is documentation for developers. |
| 131 | It also has a nice ben |