task_categories:question-answering language:en license:mit size_categories:1K<n<10K format:json modality:text library:datasets library:pandas library:mlcroissant library:polars region:us finance
---
license: mit
task_categories:
- question-answering
language:
- en
tags:
- finance
size_categories:
- 1K<n<10K
---
This data comprises synthetic question and answer pairs created by GPT-4-turbo on SEC filings for 29 companies. The dataset has the following columns:

questions, answers, chunks and sorted_chunks.

questions: the list of questions, there were 5 questions created for a 2000 word section of different SEC filings. 

answers: the answer generated by GPT-4.

chunks: these are the bits of text that are segmented.

sorted_chunks: these are the chunks being sorted, using Dense Passage Retrieval (DPR). 

The sorting is done by using the embeddings extracted from the BERT models fine-tuned for query and context embedding for DPR. The sorting is then done by using FAISS using IndexFlatL2.

The following is an example of how one can sort the chunks using DPR pre-trained embeddings and FAISS:

First the DPR models should be imported as such:

```python 
from transformers import DPRConfig, DPRContextEncoder, DPRQuestionEncoder, DPRQuestionEncoderTokenizer, DPRContextEncoderTokenizer

# context tokenizer and encoder
tokenizer_c = DPRContextEncoderTokenizer.from_pretrained("facebook/dpr-ctx_encoder-single-nq-base")
model_c = DPRContextEncoder.from_pretrained("facebook/dpr-ctx_encoder-single-nq-base", device_map = 'cuda')

# question tokenizer and encoder
tokenizer_q = DPRQuestionEncoderTokenizer.from_pretrained("facebook/dpr-question_encoder-single-nq-base")
model_q = DPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base", device_map = 'cuda')
```

The created chunks can then be sorted using FAISS as such:

```python 
import faiss

sorted_chunks = []

print(f'----- total rows: {len(answers)}\n')

for q,a,c in tqdm(zip(questions[:], answers[:], chunks[:])):
  index = faiss.IndexFlatL2(768)  # Assuming embeddings are of size 768
  # Encode texts using context encoder
  encoded_texts = tokenizer_c(c, return_tensors='pt', padding = 'max_length')['input_ids']
  encoded_texts = model_c(encoded_texts.cuda()).pooler_output

  # Encode question using question encoder
  encoded_question = tokenizer_q(q, return_tensors='pt')['input_ids']
  encoded_question = model_q(encoded_question.cuda()).pooler_output

  if len(c)>4:
    # Perform FAISS search to get top 5 texts
    faiss_texts = encoded_texts.detach().cpu().numpy()
    faiss_question = encoded_question.detach().cpu().numpy()
    # faiss_question = encoded_question.detach().cpu().numpy()
    index.add(np.array(faiss_texts))
    _, faiss_topk = index.search(faiss_question, k=4)
    top_texts = [c[j] for j in faiss_topk[0]]
    top_t = ''
    for j in top_texts:
      top_t += j + ', '
    sorted_chunks.append(top_t)

  else:
    # Perform FAISS search to get top 5 texts
    faiss_texts = encoded_texts.detach().cpu().numpy()
    faiss_question = encoded_question.detach().cpu().numpy()
    # faiss_question = encoded_question.detach().cpu().numpy()
    index.add(np.array(faiss_texts))
    _, faiss_topk = index.search(faiss_question, k=len(c))
    top_texts = [c[j] for j in faiss_topk[0]]
    top_t = ''
    for j in top_texts:
      top_t += j + ', '
    sorted_chunks.append(top_t)



df = pd.DataFrame(data = {'questions':questions[:],
                          'answers':answers[:], 'chunks':chunks[:], 'sorted_chunks':sorted_chunks})
```