Skip to main content

テキストによる画像検索

テキストによる画像検索ツールを使用すると、モデルはテキストによる説明に基づいてインターネットで関連画像を検索できます。その後、モデルは画像コンテンツを記述し、推論を実行できます。これは、視覚的な Q&A や画像レコメンデーションなどのシナリオで役立ちます。

使用方法

Responses API を使用して、テキストによる画像検索機能を呼び出すことができます。web_search_image ツールを tools パラメーターに追加します。
# 依存関係をインポートし、クライアントを作成します...
response = client.responses.create(
    model="qwen3.8-max",
    input="Find a tech-style background image for a PPT cover",
    tools=[{"type": "web_search_image"}]
)

print(response.output_text)

サポートされているモデル

推奨モデル

ツール呼び出しで最良の結果を得るには、次のモデルの使用を推奨します。 Qwen-Plus: Qwen3.7-Plus シリーズ、Qwen3.6-Plus シリーズ、Qwen3.5-Plus シリーズ Qwen-Max:qwen4-max-preview (Token Plan 限定)、Qwen3.8-Max シリーズ、qwen3.7-max-2026-06-08 qwen3.8-27b

その他のモデル

次のモデルもこのツール呼び出しをサポートしていますが、パフォーマンスは推奨モデルほど高くありません。
  • Qwen-Flash: Qwen3.7-Flash シリーズ、Qwen3.6-Flash シリーズ、Qwen3.5-Flash シリーズ
このツールは Responses API からのみ呼び出すことができます。

クイックスタート

次のコードを実行して、Responses API を使用してテキストによる画像検索ツールを呼び出します。このコードは、テキストによる説明に基づいてインターネットで画像を検索します。
API キーを取得し、API キーを環境変数として設定する必要があります。
import os
import json
from openai import OpenAI

client = OpenAI(
    # 環境変数を設定していない場合は、次の行を api_key="sk-xxx" (非推奨) に置き換えて、Model Studio の API キーを使用します。
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # 次の URL は、シンガポールリージョン用です。{WorkspaceId} をお使いのワークスペース ID に置き換えてください。URL はリージョンによって異なります。
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
)

response = client.responses.create(
    model="qwen3.8-max",
    input="Find a tech-style background image for a PPT cover",
    tools=[
        {
            "type": "web_search_image"
        }
    ]
)

for item in response.output:
    if item.type == "web_search_image_call":
        print(f"[Tool Call] Text-to-image search (status: {item.status})")
        # 検索された画像リストを解析して表示します
        if item.output:
            images = json.loads(item.output)
            print(f"  Found {len(images)} images:")
            for img in images[:5]:  # 最初の 5 枚の画像を表示します
                print(f"  [{img['index']}] {img['title']}")
                print(f"      {img['url']}")
            if len(images) > 5:
                print(f"  ... {len(images)} images in total")
    elif item.type == "message":
        print(f"\n[Model Response]")
        print(response.output_text)

# トークン使用量とツール呼び出しの統計情報を表示します
print(f"\n[Token Usage] Input: {response.usage.input_tokens}, Output: {response.usage.output_tokens}, Total: {response.usage.total_tokens}")
if hasattr(response.usage, 'x_tools') and response.usage.x_tools:
    for tool_name, info in response.usage.x_tools.items():
        print(f"[Tool Statistics] {tool_name} calls: {info.get('count', 0)}")
上記のコードは、以下のレスポンスを返します:
[Tool Call] Text-to-image search (status: completed)
  Found 30 images:
  [1] Best Free Information Technology Background S Google Slides Themes ...
      https://image.slidesdocs.com/responsive-images/slides/0-technology-line-network-information-training-courseware-powerpoint-background_17825ea41f__960_540.jpg
  [2] Data Technology Blue Abstract Business Glow Powerpoint Background ...
      https://image.slidesdocs.com/responsive-images/background/data-technology-blue-abstract-business-glow-powerpoint-background_e667bfafcb__960_540.jpg
  [3] PPT Technology Style Background Template Banner Backgrounds | PSD ...
      https://img.pikbest.com/backgrounds/20190418/ppt-technology-style-background-template-banner_1889599.jpg!bw700
  [4] Download Now! PowerPoint Background Design Technology
      https://www.slideegg.com/image/catalog/89734-powerpoint-background-design-technology.png
  [5] Powerpoint Template Technology Images ...
      https://t4.ftcdn.net/jpg/07/53/21/13/360_F_753211329_cVkWkZdxs9tNEoS5q2d8ZH362YQnAH0p.jpg
  ... 30 images in total

[Model Response]
Here are a few tech-style background images that are perfect for a PPT cover. You can choose one based on your specific theme:

**1. Classic blue circuit board and chip style**
Suitable for topics: Hardware, chips, electronic engineering, low-level technology.
![Technology Background](https://www.slideegg.com/image/catalog/89734-powerpoint-background-design-technology.png)

**2. Abstract particles and network connection style**
Suitable for topics: Big data, artificial intelligence, network security, cloud computing.
![Technology Background](https://img.freepik.com/free-vector/gradient-technology-futuristic-background_23-2149115239.jpg)

...

[Token Usage] Input: 4326, Output: 645, Total: 4971
[Tool Statistics] web_search_image calls: 1

ストリーミング出力

テキストによる画像検索ツールは、処理に時間がかかる場合があります。ストリーミング出力を有効にすると、中間結果をリアルタイムで受信できます。
import os
import json
from openai import OpenAI

client = OpenAI(
    # 環境変数を設定していない場合は、次の行を api_key="sk-xxx" (非推奨) に置き換えて、Model Studio の API キーを使用します。
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # 次の URL は、シンガポールリージョン用です。{WorkspaceId} をお使いのワークスペース ID に置き換えてください。URL はリージョンによって異なります。
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
)

stream = client.responses.create(
    model="qwen3.8-max",
    input="Find a tech-style background image for a PPT cover",
    tools=[{"type": "web_search_image"}],
    stream=True
)

for event in stream:
    # ツール呼び出し開始
    if event.type == "response.output_item.added":
        if event.item.type == "web_search_image_call":
            print("[Tool Call] Text-to-image search in progress...")
    # ツール呼び出し完了。検索された画像リストを解析して表示します。
    elif event.type == "response.output_item.done":
        if event.item.type == "web_search_image_call":
            print(f"[Tool Call] Text-to-image search complete (status: {event.item.status})")
            if event.item.output:
                images = json.loads(event.item.output)
                print(f"  Found {len(images)} images:")
                for img in images[:5]:  # 最初の 5 枚の画像を表示します
                    print(f"  [{img['index']}] {img['title']}")
                    print(f"      {img['url']}")
                if len(images) > 5:
                    print(f"  ... {len(images)} images in total")
    # モデルのレスポンス開始
    elif event.type == "response.content_part.added":
        print(f"\n[Model Response]")
    # ストリーミングされたテキスト出力
    elif event.type == "response.output_text.delta":
        print(event.delta, end="", flush=True)
    # レスポンス完了。使用量を出力します。
    elif event.type == "response.completed":
        usage = event.response.usage
        print(f"\n\n[Token Usage] Input: {usage.input_tokens}, Output: {usage.output_tokens}, Total: {usage.total_tokens}")
        if hasattr(usage, 'x_tools') and usage.x_tools:
            for tool_name, info in usage.x_tools.items():
                print(f"[Tool Statistics] {tool_name} calls: {info.get('count', 0)}")
上記のコードは、以下の出力を返します:
[Tool Call] Text-to-image search in progress...
[Tool Call] Text-to-image search complete (status: completed)
  Found 30 images:
  [1] Free Technology Background PowerPoint & Google Slides Themes
      https://slidechef.net/wp-content/uploads/2023/11/TECHNOLOGY-BACKGROUND.jpg
  [2] Best Free Information Technology Background S Google Slides Themes ...
      https://image.slidesdocs.com/responsive-images/slides/0-technology-line-network-information-training-courseware-powerpoint-background_17825ea41f__960_540.jpg
  [3] PPT Technology Style Background Template Banner Backgrounds | PSD ...
      https://img.pikbest.com/backgrounds/20190418/ppt-technology-style-background-template-banner_1889599.jpg!bw700
  [4] Download Now! PowerPoint Background Design Technology
      https://www.slideegg.com/image/catalog/89734-powerpoint-background-design-technology.png
  [5] Powerpoint Template Technology Images ...
      https://t4.ftcdn.net/jpg/07/53/21/13/360_F_753211329_cVkWkZdxs9tNEoS5q2d8ZH362YQnAH0p.jpg
  ... 30 images in total

[Model Response]
Here are a few tech-style background images that are perfect for a PPT cover. You can choose one based on your specific theme:

**1. Minimalist network connector style (suitable for big data, connectivity, and communication topics)**
This image has a dark blue background with simple node connectors in the corner and a lot of white space in the middle...
![Technology Background](https://slidechef.net/wp-content/uploads/2023/11/TECHNOLOGY-BACKGROUND.jpg)

**2. Hardcore circuit and chip style (suitable for artificial intelligence, hardware, and low-level technology topics)**
The left side features complex circuit board textures and a HUD-like ring design...
![Circuit Technology Background](https://www.slideegg.com/image/catalog/89734-powerpoint-background-design-technology.png)

...

[Token Usage] Input: 7180, Output: 558, Total: 7738
[Tool Statistics] web_search_image calls: 1

課金

料金には以下が含まれます:
  • モデル呼び出し料金:画像検索の結果がプロンプトに追加されます。これにより、モデルの入力トークン数が増加します。料金は、モデルの標準料金に基づいて課金されます。料金の詳細については、Model Studio コンソールをご参照ください。
  • ツール呼び出し料金:呼び出し 1,000 回あたりの料金は、シンガポールリージョンでのデプロイの場合は $8、中国北部 2 (北京) リージョンでのデプロイの場合は $3.44 です。