使用 LangSmith Python 和 TypeScript SDK 管理 **代理代码库** 和 **技能代码库** 在 Context Hub programmatically. 推送 来自 CI 的新版本, 拉取 运行时拉取最新版本或固定提交以向代理注入上下文,并使用其他方法进行 检查存在性, 列出和搜索 代码库,并 删除 不再需要的内容。
设置
- 安装包:
pip install -U langsmith
uv add langsmith
yarn add langsmith
- 配置环境变量。如果您已在环境中设置了
LANGSMITH_API_KEY,请跳过此步骤。否则,请在 **设置 > API 密钥 > 创建 API 密钥** 在 LangSmith 中创建 API 密钥,然后将其设置为环境变量:
推送代理
创建新的代理代码库或提交现有代码库的新版本。如果 代码库尚不存在,则会使用您提供的元数据创建 (description, readme, tags, is_public)。如果已存在, 这些字段仅在显式传递时才会被修补。
该方法返回指向 LangSmith UI:
from langsmith import Client
from langsmith.schemas import FileEntry
client = Client()
url = client.push_agent(
"email-assistant",
files={
"AGENTS.md": FileEntry(
content="You are an email triage assistant.",
),
"tools.json": FileEntry(content='{"tools": []}'),
},
description="Triages and drafts replies to incoming email.",
tags=["email", "productivity"],
is_public=False,
)
print(url)
const client = new Client();
const url = await client.pushAgent("email-assistant", {
files: {
"AGENTS.md": {
type: "file",
content: "You are an email triage assistant.",
},
"tools.json": { type: "file", content: '{"tools": []}' },
},
description: "Triages and drafts replies to incoming email.",
tags: ["email", "productivity"],
isPublic: false,
});
console.log(url);
中新建提交的 URL。推送技能
与 push_agent相同的接口,但提交到技能代码库。使用 来实现可重用的功能,其他代理可以依赖:
from langsmith import Client
from langsmith.schemas import FileEntry
client = Client()
url = client.push_skill(
"deep-research",
files={
"SKILL.md": FileEntry(content="Conduct deep multi-step research."),
},
description="Multi-step web research with citations.",
tags=["research"],
)
print(url)
const client = new Client();
const url = await client.pushSkill("deep-research", {
files: {
"SKILL.md": {
type: "file",
content: "Conduct deep multi-step research.",
},
},
description: "Multi-step web research with citations.",
tags: ["research"],
});
console.log(url);
链接到其他代码库
中的条目可以是指向另一个代理或技能代码库的链接,而不是内联文件内容,这样您可以在不重复的情况下组合上下文 files 中的条目可以是指向 另一个代理或技能代码库的链接,这样您可以在不重复内容的情况下组合上下文 跨代码库的内容。例如,委托给共享技能的代理。
如果您省略 commit_id, LangSmith会在您推送此提交时链接到该仓库的最新提交。如果链接的仓库之后有更新,LangSmith会将该更新传播到引用它的父仓库。
from langsmith import Client
from langsmith.schemas import AgentEntry, FileEntry, SkillEntry
client = Client()
url = client.push_agent(
"email-assistant",
files={
"AGENTS.md": FileEntry(content="You are an email triage assistant."),
# Link to the deep-research skill repo. Omit commit_id to always
# resolve to the latest version, or pin it for reproducibility.
"skills/research": SkillEntry(repo_handle="deep-research"),
# Link to another agent repo.
"agents/scheduler": AgentEntry(repo_handle="calendar-agent"),
},
)
print(url)
const client = new Client();
const url = await client.pushAgent("email-assistant", {
files: {
"AGENTS.md": { type: "file", content: "You are an email triage assistant." },
// Link to the deep-research skill repo. Omit commit_id to always
// resolve to the latest version, or pin it for reproducibility.
"skills/research": { type: "skill", repo_handle: "deep-research" },
// Link to another agent repo.
"agents/scheduler": { type: "agent", repo_handle: "calendar-agent" },
},
});
console.log(url);
推送参数
两者 push_agent / pushAgent 和 push_skill / pushSkill 接受以下参数:
| 参数 | 类型 | 描述 |
|---|---|---|
identifier | string | 仓库的标识符。 |
files | `dict[str, Entry \ | None]` |
parent_commit / parentCommit | string (可选) | 父提交哈希前缀,用于乐观并发控制。提供时必须为8-64个字符。如果与最新提交不匹配,API将返回409冲突。 |
description | string (可选) | 仓库描述。在创建时设置或在更新时修补。 |
readme | string (可选) | 仓库自述文件内容。 |
tags | string[] (可选) | 仓库标签。 |
is_public / isPublic | boolean (可选) | 仓库是否公开可发现。 |
拉取智能体
拉取智能体仓库的快照。默认返回最新提交;通过以下方式传递提交哈希或标签 version (或将其嵌入标识符中为 owner/name:version)以拉取特定版本:
from langsmith import Client
client = Client()
agent = client.pull_agent("email-assistant")
print(agent.commit_hash)
print(list(agent.files))
# Pull a specific commit.
pinned = client.pull_agent("email-assistant", version="7ca95573")
# Pull a tagged commit (for example, the production tag).
prod = client.pull_agent("email-assistant:production")
const client = new Client();
const agent = await client.pullAgent("email-assistant");
console.log(agent.commit_hash);
console.log(Object.keys(agent.files));
// Pull a specific commit.
const pinned = await client.pullAgent("email-assistant", {
version: "7ca95573",
});
// Pull a tagged commit.
const prod = await client.pullAgent("email-assistant:production");
拉取技能
拉取技能仓库的快照。功能与 pull_agent 相同,但返回 SkillContext:
from langsmith import Client
client = Client()
skill = client.pull_skill("deep-research")
print(skill.files["SKILL.md"].content)
const client = new Client();
const skill = await client.pullSkill("deep-research");
const skillFile = skill.files["SKILL.md"];
if (skillFile.type === "file") {
console.log(skillFile.content);
}
拉取参数
两者 pull_agent / pullAgent 和 pull_skill / pullSkill 接受以下参数:
| 参数 | 类型 | 描述 |
|---|---|---|
identifier | string | 仓库的标识符。可能包含内联版本: owner/name:version. |
version | string (可选) | 要拉取的提交哈希或标签。会覆盖标识符中嵌入的版本。 |
pull_agent 返回一个 AgentContext; pull_skill 返回一个 SkillContext.
检查仓库是否存在
使用这些方法检查您的工作区中是否存在智能体或技能仓库 ,然后再进行推送或拉取:
from langsmith import Client
client = Client()
if client.agent_exists("email-assistant"):
print("agent already exists")
if not client.skill_exists("deep-research"):
print("skill not found")
const client = new Client();
if (await client.agentExists("email-assistant")) {
console.log("agent already exists");
}
if (!(await client.skillExists("deep-research"))) {
console.log("skill not found");
}
列出智能体和技能
列出任一类型的仓库,可选择按可见性、存档状态和搜索查询进行过滤:
from langsmith import Client
client = Client()
# Python returns a paginated response.
result = client.list_agents(limit=20, query="email")
for repo in result.repos:
print(repo.repo_handle)
skills = client.list_skills(is_public=True)
const client = new Client();
// TypeScript yields one repo at a time, auto-paginating.
for await (const repo of client.listAgents({ query: "email" })) {
console.log(repo.repo_handle);
}
for await (const skill of client.listSkills({ isPublic: true })) {
console.log(skill.repo_handle);
}
| 参数 | 类型 | 描述 |
|---|---|---|
limit | int (仅 Python) | 每页返回的最大仓库数。默认为 100。 |
offset | int (仅 Python) | 要跳过的仓库数。默认为 0。 |
is_public / isPublic | boolean (可选) | 仅过滤公开(或仅私有)仓库。 |
is_archived / isArchived | boolean (可选) | 按存档状态过滤。默认为 False. |
query | string (可选) | 在仓库句柄、所有者句柄、描述和标签中搜索。 |
删除代理或技能
从您的工作区中删除代理或技能仓库:
from langsmith import Client
client = Client()
client.delete_agent("email-assistant")
client.delete_skill("deep-research")
const client = new Client();
await client.deleteAgent("email-assistant");
await client.deleteSkill("deep-research");