接口介绍
该接口用于按智能体检索知识库内容。调用方可通过传入智能体 Code 和检索关键词,从指定智能体关联的知识库中查询相关文档片段或问答内容,并返回匹配结果列表。
接口支持控制返回结果数量、是否进行重排序、是否优先检索 QA 内容,以及结果分数阈值等参数,适用于智能问答、知识库召回、RAG 检索、内容匹配等场景。返回结果包含文档 ID、文件名、问题、答案、正文内容、内容类型、匹配分数及相关元数据,便于后续进行答案生成、结果展示或业务处理。
调用样例
接口采用 AK/SK 签名鉴权,需在请求头中携带 X-App-Id、X-App-Key、X-Timestamp、X-Nonce 及 X-Signature。其中签名 X-Signature 由 METHOD\nPATH\nTIMESTAMP\nNONCE 拼接后,使用 AppSecret 进行 HMAC-SHA256 计算得到(签名只使用 URL 的 path,不含 query string)。
以下为 Python 调用样例(仅依赖标准库):
#!/usr/bin/env python3
import hashlib
import hmac
import json
import random
import string
import time
import urllib.parse
import urllib.request
import urllib.error
# ---------- 鉴权凭证(请替换为实际值) ----------
APP_ID = "app_xxxxxxxx" # AppID(app_ 前缀)
APP_KEY = "ak_xxxxxxxx" # AppKey(ak_ 前缀)
APP_SECRET = "sk_xxxxxxxx" # AppSecret(sk_ 前缀)
API_URL = "https://open.agibot.com/api/V1/open-portal/app/retriever/search/agent_kb"
def _nonce(n: int = 16) -> str:
return "".join(random.choices(string.ascii_lowercase + string.digits, k=n))
def _sign(method: str, path: str, ts_ms: int, nonce: str, secret: str) -> str:
payload = f"{method.upper()}\n{path}\n{ts_ms}\n{nonce}"
return hmac.new(secret.encode(), payload.encode(), hashlib.sha256).hexdigest()
def call(method: str, url: str, body=None):
path = urllib.parse.urlparse(url).path # 签名只用 path,不含 query string
ts_ms = int(time.time() * 1000)
nonce = _nonce()
sig = _sign(method, path, ts_ms, nonce, APP_SECRET)
data = json.dumps(body).encode() if body is not None else None
req = urllib.request.Request(url, data=data, method=method.upper())
req.add_header("Content-Type", "application/json")
req.add_header("X-App-Id", APP_ID)
req.add_header("X-App-Key", APP_KEY)
req.add_header("X-Timestamp", str(ts_ms))
req.add_header("X-Nonce", nonce)
req.add_header("X-Signature", sig)
try:
with urllib.request.urlopen(req, timeout=60) as r:
return r.status, r.read().decode()
except urllib.error.HTTPError as e:
return e.code, e.read().decode()
if __name__ == "__main__":
body = {
"agent_code": "AI_AGENT_28159402", # 智能体 Code
"query": "远征", # 检索内容
"top_k": 10, # 返回 Top K 个结果,默认 10
"rerank": True, # 是否重排结果,默认 true
"check_qa_first": True, # 是否优先检索 QA,默认 true
"score_threshold": 0, # 结果分数阈值,默认 0
}
status, resp = call("POST", API_URL, body)
print(f"HTTP {status}")
try:
print(json.dumps(json.loads(resp), ensure_ascii=False, indent=2))
except Exception:
print(resp)
Parameters
Request Body
agent_codestring智能体Code
querystring检索内容
top_kinteger返回Top K个结果
rerankboolean是否重排结果
check_qa_firstboolean是否优先检索QA
score_thresholdnumber结果分数阈值
Response Schema