LangChain 1 cheatsheet
LangChain 1: Prompts and chains
3 common LangChain 1 tasks.
Create a prompt template
Build a reusable prompt with placeholders.
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages([
("system", "Translate the user's text into {language}."),
("human", "{text}"),
])
Chain a prompt, model and parser
Combine steps with the | operator, then call the chain like any other runnable.
from langchain_core.output_parsers import StrOutputParser
chain = prompt | model | StrOutputParser()
chain.invoke({"language": "French", "text": "Good morning"})
Get structured output
Make the model return data that matches a Pydantic class.
from pydantic import BaseModel
class Movie(BaseModel):
title: str
year: int
structured = model.with_structured_output(Movie)
structured.invoke("Tell me about Inception.")