107 lines
4.0 KiB
Python
107 lines
4.0 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
从「知你客服6号」复制为「知你客服7号」,并更新 LLM 提示(强化姓名与 user_profile 记忆说明)。
|
||
需本地平台已启动(默认 http://127.0.0.1:8037),账号 admin/123456。
|
||
|
||
用法:
|
||
cd backend && ..\\venv\\Scripts\\python.exe scripts/create_zhini_kefu_7.py
|
||
或: python scripts/create_zhini_kefu_7.py
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
import os
|
||
import sys
|
||
|
||
import requests
|
||
|
||
BASE = os.getenv("PLATFORM_BASE_URL", "http://127.0.0.1:8037").rstrip("/")
|
||
SOURCE_AGENT_ID = os.getenv("ZHINI_6_AGENT_ID", "2acc84d5-814b-4d61-9703-94a4b117375f")
|
||
USER = os.getenv("PLATFORM_USERNAME", "admin")
|
||
PWD = os.getenv("PLATFORM_PASSWORD", "123456")
|
||
|
||
NEW_NAME = "知你客服7号"
|
||
NEW_DESC = (
|
||
"在知你客服6号工作流基础上,配合引擎修复多轮记忆:"
|
||
"对话历史写入真实助手回复、合并 user_profile(含姓名);"
|
||
"LLM 提示词强调用户姓名与 user_profile 的维护。"
|
||
)
|
||
|
||
LLM_PROMPT = """你是客服助手。根据「用户当前输入」「已知用户信息」「相关历史(检索)」和「最近几轮」完成:
|
||
1)判断意图;
|
||
2)生成一句自然、有帮助的回复;
|
||
3)【强制】只要用户说出或暗示自己的姓名、昵称,必须在 user_profile 里用字段 name 保存,例如用户说「我叫王小明」则 JSON 必须包含 "user_profile":{"name":"王小明"}(若已有其它字段则合并,不要丢字段);
|
||
4)若用户问「我叫什么」「你还记得我名字吗」等,必须根据「已知用户信息」里的 user_profile.name 与对话历史回答;若已有 name 则禁止说「还不知道」。
|
||
|
||
只输出一行合法 JSON,不要 markdown。格式示例:
|
||
{"intent":"greeting","reply":"你好王小明!","user_profile":{"name":"王小明"}}
|
||
|
||
用户输入:{{user_input}}
|
||
已知用户信息:{{memory.user_profile}}
|
||
相关历史(检索到的):{{memory.relevant_from_retrieval}}
|
||
最近几轮:{{memory.recent_turns}}
|
||
|
||
要求:reply 简洁自然,200 字以内;user_profile 为对象,至少包含 name(当用户自我介绍时)。"""
|
||
|
||
|
||
def main() -> int:
|
||
r = requests.post(
|
||
f"{BASE}/api/v1/auth/login",
|
||
data={"username": USER, "password": PWD},
|
||
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
||
timeout=15,
|
||
)
|
||
if r.status_code != 200:
|
||
print("登录失败:", r.status_code, r.text[:500], file=sys.stderr)
|
||
return 1
|
||
token = r.json().get("access_token")
|
||
if not token:
|
||
print("无 access_token", file=sys.stderr)
|
||
return 1
|
||
h = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
||
|
||
dup = requests.post(
|
||
f"{BASE}/api/v1/agents/{SOURCE_AGENT_ID}/duplicate",
|
||
headers=h,
|
||
json={"name": NEW_NAME},
|
||
timeout=30,
|
||
)
|
||
if dup.status_code != 201:
|
||
print("复制失败:", dup.status_code, dup.text[:800], file=sys.stderr)
|
||
return 1
|
||
new_id = dup.json()["id"]
|
||
print("已创建副本:", new_id, NEW_NAME)
|
||
|
||
g = requests.get(f"{BASE}/api/v1/agents/{new_id}", headers=h, timeout=30)
|
||
if g.status_code != 200:
|
||
print("读取 Agent 失败:", g.text, file=sys.stderr)
|
||
return 1
|
||
agent = g.json()
|
||
wf = agent["workflow_config"]
|
||
nodes = wf.get("nodes") or []
|
||
for n in nodes:
|
||
if n.get("id") == "llm-unified":
|
||
n.setdefault("data", {})["prompt"] = LLM_PROMPT
|
||
break
|
||
|
||
up = requests.put(
|
||
f"{BASE}/api/v1/agents/{new_id}",
|
||
headers=h,
|
||
json={
|
||
"description": NEW_DESC,
|
||
"workflow_config": wf,
|
||
},
|
||
timeout=60,
|
||
)
|
||
if up.status_code != 200:
|
||
print("更新失败:", up.status_code, up.text[:800], file=sys.stderr)
|
||
return 1
|
||
print("已更新描述与 llm-unified 提示词")
|
||
print("Agent ID:", new_id)
|
||
print(json.dumps({"id": new_id, "name": NEW_NAME}, ensure_ascii=False))
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|