Add Document Index Information for QA Inline Citations
Labels: name: auto:improvement

### Feature request

Include source document index information in the combine document chain prompt. For example in `_get_inputs` of `StuffDocumentChain` we could make the following change (in addition to corresponding changes to `format_document`):

```
# From
doc_strings = [format_document(doc, self.document_prompt) for doc in docs]

# To
doc_strings = [format_document(doc, i, self.document_prompt) for i, doc in enumerate(docs)]
```

### Motivation

The point of this change is to enable QA based chains (e.g. `ConversationalRetrievalChain`) to easily do inline citations using the source document's index.

## Example

### Prompt
```
Context:
[1] Harrison went to Harvard.
[2] Ankush went to Princeton.
[3] Emma went to Yale.

Question:
Where did Harrison and Emma go to college?
```

### Response
```
Harrison went to Harvard【1】 and Emma went to Yale【3】.
```

This type of structure is also found in popular "QA" models like Bing Chat and ChatGPT's Browse feature (WebGPT etc.). I feel like there should at least be high-level options to enable something like this without having to make custom modifications/extensions to the existing chains.

Without explicitly including these document indices, I find prompting the LLM to cite documents by index could lead to hallucinated citations (e.g. with retrieved `k=4` documents it cites "[8]")

## More Details

As far as I can tell, existing QA implementations in Langchain seem to return source documents separately (i.e. `return_source_documents=True`), or at the end of the response (e.g. `{'output_text': ' The president thanked Justice Breyer for his service.\nSOURCES: 30-pl'}` as in [document-qa-with-sources](https://python.langchain.com/docs/modules/chains/additional/question_answering#document-qa-with-sources)) rather than provide them in-line.

Even newer approaches using OpenAI's Functions API e.g. from `create_citation_fuzzy_match_chain` and `create_qa_with_sources_chain` do not provide this option.

The approach of [QA with sources chain](https://python.langchain.com/docs/modules/chains/additional/openai_functions_retrieval_qa) has a drawback that the LLM has to generate the entire source of the document (e.g. full URL). This is slower because we can just reference the index of the document instead. Also, this is prone to hallucination especially with `gpt-3.5` where fake sources (like URLs) could be generated.

Similarly, the method in [Question-Answering Citations](https://python.langchain.com/docs/modules/chains/additional/qa_citations) provides quotes from source documents, but doesn't actually identify which document they're from. Referencing documents by index should help reduce hallucination and generation speed here as well.

### Your contribution

I'm happy to assist with this, but first I'd like to gather feedback on the idea. It's possible that there are existing approaches or best practices that I'm not familiar with, which could facilitate inline citations without additional implementation. If there are any recommendations on how to proceed with this, I'd be interested in having a discussion around that.