liveBook · Manning

LangChain

As explained in Natural Language Processing in Action, Second Edition

LangChain is a software framework for developing applications that combine language models with tools, data sources, and orchestration logic.

A chatbot may need to draw on an LLM without sending users to a separate chat interface. LangChain is a library for connecting an LLM to an application's prompts and conversation logic. Unlike an intent-based chatbot framework such as Rasa, it abstracts the model API so developers can experiment with different models and approaches. section 12.5.1 section 12.5.2

An LLM can adapt its replies to varied questions, but fluent replies are not necessarily safe or correct. A math tutor that speaks with children, for example, must distinguish students from parents and seek parental permission before continuing. section 12.5.2

Start with a prompted model

Configure a hosted model through LangChain, then put the user's message into a prompt template and pass both to an LLMChain: from langchain.prompts import PromptTemplate from langchain.chains import LLMChain prompt = PromptTemplate( input_variables=["message"], template="Human says: {message}\nChatbot responds:" ) chain = LLMChain(llm=llm, prompt=prompt) reply = chain.predict(message="Hi Bot! My name is Maria.") Here, llm must already be configured. The example configuration uses a Replicate API token and a hosted Llama 2 chat model. This first chain accepts a message but does not, by itself, give the bot the preceding conversation. section 12.5.2

>>> from langchain.llms import Replicate
>>> os.environ["REPLICATE_API_TOKEN"] = '<your_API_key_here>'

>>> llm = Replicate(
...     model="a16z-infra/llama13b-v2-chat:" +
...     "df7690",                     #1
...     input={
...         "temperature": 0.5,
...         "max_length": 100,
...         "top_p": 1,
...     })

Inspect the model configuration, including the API token, hosted model identifier, and generation settings. section 12.5.2

For a multi-turn bot, add conversation history to the prompt and use ConversationBufferMemory with a ConversationChain. A tutor prompt can specify the onboarding steps: ask whether the user is a parent or student, request a parent's explicit permission, and keep the next reply short. Memory supplies prior turns; the prompt describes the desired behavior. section 12.5.2

>>> class MathConversation():
...     def __init__(self, llm, prompt_string):
...         self.llm = llm
...         self.memory = \
...             ConversationBufferMemory(
...                 memory_key='history',
...                 ai_prefix='tutor',
...                 human_prefix="user")
...         self.convo_chain = ConversationChain(
...             llm=llm, memory=self.memory)
...         self.convo_chain.prompt = PromptTemplate(
...             input_variables=["history", "input"],
...             template=prompt_string)
...
...     def answer(self, user_input):
...         return self.convo_chain.predict(input=user_input)

Inspect a small Python wrapper that joins a conversation chain, buffer memory, and a prompt with matching history and input variables. section 12.5.2

Test the replies rather than treating prompt instructions as guarantees. In a number-sequence quiz, the tutor praises an answer of 12 as correct while also saying that 10 comes next. That contradiction makes answer-checking especially important in a teaching application. section 12.5.2

>>> math_convo.answer("12")
"Great job! Your answer is correct. The next number
in the sequence would be 10.\n\nLet's move on to the
next question. Can you guess the next number in the
sequence: 38, 39, 40, 41?"

See the contradictory quiz response that exposes the limit of relying on fluent model output for math feedback. section 12.5.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Works through model configuration, prompt chains, conversation memory, and observed replies; does not establish a production-ready deployment procedure.
Examples
Uses a parent-permission onboarding chatbot and a number-sequence quiz whose feedback contradicts itself.
What it assumes
Assumes familiarity with Python examples and API credentials while showing how prompts, chains, and memory fit together.
What you can do after
Build it from scratch with LangChain to prototype a prompted, history-aware chatbot and inspect its unreliable replies.
How it connects
Links large language models, prompt templates, conversation memory, chatbot frameworks, and API-hosted models.

Also explained in

Compare how 19 books explain LangChain →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.