Azure OpenAI citations with message correlation

题意:"Azure OpenAI 引用与消息关联"

问题背景:

I am trying out Azure OpenAI with my own data. The data is uploaded to Azure Blob Storage and indexed for use with Azure AI search

"我正在尝试使用自己的数据进行 Azure OpenAI。数据已上传到 Azure Blob 存储并为 Azure AI 搜索进行了索引。"

I do a call to the endpoint in the form of POST {endpoint}/openai/deployments/{deployment-id}/chat/completions?api-version={api-version}, as referenced here.

"我对端点进行了一次调用,形式为 `POST {endpoint}/openai/deployments/{deployment-id}/chat/completions?api-version={api-version}`,就像这里所引用的。"

However, in the response I cannot figure out how the choices[0]['message']['context']['citations'] field correspond to the choices[0]['message']['content'].

"然而,在响应中,我无法弄清楚 `choices0'message''context''citations'` 字段是如何与 `choices0'message''content'` 对应的。"

For example, I can have a content as something like:

"例如,我的 `content` 可能是这样的:"

cs 复制代码
I have a pear [doc1][doc2]. I have an apple [doc1][doc3].

However, in my citations it looks like:

"然而,在我的 `citations` 中,它看起来像这样:"

cs 复制代码
citations[0].filepath == 'file1.pdf'
citations[1].filepath == 'file2.pdf'
citations[2].filepath == 'file1.pdf'
citations[3].filepath == 'file3.pdf'
citations[4].filepath == 'file4.pdf'

In summary, my question is whether if there is some sort of mapping from doc as shown in the message to the citations.filepath.

"总而言之,我的问题是是否存在一种映射,将消息中显示的 `doc` 与 `citations.filepath` 关联起来。"

问题解决:

Actually, it is not about the length of the citations; it is about how many times the file is referred.

"实际上,这与 `citations` 的长度无关,而是与文件被引用的次数有关。"

If you observe clearly, you can see 'file1.pdf' is referred twice, so mappings will be based on the first appearance and reuse of docs like below:

"如果你仔细观察,可以看到 `file1.pdf` 被引用了两次,因此映射将基于第一次出现和重用文档,如下所示:"

  • doc1 -> citations[0] (file1.pdf).
  • doc2 -> citations[1] (file2.pdf).
  • Reuse of doc1 -> Refers back to the first document (citations[2], file1.pdf).
  • doc3 -> citations[3] (file3.pdf).

Use the code below to get mappings and use it in the content.

"使用下面的代码来获取映射,并在内容中使用它。"

cs 复制代码
import re

def map_citations(content, citations):
    
    pattern = re.compile(r'\[doc(\d+)\]')
    segments = pattern.split(content)
    
    doc_numbers = []
    for segment in segments:
        if segment.isdigit():
            doc_numbers.append(int(segment))
    
    

    doc_to_file_map = {}
    for i, doc_num in enumerate(doc_numbers):
        doc_to_file_map[f'doc{doc_num}'] = citations[i]['filepath']

    print(doc_to_file_map)
    
    def replace_placeholder(match):
        doc_num = match.group(1)
        return f"[{doc_to_file_map[f'doc{doc_num}']}]"
    
    mapped_content = pattern.sub(replace_placeholder, content)
    
    return mapped_content

content = "I have a pear [doc1][doc2]. I have an apple [doc1][doc3]."
citations = [
    {'filepath': 'file1.pdf'},
    {'filepath': 'file2.pdf'},
    {'filepath': 'file1.pdf'},
    {'filepath': 'file3.pdf'},
    {'filepath': 'file4.pdf'}
]

mapped_content = map_citations(content, citations)
print(mapped_content)
相关推荐
全栈弄潮儿4 小时前
我的代码审查 Prompt:从“能跑”到“可维护”
aigc·openai·ai编程
赛博仓鼠10 小时前
秋叶ComfyUI 3.2整合包实测:Python 3.13+Torch 2.13全栈升级,一键跑通MiniMax H3(附完整部署)
ai·ai作画·aigc·音视频
todoitbo10 小时前
本地图库语义搜索实战:接上蓝耘元生代,让“傍晚的海边“能搜到图
人工智能·ai·api·工具实战
天远API13 小时前
零信任架构实战:基于天远车信盟出险构建自动化车险评估网关
人工智能·ai·工具分享
JavaPub-rodert13 小时前
Codex 里的 GPT-6 Astra、GPT-5.6 Sol、Terra、Luna 怎么选?
ai·codex·skill
梅梅绵绵冰13 小时前
大模型应用开发-SpringAI框架
spring·ai
蜡台14 小时前
向量数据库完全解析:原理、核心指标、RAG工作流 \+ Milvus实战Demo
ai·agent
云烟成雨TD14 小时前
LlamaIndex 系列【32】检索增强策略:自问自答(Self Ask)
ai·agent·rag·llamaindex
slacker-kian14 小时前
[实践]-让 SAP 工程 Skill 脱离 opencode跑在自定义Agent 上
ai·llm·sap·agent·abap·adt·opencode
合米AI SOP系统15 小时前
人工巡检的局限性,正在悄悄消耗工厂利润,合米科技AI SOP视觉防错怎么破?
人工智能·ai