from llama_index import GPTVectorStoreIndex, ListIndex, VectorStoreIndex
from llama_index.indices.postprocessor import SimilarityPostprocessor
from llama_index.readers.schema.base import Document
query = "how to lose weight"
documents = [
Document(text="exercise more"),
Document(text="reduce caloric intake"),
Document(text="go on a diet"),
Document(text="eat less calories"),
Document(text="Hire a nutritionist"),
Document(text="Hit the gym"),
]
index = GPTVectorStoreIndex.from_documents(documents)
postprocessor = SimilarityPostprocessor(similarity_cutoff=0.8)
index = VectorStoreIndex.from_documents(documents)
retriever = index.as_retriever(similarity_top_k=50)
nodes = retriever.retrieve(query)
filtered_nodes = postprocessor.postprocess_nodes(nodes)
index = ListIndex([x.node for x in filtered_nodes])
summary = index.as_query_engine(response_mode="tree_summarize", use_async=True).query(
"Summarize these reddit comments."
)
print(str(summary))
result: "These reddit comments suggest various ways to achieve weight loss or maintain a healthy lifestyle. The suggestions include eating fewer calories, going on a diet, reducing caloric intake, exercising more, hitting the gym, and hiring a nutritionist."