nasirus / llama_index

MIT License
1 stars 0 forks source link

Can i get similarity_top_k in faiss index? #3

Open nasirus opened 1 year ago

nasirus commented 1 year ago

I am using GPTFaissIndex and want to get the top 5 closest matching term to the query. Can someone clarify where to set the parameter? i have tried to set in index.query but failed Code:

query = "Who is the author in the book?"
d = 1536
faiss_index = faiss.IndexFlatL2(d)
documents = SimpleDirectoryReader('./test').load_data()
promptHelper = PromptHelper(2000, 512,20)
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=promptHelper)
index = GPTFaissIndex.from_documents(documents, faiss_index=faiss_index, service_context=service_context)
result = index.query(
      query_final,
      similarity_top_k=5,
      text_qa_template=prompt,
      mode="embedding",
)
nasirus commented 1 year ago

In order to get the top 5 closest matching terms to the query, you need to set the similarity_top_k parameter in the query_kwargs of the GPTFaissIndex object. The code snippet below shows how to do this:

query = "Who is the author in the book?"
d = 1536
faiss_index = faiss.IndexFlatL2(d)
documents = SimpleDirectoryReader('./test').load_data()
promptHelper = PromptHelper(2000, 512,20)
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=promptHelper)
index = GPTFaissIndex.from_documents(documents, faiss_index=faiss_index, service_context=service_context)
result = index.query(
      query_final,
      similarity_top_k=5,
      text_qa_template=prompt,
      mode="embedding",
)

For more information on how to use the GPTFaissIndex object, please refer to the GPT-Index documentation.