Skip to main content

Agent-Loop工作原理Demo

·2 mins

AI Agent 工作原理演示 Demo #

本示例用于演示 AI Agent 与大模型之间的循环交互(Agent Loop)。 场景:用户希望规划一个周末去杭州的旅行。

为了让流程更真实,Agent 具有多个工具(即使有些不会用到)。

可用工具列表 #

工具 说明


weather_api(city) 查询城市天气 flight_search(from, to, date) 查询机票 hotel_search(city, date) 查询酒店 map_distance(from, to) 查询距离 restaurant_search(city, type) 查询餐厅推荐


0 Agent 初始化 #

System Prompt #

You are TravelAgent.

Your goal:
Help the user plan a short weekend trip.

Rules:
1. Think step by step.
2. If external information is required, call tools.
3. Use XML format for tool calls.
4. When the task is complete output <END>.

Available tools:

weather_api(city)
flight_search(from, to, date)
hotel_search(city, date)
map_distance(from, to)
restaurant_search(city, type)

1 用户提出需求 #

User:

我想这周末去杭州玩两天,帮我规划一下

2 Agent 第一次调用模型 #

发送给模型:

<SystemPrompt>
You are TravelAgent.
Tools:
weather_api
flight_search
hotel_search
map_distance
restaurant_search
</SystemPrompt>

<User>
我想这周末去杭州玩两天,帮我规划一下
</User>

3 模型第一次响应 #

模型思考:

<thought>
To plan a trip I first need to know where the user is traveling from.
</thought>

<ask_user>
请问你从哪个城市出发?
</ask_user>

4 Agent 请求用户参与 #

Agent 输出:

请问你从哪个城市出发?

5 用户提供信息 #

User:

上海

6 Agent 第二次调用模型 #

<SystemPrompt>
You are TravelAgent
</SystemPrompt>

<Conversation>

User:
我想这周末去杭州玩两天

Assistant:
请问你从哪个城市出发?

User:
上海

</Conversation>

7 模型第二次响应(调用工具) #

模型决定查询天气:

<thought>
Check the weather in Hangzhou to determine travel conditions.
</thought>

<tool_call>
<weather_api>
<city>杭州</city>
</weather_api>
</tool_call>

8 Agent 执行工具 #

Agent 解析 XML 并调用工具:

weather_api("杭州")

工具返回结果:

city: 杭州
temperature: 22°C
weather: 多云
humidity: 60%

9 Agent 将工具结果加入上下文 #

再次调用模型:

<SystemPrompt>
You are TravelAgent
</SystemPrompt>

<ToolResult>

city: 杭州
temperature: 22°C
weather: 多云
humidity: 60%

</ToolResult>

<User>
我想这周末去杭州玩两天
</User>

10 模型第三次响应(再次调用工具) #

模型认为需要查询交通:

<thought>
User is traveling from Shanghai to Hangzhou.
Check distance to estimate travel plan.
</thought>

<tool_call>
<map_distance>
<from>上海</from>
<to>杭州</to>
</map_distance>
</tool_call>

11 Agent 执行工具 #

调用:

map_distance("上海","杭州")

返回:

distance: 170km
transport: 高铁约1小时

12 Agent 再次调用模型 #

<ToolResult>

distance: 170km
transport: 高铁约1小时

</ToolResult>

13 模型生成最终计划 #

<thought>
Distance is short and weather is good.
A weekend trip plan can be generated.
</thought>

<final_answer>

周末杭州两日游建议:

Day1
- 从上海乘高铁前往杭州(约1小时)
- 游览西湖
- 晚上逛河坊街

Day2
- 灵隐寺
- 龙井村
- 下午返回上海

天气:22°C 多云,适合出行。

</final_answer>

<END/>

14 Agent 发现 END 标志 #

Agent 逻辑:

if END:
    return final_answer

最终返回给用户:

周末杭州两日游建议:

Day1
- 高铁上海 → 杭州
- 西湖
- 河坊街

Day2
- 灵隐寺
- 龙井村

Agent Loop 工作原理 #

核心循环:

while True:

    response = LLM(context)

    if tool_call:
        result = run_tool()
        context.append(result)

    elif ask_user:
        input = human()
        context.append(input)

    elif END:
        break

总结 #

AI Agent =

LLM
+
工具系统
+
循环控制(Agent Loop)

流程:

用户输入
↓
LLM 思考
↓
调用工具
↓
Agent 执行工具
↓
结果返回给 LLM
↓
继续推理
↓
直到 <END>