LangChain Decorators ✨
Disclaimer: `LangChain decorators` is not created by the LangChain team and is not supported by it.
LangChain decorators
is a layer on the top of LangChain that provides syntactic sugar 🍭 for writing custom langchain prompts and chainsFor 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 ✨
@llm_prompt
def write_me_short_post(topic:str, platform:str="twitter", audience:str = "developers")->str:
"""
Write me a short header for my post about {topic} for {platform} platform.
It should be for {audience} audience.
(Max 15 words)
"""
return
# run it naturally
write_me_short_post(topic="starwars")
# or
write_me_short_post(topic="starwars", platform="redit")
Quick start
Installation
pip install langchain_decorators
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:
- Using Global settings:
# define global settings for all prompty (if not set - chatGPT is the current default)
from langchain_decorators import GlobalSettings
GlobalSettings.define_settings(
default_llm=ChatOpenAI(temperature=0.0), this is default... can change it here globally
default_streaming_llm=ChatOpenAI(temperature=0.0,streaming=True), this is default... can change it here for all ... will be used for streaming
)
- Using predefined prompt types
#You can change the default prompt types
from langchain_decorators import PromptTypes, PromptTypeSettings
PromptTypes.AGENT_REASONING.llm = ChatOpenAI()
# Or you can just define your own ones:
class MyCustomPromptTypes(PromptTypes):
GPT4=PromptTypeSettings(llm=ChatOpenAI(model="gpt-4"))
@llm_prompt(prompt_type=MyCustomPromptTypes.GPT4)
def write_a_complicated_code(app_idea:str)->str:
...
- Define the settings directly in the decorator
from langchain_openai import OpenAI
@llm_prompt(
llm=OpenAI(temperature=0.7),
stop_tokens=["\nObservation"],
...
)
def creative_writer(book_title:str)->str:
...
API Reference:OpenAI