In llamaindex / gptindex, how do i control number of responses to a query

265 Views Asked by At

In the following code

def load_index():
    # index if dir 'storage' does not exist
    if not os.path.exists('storage'):
        print('Building index...')
        build_index()
    storage_context = StorageContext.from_defaults(persist_dir='./storage')
    # doc_hash_to_filename = json.load(open('doc_hash_to_filename.json', 'r'))
    return load_index_from_storage(storage_context)

def ask_question(index, query):
    query_engine = index.as_query_engine()
    response = query_engine.query(query)
    return response

I always get 2 responses right now, for any query. How can I get more? is there a parameter I can change?

1

There are 1 best solutions below

0
On

Here's an example of how to extract the top k = 5 window and sentences using sentence window retrieval. Your case may also be similar.

from llama_index.core.indices.postprocessor import MetadataReplacementPostProcessor

query_engine = sentence_index.as_query_engine(
    similarity_top_k=5, # set similarity_top_k to the number of responses you want to get
    node_postprocessors=[
        MetadataReplacementPostProcessor(target_metadata_key="window")
    ],
)

Replace i with 0, 1, 2, 3, 4 to get the top 5 results.

window = window_response.source_nodes[i].node.metadata["window"]
sentence = window_response.source_nodes[i].node.metadata["original_text"]