0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Azure Functions blob trigger|bindings を使って MarkItDown

Posted at

MarkItDown そのものの解説や、Http Trigger や PowerApps 経由での利用例は下記が参考になります。

この記事では、Azure Functions Azure Blob Storage のバインドとトリガー を用いて MarkItDown を動かしてみました。

作ったもの

はまったところ

MarkItDown そのものとはあまり関係ない部分もあるけどはまったところ

保存時のblobnameの指定

使用法
関数のパラメーターを次の型で宣言し、BLOB ストレージに書き込むことができます。

func.Out[str] は文字列
func.Out[func.InputStream] はストリーム
詳細については、「出力 - 例」を参照してください。

このとき例では

sample.py
@app.blob_output(arg_name="outputblob",
                path="newblob/test.txt",
                connection="<BLOB_CONNECTION_SETTING>")

となっており、blob 名が固定となっている。
どうしたものかと色々試した結果、trigger 側の blob 名をパターン指定することで取ってこれるらしい。
すなわち、以下のアノテーションとすることで、input/test.docxで関数がトリガーされた結果、output/test.docx.md として保存することができる。

function_app.py
@app.blob_trigger(
    arg_name="input", path="input/{blobname}", connection="markitdown_blobstorage"
)
@app.blob_output(arg_name="outputblob", path="output/{blobname}.md", connection="markitdown_blobstorage")

変換できるファイルが限られている模様

OpenAI を使うのは画像系ファイルの場合".jpg", ".jpeg", ".png" に限られていたり、
何でもかんでも対応しているわけではないっぽくて、拡張子を見て(あるいは mimetypes.guess_typeして)変換している様子

そのため、以下のようにした。

function_app.py
default_supported_extensions = (
    '.pptx', '.docx', '.xlsx', '.pdf', '.csv', '.zip', '.html', '.htm'
    # ".xml", ".rss", ".atom",'.ipynb', // not working
)
llm_supported_image_extensions = ('.jpg', '.jpeg', '.png')

use_llm = True if os.environ.get('markitdown_azureopenai_key') else False
if use_llm:
    # create an instance of the AzureOpenAI class
    client = AzureOpenAI(
        azure_endpoint=os.environ.get('markitdown_azureopenai_endpoint'),
        api_key=os.environ.get('markitdown_azureopenai_key'),
        api_version=os.environ.get('markitdown_azureopenai_apiversion'),
    )
    md = MarkItDown(llm_client=client, llm_model="gpt-4o")
    supported_extensions = default_supported_extensions + llm_supported_image_extensions
else:
    md = MarkItDown()
    supported_extensions = default_supported_extensions
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?