query_engine
object to query the index. The query_engine
object is created from the index
object using the as_query_engine()
method. OpenAIEmbedding
model to generate the embedding for your query. Here is an example of how to do it:from llama_index.core import QueryBundle from llama_index.embeddings.openai import OpenAIEmbedding # By default, typesense vector store uses vector search. You need to provide the embedding yourself. query_str = "What did the author do growing up?" embed_model = OpenAIEmbedding() query_embedding = embed_model.get_agg_embedding_from_queries(query_str) query_bundle = QueryBundle(query_str, embedding=query_embedding) response = index.as_query_engine().query(query_bundle)
QueryBundle
object is created with the query string and its corresponding embedding. The query()
method of the query_engine
object is then used to query the index with this QueryBundle
.retrieve_documents
method of the MetaphorToolSpec
class. This method takes a list of document IDs and returns a list of corresponding documents. Here is an example of how to use it:from llama_index.tools.metaphor import MetaphorToolSpec # Assuming you have a MetaphorToolSpec instance named metaphor_tool document_ids = ["doc1", "doc2", "doc3"] # replace with your actual document IDs documents = metaphor_tool.retrieve_documents(document_ids)
documents
will be a list of Document
objects corresponding to the IDs in document_ids
.retrieve_documents(ids: List[str]) -> List[Document]
metaphor_search
, using the ID field.ids
| List[str]
| the ids of the documents to retrieve | required |"doc1"
, "doc2"
, and "doc3"
with the actual IDs of the documents you want to retrieve.similarity_top_k
parameter when building the query_engine
. This parameter determines the number of most similar documents to return. Here is an example:from llama_index.core import VectorStoreIndex, SimpleDirectoryReader documents = SimpleDirectoryReader("data").load_data() index = VectorStoreIndex.from_documents(documents) query_engine = index.as_query_engine(similarity_top_k=5) # retrieve top 5 most similar documents response = query_engine.query("What did the author do growing up?") print(response)
query_engine
is configured to return the top 5 most similar documents to the query "What did the author do growing up?".retrieve()
method of a retriever
object to retrieve multiple documents relevant to a query. Here's an example:retriever = index.as_retriever() nodes = retriever.retrieve("Who is Paul Graham?")
nodes
will be a list of NodeWithScore
objects, which are the retrieved documents relevant to the query "Who is Paul Graham?". The number of documents retrieved depends on the configuration of the retriever.mmr_diversity_bias
parameter when creating the retriever. The mmr_diversity_bias
parameter controls the trade-off between relevance and diversity. Here's an example:retriever = index.as_retriever(mmr_diversity_bias=1) nodes = retriever.retrieve("movie about toys")
mmr_diversity_bias
is set to 1, which means the retriever will prioritize diversity over relevance. The retrieve()
method is then used to retrieve documents relevant to the query "movie about toys".