---
title: "AI Search"
description: "搜索并回答用户问题，返回网页、图片、多模态参考源、总结答案和追问问题。"
slug: "/api-reference/ai-search"
version: "1.0.0"
source: "https://api.bocha.cn/v1/ai-search"
---

# AI Search

## 接口信息

- **请求方式**：`POST`
- **接口路径**：`/v1/ai-search`
- **完整地址**：`https://api.bocha.cn/v1/ai-search`

搜索并回答用户问题，返回网页、图片、多模态参考源、总结答案和追问问题。

## OpenAPI 定义

~~~yaml
/v1/ai-search:
    post:
      summary: AI Search
      operationId: aiSearch
      # 与网页搜索页共用首屏规则：简介之后由原生接口卡承接调用方式和 Try It，
      # 字段级说明继续维护在 requestBody / responses 的 schema 中，保证调试面板与文档同源。
      description: 搜索并回答用户问题，返回网页、图片、多模态参考源、总结答案和追问问题。
      tags:
        - 搜索
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/AISearchRequest"
            examples:
              nonStreaming:
                summary: 非流式搜索
                value:
                  query: 西瓜的功效与作用
                  freshness: noLimit
                  answer: false
                  stream: false
              streaming:
                summary: SSE 流式搜索
                value:
                  query: 天空为什么是蓝色的
                  answer: true
                  stream: true
      responses:
        "200":
          description: AI 搜索成功。根据 `stream` 参数，响应为 JSON 或 SSE 数据流。
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/AISearchResponse"
              example:
                code: 200
                log_id: e47a7cdb4998c002
                conversation_id: c9bbdb5aecec4d9d941a605700d5813c
                msg: success
                messages:
                  - role: assistant
                    type: answer
                    content_type: text
                    content: 西瓜富含水分、维生素和矿物质。
            text/event-stream:
              schema:
                $ref: "#/components/schemas/AISearchStreamEvent"
              examples:
                message:
                  summary: 一条流式回答片段
                  value:
                    event: message
                    message:
                      role: assistant
                      type: answer
                      content_type: text
                      content: 天空呈现蓝色是因为瑞利散射。
                    index: 0
                    conversation_id: 4cc9e91fded948328e82739390db9c36
                    seq_id: 0
                    is_finish: false
                done:
                  summary: 正常结束事件
                  value:
                    event: done
        "400":
          description: 请求参数错误，例如缺少 `query`
        "401":
          description: 鉴权失败，API Key 缺失或无效
        "403":
          description: 账户余额不足
        "429":
          description: 请求频率达到限制
        "500":
          description: 搜索服务内部错误

    AISearchRequest:
      type: object
      required:
        - query
      properties:
        query:
          type: string
          description: 用户的搜索内容。
        freshness:
          type: string
          default: noLimit
          enum: [noLimit, oneDay, oneWeek, oneMonth, oneYear]
          description: 搜索指定时间范围内的网页；默认为 `noLimit`。
        include:
          type: string
          description: |
            限定搜索的站点范围。多个根域名或子域名以 `|` 或 `,` 分隔，最多 100 个；
            也可以在 `query` 中使用 `site:` 指定，例如 `site:qq.com|m.163.com 阿里ESG报告`。
        count:
          type: integer
          minimum: 1
          maximum: 50
          default: 10
          description: 每次返回的搜索结果数量，最多 50 条；实际数量可能少于指定值。
        answer:
          type: boolean
          default: true
          description: |
            是否使用大模型生成回答。`true` 时返回总结答案和推荐追问；`false` 时仅返回搜索参考源。
        stream:
          type: boolean
          default: false
          description: |
            是否启用 SSE 流式输出。`false` 时返回完整 JSON；`true` 时实时增量返回，客户端需要按消息类型组装结果。

    AISearchResponse:
      type: object
      description: '`stream: false` 时的非流式响应。'
      properties:
        conversation_id:
          type: string
          description: 会话 ID。
        messages:
          type: array
          description: 已完成的全部消息；消息结构见 `AIMessage`。
          items:
            $ref: "#/components/schemas/AIMessage"
        code:
          type: integer
          description: 状态码，`200` 表示调用成功。
        log_id:
          type: string
          description: 本次请求的追踪 ID。
        msg:
          type: string
          description: 状态信息。

    AISearchStreamEvent:
      description: '`stream: true` 时每个 SSE `data:` 行中的 JSON 内容。'
      oneOf:
        - $ref: "#/components/schemas/AISearchStreamMessageEvent"
        - $ref: "#/components/schemas/AISearchStreamDoneEvent"
        - $ref: "#/components/schemas/AISearchStreamErrorEvent"

    AIMessage:
      type: object
      properties:
        role:
          type: string
          enum: [user, assistant]
          description: 消息发送方。`user` 表示用户消息，`assistant` 表示博查返回的消息。
        type:
          type: string
          enum: [source, answer, follow_up]
          description: |
            消息类型。`source` 为参考源；`answer` 为最终回答；`follow_up` 为推荐追问。
            没有搜索结果时不会返回 `source` 消息。
        content:
          type: string
          description: |
            消息内容。`type: answer` 时为 Markdown 文本；当 `content_type` 不是 `text` 时，
            该字段为 JSON 编码后的字符串，需要先解析为 JSON。
        content_type:
          type: string
          description: |
            内容类型。`text` 表示文本；网页、图片和视频参考源分别为 `webpage`、`image`、`video`；
            也可能为 AI Search 支持的多模态卡类型（例如 `weather_china_v2`、`baike_pro_v2`）。

    AISearchStreamMessageEvent:
      type: object
      required:
        - event
        - message
      properties:
        event:
          type: string
          const: message
          description: 表示本包包含增量消息内容。
        message:
          $ref: "#/components/schemas/AIMessage"
        is_finish:
          type: boolean
          description: 当前消息是否已发送完毕。仅表示该条消息结束，不表示整个响应结束。
        index:
          type: integer
          description: 相同 `index` 的增量内容属于同一条消息。
        conversation_id:
          type: string
          description: 会话 ID。
        seq_id:
          type: integer
          description: 当前事件在整个数据流中的序号，从 0 开始。

    AISearchStreamDoneEvent:
      type: object
      required:
        - event
      properties:
        event:
          type: string
          const: done
          description: 正常结束标志。

    AISearchStreamErrorEvent:
      type: object
      required:
        - event
        - error_information
      properties:
        event:
          type: string
          const: error
          description: 错误结束标志。
        error_information:
          $ref: "#/components/schemas/AISearchErrorInformation"

    AISearchErrorInformation:
      type: object
      properties:
        code:
          type: integer
          description: 错误码。
        msg:
          type: string
          description: 错误信息。

~~~
