このトピックでは、MaxCompute で MaxFrame AI 関数を使用する方法について説明し、大規模モデルのオフライン推論アプリケーションを始めるためのユースケースを提供します。
関数の概要
MaxFrame AI 関数は、Alibaba Cloud MaxCompute プラットフォーム上で大規模モデルのオフライン推論を行うためのエンドツーエンドソリューションです。データ処理と AI 機能をシームレスに統合し、ビッグデータプラットフォーム上で大規模モデルのインテリジェンスを利用可能にします。この統合により、データ処理のワークフローが簡素化され、結果の品質が向上し、データ分析における AI 利用のハードルが下がり、エンタープライズレベルの大規模モデルアプリケーションの開発が効率化されます。
設計思想:「データ入力、データ出力」。MaxFrame の Python 開発フレームワークと Pandas スタイルの API を使用して、MaxCompute エコシステム内でワークフロー全体を完結させることができます。このワークフローには、データ準備、データ処理、モデル推論、結果ストレージが含まれます。
コア機能:
大規模データ処理:ログ分析やユーザー行動データなどの大量の構造化データ、およびテキスト翻訳やドキュメント要約などの非構造化データを処理します。1 つのタスクでペタバイト級のデータを処理できます。
低レイテンシーと線形スケーリング:分散コンピューティングアーキテクチャにより、低レイテンシーと線形スケーリングを実現します。
大規模モデルの統合:MaxFrame は、
Qwen 3、Qwen 2.5、Deepseek-R1-Distill-Qwenなどの大規模モデルシリーズをすぐに利用可能な形でサポートします。モデルは MaxCompute プラットフォーム内でオフラインでホストされるため、モデルのダウンロード、配布、API 呼び出しの同時実行数制限について心配する必要はありません。API を使用してモデルを呼び出すことができます。これにより、MaxCompute の大規模な計算リソースを最大限に活用し、高い全体的なトークンスループットと同時実行性でテキスト処理タスクを完了できます。
適用シナリオ:テキストからの構造化情報の抽出、コンテンツの要約、概要の生成、言語翻訳、テキスト品質評価、感情分類など、さまざまなタスクに利用できます。これにより、大規模モデルのデータ処理ワークフローが大幅に簡素化され、結果の品質が向上します。
利点の概要:
次元
MaxFrame AI 関数
使いやすさ
使い慣れた Python API、すぐに使えるモデルライブラリ、デプロイコストゼロ。
拡張性
MaxCompute の CU および GU 計算リソースに依存し、大規模並列処理をサポートし、全体的なトークンスループットを向上させます。
データと AI の統合
統一されたプラットフォームでデータの読み取り、データ処理、AI 推論、結果のストレージを実行します。これにより、データ移行コストが削減され、開発効率が向上します。
シナリオカバレッジ
翻訳、構造化抽出、ベクトル化など、10 以上の高頻度シナリオをカバーします。
全体アーキテクチャ
MaxFrame AI 関数は、モデルタイプを選択し、MaxCompute テーブルとプロンプトを入力パラメーターとして使用できる、柔軟な
generateインターフェイスを提供します。インターフェイスが実行されると、MaxFrame はまずテーブルデータをチャンクに分割します。次に、データ量に基づいて適切な同時実行レベルを設定し、ワーカーグループを起動して計算タスクを実行します。各ワーカーは、指定されたプロンプトパラメーターをテンプレートとして使用します。入力データ行をレンダリングしてモデルの入力を構築します。推論結果と成功ステータスは MaxCompute に書き込まれます。
以下の図は、全体的なアーキテクチャとプロセスを示しています。

適用範囲
サポート対象リージョン:
中国 (杭州)、中国 (上海)、中国 (北京)、中国 (ウランチャブ)、中国 (深セン)、中国 (成都)、中国 (香港)、China (Hangzhou) Finance Cloud、China (Shenzhen) Finance Cloud、シンガポール、インドネシア (ジャカルタ)。
サポート対象 Python バージョン:Python 3.11。
サポート対象 SDK バージョン:MaxFrame SDK V2.3.0 以降。次のコマンドを実行して、ご利用のバージョンを確認できます:
// Windows pip list | findstr maxframe // Linux pip list | grep maxframeご利用のバージョンが必要なバージョンより古い場合は、次のコマンドを実行して最新バージョンをインストールしてください:
pip install --upgrade maxframe最新の MaxFrame クライアントがインストールされていること。
モデルサポートシステム
MaxFrame は、Qwen 3、Qwen 2.5、Deepseek-R1-Distill-Qwen などの大規模モデルシリーズをすぐに利用可能な形でサポートします。すべてのモデルは MaxCompute プラットフォーム内でオフラインでホストされます。モデルのダウンロード、配布、API 呼び出しの同時実行数制限について心配する必要はありません。API を使用してモデルを呼び出し、大規模モデルでオフライン推論タスクを実行できます。これにより、MaxCompute の大規模な計算リソースを最大限に活用し、高い全体的なトークンスループットと同時実行性を実現できます。
Qwen 3 シリーズ:Qwen 3 モデルをベースにした推論最適化バージョンです。多言語翻訳、複雑なテキスト生成、コード生成タスクをサポートします。高精度の出力が必要なシナリオに適しています。
Qwen Embedding モデル:ベクトル化タスク用に設計されています。テキストからベクトルへの効率的な変換をサポートし、セマンティック検索や類似性マッチングなどのシナリオに適しています。
Deepseek-R1-Distill-Qwen シリーズ:知識蒸留を使用して圧縮された軽量モデルです。リソースに制約のある環境での高速推論に適しています。
まずは小さいモデルから始めてください。出力が期待どおりでない場合は、より大きなモデルに切り替えてください。大きなモデルはより多くのリソースを消費し、処理時間も長くなります。小さいモデルを優先することで、出力要件を満たしつつ、効率を向上させ、コストを削減できます。
対応モデル(随時更新)
モデルタイプ | モデル名 | サポート対象リソースタイプ |
Qwen 3 シリーズモデル |
| CU |
| GU | |
Qwen Embedding モデル |
| CU |
| GU | |
Qwen 2.5 テキストシリーズモデル |
| CU |
Deepseek-R1-Distill-Qwen シリーズモデル |
| CU |
Deepseek-R1-0528-Qwen3 モデル |
| CU |
インターフェイスの説明
MaxFrame AI 関数は、generate と Task のデュアルインターフェイスを通じて、柔軟性と使いやすさのバランスを提供します:
汎用インターフェイス:generate
このインターフェイスは、カスタムプロンプトテンプレートとパラメーター設定をサポートします。推論ロジックを高度に制御する必要があるシナリオに適しています。例:
from maxframe.learn.contrib.llm.models.managed import ManagedTextLLM
llm = ManagedTextLLM(name="<model_name>")
# プロンプトテンプレート
messages = [
{"role": "system", "content": "system_messages"},
{"role": "user", "content": "user_messages"},
]
result_df = llm.generate(<df> , prompt_template=messages)
print(result_df.execute())
パラメーター:
model_name:必須。モデルの名前。
df:必須。分析対象のテキストまたはデータ。DataFrame にカプセル化されています。
prompt_template:必須。OpenAI Chat フォーマットと互換性のあるメッセージのリスト。コンテンツ内で、
f-stringを使用してテーブル列のコンテンツを参照できます。
シナリオ特化型インターフェイス:Task - GU 計算リソースのみでサポート
このインターフェイスは、一般的なシナリオの開発を簡素化するために、プリセットされた標準化されたタスクインターフェイスを提供します。現在サポートされている task インターフェイスには、translate と extract が含まれます。
from maxframe.learn.contrib.llm.models.managed import ManagedTextLLM
llm = ManagedTextLLM(name="<model_name>")
# テキスト翻訳
translated_df = llm.translate(
df["english_column"],
source_language="english",
target_language="Chinese",
examples=[("Hello", "你好"), ("Goodbye", "再见")],
)
translated_df.execute()例
MaxFrame を使用してデータ処理を初めて行う場合は、「MaxFrame のクイックスタート」を参照して、その機能について学んでください。
この例はローカルモードで実行されます。次のコードは、完全なスクリプトとその説明を示しています:
import os
from maxframe import new_session
from odps import ODPS
import pandas as pd
import maxframe.dataframe as md
from maxframe.learn.contrib.llm.models.managed import ManagedTextLLM
o = ODPS(
os.getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'),
os.getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
project='<maxcompute_project_name>',
endpoint='https://service.cn-hangzhou.maxcompute.aliyun.com/api',
)
# 1. MaxFrame SDK を使用してセッションを作成します。
session = new_session(odps_entry=o)
# 列のテキストを切り捨てずに完全なコンテンツを表示します。
pd.set_option("display.max_colwidth", None)
# 中間の列が省略されないように、すべての列を表示します。
pd.set_option("display.max_columns", None)
# 2. DataFrame をインポートし、5 つの質問を作成します。
query_list = [
"What is the average distance between Earth and the sun?",
"In what year did the American Revolutionary War begin?",
"What is the boiling point of water?",
"How can I quickly relieve a headache?",
"Who is the protagonist of the Harry Potter series?",
]
df = md.DataFrame({"query": query_list})
# 3. ManagedTextLLM オブジェクトを作成し、使用するモデルとして qwen3-14b を指定します。
llm = ManagedTextLLM(name="qwen3-14b")
# 4. システムメッセージとユーザーメッセージを含むプロンプトテンプレートを定義します。
# ユーザーメッセージでは、f-string 形式で変数 {query} を予約します。このプレースホルダーは、DataFrame の query 列のコンテンツに置き換えられます。プレースホルダーは、DataFrame の複数の列を使用することをサポートしており、すべて対応する列名で参照されます。
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Please answer the following question: {query}"},
]
# 5. LLM オブジェクトの generate メソッドを呼び出します。DataFrame とプロンプトテンプレートを渡して、各質問に対する回答を取得します。
result_df = llm.generate(df, prompt_template=messages)
# 6. execute を使用して、結果の DataFrame の計算をトリガーします。
# すべての計算は MaxCompute クラスター上で行われます。入力 DataFrame は、計算の規模に基づいて自動的にシャーディングされ、並列処理されます。
print(result_df.execute())
次の結果が返されます:
行番号 | response_json | success |
0 |
| True |
1 |
| True |
2 |
| True |
3 |
| True |
4 |
| True |
適用シナリオ 1:言語翻訳
シナリオの説明:多国籍企業が 100,000 件の英語の契約書を中国語に翻訳し、主要な条項に注釈を付ける必要があります。
以下はサンプルコードです:
サンプルデータ
CREATE TABLE IF NOT EXISTS raw_contracts ( id BIGINT, en STRING ); -- サンプルデータを挿入します。 INSERT INTO raw_contracts VALUES (1, 'This Agreement is made and entered into as of the Effective Date by and between Party A and Party B.'), (2, 'The Contractor shall perform the Services in accordance with the terms and conditions set forth herein.'), (3, 'All payments shall be made in US Dollars within thirty (30) days of receipt of invoice.'), (4, 'Either party may terminate this Agreement upon thirty (30) days written notice to the other party.'), (5, 'Confidential Information shall not be disclosed to any third party without prior written consent.');モデルの呼び出し
from maxframe.learn.contrib.llm.models.managed import ManagedTextLLM # 1. Qwen3 1.7B モデルを使用します。 llm = ManagedTextLLM(name="Qwen3-1.7B") # 2. データを準備します。まず MaxCompute テーブル raw_contracts を作成し、翻訳対象のデータを準備する必要があります。 df = md.read_odps_table("raw_contracts") # 3. プロンプトテンプレートを定義します。 messages = [ { "role": "system", "content": "あなたは、ユーザーが提供する英語のテキストを流暢に中国語に翻訳できるドキュメント翻訳の専門家です。", }, { "role": "user", "content": "次の英語のテキストを中国語に翻訳してください。翻訳されたテキストのみを出力し、それ以外のものは出力しないでください。\n\n 例:\n入力:Hi\n出力:你好。\n\n 処理するテキストは次のとおりです:\n\n{en}", }, ] # 4. `generate` インターフェイスを直接使用し、プロンプトを定義し、対応するデータ列を参照します。 result_df = llm.generate( df, prompt_template=messages, params={ "temperature": 0.7, "top_p": 0.8, }, ).execute() # 5. 結果データを MaxCompute テーブルに書き込みます。 result_df.to_odps_table("raw_contracts_result").execute()次の結果が返されます:
+------------+------------+---------+ | index | response | success | +------------+------------+---------+ | 0 | {"id": "chatcmpl-1765195462", "object": "chat.completion", "created": 1765195462, "model": "qwen3-1.7b", "choices": [{"index": 0, "message": {"role": "assistant", "content": "This agreement is made and entered into by and between Party A and Party B as of the effective date.", "reasoning_content": "Okay, I need to translate the English sentence provided by the user into Chinese. First, let's look at the sentence structure: 'This Agreement is made and entered into as of the Effective Date by and between Party A and Party B.'\n\nFirst, 'This Agreement' translates to '本协议' (this agreement), which is appropriate. 'is made and entered into' has two verbs. 'made' usually refers to making an agreement, while 'entered into' might mean formal signing. But according to legal terminology, '签订' (to sign) or '达成协议' (to reach an agreement) might be more common. However, 'entered into' is often used in legal documents for formal signing, so an adjustment might be needed. For example, '本协议经双方于有效日期签订' (This agreement is signed by both parties on the effective date).\n\nThen, 'as of the Effective Date' translates to '自生效日期起' (from the effective date) or '自有效日期起' (from the valid date). Here, 'Effective Date' is usually translated as '生效日期' (effective date), so the whole sentence structure might need adjustment, like '本协议由双方于生效日期签订' (This agreement is signed by both parties on the effective date).\n\nFinally, 'by and between Party A and Party B' translates to '由当事人A和B之间签订' (signed between Party A and Party B) or '由当事人A和B签署' (signed by Party A and Party B). According to legal terminology, '由当事人A和B签署' or '由A和B之间签订' might be more common.\n\nPutting it all together, a possible translation is: '本协议由当事人A和B于生效日期签署。' (This agreement is signed by Party A and Party B on the effective date.) or '本协议由双方于生效日期签订。' (This agreement is signed by both parties on the effective date.) But I need to ensure the verb usage is correct. For example, 'entered into' might be more accurately '签订' (to sign) rather than '进入' (to enter), so an adjustment might be needed.\n\nLet's double-check. The user's example is 'Hi' translated to '你好' (hello), so a direct translation is fine. It might be necessary to maintain formal legal terminology, like '签订' (to sign) instead of '进入' (to enter)."}, "finish_reason": "stop"}], "usage": {"prompt_tokens": 90, "completion_tokens": 356, "total_tokens": 446}} | true | | 1 | {"id": "chatcmpl-1765195487", "object": "chat.completion", "created": 1765195487, "model": "qwen3-1.7b", "choices": [{"index": 0, "message": {"role": "assistant", "content": "The Contractor shall provide the services in accordance with the terms and conditions set forth in this agreement.", "reasoning_content": "Okay, I need to translate the English sentence provided by the user into Chinese. First, let's look at the sentence structure. The original sentence is: 'The Contractor shall perform the Services in accordance with the terms and conditions set forth herein.'\n\nFirst, let's identify the proper nouns. 'Contractor' here should be translated as '承包商' (contractor) or '承包人' (contractor), and a more specific term might be needed depending on the context, but '承包商' is probably more appropriate here. 'Services' is '服务' (services) or '工作' (work), but a more accurate translation might be needed depending on the context, like '服务' or '工作'. 'in accordance with' translates to '按照' (according to) or '依照' (in accordance with). 'terms and conditions' is '条款和条件'. 'set forth herein' is '此处列出' (listed here) or '此处规定' (stipulated here).\n\nSo the general meaning of the sentence is: 'The contractor shall provide the services in accordance with the terms and conditions listed in this agreement.'\n\nI need to check if there's a more natural way to express this. For example, in 'perform the Services,' 'Services' might refer to specific tasks or work, so '提供服务' (provide services) is appropriate. Also, 'set forth herein' might be more naturally translated as '此处列明' (listed here) or '此处规定' (stipulated here).\n\nPutting it all together, the translation should be: '承包商应按照本协议中列明的条款和条件提供服务。' (The contractor shall provide the services in accordance with the terms and conditions listed in this agreement.)\n\nI'll confirm there are no omissions or mistranslations and ensure the professional terms are accurate, for example, whether 'Contractor' needs a more specific job title, but based on common usage, '承包商' is fine here. Finally, I'll keep the sentence concise and fluent."}, "finish_reason": "stop"}], "usage": {"prompt_tokens": 86, "completion_tokens": 325, "total_tokens": 411}} | true | | 2 | {"id": "chatcmpl-1765195505", "object": "chat.completion", "created": 1765195505, "model": "qwen3-1.7b", "choices": [{"index": 0, "message": {"role": "assistant", "content": "All payments shall be made in US dollars within 30 days of receipt of the invoice.", "reasoning_content": "Okay, I need to translate the English sentence provided by the user into Chinese. First, let's look at the sentence structure: 'All payments shall be made in US Dollars within thirty (30) days of receipt of invoice.'\n\nFirst, 'All payments' translates to '所有付款' (all payments), which is appropriate. 'shall be made' is '将被作出' (will be made) or '将被支付' (will be paid), where '将被支付' might be more formal. 'in US Dollars' is '以美元计付' (paid in US dollars) or '以美元支付' (paid in US dollars), but 'US Dollars' is usually translated as '美元' (dollars), so '以' (in) is not needed. 'within thirty (30) days' is '在收到发票后的30天内' (within 30 days of receiving the invoice). 'of receipt of invoice' is '收到发票之日' (the day the invoice is received).\n\nSo the whole sentence can be translated as: '所有付款应在收到发票后30天内以美元支付。' (All payments shall be made in US dollars within 30 days of receipt of the invoice.)\n\nI need to check if there's a more natural expression, for example, if '收到发票后30天内' (within 30 days after receiving the invoice) is accurate, or if the word order needs to be adjusted. For example, 'within thirty days of receipt of invoice' might be more naturally expressed as '收到发票后30天内'.\n\nAfter confirming there are no errors, I'll output the translation directly."}, "finish_reason": "stop"}], "usage": {"prompt_tokens": 89, "completion_tokens": 256, "total_tokens": 345}} | true | | 3 | {"id": "chatcmpl-1765195538", "object": "chat.completion", "created": 1765195538, "model": "qwen3-1.7b", "choices": [{"index": 0, "message": {"role": "assistant", "content": "Either party has the right to terminate this Agreement upon thirty days' written notice.", "reasoning_content": "Okay, I need to translate the English sentence provided by the user into Chinese. First, let's look at the sentence structure: 'Either party may terminate this Agreement upon thirty (30) days written notice to the other party.'\n\nFirst, let's break down the sentence components. 'Either party' refers to any party, 'may terminate' means can terminate, 'this Agreement' is this agreement, 'upon thirty (30) days written notice' is thirty days' written notice, and 'to the other party' is to notify the other party.\n\nWhen translating, I need to be accurate with legal terms. For example, 'Either party' is usually translated as '任何一方' (any party) or '任一方' (either party). 'may terminate' is '有权终止' (has the right to terminate) or '可以终止' (can terminate). 'this Agreement' is '本协议' (this agreement). 'upon thirty (30) days written notice' needs to accurately express 'thirty days' written notice'. 'to the other party' is '通知另一方' (notify the other party).\n\nThen, I'll consider the fluency of the sentence. The original sentence is a compound sentence, so I need to ensure the Chinese translation has a reasonable structure. For example, 'Either party may terminate this Agreement upon thirty (30) days written notice to the other party.' can be translated as '任何一方有权在书面通知另一方三十天后终止本协议。' (Either party has the right to terminate this agreement thirty days after written notice to the other party.) or more concisely '任何一方有权在三十天书面通知后终止本协议。' (Either party has the right to terminate this agreement after thirty days' written notice.)\n\nLet's check for a more natural expression. For example, 'upon' here might be better translated as '在……之后' (after...). Also, 'written notice' is '书面通知' (written notice), and 'days' is '天数' (number of days).\n\nFinally, I'll settle on the translation: '任何一方有权在三十天书面通知后终止本协议。' (Either party has the right to terminate this agreement after thirty days' written notice.) or more concisely '任何一方有权在三十天书面通知后终止本协议。' I need to ensure no information is lost, for example, 'terminate' is '终止' (terminate) and not '终止或解除' (terminate or rescind).\n\nAfter confirming it's correct, I'll output the translation directly."}, "finish_reason": "stop"}], "usage": {"prompt_tokens": 89, "completion_tokens": 394, "total_tokens": 483}} | true | | 4 | {"id": "chatcmpl-1765195574", "object": "chat.completion", "created": 1765195574, "model": "qwen3-1.7b", "choices": [{"index": 0, "message": {"role": "assistant", "content": "Confidential Information shall not be disclosed to any third party without prior written consent.", "reasoning_content": "Okay, the user wants me to translate a piece of English into Chinese and output it directly without any other content. First, I need to confirm what the English sentence provided by the user is. The example sentence given by the user is 'Confidential Information shall not be disclosed to any third party without prior written consent.', and the user's example is input 'Hi' output '你好' (hello). Now the text provided by the user is 'Confidential Information shall not be disclosed to any third party without prior written consent.', which needs to be translated into Chinese.\n\nFirst, I need to accurately understand the meaning of this sentence. 'Confidential Information' should be '机密信息' (confidential information) or '保密信息' (secret information). 'shall not be disclosed' is '不得泄露' (shall not be disclosed). 'to any third party' is '任何第三方' (any third party). 'without prior written consent' is '未经事先书面同意' (without prior written consent). So the Chinese translation of the whole sentence should be '机密信息不得以任何第三方未经事先书面同意而泄露。' (Confidential information shall not be disclosed by any third party without prior written consent.)\n\nHowever, the user might want a more formal translation, like an expression commonly used in legal documents. For example, '机密信息不得向任何第三方披露,除非获得事先书面同意。' (Confidential information shall not be disclosed to any third party unless prior written consent is obtained.) This is more in line with legal terminology. But the user didn't specify, so I might need to stick to a direct translation but ensure accuracy.\n\nAlso, I need to pay attention to the sentence structure. 'shall not be disclosed' in the original sentence is '不得被披露' (shall not be disclosed), so I need to ensure the tense and voice of the verb are correct in the translation. At the same time, translating 'to any third party' as '任何第三方' (any third party) is correct, but sometimes '任何第三方' might need adjustment, for example, '任何第三方或任何其他个人' (any third party or any other individual)? But the example sentence provided by the user is '任何第三方', so I should probably keep it as is.\n\nLet's check for a more natural expression. For example, '机密信息不得未经事先书面同意向任何第三方披露' (Confidential information shall not be disclosed to any third party without prior written consent) might be more in line with Chinese expression habits. But the user might want a direct translation, so I need to weigh that. However, the example sentence given by the user is '机密信息不得向任何第三方泄露,除非获得事先书面同意。' (Confidential information shall not be disclosed to any third party unless prior written consent is obtained.), so I might need to maintain a similar structure.\n\nFinally, I'll ensure the translated sentence is fluent, conforms to Chinese grammar, and has no missing information. After confirming it's correct, I'll output the translation directly."}, "finish_reason": "stop"}], "usage": {"prompt_tokens": 85, "completion_tokens": 454, "total_tokens": 539}} | true | +------------+------------+---------+
適用シナリオ 2:キーワード抽出
シナリオの説明:このシナリオは、非構造化データを処理する MaxFrame AI 関数の能力を示します。非構造化データの大部分を占めるテキストや画像は、ビッグデータ分析において大きな課題となります。以下の例は、AI 関数を使用してこのプロセスを簡素化する方法を示しています。
次のコードは、ランダムに生成されたテキストを含む履歴書から、AI 関数を使用して候補者の職務経験を取得する方法を示しています。
以下はサンプルコードです:
サンプルデータ
CREATE TABLE IF NOT EXISTS traditional_chinese_medicine ( index BIGINT, text STRING ); -- サンプルデータを挿入します。 INSERT INTO traditional_chinese_medicine VALUES (1, '患者 張氏、男性、45歳。主訴: 2週間続く反復性の咳。現病状: 多量の白色粘稠痰を伴う咳、胸部圧迫感、息切れ、食欲不振、軟便。舌: 白く膩のある苔。脈: 滑脈。診断: 痰湿阻肺。治療原則: 燥湿化痰、理気止咳。処方: 二陳湯加減。'), (2, '患者 李氏、女性、32歳。「1ヶ月続く不眠と多夢」を主訴に来院。動悸、物忘れ、精神疲労、萎黄色の顔色を伴う。舌: 淡く、薄い白苔。脈: 細弱脈。診断: 心脾両虚証。処方: 帰脾湯加減、1日1服、水で煎じて服用。'), (3, '患者 王氏、68歳。主訴: 半年前からの腰と膝の痛み、頻繁な夜間尿。悪寒、四肢の冷え、蝉の鳴き声のような耳鳴り、無気力を伴う。舌: 淡く、歯痕のある腫れ、白く滑らかな苔。脈: 沈細脈。中医学診断: 腎陽虚証。治療: 温補腎陽。処方: 金匱腎気丸加減。'), (4, '患者 趙氏、5歳。3日間の発熱、最高体温 39.5°C、軽度の悪風寒、濁った鼻汁を伴う鼻づまり、赤く腫れて痛む喉。舌: 舌先が赤く、薄い黄苔。脈: 浮数脈。診断: 風熱犯肺証。治療原則: 疏風清熱、宣肺止咳。処方: 銀翹散加減。'), (5, '患者 劉氏、男性、50歳。3年間続く反復性の上腹部膨満感と痛み、1週間前から悪化。症状: 上腹部の満腹感、頻繁なげっぷ、感情の変動による症状の悪化、不規則な便通。舌: 赤く、薄い黄苔。脈: 弦脈。診断: 肝胃不和証。治療原則: 疏肝和胃、理気止痛。処方: 柴胡疏肝散と左金丸の合方加減。');モデルの呼び出し
from maxframe.learn.contrib.llm.models.managed import ManagedTextLLM from pydantic import BaseModel from typing import List, Optional # 1. Qwen3-4B-Instruct-2507-FP8 モデルを使用します。 llm = ManagedTextLLM(name="Qwen3-4B-Instruct-2507-FP8") df = md.read_odps_table("traditional_chinese_medicine", index_col="index") # 4つの同時実行パーティション parallel_partitions = 4 df = df.mf.rebalance(num_partitions=parallel_partitions) class MedicalRecord(BaseModel): """ 漢方医学の診察記録の構造化スキーマ """ patient_name: Optional[str] = None # 患者名 (例: "張") age: Optional[int] = None # 年齢 gender: Optional[str] = None # 性別 ("男性"/"女性") chief_complaint: str # 主訴 symptoms: List[str] # 症状のリスト tongue: str # 舌診 (例: "舌苔白膩") pulse: str # 脈診 (例: "脈滑") diagnosis: str # 漢方医学の証候診断 treatment_principle: str # 治療原則 prescription: str # 処方名 # extract プリセットタスクインターフェイスを使用します。 result_df = llm.extract( df["text"], description="以下の診察記録から構造化データを順番に抽出してください。スキーマに従って厳密な JSON 形式で結果を返してください。", schema=MedicalRecord ) result_df.execute() result_df.to_odps_table("result").execute()
リソース管理とパフォーマンス最適化
異種リソーススケジューリングポリシー
MaxFrame は 2 種類のリソースをサポートします:
CU (コンピュートユニット):汎用 CPU 計算リソース。小規模モデルや小規模データ推論タスクに適しています。
GU (GPU ユニット):GPU 計算リソース。大規模モデルの推論に最適化されており、より大きなモデルサイズをサポートします。
8B 以上の大規模モデルでは、CPU 推論は効率が低くなります。GU 計算リソースに切り替えて推論を実行できます。
session を設定することで、リソースを動的に選択できます:
# CU/GU リソースの使用を設定します。
# CU 計算リソースを使用します。
options.session.quota_name = "mf_cpu_quota"
# GU 計算リソースを使用します。
options.session.gu_quota_name = "mf_gpu_quota"
並列推論
MaxFrame は並列計算メカニズムを使用して、大規模データに対するオフライン推論を実行します:
データチャンキング:
rebalanceインターフェイスを使用して、指定されたパーティション数 (num_partitions) に基づいて、入力データテーブルを複数のワーカーノードに均等に分散させることができます。並列モデル読み込み:各ワーカーは独立してモデルを読み込み、プリフェッチすることで、モデル読み込みによるコールドスタートレイテンシーを回避します。
結果の集約:出力結果はパーティションごとに MaxCompute テーブルに書き込まれ、後続のデータ分析をサポートします。
大規模データ推論ジョブでは、rebalance インターフェイスを使用してデータを事前にチャンクに分割し、同時実行性を確保できます。これにより、データの並列処理が可能になります。