Skip to main content
工具調用

文搜圖

文搜圖工具使模型能夠根據文本描述從互連網搜尋相關圖片,並基於圖片內容進行描述和推理,適用於可視化問答、配圖推薦等情境。

使用方式

文搜圖功能通過 Responses API 呼叫,在 tools 中添加 web_search_image 工具。
# 匯入依賴與建立用戶端...
response = client.responses.create(
    model="qwen3.8-max",
    input="幫我找一張適合做 PPT 封面的科技感背景圖",
    tools=[{"type": "web_search_image"}]
)

print(response.output_text)

支援的模型

推薦模型

為獲得最佳工具調用效果,推薦使用以下模型: 千問Plus:Qwen3.7-Plus系列、Qwen3.6-Plus系列、Qwen3.5-Plus系列 千問Max:Qwen3.8-Max系列、qwen3.7-max-2026-06-08 qwen3.8-27b

其他模型

以下模型也支援此工具調用,但效果不如推薦模型。
  • 千問Flash:Qwen3.7-Flash系列、Qwen3.6-Flash系列、Qwen3.5-Flash系列
僅支援通過 Responses API 呼叫。

快速開始

運行以下代碼,通過 Responses API 呼叫文搜圖工具,根據文本描述搜尋互連網圖片。
需要已擷取與配置 API Key配置API Key到環境變數
import os
import json
from openai import OpenAI

client = OpenAI(
    # 若沒有配置環境變數,請用百鍊API Key將下行替換為:api_key="sk-xxx"(不建議),
    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="幫我找一張適合做 PPT 封面的科技感背景圖",
    tools=[
        {
            "type": "web_search_image"
        }
    ]
)

for item in response.output:
    if item.type == "web_search_image_call":
        print(f"[工具調用] 文搜圖 (status: {item.status})")
        # 解析並展示搜尋到的圖片列表
        if item.output:
            images = json.loads(item.output)
            print(f"  搜尋到 {len(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)} 張圖片")
    elif item.type == "message":
        print(f"\n[模型回複]")
        print(response.output_text)

# 展示 Token 用量和工具調用統計
print(f"\n[Token 用量] 輸入: {response.usage.input_tokens}, 輸出: {response.usage.output_tokens}, 合計: {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_name} 調用次數: {info.get('count', 0)}")
運行以上代碼可擷取如下回複:
[工具調用] 文搜圖 (status: completed)
  搜尋到 30 張圖片:
  [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 張圖片

[模型回複]
為您找到幾張非常適合做PPT封面的科技感背景圖,您可以根據具體的主題風格進行選擇:

**1. 經典藍色電路板與晶片風格**
適合主題:硬體、晶片、電子工程、底層技術。
![Technology Background](https://www.slideegg.com/image/catalog/89734-powerpoint-background-design-technology.png)

**2. 抽象粒子與網路連接風格**
適合主題:巨量資料、人工智慧、網路安全、雲端運算。
![Technology Background](https://img.freepik.com/free-vector/gradient-technology-futuristic-background_23-2149115239.jpg)

...

[Token 用量] 輸入: 4326, 輸出: 645, 合計: 4971
[工具統計] web_search_image 調用次數: 1

流式輸出

文搜圖工具運行較慢,建議啟用流式輸出,即時擷取中間過程的輸出結果。
import os
import json
from openai import OpenAI

client = OpenAI(
    # 若沒有配置環境變數,請用百鍊API Key將下行替換為:api_key="sk-xxx",
    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="幫我找一張適合做 PPT 封面的科技感背景圖",
    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("[工具調用] 文搜圖搜尋中...")
    # 工具調用完成,解析並展示搜尋到的圖片列表
    elif event.type == "response.output_item.done":
        if event.item.type == "web_search_image_call":
            print(f"[工具調用] 文搜圖完成 (status: {event.item.status})")
            if event.item.output:
                images = json.loads(event.item.output)
                print(f"  搜尋到 {len(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)} 張圖片")
    # 模型回複開始
    elif event.type == "response.content_part.added":
        print(f"\n[模型回複]")
    # 流式文本輸出
    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_tokens}, 輸出: {usage.output_tokens}, 合計: {usage.total_tokens}")
        if hasattr(usage, 'x_tools') and usage.x_tools:
            for tool_name, info in usage.x_tools.items():
                print(f"[工具統計] {tool_name} 調用次數: {info.get('count', 0)}")
運行以上代碼可擷取如下輸出:
[工具調用] 文搜圖搜尋中...
[工具調用] 文搜圖完成 (status: completed)
  搜尋到 30 張圖片:
  [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 張圖片

[模型回複]
為您找到幾張非常適合做PPT封面的科技感背景圖,您可以根據具體的主題風格進行選擇:

**1. 極簡網路連線風(適合巨量資料、串連、通訊主題)**
這張圖以深藍色為底,角落有簡潔的節點連線,中間留白非常多...
![Technology Background](https://slidechef.net/wp-content/uploads/2023/11/TECHNOLOGY-BACKGROUND.jpg)

**2. 硬核電路與晶片風(適合人工智慧、硬體、底層技術主題)**
左側有複雜的電路板紋理和類似HUD的圓環設計...
![Circuit Technology Background](https://www.slideegg.com/image/catalog/89734-powerpoint-background-design-technology.png)

...

[Token 用量] 輸入: 7180, 輸出: 558, 合計: 7738
[工具統計] web_search_image 調用次數: 1

計費說明

計費涉及以下方面:
  • 模型調用費用:圖片搜尋的結果資訊會拼接到提示詞中,增加模型的輸入 Token,按照模型的標準價格計費。價格詳情請參考百鍊控制台。
  • 工具調用費用:每1000次調用收費:新加坡地區為$8,華北2(北京)地區為$3.44。
Token Plan
模型體驗
用量統計與效能監控
資產中心
服務支援
文搜圖 - Alibaba Cloud Model Studio