Performance: Question reformulation with history
This concept affects the speed and responsiveness of conversational AI by managing how previous user inputs are reused and reformulated for new queries.
def reformulate_question(history, new_question, max_history=3): recent_context = ' '.join(history[-max_history:]) return f"{recent_context} {new_question}"
def reformulate_question(history, new_question): full_context = '' for turn in history: full_context += turn + ' ' full_context += new_question return full_context
| Pattern | Context Size | Processing Time | Memory Use | Verdict |
|---|---|---|---|---|
| Full history concatenation | Large (all turns) | High (linear growth) | High (linear growth) | [X] Bad |
| Limited recent history | Small (last 3 turns) | Low (constant) | Low (constant) | [OK] Good |
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
llm = SomeLLM()
memory = ConversationBufferMemory()
chain = ConversationChain(llm=llm, memory=memory)
print(chain.run("What is AI?"))
print(chain.run("And how does it learn?"))from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
llm = SomeLLM()
chain = ConversationChain(llm=llm, memory=ConversationBufferMemory)
response = chain.run("Explain machine learning.")
print(response)Pass the test and this pattern moves unknown