Files
2026-10-31 18:51:20 +08:00

250 lines
7.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Agent Plan API 参考
所有接口统一使用 `https://api.map.baidu.com`,鉴权由 `http_get` 工具自动注入:
`Authorization: Bearer <BAIDU_MAP_AUTH_TOKEN>`
调用时不要把 token 写进 URL、headers 或回答。查询参数放在 `params` 对象中,工具会统一做 URL 编码。
## 目录
- [Place](#place)
- [Direction](#direction)
- [Geocoding](#geocoding)
- [Reverse Geocoding](#reverse-geocoding)
- [Weather](#weather)
- [MapRender](#maprender地图展示)
- [响应与排错](#响应与排错)
## Place
`GET /agent_plan/v1/place`
必填参数:
- `user_raw_request`:完整保留用户原始需求和约束词,不压缩成关键词。
- `region`:城市或区域限制。
可选参数:
- `center`:检索中心和排序参考点,格式为 `lat,lng`,坐标系为 `gcj02`。
- `sort`:`distance` 或 `relevance`,默认为 `relevance`。
规则:
- `sort=distance` 时必须传 `center`。
- `center` 必须来自用户或可信接口结果,不得推测;经纬度至少保留 6 位小数。
示例:
```text
http_get({
url: "https://api.map.baidu.com/agent_plan/v1/place",
params: {
user_raw_request: "帮我查一下八达岭长城附近的五星级酒店",
region: "延庆区",
sort: "relevance"
}
})
```
按距离排序:
```text
http_get({
url: "https://api.map.baidu.com/agent_plan/v1/place",
params: {
user_raw_request: "离我最近的火锅店",
region: "北京市",
center: "40.056800,116.308300",
sort: "distance"
}
})
```
## Direction
`GET /agent_plan/v1/direction`
必填参数:
- `user_raw_request`:用户原始需求,包含起点、终点、途经点;支持沿途搜索 POI(如"路上买杯咖啡");保留路线约束词。出现驾车/步行/骑行/公交以外的交通方式(如打车、坐地铁)时,需推理改写为这四种之一(如"打车"改写为"驾车")。
- `location`:用户当前位置,格式为 `lat,lng`,坐标系为 `gcj02`;用于推理模糊或同名起点。
可选参数:
- `refer_pois`:地点精确映射,格式 `地点名称:uid,纬度,经度;地点名称:uid,纬度,经度`。默认不传,仅在澄清消歧后重新规划时传入。
规则:
- `location` 与 `refer_pois` 的坐标必须来自用户或可信接口结果,经纬度至少保留 6 位小数。
- 响应通过 `answer_type` 区分两类结果:
- `gptmodel_navigate`:路线规划结果,直接解析路线方案回答用户。
- `gptmodel_poi_clarify` / `gptmodel_onway_search_clarify`:起终点 POI 澄清或沿途搜索 POI 澄清。先根据用户需求推理最合理候选;无法唯一确定时展示候选并请用户选择。
- 澄清后重新规划必须同时满足两个条件:
1. 改写 `user_raw_request`,明确指定选中的 POI(如"路上买杯咖啡" → "先去星巴克(知春路店)买杯咖啡");
2. 传入选中 POI 的 `refer_pois` 精确坐标映射。
示例:
```text
http_get({
url: "https://api.map.baidu.com/agent_plan/v1/direction",
params: {
user_raw_request: "从故宫驾车到颐和园",
location: "39.914590,116.403770"
}
})
```
别名精确映射:
```text
http_get({
url: "https://api.map.baidu.com/agent_plan/v1/direction",
params: {
user_raw_request: "步行去我家附近最近的中餐厅",
location: "40.056800,116.308300",
refer_pois: "我家:fbc88a21464370106e3e1b52,40.092180,116.345310"
}
})
```
交通方式推理改写(打车 → 驾车):
```text
http_get({
url: "https://api.map.baidu.com/agent_plan/v1/direction",
params: {
user_raw_request: "从王府井驾车去三里屯要多久",
location: "39.914590,116.403770"
}
})
```
沿途搜索(顺路买咖啡):
```text
http_get({
url: "https://api.map.baidu.com/agent_plan/v1/direction",
params: {
user_raw_request: "从百度大厦驾车到颐和园,路上顺便买一杯咖啡",
location: "39.914590,116.403770"
}
})
```
## Geocoding
`GET /agent_plan/v1/geocoding`
- 必填 `address`:尽量完整的地址。
- 可选 `region`:城市或区域提示,用于减少歧义。
示例:
```text
http_get({
url: "https://api.map.baidu.com/agent_plan/v1/geocoding",
params: {
address: "北京市海淀区上地十街10号百度大厦",
region: "北京市"
}
})
```
## Reverse Geocoding
`GET /agent_plan/v1/reverse_geocoding`
必填 `location`,格式为 `lat,lng`,坐标系为 `gcj02`,经纬度至少保留 6 位小数。
示例:
```text
http_get({
url: "https://api.map.baidu.com/agent_plan/v1/reverse_geocoding",
params: {
location: "40.056800,116.308300"
}
})
```
## Weather
`GET /agent_plan/v1/weather`
- `region`:行政区划名称。
- `location`:`lat,lng` 格式的 gcj02 坐标。
两者至少传一个。传坐标时,经纬度至少保留 6 位小数。
按行政区划:
```text
http_get({
url: "https://api.map.baidu.com/agent_plan/v1/weather",
params: {
region: "北京市"
}
})
```
按坐标:
```text
http_get({
url: "https://api.map.baidu.com/agent_plan/v1/weather",
params: {
location: "38.766230,116.432130"
}
})
```
## MapRender(地图展示)
在用户明确表达"在地图上显示/标出""打开地图查看""地图里看看"等可视化意图时,基于上一步 `place` / `direction` 响应中的 `resource_key` 字段生成地图展示链接,回复给用户。
参数:
- `resource_key`(必填):取自上一步 `place` / `direction` 接口响应中的 `resource_key` 字段。
规则:
1. 必须先调用 `place` 或 `direction` 拿到 `resource_key`,才能生成展示链接。
2. 展示链接格式:`https://lbs.baidu.com/mapstatic/agentui_resource.html?resource_key=<resource_key>`。
3. 本环境没有浏览器/shell 工具,**不要尝试打开浏览器**;直接把链接作为回复内容发给用户,由用户自行点击查看。
4. 仅在用户明确表达可视化意图时使用;单纯信息查询("附近有什么""怎么走""多久到")不触发。
5. `direction` 返回非 `gptmodel_navigate` 结果(澄清列表)时,不生成展示链接。
示例:
```text
# 用户:"帮我找北京可带宠物的咖啡馆,在地图上标出来"
# 步骤1: 调用 place 获取 POI 列表和 resource_key
# 步骤2: 回复用户结果,并附上链接
https://lbs.baidu.com/mapstatic/agentui_resource.html?resource_key=abc123def456
```
## 响应与排错
`http_get` 返回结构:
```json
{
"success": true,
"status": 200,
"body": {},
"truncated": false
}
```
- 业务数据在 `body` 中;`body` 可能是字符串,也可能是已解析的 JSON 对象,按实际类型读取。
- `success: false`:网络、DNS、超时或出网策略拦截,查看 `error` 字段并向用户说明。
- `401` / `403`:服务端未配置 `BAIDU_MAP_AUTH_TOKEN`,或 token 已失效;告知普通用户地图服务暂不可用,管理员侧检查 `conf.json` 的 `agent.baiduMap.authToken` / 环境变量,不要索要明文 token。
- `400`:参数错误;检查必填项、坐标格式、`sort=distance` 是否带 `center`。
- `429`:频控;提示用户稍后重试。
- `5xx`:百度地图服务异常;提示用户稍后重试。
- `truncated: true`:响应超过 2MB 被截断;缩小检索范围或改查更具体的接口。
- 不要用正则从响应文本中猜测字段;直接读取 JSON 结构。