Back to Llama Index

Chat Engine - Condense Question Mode

docs/examples/chat_engine/chat_engine_condense_question.ipynb

0.14.212.6 KB
Original Source

<a href="https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/chat_engine/chat_engine_condense_question.ipynb" target="_parent"></a>

Chat Engine - Condense Question Mode

Condense question is a simple chat mode built on top of a query engine over your data.

For each chat interaction:

  • first generate a standalone question from conversation context and last message, then
  • query the query engine with the condensed question for a response.

This approach is simple, and works for questions directly related to the knowledge base. Since it always queries the knowledge base, it can have difficulty answering meta questions like "what did I ask you before?"

If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙.

python
%pip install llama-index-llms-openai
python
!pip install llama-index

Download Data

python
!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'

Get started in 5 lines of code

Load data and build index

python
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader

data = SimpleDirectoryReader(input_dir="./data/paul_graham/").load_data()
index = VectorStoreIndex.from_documents(data)

Configure chat engine

python
chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True)

Chat with your data

python
response = chat_engine.chat("What did Paul Graham do after YC?")
python
print(response)

Ask a follow up question

python
response = chat_engine.chat("What about after that?")
python
print(response)
python
response = chat_engine.chat("Can you tell me more?")
python
print(response)

Reset conversation state

python
chat_engine.reset()
python
response = chat_engine.chat("What about after that?")
python
print(response)

Streaming Support

python
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.llms.openai import OpenAI

llm = OpenAI(model="gpt-3.5-turbo", temperature=0)

data = SimpleDirectoryReader(input_dir="../data/paul_graham/").load_data()

index = VectorStoreIndex.from_documents(data)
python
chat_engine = index.as_chat_engine(
    chat_mode="condense_question", llm=llm, verbose=True
)
python
response = chat_engine.stream_chat("What did Paul Graham do after YC?")
for token in response.response_gen:
    print(token, end="")