| name | add-chain |
| description | Step-by-step procedure to create a new LangChain LCEL chain in a genai-tk project. |
Add an LCEL Chain
Follow these steps to add a new LangChain Expression Language (LCEL) chain to a genai-tk project.
Prerequisites
- The project was initialized with
cli init
- The project has a
<package>/chains/ directory
Step 1: Create the Chain Module
Create a new file in <package>/chains/my_chain.py:
"""My custom chain — describe what it does."""
from genai_tk.core.factories.llm_factory import get_llm
from genai_tk.core.prompts import def_prompt
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import Runnable
def get_chain(config: dict | None = None) -> Runnable:
"""Create the chain: prompt | llm | parser."""
llm = get_llm()
prompt = def_prompt(
system="You are an expert at [domain]. Be concise and accurate.",
user="{input}",
)
return prompt | llm | StrOutputParser()
Step 2: Test the Chain
Via CLI (using the core run command)
uv run cli core run "My Chain" "example input"
Via Webapp
Launch make webapp and navigate to the Runnable Playground page. The chain will appear in the dropdown.
Programmatically
from <package_name>.chains.my_chain import get_chain
chain = get_chain()
result = chain.invoke({"input": "test"})
print(result)
Common LCEL Patterns
Chain with Multiple Steps
from langchain_core.runnables import RunnablePassthrough
chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
Chain with Branching
from langchain_core.runnables import RunnableBranch
chain = RunnableBranch(
(lambda x: "math" in x["topic"], math_chain),
(lambda x: "code" in x["topic"], code_chain),
default_chain,
)
Chain with Structured Output
from pydantic import BaseModel
class Answer(BaseModel):
reasoning: str
answer: str
confidence: float
chain = prompt | llm.with_structured_output(Answer)
RAG Chain
from genai_tk.core.factories.llm_factory import get_llm
from genai_tk.core.factories.embeddings_factory import get_embeddings_store
store = get_embeddings_store()
retriever = store.as_retriever(search_kwargs={"k": 4})
rag_chain = (
{"context": retriever, "question": RunnablePassthrough()}
| def_prompt(
system="Answer based on the context provided.",
user="Context: {context}\n\nQuestion: {question}",
)
| get_llm()
| StrOutputParser()
)
Registration Options
register_runnable(
RunnableItem(
tag="Category",
name="Display Name",
runnable=get_chain,
examples=[
Example(query=["input 1"]),
Example(query=["input 2"]),
],
)
)