A generative chatbot produces replies from a user’s message rather than choosing only from predefined answers. A pretrained language model can handle unexpected questions without a designer writing every response. That flexibility comes with a tradeoff: replies can be difficult to predict or control. section 12.4
A practical setup separates the model from the conversation around it:
- Connect to a language model through an API; LangChain provides an interface for experimenting with different models. section 12.5.2
- Write a prompt that states the bot’s role, the user’s message, and the response it should produce. For a math tutor, the prompt can specify how to ask a parent for permission before proceeding. section 12.5.2
- Include conversation history when the next reply depends on earlier turns. A prompt receiving only the latest message did not reliably retain a user’s name. 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)
This Python wrapper connects a prompt template to a LangChain conversation chain and memory, so each answer can use prior turns. section 12.5.2
A convincing reply can still be wrong
Asked to continue 2, 4, 6, 8, the tutor praised the answer “12” as correct while also saying the next number was 10. Check generated feedback against the actual task, not just its friendly tone. section 12.5.2
Do not rely on an unguarded model for sensitive or consequential exchanges. Ground or fine-tune it, add guardrails, and consider intent recognition to flag risky messages or assess proposed replies. Generative responses are useful for unusual cases, but fluency is not a reliability guarantee. section 12.4 section 12.5.2
