skytin1004 / teach-chatgpt-to-answer

This repository is a lab for a post published on the Microsoft Tech Community blog.
0 stars 0 forks source link

IndexError: list index out of range #1

Open skytin1004 opened 7 months ago

skytin1004 commented 7 months ago

Problem

I've noticed that if the value of result['@search.rerankerScore'] is too high, I get an index error. This is probably caused by too many documents being filtered out when we don't have enough documents.

Total Documents Found: 5, Top Documents: 3
Traceback (most recent call last):
  File "c:\Users\sms79\azure-proj\gpt-proj1\example(semantic_kernel).py", line 172, in <module>
    asyncio.run(main())
  File "C:\Users\sms79\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\sms79\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 646, in run_until_complete        
    return future.result()
  File "c:\Users\sms79\azure-proj\gpt-proj1\example(semantic_kernel).py", line 163, in main
    answer = await answer_with_sk(kernel, QUESTION, related_page)
  File "c:\Users\sms79\azure-proj\gpt-proj1\example(semantic_kernel).py", line 134, in answer_with_sk
    context['related_materials'] = related_page[0].text
IndexError: list index out of range

Solution

I would recommend replacing "result['@search.rerankerScore'] > 1.5" in the "filter_documents" function with "result['@search.rerankerScore'] > 1.1" or 1.0 and try running the program again.

async def filter_documents(search_results):
    """Filter documents that score above a certain threshold in semantic search"""
    file_content = OrderedDict()
    for result in search_results['value']:
        # The '@search.rerankerScore' range is 1 to 4.00, where a higher score indicates a stronger semantic match.
        if result['@search.rerankerScore'] > 1.1:
            file_content[result['metadata_storage_path']] = {
                'chunks': result['pages'][:10],
                'captions': result['@search.captions'][:10],
                'score': result['@search.rerankerScore'],
                'file_name': result['metadata_storage_name']
            }

    return file_content