admin管理员组

文章数量:1562421

上篇文章介绍了整体的处理思路,Azure OpenAI Studio Chat里有处理自有数据的功能,核心思路是:

1. Cognitive Search会对文件(PDF,DOCX,PPTX,TXT,MD格式文件)内容进行识别、分段(Chunk)并存储在索引里

2. OpenAI通过Emedding模型将分段后的内容向量化后更新到索引里

3. 后面聊天的部分调用OpenAI的Embedding模型让OpenAI基于从Congnitive Search的索引获取回的向量内容进行对话

当让也可以省掉2、3步,经过测试回答的质量会下降很多,机器人回答的感觉会非常明显。

后续进一步的测试研究发现了这套自有处理数据模式的限制点和一些潜在的解决方案:

1. 上述处理逻辑一次只能处理不超过16M的文件,后续的处理目前只能通过代码去更新Cognitive Search的索引(虽然可以通过索引器来定时更新,但索引器没法产生向量数据),过程中通过OpenAI Embedding模型获取向量数据后存储在索引里一个叫做contentVector的字段里。同时根据微软官方文章整个下来处理的文件大小不能超过1G,每一批不能超过16M

2. Cognitive Search能够处理PDF文件、PPTX文件、DOCX文件,不过证明了在处理包含在这些文件格式里的图片文件时是处理不了的,Cognitive Search无法识别图片文件。这时就需要通过OCR类的服务来讲图片里的内容识别出来后进行索引及向量化处理等

3. 微软提供的无论是Computer Vision还是AI Service里的OCR服务对图片像素的大小最高不到Document Intelligence服务的一半,因此一些长图用Document Intelligence服务的API来处理会更高,当然两边接口价格差别需要进行单独比较。

4. Document Intelligence服务来OCR识别图片最好用SDK,最开始用Rest API调用返回的JSON内容比较庞杂(有按手写识别的,有按行识别的,有按字识别的),自己处理起来比较啰嗦,用SDK有直接的脚本(Use Document Intelligence client library SDKs or REST API - Azure AI services | Microsoft Learn)可以参考,下面是我用到的识别图片的脚本:

import requests
from azure.ai.formrecognizer import DocumentAnalysisClient
from azure.core.credentials import AzureKeyCredential

# 认知服务的终结点和Key
endpoint = '*****'
key = '*****'

# 处理格式的方法
def format_polygon(polygon):
    if not polygon:
        return "N/A"
    return ", ".join(["[{}, {}]".format(p.x, p.y) for p in polygon])

ef readimageviasdk(imageurl):
    document_analysis_client = DocumentAnalysisClient(
        endpoint=endpoint, credential=AzureKeyCredential(key)
    )
    poller = document_analysis_client.begin_analyze_document_from_url(
        "prebuilt-read", imageurl
    )
    result = poller.result()

    print("Document contains content: ", result.content)

    for idx, style in enumerate(result.styles):
        print(
            "Document contains {} content".format(
                "handwritten" if style.is_handwritten else "no handwritten"
            )
        )

    for page in result.pages:
        print("----Analyzing Read from page #{}----".format(page.page_number))
        print(
            "Page has width: {} and height: {}, measured with unit: {}".format(
                page.width, page.height, page.unit
            )
        )

        for line_idx, line in enumerate(page.lines):
            print(
                "...Line # {} has text content '{}' within bounding box '{}'".format(
                    line_idx,
                    line.content,
                    format_polygon(line.polygon),
                )
            )

        for word in page.words:
            print(
                "...Word '{}' has a confidence of {}".format(
                    word.content, word.confidence
                )
            )

    print("----------------------------------------")

if __name__ == '__main__':
    #print('hello world')
    imageurl = '****'
    data = readimageviasdk(imageurl)
    print(data)

本文标签: 数据openAIAzureCognitivegpt