テキスト、画像生成・編集、動画の作成・取得・ポーリング・ダウンロードは OpenAI SDK に対応しています。base_url を Montagea に設定し、Montagea のモデル ID を使用します。追加のネイティブ設定は extra_body に渡します。動画の画像アップロードは input_reference、画像編集は image を使用します。
from openai import OpenAI
client = OpenAI(base_url="https://api.montagea.com/api/v1", api_key="mtg_...")
reply = client.chat.completions.create(
model="qwen-27b",
messages=[{"role": "user", "content": "Describe a quiet mountain lake."}],
max_tokens=1024,
extra_headers={"Idempotency-Key": "chat-example-2"},
)
print(reply.choices[0].message.content)
image = client.images.generate(
model="qwen-image",
prompt="A quiet mountain lake at sunrise",
n=1,
extra_body={"resolution": "1K", "aspect_ratio": "16:9"},
extra_headers={"Idempotency-Key": "image-example-2"},
)
print(image.data[0].url)
with open("portrait.png", "rb") as reference:
edited = client.images.edit(
model="qwen-image",
image=reference,
prompt="Change the background to a sunny garden",
extra_body={"resolution": "1K", "seed": 42},
extra_headers={"Idempotency-Key": "edit-example-1"},
)
print(edited.data[0].url)
encoded = client.images.generate(
model="qwen-image", prompt="A quiet lake",
size="1536x1024", response_format="b64_json",
extra_headers={"Idempotency-Key": "image-base64-1"},
)
import base64
with open("lake.png", "wb") as output:
output.write(base64.b64decode(encoded.data[0].b64_json))
video = client.videos.create_and_poll(
model="minimax-h3", prompt="A quiet lake at sunrise",
seconds="4", size="1280x720", poll_interval_ms=5000,
extra_headers={"Idempotency-Key": "video-sdk-1"},
)
if video.status == "completed":
client.videos.download_content(video.id).write_to_file("lake.mp4")
else:
print(video.error)
関数ツールは auto、none、required、関数名指定の tool_choice と parallel_tool_calls に対応します。ツールは利用者のアプリで実行し、対応する tool_call_id を付けた role=tool メッセージで結果を返します。response_format は text、json_object、json_schema に対応し、検証済みの構造化出力だけを返します。通常テキストへの自動切り替えはありません。Schema は各 16,000 文字まで。循環しないローカル参照に対応し、外部参照、動的参照、正規表現の制約は非対応です。ツールの説明、引数、結果、Schema も審査対象です。
# Tool calls are returned to your app; Montagea does not execute them.
reply = client.chat.completions.create(
model="qwen-27b",
messages=[{"role": "user", "content": "What is the weather in Paris?"}],
tools=[{"type": "function", "function": {
"name": "get_weather", "strict": True,
"parameters": {
"type": "object", "properties": {"city": {"type": "string"}},
"required": ["city"], "additionalProperties": False,
},
}}],
tool_choice="required",
)
print(reply.choices[0].message.tool_calls)
structured = client.chat.completions.create(
model="qwen-27b",
messages=[{"role": "user", "content": "Return the city Paris."}],
response_format={"type": "json_schema", "json_schema": {
"name": "city", "strict": True,
"schema": {
"type": "object", "properties": {"city": {"type": "string"}},
"required": ["city"], "additionalProperties": False,
},
}},
)
print(structured.choices[0].message.content)
import base64
with open("portrait.png", "rb") as image:
image_url = "data:image/png;base64," + base64.b64encode(image.read()).decode()
vision = client.chat.completions.create(
model="qwen-27b",
messages=[{"role": "user", "content": [
{"type": "text", "text": "Describe this picture."},
{"type": "image_url", "image_url": {"url": image_url}},
]}],
)
print(vision.choices[0].message.content)