Missing citations from OpenAI + AI-Search RAG

I’ve build a RAG using AI-Search and OpenAI that generally works fine however inline sources are not returned accurately. The response object always includes citations but these are just the X results from the AI-Search vector search. At this point you do not know which of these 5 or 10 sources are actually used to answer the question.

Usually the actual answer includes inline citations like [doc2]. I use regex to find these tags, extract the number and return the second document from my citations object. With this method I can specify only the sources that are actually used.
The problem is that this is incredibly unreliable for me. At least 7/10 times there is no [docX] tag anywhere in the actual response and therefore no sources are listed.

For now I just list all sources and call it “potential sources” but this is less than ideal.

I currently use this package version:
openai == 1.14.2

And this is my code:

def ask_llm_citation(USER_INPUT:str, history: list, config: dict):
    def parse_multi_columns(columns: str) -> list:
        if "|" in columns:
            return columns.split("|")
        else:
            return columns.split(",")

    # Add the Conversation History to the prompt
    messages = []
    for question, answer in history[-AZURE_OPENAI_CONVERSATION_HISTORY:]:
        messages.append({ "role": "user", "content": question })
        messages.append({ "role": "assistant", "content": answer })

    # Add the latest user input to the prompt
    messages.append({ "role": "user", "content": USER_INPUT })
    client = openai.AzureOpenAI(
        base_url=f"{OPENAI_API_BASE}/openai/deployments/{OPENAI_DEPLOYMENT_NAME}/extensions",
        api_key=OPENAI_API_KEY,
        api_version=OPENAI_API_VERSION_CITATION
    )

    response = client.chat.completions.create(
        messages=messages,
        model=OPENAI_DEPLOYMENT_NAME,
        temperature=OPENAI_API_GPT_TEMPERATURE,
        seed=12345,
        extra_body={
            "dataSources": [
                {
                    "type": "AzureCognitiveSearch",
                    "parameters": {
                        "endpoint": AZURE_COGNITIVE_SEARCH_ENDPOINT,
                        "key": AZURE_COGNITIVE_SEARCH_KEY,
                        "indexName": AZURE_COGNITIVE_SEARCH_INDEX_NAME,
                        "fieldsMapping": {
                            "contentFields": parse_multi_columns("content"),
                            "urlField": "url_name",
                            "filepathField": "file_name",
                            "vectorFields": parse_multi_columns("content_vector")
                        },
                        "embeddingDeploymentName": OPENAI_API_DEPLOYMENT_NAME_EMBEDDING,
                        "query_type": AZURE_COGNITIVE_SEARCH_QUERY_TYPE,
                        "inScope": True,
                        "roleInformation": AZURE_OPENAI_SYSTEM_MESSAGE,
                        "topNDocuments": AZURE_COGNITIVE_SEARCH_NR_DOCUMENTS,
                        "strictness":  AZURE_COGNITIVE_SEARCH_STRICTNESS
                    }
                }
            ]
        },
        stream=True,
    )
    for chunk in response:
        try:
            yield chunk.choices[0].delta
        except:
            pass

Does anyone know if there is a fix to it or another method to pinpoint the sources that were actually used to answer the question if you compare the given llm answer with the 5 or 10 documents from AI-Search?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật