网站开发中常见的注册界面,网站建立连接不安全怎么解决,做网站深圳,丽水企业网站建设公司在大语言模型#xff08;LLM#xff09;的应用开发中#xff0c;如何让模型具备调用外部工具的能力是一个关键问题。我们不希望模型只是“生成答案”#xff0c;而是能像一个智能体#xff08;Agent#xff09;一样#xff0c;按照推理链条自主决定调用搜索、计算、或数…在大语言模型LLM的应用开发中如何让模型具备调用外部工具的能力是一个关键问题。我们不希望模型只是“生成答案”而是能像一个智能体Agent一样按照推理链条自主决定调用搜索、计算、或数据库查询等工具再结合结果给出最终答案。
本文将通过一段简洁的 Python 代码演示如何实现一个迷你版的 ReAct AgentReasoning Acting。这个智能体能与用户进行交互自动选择调用 Wikipedia 查询、计算器 或 博客搜索 API 来辅助推理并逐步生成最终答案。 1. 背景ReAct 模式与工具调用
ReActReasonAct是一种大模型交互模式流程大致为
Thought模型根据问题思考下一步的策略。Action模型选择一个工具并传入参数。Observation外部环境返回结果。循环模型继续思考并执行下一个动作直到能直接给出最终答案。
这种模式能让 LLM 从“单纯生成”转变为“与环境交互”具备更强的可扩展性。 2. 核心代码结构
我们先来看一段简化的实现
import re
import httpx
from langchain_openai import ChatOpenAIclient ChatOpenAI(base_urlhttps://dashscope.aliyuncs.com/compatible-mode/v1,api_keyyour_secret_key,modelqwen2.5-72b-instruct
)这里我们使用 ChatOpenAI 封装了一个大模型客户端可替换为任意兼容 OpenAI 接口的模型例如 Qwen、GPT-4、Claude 等。
接下来定义了一个 ChatBot 类用于管理消息上下文
class ChatBot:def __init__(self, system):self.system systemself.messages []if self.system:self.messages.append({role: system, content: system})def __call__(self, message):self.messages.append({role: user, content: message})result self.execute()self.messages.append({role: assistant, content: result})return resultdef execute(self):completion client.invoke(inputself.messages)return completion.content关键点
self.messages 保存了完整的对话历史system prompt user prompt assistant response。__call__ 让 ChatBot 实例可以直接作为函数调用便于迭代。每次执行都调用 client.invoke()并把所有上下文交给大模型。 3. Prompt 设计引导 LLM 遵循 ReAct 格式
prompt
You run in a loop of Thought, Action, PAUSE, Observation.
At the end of the loop you output an Answer
Use Thought to describe your thoughts about the question you have been asked.
Use Action to run one of the actions available to you - then return PAUSE.
Observation will be the result of running those actions.Your available actions are:calculate:
e.g. calculate: 4 * 7 / 3
Runs a calculation and returns the number - uses Python so be sure to use floating point syntax if necessarywikipedia:
e.g. wikipedia: Django
Returns a summary from searching Wikipediasimon_blog_search:
e.g. simon_blog_search: Django
Search Simons blog for that termAlways look things up on Wikipedia if you have the opportunity to do so.Example session:Question: What is the capital of France?
Thought: I should look up France on Wikipedia
Action: wikipedia: France
PAUSEYou will be called again with this:Observation: France is a country. The capital is Paris.You then output:Answer: The capital of France is Paris
.strip()这段 system prompt 明确规定了交互格式
模型必须先写 Thought。如果需要调用工具则写 Action: 工具名: 参数然后返回 PAUSE。工具执行结果会以 Observation: ... 的形式喂回给模型。最后模型才能输出 Answer:。
通过严格约束我们让模型进入一个 循环推理-调用-观察 的流程。 4. 动作解析与执行
利用正则表达式匹配模型输出中的 Action
action_re re.compile(^Action: (\w): (.*)$)在主循环 query() 里
def query(question, max_turns5):i 0bot ChatBot(prompt)next_prompt questionwhile i max_turns:i 1result bot(next_prompt)print(result)actions [action_re.match(a) for a in result.split(\n) if action_re.match(a)]if actions:# There is an action to runaction, action_input actions[0].groups()if action not in known_actions:raise Exception(Unknown action: {}: {}.format(action, action_input))print( -- running {} {}.format(action, action_input))observation known_actions[action](action_input)print(Observation:, observation)next_prompt Observation: {}.format(observation)else:return这里的逻辑是
把用户问题送进模型获取输出。如果输出里有 Action则调用对应工具函数。把工具的结果作为 Observation 再送回模型。如果模型直接输出 Answer就结束循环。 5. 工具实现
目前实现了三个工具
def wikipedia(q):return httpx.get(https://en.wikipedia.org/w/api.php, params{action: query,list: search,srsearch: q,format: json}).json()[query][search][0][snippet]def simon_blog_search(q):results httpx.get(https://datasette.simonwillison.net/simonwillisonblog.json, params{sql: selectblog_entry.title || : || substr(html_strip_tags(blog_entry.body), 0, 1000) as text,blog_entry.createdfromblog_entry join blog_entry_fts on blog_entry.rowid blog_entry_fts.rowidwhereblog_entry_fts match escape_fts(:q)order byblog_entry_fts.ranklimit1.strip(),_shape: array,q: q,}).json()return results[0][text]def calculate(what):return eval(what)known_actions {wikipedia: wikipedia,calculate: calculate,simon_blog_search: simon_blog_search
}Wikipedia通过官方 API 获取搜索摘要。Simon Blog Search调用 Simon Willison 的博客 API 进行全文检索。Calculate直接用 Python eval() 计算表达式仅演示实际生产中要做安全防护。 6. 效果演示
执行
query(What does India share borders with?)可得到以下推理过程
Thought: To answer this question, I need to look up information about Indias geography and its neighboring countries on Wikipedia.
Action: wikipedia: India
PAUSE-- running wikipedia India
Observation: span classsearchmatchIndia/span, officially the Republic of span classsearchmatchIndia/span, is a country in South Asia. It is the seventh-largest country by area; the most populous country since 2023;
Thought: The provided excerpt does not contain the specific information about the countries that share borders with India. I need to refine my search to get more detailed geographical information.
Action: wikipedia: Geography of India
PAUSE-- running wikipedia Geography of India
Observation: span classsearchmatchOf/span span classsearchmatchIndia/span. 2007. ISBN 978-81-230-1423-4. Wikimedia Commons has media related to span classsearchmatchGeography/span span classsearchmatchof/span span classsearchmatchIndia/span. Singh, R.L. (1971). span classsearchmatchIndia/span A Regional span classsearchmatchGeography/span. National
Thought: The current observation still does not provide the specific information about the countries that share borders with India. I will try a more direct search to find this information.
Action: wikipedia: Borders of India
PAUSE-- running wikipedia Borders of India
Observation: The Republic span classsearchmatchof/span span classsearchmatchIndia/span shares span classsearchmatchborders/span with several sovereign countries; it shares land span classsearchmatchborders/span with China, Bhutan, Nepal, Pakistan, Bangladesh, and Myanmar
Answer: India shares borders with China, Bhutan, Nepal, Pakistan, Bangladesh, and Myanmar.可以看到模型先思考再调用 Wikipedia API拿到结果后生成最终答案。 7. 可扩展的方向
这个简单的 Demo 展示了 ReAct 智能体的核心循环。在实际应用中读者朋友们可以进一步扩展
增加更多工具如数据库查询、文件系统、搜索引擎、第三方 API 等。错误处理对 eval() 和网络请求增加异常捕获和安全限制。并行工具调用让模型一次调用多个工具合并结果后继续推理。LangChain/LangGraph 集成结合更强的智能体框架实现任务规划、子任务拆解与状态管理。 8. 总结
通过不到 200 行代码我们实现了一个简洁的 ReAct 风格智能体。它展示了以下关键点
利用 system prompt 约束 LLM 输出格式。通过 Action/Observation 循环 让模型与外部环境交互。把 工具调用 抽象成函数方便扩展和维护。
这类模式是构建 大模型智能体 的核心思路未来读者朋友们可以在此基础上扩展成更强大的多工具、多任务智能体。