from llama_index.core.llms import ChatMessage
retriever = index.as_retriever(similarity_top_k=2)
chat_history = memory.get()
nodes = retriever.retrieve(user_msg_str)
nodes_context = "\n\n".join(x.text for x in nodes)
system_prompt = ChatMessage(role="system", content=f"Here is some relevant context to help you assist the user:\n\n{nodes_context}")
llm_input = [system_prompt] + chat_history + [ChatMessage(role="user", content=user_msg_str)]
resp = llm.chat(llm_input)
memory.put(ChatMessage(role="user", content=user_msg_str))
memory.put(resp.message)
print(resp.message)
This is roughly what the context chat engine is doing for you