工具

如何构建有效的代理

Over the past year, we’ve worked with dozens of teams building large language model (LLM) agents across industries. Consistently, the most successful implementations weren’t using complex frameworks or specialized libraries. Instead, they were building with simple, composable patterns.

过去一年,我们与数十个团队合作,构建了跨行业的LLM大型语言模型代理。始终如一的是,最成功的实现并没有使用复杂的框架或专门的库。相反,他们使用简单、可组合的模式进行构建。

In this post, we share what we’ve learned from working with our customers and building agents ourselves, and give practical advice for developers on building effective agents.

在这篇文章中,我们将分享我们从与客户合作和构建代理的过程中学到的知识,并为开发者提供构建有效代理的实用建议。

What are agents? 代理是什么?

“Agent” can be defined in several ways. Some customers define agents as fully autonomous systems that operate independently over extended periods, using various tools to accomplish complex tasks. Others use the term to describe more prescriptive implementations that follow predefined workflows. At Anthropic, we categorize all these variations as agentic systems, but draw an important architectural distinction between workflows and agents:

给网站增加显示Mermaid图形能力

网站都是用Markdown书写的,支持Mermaid似乎也在情理之中,下边咱们来实现一下。

对mermaid的支持需要从网站模版层面入手,这样,每次渲染的时候,就会自动渲染mermaid代码。

我们在我们的模版themes/mainroad下新建目录layouts/_default/_markup/,然后新建文件render-codeblock-mermaid.html,内容如下:

<pre class="mermaid">
  {{- .Inner | htmlEscape | safeHTML }}
</pre>
{{ .Page.Store.Set "hasMermaid" true }}

接着,我们在layouts/_default/baseof.html的head标签最后,增加以下代码:

{{ if .Store.Get "hasMermaid" }}
  <script type="module">
    import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';
    mermaid.initialize({ startOnLoad: true });
  </script>
{{ end }}
// 以下代码是为了支持awesome符号
<link
  href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css"
  rel="stylesheet"
/>

我们测试如下:

流程图

flowchart TD
 A[所有工作] -->B(1.申请免费域名)
 A-->D(2.申请Cloudflare账号)
 D-->E(绑定域名到Cloudflare)
 E-->R(创建站点并授权)
 R-->S(设置域名)
 A-->C(3.申请Github账号)
 C-->G(克隆mainroad模版库)
 G-->F(创建仓库myblog)
 F-->H(创建personal access tokens)
 H--settings/developer settings-->J(创建Fine-grained Token)
 J--myblog/settings/secrets and variables-->K(创建Actions secrets<PERSONAL_TOKEN>)
 A-->L(4.Hugo环境)
 L-->M(创建网站)
 M-->N(配置网站)
 N-->P(创建文章)
 P-->Q(配置工作流)
 A-->T(5.Mermaid)
 T-->T1(Font Awesome)

架构图

architecture-beta
    group api(cloud)[API]

    service db(database)[Database] in api
    service disk1(disk)[Storage] in api
    service disk2(disk)[Storage] in api
    service server(server)[Server] in api

    db:L -- R:server
    disk1:T -- B:server
    disk2:T -- B:db

看板

---
config:
  kanban:
    ticketBaseUrl: 'https://mermaidchart.atlassian.net/browse/#TICKET#'
---
kanban
  Todo
    [Create Documentation]
    docs[Create Blog about the new diagram]
  [In progress]
    id6[Create renderer so that it works in all cases. We also add som extra text here for testing purposes. And some more just for the extra flare.]
  id9[Ready for deploy]
    id8[Design grammar]@{ assigned: 'knsv' }
  id10[Ready for test]
    id4[Create parsing tests]@{ ticket: MC-2038, assigned: 'K.Sveidqvist', priority: 'High' }
    id66[last item]@{ priority: 'Very Low', assigned: 'knsv' }
  id11[Done]
    id5[define getData]
    id2[Title of diagram is more than 100 chars when user duplicates diagram with 100 char]@{ ticket: MC-2036, priority: 'Very High'}
    id3[Update DB function]@{ ticket: MC-2037, assigned: knsv, priority: 'High' }

  id12[Can't reproduce]
    id3[Weird flickering in Firefox]

序列图

sequenceDiagram
    actor Alice
    actor Bob
    Alice->>Bob: Hi Bob
    Bob->>Alice: Hi Alice

Hugo + Github Action + Cloudflare 建站指北

先给个指北全图

准备工作

拥有一个免费域名

尽管Cloudflare Pages建站可以提供免费域名,毕竟太长、太难记,一个网站有简单好记的域名非常重要。

咱们已经找好了可以没费注册的域名,它的后缀是us.kg,地址如下:

立即前往注册

申请界面

把域名绑定到Cloudflare

申请注册Cloudflare,进去后,点击网站、添加域,输入申请的域名进行添加。

绑定域名界面

Github账号设置

  1. 申请注册Github账号。

  2. 确定网站模版,并Clone到自己的Github账号下

Hugo的网站去找一个自己心意的网站模版,我以mainroad模版为例。

进入mainroad的Github主页,点击页面右上方的Fork按钮,先复制一份到自己的Github账号。这一步主要是为了自己网站模版的版本管理。

  1. 创建自己网站的仓库

点击右上角的+号,选择创建仓库,设置名字,比如myblog,是私有还是公开都可以,其他默认。

  1. Github自动发布网站的密钥设置

我们在管理网站的时候,希望本地要发布的内容推送到Github,由Github自动编译,这时候我们要给Github授权,这是就是设置授权。

进入Developer settiongs,生成Fine-granted tokens,并复制。

Fine-granted tokens

打开项目myblog,设置Actions secrets,名字是PERSONAL_TOKEN,密码是上一步的token。

personal-token

开始正式建站

以下所有操作基于MacOS,其他操作系统类似,可参考Hugo官方网站。

  1. 安装hugo
brew install Hugo
  1. 创建站点
hugo create site myblog
  1. 配置网站模版

进入myblog目录,前边我们已经fork了mainroad主题,现在,我们把它作为子模块放到主题目录。

cd myblog
git init
git submodule add https://github.com/cryptowww/Mainroad themes/mainroad
  1. 网站设置

打开hugo.toml,进行设置。

baseurl = "/"
title = "巴贝奇的织机"
languageCode = "en-us"
theme = "mainroad"

[services.disqus]
  shortname = "" # Enable Disqus by entering your Disqus shortname
[services.googleAnalytics]
ID = "" # Enable Google Analytics by entering your tracking ID

[Params.author]
  name = "James"
  bio = "Stay Hungry, Stay Foolish."
  avatar = "img/avatar.png" # 要放到mainroad/static/img/下

[Params]
  description = "科技领域的新鲜事"           # Description of your site
  opengraph = true
  twitter_cards = false
  readmore = false                   # Show "Read more" button in list if true
  authorbox = false
  pager = true
  post_meta = ["date", "categories"] # Order of post meta information
  mainSections = ["posts", "docs"]   # 指定哪些文件目录里的文章显示到主页

[Params.logo]
  subtitle = "科技分享"      # Logo subtitle
  image = "img/logo.JPG" # Logo image

[Params.links] # 定义 links 表
  [[Params.links.items]] # 定义链接数组
    name = "Github"
    url = "https://github.com/cryptowww"

[Params.sidebar] # 定义侧边栏显示的位置和内容
  home = "right"   # Configure layout for home page
  list = "right"   # Configure layout for list pages
  single = "right" # Configure layout for single pages
# Enable widgets in given order
  widgets = ["search", "recent", "categories", "taglist", "links"]

[Params.widgets]
  recent_num = 6            # Set the number of articles in the "Recent articles" widget
  tags_counter = true       # Enable counter for each tag in "Tags" widget (disabled by default)
  categories_counter = true # Enable counter for each category in "Categories" widget (disabled by default)

[menus] # 设置两个菜单
  [[menus.main]]
    name = "主页"
    title = "home"
    pageRef = "/"
    weight = 1
  [[menus.main]]
    name = "编程"
    pageRef = "/program"
    weight = 2
  1. 创建文章

使用命令