Skip to main content
LangChain decorators is a layer on the top of LangChain that provides syntactic sugar 🍭 for writing custom langchain prompts and chains For Feedback, Issues, Contributions - please raise an issue here: ju-bezdek/langchain-decorators
Main principles and benefits:
  • more pythonic way of writing code
  • write multiline prompts that won’t break your code flow with indentation
  • making use of IDE in-built support for hinting, type checking and popup with docs to quickly peek in the function to see the prompt, parameters it consumes etc.
  • leverage all the power of 🦜🔗 LangChain ecosystem
  • adding support for optional parameters
  • easily share parameters between the prompts by binding them to one class
Here is a simple example of a code written with LangChain Decorators ✨

Quick start

Installation

Examples

Good idea on how to start is to review the examples here:

Defining other parameters

Here we are just marking a function as a prompt with llm_prompt decorator, turning it effectively into a LLMChain. Instead of running it Standard LLMchain takes much more init parameter than just inputs_variables and prompt… here is this implementation detail hidden in the decorator. Here is how it works:
  1. Using Global settings:
  1. Using predefined prompt types
  1. Define the settings directly in the decorator

Passing a memory and/or callbacks:

To pass any of these, just declare them in the function (or use kwargs to pass anything)

Simplified streaming

If we want to leverage streaming:
  • we need to define prompt as async function
  • turn on the streaming on the decorator, or we can define PromptType with streaming on
  • capture the stream using StreamingContext
This way we just mark which prompt should be streamed, not needing to tinker with what LLM should we use, passing around the creating and distribute streaming handler into particular part of our chain… just turn the streaming on/off on prompt/prompt type… The streaming will happen only if we call it in streaming context … there we can define a simple function to handle the stream

Prompt declarations

By default the prompt is the whole function docs, unless you mark your prompt

Documenting your prompt

We can specify what part of our docs is the prompt definition, by specifying a code block with <prompt> language tag
Now only to code block above will be used as a prompt, and the rest of the docstring will be used as a description for developers. (It has also a nice benefit that IDE (like VS code) will display the prompt properly (not trying to parse it as markdown, and thus not showing new lines properly)) """ return

human message

(we are using the real role that are enforced by the LLM - GPT supports system, assistant, user)
a reply:
we can also add some history using placeholder
Now only to code block above will be used as a prompt, and the rest of the docstring will be used as a description for developers. (It has also a nice benefit that IDE (like VS code) will display the prompt properly (not trying to parse it as markdown, and thus not showing new lines properly)) """ pass

Output parsers

  • llm_prompt decorator natively tries to detect the best output parser based on the output type. (if not set, it returns the raw string)
  • list, dict and pydantic outputs are also supported natively (automatically)

More complex structures

for dict / pydantic you need to specify the formatting instructions… this can be tedious, that’s why you can let the output parser generate you the instructions based on the model (pydantic)

Binding the prompt to an object

""" personality = AssistantPersonality(assistant_name=“John”, assistant_role=“a pirate”) print(personality.introduce_your_self(personality))