----------------------
To use
CondenseQuestionChatEngine
with
index.as_query_engine()
to question the documents, you need to follow these steps:
- First, you need to create a
query_engine
from your index. This engine will be used to query your documents.
query_engine = index.as_query_engine()
- Then, you can create a
CondenseQuestionChatEngine
using the from_defaults
method. This method allows you to configure the chat engine with default parameters. You can also specify a custom prompt and chat history if needed.
from llama_index.prompts import Prompt
from llama_index.llms import ChatMessage, MessageRole
custom_prompt = Prompt("""\
Given a conversation (between Human and Assistant) and a follow up message from Human, \
rewrite the message to be a standalone question that captures all relevant context \
from the conversation.
<Chat History>
{chat_history}
<Follow Up Message>
{question}
<Standalone question>
""")
# list of `ChatMessage` objects
custom_chat_history = [
ChatMessage(
role=MessageRole.USER,
content='Hello assistant, we are having a insightful discussion about Paul Graham today.'
),
ChatMessage(
role=MessageRole.ASSISTANT,
content='Okay, sounds good.'
)
]
chat_engine = CondenseQuestionChatEngine.from_defaults(
query_engine=query_engine,
condense_question_prompt=custom_prompt,
chat_history=custom_chat_history,
verbose=True
)
- Finally, you can use the
chat
or stream_chat
method of the CondenseQuestionChatEngine
to ask questions to the documents. The stream_chat
method is used for streaming responses.
response = chat_engine.chat("What did Paul Graham do after YC?")
or
response = chat_engine.stream_chat("What did Paul Graham do after YC?")
for token in response.response_gen:
print(token, end="")