0Pricing
Prompt Engineering & LLM Optimization for Developers · 课时

构建简单的 LLM 智能体

使用 LLM 和外部工具创建能够推理、规划并执行多步骤任务的基础自主智能体。

构建简单的 LLM 智能体 是 CoddyKit 上的免费 Prompt Engineering & LLM Optimization for Developers 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Prompt Engineering & LLM Optimization for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Prompt Engineering & LLM Optimization for Developers 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What are LLM Agents?

Welcome! In this lesson, we'll dive into LLM agents. An agent is an LLM that can make decisions, take actions, and achieve goals by interacting with its environment.

Think of it as an LLM with a brain and hands. The "brain" is the LLM for reasoning, and the "hands" are tools it can use to perform tasks.

Agent's Core Components

Every LLM agent has key parts working together to enable its autonomous behavior:

  • LLM (The Brain): The core language model for reasoning, understanding, and decision-making.
  • Memory (State): Where the agent stores past interactions, observations, or thoughts to maintain context.
  • Tools (Actions): External functions or APIs the agent can call to perform specific tasks.
  • Planner (Reasoning): The LLM's ability to break down a complex goal into smaller, manageable steps.

How Agents Work: The Loop

Agents operate in a continuous cycle, often called the Agentic Loop. This loop allows them to adapt and progress towards a goal:

  1. Observe: The agent receives new input (e.g., user query) or checks its environment.
  2. Think: The LLM processes observations, plans next steps, decides if a tool is needed, and what to do.
  3. Act: The agent executes a chosen tool (if applicable) or generates a direct response.

This loop repeats until the goal is achieved or no further action is required.

Tools: Extending Capabilities

You've learned about Function Calling and Tool Use. For an agent, tools are absolutely crucial!

They allow the LLM to go beyond just generating text. Tools give agents the ability to *do* things in the real world, such as:

  • Search the web for up-to-date information.
  • Access databases or read files.
  • Perform calculations or data analysis.
  • Send emails, interact with APIs, or control other software.

Agent Decides to Use a Tool

Imagine an agent needs to find the current weather. It doesn't know this directly, but it's aware it has a 'get_weather' tool.

Here's a simplified view of its internal thought process, demonstrating the 'Think' step leading to an 'Act' step:

User input: "What's the weather in London?"

Agent thought process:
1.  Observe: User wants current weather info.
2.  Think: My knowledge is limited to my training data. I have a 'get_weather' tool that can provide this.
    I need to use 'get_weather' with the location "London".
3.  Act: Call get_weather("London").
    (Tool executes and returns result: "Sunny, 20°C")
4.  Think: I have the weather data. Now, I should present it clearly to the user.
5.  Act: "The weather in London is Sunny, 20°C."

Putting it Together: A Simple Agent

To build a simple agent, you primarily instruct the LLM to:

  • Understand its Role: Define its persona (e.g., "You are a helpful assistant").
  • Know Available Tools: Provide a clear list of tools it can use, including their names, descriptions, and how to call them.
  • Decide & Act: Empower it to decide when and how to use those tools based on the user's request.

The LLM's inherent reasoning capability drives the entire process, using tools when its internal knowledge isn't sufficient.

Prompting Your Agent

The prompt is absolutely key to an agent's behavior. It acts as the agent's initial programming. You need to clearly define:

  • Its Persona: "You are a friendly travel agent helping users plan trips."
  • Its Goal: "Your main goal is to assist users in finding flights, hotels, and local attractions."
  • Available Tools: Describe each tool, its purpose, and the exact syntax for calling it (e.g., search_flights(destination, date)).
  • Decision Process: "Always think step-by-step before deciding on an action or generating a response."

This comprehensive prompt guides the LLM to act as an effective agent.

Agent Scenario: Trip Planner

Let's consider a simple "Trip Planner" agent. This agent has access to tools like get_flights(destination, date), get_hotels(city, dates), and get_attractions(city).

User: "Plan a weekend trip to Paris next month."

The agent would follow its loop:

  1. Think: The user wants a trip plan. I need flights, hotels, and attractions for Paris.
  2. Act (Tool): Call get_flights("Paris", "next month").
  3. Act (Tool): Call get_hotels("Paris", "next month").
  4. Act (Tool): Call get_attractions("Paris").
  5. Think & Act (Response): Combine all results into a coherent trip plan for the user.

Simple Agent Challenges

While powerful, simple agents, especially with minimal prompting, have limitations:

  • Hallucinations: They might invent tool calls, parameters, or facts.
  • Poor Planning: Can struggle with highly complex, multi-step reasoning or recovery from errors.
  • Cost & Latency: Each "think" and "act" step typically involves an LLM API call, increasing cost and response time.
  • Prompt Sensitivity: Small changes in instructions can sometimes drastically alter agent behavior.

These are common challenges that more advanced agentic patterns and frameworks aim to address.

Agent Components Check

An LLM agent combines several elements to achieve its goals. Which of the following are essential components of an LLM agent?

Recap: Building Simple Agents

Great job! You've learned the fundamentals of building simple LLM agents.

  • Agents are LLMs that can reason, plan, and act autonomously.
  • They are composed of an LLM, memory, and tools.
  • Agents operate in a continuous Observe-Think-Act loop.
  • Effective prompting is crucial for defining an agent's persona, goals, and available tools.

While powerful, simple agents have limitations. In future lessons, we'll explore more advanced agentic patterns and frameworks to build even more capable applications.

常见问题解答

「构建简单的 LLM 智能体」课时是免费的吗?

是的 — 「构建简单的 LLM 智能体」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Prompt Engineering & LLM Optimization for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Prompt Engineering & LLM Optimization for Developers 课程共包含 4 节课。

「构建简单的 LLM 智能体」这节课中我会学到什么?

使用 LLM 和外部工具创建能够推理、规划并执行多步骤任务的基础自主智能体。 你通过在浏览器中直接运行的动手代码来练习 Prompt Engineering & LLM Optimization for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Prompt Engineering & LLM Optimization for Developers 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Prompt Engineering & LLM Optimization for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「构建简单的 LLM 智能体」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Prompt Engineering & LLM Optimization for Developers 课中编写并运行代码吗?

能。每节 Prompt Engineering & LLM Optimization for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 检索增强生成(RAG)
  2. 函数调用与工具使用
  3. 构建简单的 LLM 智能体
  4. 向用户流式传输 LLM 响应
← 返回 Prompt Engineering & LLM Optimization for Developers