liveBook · Manning

Hybrid chatbot

As explained in Natural Language Processing in Action, Second Edition

A hybrid chatbot combines generative language models with rules, templates, or other conventional conversational techniques.

A hybrid chatbot combines an LLM’s flexible conversation with techniques that constrain or ground its replies. Rules alone cannot anticipate every question a student might ask, while an unconstrained LLM can sound convincing when it is wrong. Retrieval-augmented generation (RAG) and rule-based filters are ways to use that conversational flexibility without trusting it unconditionally. section 12.4

A math tutor illustrates the division of work. An LLM can adapt its wording to a student’s questions; the application still needs to handle requirements such as obtaining a parent’s permission before tutoring a child over WhatsApp. LangChain provides a way to experiment with the LLM-backed conversation, but a prompt describing a rule is not the same as an independently enforced rule. section 12.5.2

The prototype builds up the conversational side:

  • Connect an LLM through LangChain and put the user’s message into a tutor prompt. section 12.5.2
  • Add conversation history so the next reply can depend on earlier turns; specify the parent-permission sequence in the onboarding prompt. section 12.5.2
  • Test the replies, especially where a mistaken answer or skipped permission step would matter. Prompt instructions alone do not establish reliable 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)

The Python wrapper pairs a ConversationChain with conversation memory and a supplied prompt. It prototypes context-aware dialogue; it does not implement RAG or a separate rule-based filter. section 12.5.2

A fluent reply can still be wrong

In a number-sequence quiz, the tutor praises “12” as correct for 2, 4, 6, 8, then says the next number is 10. The contradiction shows why a teaching chatbot should not treat an LLM’s confident response as a checked answer. section 12.5.2

The practical pattern is to let the LLM help with open-ended interaction while grounding or checking what the application cannot afford to get wrong. The LangChain tutor demonstrates the generative component and its limits, not a completed RAG-and-filter system. section 12.4 section 12.5.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Works through a LangChain conversational prototype and its failures, but does not implement the proposed RAG or rule-based filters.
Examples
Uses a WhatsApp math tutor’s parent-permission dialogue and a number-sequence quiz that produces contradictory feedback.
What it assumes
Assumes familiarity with Python examples while introducing LangChain’s role as an abstraction over LLM APIs.
What you can do after
Apply it via LangChain to prototype a conversational tutor and identify replies that need grounding or independent checks.
How it connects
Connects large language models, retrieval-augmented generation, rule-based filters, prompt templates, and conversation memory.

Also explained in

Compare how 2 books explain hybrid chatbot →

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.