pipeline_core/wwwroot/api/llm_token_create.dspy

39 lines
1.7 KiB
Plaintext
Raw 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.

# llm_token_create.dspy — 签发短期 LLM 代理 token登录用户机构隔离
#
# 用途给运行环境browser-use 子进程 / 沙箱脚本 / 被开发的 AI 应用)发一把
# 短期钥匙,代替下发真实模型 api_key。真 key 永不出服务进程。
user_id = await get_user()
if not user_id:
return json.dumps({"success": False, "error": "未登录"}, ensure_ascii=False)
p = params_kw or {}
project_id = (p.get('project_id', '') or '').strip()
task_id = (p.get('task_id', '') or '').strip()
model_name = (p.get('model_name', '') or '').strip()
purpose = (p.get('purpose', '') or '').strip()
ttl_hours = p.get('ttl_hours', 8)
max_calls = p.get('max_calls', 500)
# 机构隔离token 的 org_id 一律取当前登录用户所属机构,不接受前端传入
org_id = await get_userorgid()
if not org_id:
org_id = '0'
# 指定了 project_id 时校验 owner防越权给别人的项目签发 token
if project_id:
ok_owner, msg_owner = await check_project_owner(project_id, user_id)
if not ok_owner:
return json.dumps({"success": False, "error": msg_owner or "仅项目 owner 可签发"},
ensure_ascii=False)
ok, token = await create_llm_token(org_id, project_id=project_id, task_id=task_id,
model_name=model_name, purpose=purpose,
ttl_hours=ttl_hours, max_calls=max_calls,
created_by=user_id)
if ok:
return json.dumps({"success": True, "token": token, "org_id": org_id,
"base_url": entire_url('/pipeline_core/api/llm_v1')},
ensure_ascii=False)
return json.dumps({"success": False, "error": token}, ensure_ascii=False)