Lazy loaded image
Gemini CLI
Words 5979Read Time 15 min
2026-1-13
上次编辑时间
Jan 13, 2026 08:04 AM
创建时间
Jan 13, 2026 08:04 AM
多选
实践
选择
老王
💡
网络环境需要开TUN

Gemini Cli环境配置

  1. Cited:
    1. articles.zsxq.com
    2. 一段提示词 让Gemini CLI变成自动化Agent![提示词工程]_哔哩哔哩_bilibili
  1. 环境配置
  1. 应用,那个技巧:笔记本记录
process:
prompt1
输入:文章URL:https://blog.langchain.com/how-to-build-an-agent/ 输出:格式化后的原文article.md;中英文双语版article-en-cn.md;中文版article-cn.md;文章中所有的图片资源
prompt2
输入: 文章URL:https://blog.langchain.com/how-to-build-an-agent/
输出:
  1. 格式化后的原文article.md
  1. 中英文双语版article-en-cn.md
  1. 中文版article-cn.md
  1. 文章中所有的图片资源
步骤1: 访问网站
  • 访问上文输入中网址
  • 必须使用 "lynx -dump -image_links URL" 命令访问网站
  • 网站内容保存在raw.txt中
步骤2:下载图片
  • 从raw.txt中提取文章相关图片链接
  • 逐一下载到 resources/ 文件夹
  • 你必须使用curl命令进行下载
步骤3:改写成markdown
  • 把raw.txt改写成markdown格式
  • 保存在article.md中
  • 将article.md中的图片链接指向 resources/ 文件夹
步骤4:翻译成中英文
  • 把article.md翻译成中英文对照
  • 保存在article-en-cn.md中
步骤5:翻译成中文
  • 提取article-en-cn.md中的中文
  • 保存在article-cn.md中
prompt3(lynx安装需要注册,还得添加到path中,或者安装Linux虚拟机环境。这是替代方案,主要将lynx的工作转化为一个powershell脚本。)

输入

输出

  1. 图片资源链接links.txt
  1. 一个用于下载所有图片资源的 PowerShell 脚本。
  1. 文章中所有的图片资源。
  1. 格式化后的原文 article.md
  1. 中英文双语版 article-en-cn.md
  1. 中文版 article-cn.md

任务步骤

步骤1:内容提取与资源分析
  1. 直接访问并读取输入的 URL 内容。
  1. 解析文章的主体文本内容。
  1. 提取文章中所有相关的图片链接(URLs),并列出来,保存在links.txt中。
步骤2:生成图片下载脚本并执行
  1. 根据步骤1中提取的图片链接列表,生成一个 PowerShell 脚本
  1. 该脚本需要包含以下逻辑:
      • 首先检查是否存在名为 "resources" 的文件夹,如果不存在则创建它。
      • 对于列表中的每一个图片 URL,使用命令逐一将其下载到 "resources" 文件夹中。
      • 保持图片原有的文件名。
  1. 执行该脚本。
步骤3:创建原文 Markdown (article.md)
  1. 将步骤1中解析的文章主体文本转换成格式良好的 Markdown。
  1. 在 Markdown 中,将所有原始的图片链接替换为指向本地 "resources" 文件夹的相对路径。
步骤4:创建双语版 Markdown (article-en-cn.md)
  1. article.md 的内容进行翻译,生成一段英文、一段中文的对照格式。
步骤5:创建中文版 Markdown (article-cn.md)
  1. article-en-cn.md 中只提取所有中文内容。
  1. 保持原有的 Markdown 格式。
请严格按照以上步骤顺序执行,并清晰地呈现每一个输出部分。
prompt4
输入:
文章URL:https://blog.langchain.com/how-to-build-an-agent 输出:
  1. 格式化后的原文raw.md
  1. 图片链接地址合集pic.txt
  1. 中英文双语版article-en-cn.md
  1. 中文版article-cn.md
  1. 文章中所有的图片资源
每完成一步,都必须更新progress.md 步骤0: 生成笔记
仿照例子和当前任务生成笔记 progress.md 步骤1:深度内容提取与图片链接分析
  1. 访问并获取输入的 URL 的完整 HTML 源代码;
  1. 解析文章的主体文本内容,包含文本内容以及嵌入的图片链接;
  1. 将提取到所有图片链接,以 Markdown 列表的格式清晰地展示出来,到pic.txt中
  1. 将文本(包括图片链接地址)保存到raw.md中。
 
步骤2:下载图片
  1. 从pic.txt中提取文章相关图片链接; 把图片链接写入 progress.md;
  1. 创建resources文件夹;
  1. 逐一下载到 resources文件夹;
  1. 每下载完成一个图片,必须更新图片下载进度。
步骤3:改写成markdown 将raw.md中的图片链接指向 resources文件夹。 步骤4:翻译成中英文
  1. 把raw.md翻译成中英文对照;
  1. 保存在article-en-cn.md中。
步骤5:翻译成中文
  1. 提取article-en-cn.md中的中文;
  1. 保存在article-cn.md中。
---- progress.md 笔记格式 ## 任务 [x] xxxxx [ ] yyyyy [ ] zzzzz ... ## 图片下载进度 [x] https://xxxx/yyy.png [ ] https://foo/bar.png ... ## 当前任务 正在下载https://foo/bar.png
 
 
 

难点

  1. lynx捕捉渲染后纯文本txt,用于提取正文和链接的模块,寻找替代方案
  1. Gemini cli调用的web fetch的局限性:
    1. web fetch
      好的,我们来深入解析一下 Gemini CLI 中非常重要但又经常被误解的一个核心功能:web fetch
      你之前的提问(关于 lynx 和图片链接提取)实际上都与这个功能的特性和局限性息
      息相关。

      1. web fetch 是什么?

      web fetch 并不是一个你可以直接在命令行里输入的独立命令,而是 Gemini CLI 内置的一个自动化工具(Tool)或能力
      当你向 Gemini 提出的提示词(Prompt)中包含一个 URL 时,Gemini 的“思考”过程如下:
      1. 识别意图:模型识别出你需要它处理一个来自网络的信息。
      1. 调用工具:为了获取这个信息,它会自动在后台调用 web fetch 这个内部工具。
      1. 获取内容web fetch 工具向你提供的 URL 发出一个服务器端的 HTTP GET 请求,获取网页的原始响应内容(主要是 HTML 源代码)。
      1. 返回给模型web fetch 将获取到的原始文本内容作为上下文(Context)信息,返回给 Gemini 模型。
      1. 处理并响应:Gemini 模型基于这些新获取的上下文,结合你的原始指令,最终生成给你的答案。
      关键点:这一切都发生在 Google 的服务器上,而不是在你的本地计算机上。Gemini CLI 只是你与 Google 服务器上的模型进行交互的“窗口”。

      2. 它的工作原理(一个简单的比喻)

      想象一下,Gemini 是一个坐在封闭房间里的超级大脑。
      • 你(用户)通过对讲机(Gemini CLI)对他说:“请帮我阅读并总结一下 http://example.com/news 这篇文章。”
      • Gemini 无法亲自出门,但它有一个机器人助手(web fetch)。
      • Gemini 对助手说:“去 http://example.com/news 这个地址,把那里所有的文字材料都抄回来给我。”
      • 机器人助手出发,到达目的地,把网页的**原始设计图纸(HTML 源代码)**抄写下来,然后跑回来交给 Gemini。
      • Gemini 看着这份满是代码的图纸,开始阅读和理解,并最终通过对讲机把总结好的内容告诉你。

      3. 主要功能与优势

      • 获取实时信息:这是 web fetch 最大的优势。它让 Gemini 能够获取最新的网络信息,而不是仅仅依赖于其训练数据的“知识截止日期”。
      • 处理文本内容:对于结构简单、以文本为主的网站(如博客文章、新闻、维基百科、文档),web fetch 的效果非常好。
      • 理解基本结构:它可以解析基础的 HTML 标签(如 <h1> 标题, <p> 段落, <a> 链接),从而理解文章的基本大纲和结构。
      • 无缝集成:作为用户,你无需关心背后的技术细节,只需在提示词中提供 URL 即可,使用体验非常流畅。

      4. 重要的局限性(你之前遇到问题的根源)

      理解 web fetch 的局限性至关重要,这能解释为什么它有时会“失败”。
      1. 不执行 JavaScript (No JavaScript Execution)
          • 这是最核心的局限。机器人助手只会“抄写”原始的 HTML 代码,它不会像你的 Chrome 浏览器那样去运行页面中的 JavaScript
          • 现代网站大量使用 JS 来动态加载内容(如评论、图片库、交互式图表)。web fetch 获取的 HTML 里可能只有加载这些内容的“占位符”或脚本代码,而没有最终呈现的内容。这就是为什么它经常抓取不到动态加载的图片链接。
      1. 无法处理需要登录或 Cookie 的页面
          • web fetch 的每次访问都是一次匿名的、无状态的请求。它无法登录你的账户,也无法携带你在浏览器中保存的 Cookie。因此,任何需要登录才能查看的内容(如社交媒体、订阅内容)都无法获取。
      1. 可能被反爬虫机制阻止
          • 许多网站(特别是大型商业网站)部署了 Cloudflare 等安全服务,用于检测和阻止自动化程序(机器人)的访问。web fetch 的请求很容易被识别为机器人,从而被阻止,或者返回一个验证页面(CAPTCHA)而不是真实内容。
      1. 无法进行页面交互
          • 它不能像人一样点击“加载更多”按钮、滚动页面以触发“无限滚动”加载,或填写表单。它只能获取初始加载的静态页面内容。

      5. 如何在提示词中有效利用 web fetch

      • 明确目标:尽量提供指向静态内容页面的直接链接,而不是需要大量交互的网站主页。
      • 引导解析:在提示词中可以引导模型如何处理获取到的内容。例如:“访问此 URL,专注于解析 <article> 标签内的文本内容,并将其总结成三点。”
      • 预判失败:当处理复杂网站时,可以在提示词中加入备用方案。例如:“请从这个 URL 提取所有图片链接。如果因为动态加载无法找到,请明确告诉我你获取到的原始 HTML 中不包含直接的图片链接。”

      总结

      功能
      web fetch 是...
      web fetch 不是...
      角色
      一个服务器端的、自动化的原始 HTML 获取器
      一个在你本地运行的命令行工具(如 curl, lynx)。
      能力
      能读取静态文本和解析基本 HTML 结构。
      一个能执行 JavaScript 的完整浏览器(如 Chrome)。
      状态
      匿名的、一次性的访问者。
      一个能登录、能保持会话状态的用户。
      交互
      只能进行一次性的内容获取。
      能点击按钮、滚动页面、与网页交互的工具。
      理解了 web fetch 的本质,你就能更好地设计你的提示词,并知道何时它的能力足够,何时需要借助更专业的工具(如我们之前讨论的 Python 脚本)来完成任务。

解决方案1

编写一个python脚本,进行“数字内容归档”
使用 Python 编写一个自动化脚本,模拟浏览器行为加载页面,然后解析内容,转换格式,下载文件,并重写链接。
新问题:需要引入新的python库,臃肿了
 
 

解决方案2

  1. web fecth抓取静态的html网页,从中提取两个内容:正文文本,图片链接
  1. 由llm进行拆分
行不通,web fetch抓取内容不能控制为html源码
 

解决方案3

归根结底,还是环境配置问题,换到Gemini AI Studio中试试
不成,studio中的build本质上是虚拟机没错,但有默认的system prompt,是噪音干扰。
prompt
输入: 文章URL:https://blog.langchain.com/how-to-build-an-agent
输出:
  1. 格式化后的原文article.md
  1. 中英文双语版article-en-cn.md
  1. 中文版article-cn.md
  1. 文章中所有的图片资源
每完成一步,都必须更新progress.md
步骤0: 生成笔记
  • 仿照例子和当前任务生成笔记 progress.md
步骤1: 访问网站
  • 访问上文输入中网址
  • 必须使用 "lynx -dump -image_links URL"命令访问网站
  • 网站内容保存在raw.txt中
步骤2:下载图片
  • 从raw.txt中提取文章相关图片链接
  • 把图片链接写入 progress.md
  • 逐一下载到 resources/ 文件夹
  • 每下载完成一个图片,必须更新图片下载进度
  • 你必须使用curl命令进行下载
步骤3:改写成markdown
  • 把raw.txt改写成markdown格式
  • 保存在article.md中
  • 将article.md中的图片链接指向 resources/ 文件夹
步骤4:翻译成中英文
  • 把article.md翻译成中英文对照
  • 保存在article-en-cn.md中
步骤5:翻译成中文
  • 提取article-en-cn.md中的中文
  • 保存在article-cn.md中

progress.md 笔记格式

任务

xxxxx yyyyy zzzzz ...

图片下载进度

当前任务

正在下载https://foo/bar.png
gemini studio one word app
n

How to build an Agent

Introduction

Agents are one of the most powerful and unique capabilities of LangChain. They allow language models to use tools to interact with their environment. In this blog post, we'll walk through a guide for building an agent from scratch.

What is an agent?

The core idea of an agent is to use an LLM to choose a sequence of actions to take. In a LangChain agent, these actions can be:
  1. Using a Tool: Performing an action with an external source of data or computation (e.g., Google Search, a Python REPL, a SQL database).
  1. Responding to the User: If the agent has completed its task or cannot make any more progress, it can respond to the user.

Why use an Agent?

Agents offer several benefits over a traditional LLM chain (which performs a predefined sequence of steps):
  • Flexibility: Agents can adapt to diverse and unpredictable inputs by dynamically choosing actions.
  • Problem-solving: They can break down complex problems into smaller, manageable steps, addressing each using appropriate tools.
  • Up-to-date information: By using search tools, agents can access current information, overcoming the knowledge cutoff of their training data.
  • Computation: Agents can perform calculations using tools like a Python interpreter or a calculator, going beyond the LLM's raw mathematical abilities.
  • External Interaction: They can interact with external APIs, databases, or files, extending their capabilities far beyond what an LLM alone can do.

Quickstart

Let's begin by building a simple agent that can use Google Search to answer questions.

Setup

We'll also need to set environment variables for our LLM (OpenAI) and our search tool (Tavily).

Tools

We'll give our agent a search tool to help it answer questions.

LLM

We'll use a chat model to power our agent. Any BaseChatModel will work, but we'll use OpenAI's gpt-4o in this example.

Agent

Now we can create our agent. We'll use the create_react_agent function, which creates an agent that uses the ReAct framework. The ReAct framework combines reasoning and acting, allowing the agent to deliberate on its next action and then execute it.
This function takes an LLM, a list of tools, and a prompt. It returns an AgentExecutor which is a Runnable that can be invoked with an input and a list of chat_history.
Now we can invoke our agent with a question:
The agent uses the tavily_search_results_json tool to search for "capital of France" and then responds with the answer. The verbose=True argument in AgentExecutor allows us to see the agent's thought process.

Under the Hood

Let's take a closer look at what's happening when we run an agent.

Prompt

The prompt is crucial for guiding the LLM to act as an agent. The prompt we pulled from the LangChain Hub is designed for the ReAct framework. It includes instructions for how the LLM should reason and act, including:
  • Tools available: A list of tools the agent can use, along with their descriptions.
  • Thought-Action-Observation loop: The structure for the agent's reasoning process, where it thinks, chooses an action, observes the result, and repeats.
  • Stopping condition: When and how the agent should respond to the user.
Let's print the prompt to see what it looks like:
The prompt contains several placeholders:
  • tools: This will be populated with the names and descriptions of the tools provided to the agent.
  • tool_names: A comma-separated string of the tool names.
  • input: The user's input question.
  • agent_scratchpad: This is where the agent's thoughts, actions, and observations are stored, forming the history of the current interaction.
The create_react_agent function automatically formats these placeholders based on the tools and llm we provide.

Tool usage

When the agent invokes a tool, the tool's name and input are sent to the LLM. The LLM then receives the output of the tool as an observation. This process continues until the LLM determines it has enough information to provide a Final Answer.
For example, when we asked "What is the capital of France?", the agent performed the following steps:
  1. Thought: "I need to find out the capital of France. I can use the search tool for this."
  1. Action: tavily_search_results_json
  1. Action Input: {'query': 'capital of France'}
  1. Observation: The search results containing the information about Paris.
  1. Thought: "I now know the final answer"
  1. Final Answer: "The capital of France is Paris."
This iterative process of thinking, acting, and observing allows the agent to dynamically explore and solve problems.

Advanced Usage

Adding more tools

Let's add a second tool, a Python REPL, to our agent. This will allow the agent to perform calculations and execute Python code.
Now our agent can use both search and Arxiv. Let's ask it a question that might require Arxiv:
The agent successfully used the arxiv tool to retrieve the summary of the paper and then provided the main idea.

Handling chat history

Agents can also maintain chat history, allowing for multi-turn conversations. To do this, we need to pass a chat_history argument to the agent executor.
The agent still correctly answers the question, demonstrating that it can handle chat history while performing its task. Note that the specific prompt we're using (hwchase17/react) doesn't explicitly use the chat_history for its Thought process in the same way a conversational agent might. For more advanced conversational agents that can leverage history for follow-up questions or context, you might want to use a different agent type or a prompt specifically designed for conversational agents, such as conversational-react-agent.

Streaming

Agent executors can stream their intermediate steps, allowing you to build more interactive user interfaces. To enable streaming, we can use the stream_events method on the AgentExecutor.
The stream_events method yields dictionaries containing information about the agent's actions, tool outputs, and final output, allowing you to process each step as it happens.

Conclusion

Agents are a powerful feature in LangChain that allow language models to interact with their environment using tools. In this guide, we've covered the basics of building an agent, adding multiple tools, handling chat history, and streaming intermediate steps. By combining the reasoning capabilities of LLMs with external tools, agents can solve complex problems and adapt to various tasks, making them a versatile and essential component in many AI applications.

Next Steps

Here are a few suggestions for further exploration:
  • Custom Tools: Learn how to create your own custom tools to extend your agent's capabilities.
  • Other Agent Types: Explore other agent types like conversational-react-agent for more complex chat interactions or OpenAIFunctionsAgent for agents that leverage OpenAI's function calling capabilities.
  • Advanced Prompt Engineering: Experiment with different prompt engineering techniques to optimize your agent's performance and behavior.
  • Error Handling: Implement robust error handling mechanisms to make your agents more resilient to unexpected situations.
  • Human-in-the-Loop Agents: Explore how to incorporate human feedback and intervention into your agent's decision-making process.
  • Agent Memory: Give your agent long-term memory to remember past interactions and learn from experience.

记笔记

一个笔记文件,相当于few shot的例子。用于在llm执行输出中不断更新任务完成状态。
💡
压缩上下文,补充记忆,将压缩信息(笔记中规划的步骤完成内容)放到尾部加权重,以减轻噪声影响。
  1. 将prompt打包包装为可执行命令文件,比如此处的功能实际就是翻译,打包为translate.toml,将目标网址替换为变量占位符。是一个简易的agent结构。
 

主要信息

  1. 解决执行不可控问题:规划好需求,给出步骤化提示词;
  1. 解决上下文过长的记忆问题:让AI记笔记;
  1. 开发工具上,Linux环境更方便;
 
上一篇
Game
下一篇
The secret life of pet