What is the difference between chains and runnables in langchain?

345 views Asked by At

I am new to langchain and langserve. the langserve definition goes as "LangServe helps developers deploy LangChain runnables and chains as a REST API." But could not understand the basic differneces between the two.

from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
model = ChatOpenAI()
prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
chain = prompt | model

chain = prompt | model is this a chain or runnable. If it is a chain, then how a runnable is written and what is the need for a runnable?

1

There are 1 answers

0
Tavish Aggarwal On

We can create components of the chain using Runnable methods. For example, consider the code below:

vectorstore = DocArrayInMemorySearch.from_texts(
    ["harrison worked at kensho", "bears like to eat honey"],
    embedding=OpenAIEmbeddings()
)
retriever = vectorstore.as_retriever()

template = """Answer the question based only on the following context:
{context}

Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)

retrival = RunnableMap({
    "context": lambda x: retriever.get_relevant_documents(x["question"]),
    "question": lambda x: x["question"]
})

chain = retrival | prompt | model | output_parser

As you can see in the code above both runnable and chain are sued. However, I am defining one component of the chain as a runnable component. This is done because now I can pass two arguments to the chain and also now we can run these runnables in parallel way.

Therefore, runnable and chain cannot be compared but instead components of the chain can be created as a runnable.