Selecting the number of chunks to use as context when doing RAG?

Bao, Jeremy (Cognizant) 105 Reputation points
2024-03-18T21:51:14.91+00:00

When you are using the Python SDK for Azure OpenAI to generate text based on user input and RAG, how do you specify the number of chunks of text to use as context?

I read that it involves some parameter called "topNDocuments," but where does that go? It is not recognized as an argument to client.chat.completions.create().

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
4,081 questions
{count} votes

Accepted answer
  1. Charlie Wei 3,335 Reputation points
    2024-03-19T05:29:55.88+00:00

    Hello Bao, Jeremy (Cognizant),

    Regarding the topNDocuments you mentioned, please refer to this document and try the following example code.

    from openai import AzureOpenAI
    
    client = AzureOpenAI(
        azure_endpoint=...,
        api_key=...,
        api_version="2024-02-01",
    )
    
    completion = client.chat.completions.create(
        model=...,
        messages=[...],
        extra_body={
            "data_sources": [
                {
                    "type": "azure_search",
                    "parameters": {
                        "endpoint": "<search_endpoint>",
                        "index_name": "<search_index>",
                        "authentication": {
                            "type": "system_assigned_managed_identity"
                        },
                        "top_n_documents": 5
                    }
                }
            ]
        }
    )
    

    Best regards,
    Charlie


    If you find my response helpful, please consider accepting this answer and voting 'yes' to support the community. Thank you!

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.