构建智能体
运用您的编码智能体和 UiPath 技能,在本地构建、配置和运行 LangGraph 智能体。
安装 CLI、技能就位且您的帐户通过身份验证后,您就可以构建本地项目了。
步骤 4 - 设置本地项目
为您的智能体项目创建一个新文件夹,然后在 VS Code 中打开( “文件”→“打开文件夹”)。此步骤之后的所有命令都从项目文件夹根目录运行。
创建 Python 环境
创建固定到受支持的 Python 版本的虚拟环境并激活:
mkdir QuestIntake
cd QuestIntake
uv venv --python 3.13
source .venv/bin/activate
mkdir QuestIntake
cd QuestIntake
uv venv --python 3.13
source .venv/bin/activate
Windows:使用.venv\Scripts\activate代替source .venv/bin/activate 。
Python 版本:如果 3.13 不在您的路径上, uv会自动下载和管理 Python。支持的版本为 3.11、3.12 和 3.13。
安装 LangGraph 集成
LangGraph是一个 Python 框架,用于将有状态的 LLM 智能体构建为节点和边线图。uipath-langchain是 UiPath 集成层,用于打包 LangGraph 智能体,以便在 UiPath 平台上进行部署和评估。
将 UiPath LangGraph 包安装到活动虚拟机中。这也使该框架可在下一步中用于项目框架:
uv pip install uipath-langchain
uv pip install uipath-langchain
使用 UiPath CLI 注册 Python
告诉 CLI 与 UiPath 兼容的 Python 可执行文件所在的位置:
uip codedagent setup --force
uip codedagent setup --force
您应该会看到"Result": "Success" 。在使用uip codedagent命令之前,每台计算机都需要执行一次此步骤(在任何 venv 更改后)。
创建项目框架
创建 UiPath 项目结构。uip codedagent new检测已安装的框架并生成正确的框架文件:
uip codedagent new QuestIntake
uip codedagent new QuestIntake
这将创建pyproject.toml 、 main.py 、 langgraph.json 、 uipath.json 、 entry-points.json 、 bindings.json和编码智能体上下文文件( AGENTS.md 、 CLAUDE.md和.agent/ )。main.py是占位符;您的编码智能体将在步骤 5 中替换它。
添加本地开发服务器依赖项并同步锁定文件:
uv add uipath-dev --dev
uv sync
uv add uipath-dev --dev
uv sync
生成入口点
运行 init,以从脚手架代码生成入口点架构:
uip codedagent init
uip codedagent init
此阶段项目有一个占位符入口点。在编码 Agent 写入真实智能体代码后,在步骤 5 中重新运行init 。
步骤 5 - 使用编码智能体构建智能体
这就是 UiPath 技能获得回报的地方。打开您的编码智能体,并提示其创建智能体逻辑。下面的提示很简短:描述了智能体应该执行的操作,而不是如何构建。
您的编码智能体已安装的 uipath-agents 技能已经了解 LangGraph 集成模式、正确的 SDK 导入、Pygantic 架构约定以及相关的 SDK 要求。如果没有这些技能,您将需要在提示词本身中指明所有这些信息。
使用以下提示词(或根据您的用例进行调整):
Update main.py to implement this UiPath coded agent using LangGraph as a single-node graph with no tools and no retry or error-handling logic.
The agent is a quest intake classifier for a fantasy adventurer's guild. Given a
description of an incoming quest, it classifies the difficulty as one of four tiers:
- Trivial: Simple errands anyone can handle (e.g., deliver a letter, clear rats from a cellar)
- Standard: Moderate quests requiring some skill (e.g., escort a merchant caravan)
- Heroic: Difficult quests requiring significant expertise (e.g., slay a wyvern, infiltrate a thieves' guild)
- Legendary: Extreme quests requiring top-tier heroes and special approval (e.g., defeat a lich, close a planar rift)
Return the classification tier and a brief reasoning. Use these exact field names in the State schema:
- Input field: `description` (string)
- Output fields: `tier` (a Literal type constrained to exactly "Trivial", "Standard", "Heroic", "Legendary" — not a plain string, so the model can't emit an out-of-vocabulary tier) and `reasoning` (string)
Update the existing langgraph.json to point at the new graph, and create an input.json with this exact sample quest: {"description": "Clear the rats out of the inn cellar"}.
Only touch main.py, langgraph.json, and input.json.
Don't run any uip codedagent commands or otherwise verify that the agent runs — I will do this myself.
Update main.py to implement this UiPath coded agent using LangGraph as a single-node graph with no tools and no retry or error-handling logic.
The agent is a quest intake classifier for a fantasy adventurer's guild. Given a
description of an incoming quest, it classifies the difficulty as one of four tiers:
- Trivial: Simple errands anyone can handle (e.g., deliver a letter, clear rats from a cellar)
- Standard: Moderate quests requiring some skill (e.g., escort a merchant caravan)
- Heroic: Difficult quests requiring significant expertise (e.g., slay a wyvern, infiltrate a thieves' guild)
- Legendary: Extreme quests requiring top-tier heroes and special approval (e.g., defeat a lich, close a planar rift)
Return the classification tier and a brief reasoning. Use these exact field names in the State schema:
- Input field: `description` (string)
- Output fields: `tier` (a Literal type constrained to exactly "Trivial", "Standard", "Heroic", "Legendary" — not a plain string, so the model can't emit an out-of-vocabulary tier) and `reasoning` (string)
Update the existing langgraph.json to point at the new graph, and create an input.json with this exact sample quest: {"description": "Clear the rats out of the inn cellar"}.
Only touch main.py, langgraph.json, and input.json.
Don't run any uip codedagent commands or otherwise verify that the agent runs — I will do this myself.
此提示词为何如此具体。它会命名要访问的确切文件,并告知编码智能体不要运行任何 uip codedagent 命令或验证其自身工作。这是我们特意设计的:本实验室的其余部分将直接在后续步骤中练习这些相同的 CLI 命令,因此验证的工作由您来完成,而不是由运行它的编码智能体先完成。
上面的提示词是一个非常适合您自己的项目使用的模板,但您应该删除最后两句,以使编码智能体能够测试其工作并根据自己的判断组织文件。
编码智能体是非确定性的。您生成的代码将与此处显示的任何示例代码不同;这是预期的。重要的是main.py是否能够正常运行并返回分类。
请注意,如果您的编码智能体提出“交付”问题(Studio Web、本地开发服务器或跳过),请选择“跳过 - 我现在已经完成” 。在步骤 10 中连接到 Studio Web。
编码智能体完成后,重新运行 init,以从新的 Pygantic 模式中获取更新的入口点:
uip codedagent init
uip codedagent init
您应该会看到确认检测到入口点的输出以及 ASCII 图表:
Created 'entry-points.json' file with 1 entrypoint(s).
Created 'entry-points.json' file with 1 entrypoint(s).
在继续操作之前,请打开pyproject.toml并在[project]下添加authors条目(如果尚未添加)。UiPath 需要此字段来打包项目:
authors = [{ name = "Your Name" }]
authors = [{ name = "Your Name" }]
智能体在本地运行并注册入口点后,您就可以在下一步中运行智能体了。
步骤 6 - 在本地运行智能体
使用您的编码智能体创建的示例输入文件运行智能体:
uip codedagent run agent --file input.json
uip codedagent run agent --file input.json
您应该会看到智能体对请求进行了分类,并返回一个带有推理的层级。
您也可以内联传递输入。这些示例使用 Bash 单引号语法;如果您使用的是 PowerShell,请改为将--file与 JSON 文件一起使用:
uip codedagent run agent '{"description": "Clear the rats out of the inn cellar"}'
uip codedagent run agent '{"description": "Clear the rats out of the inn cellar"}'
尝试使用自己的输入来验证分类是否合理,例如:
uip codedagent run agent '{"description": "Slay the ancient red dragon terrorizing the countryside"}'
uip codedagent run agent '{"description": "Slay the ancient red dragon terrorizing the countryside"}'
智能体正确分类后,您就可以提供一些内容来验证其答案了。