当前位置: 首页 > news >正文

Fake LLM in LangChain (one)

https://python.langchain.com.cn/docs/modules/model_io/models/llms/how_to/fake_llm

Fake LLM in LangChain

This content is based on LangChain’s official documentation (langchain.com.cn) and explains FakeLLM—a simulated LLM class for testing—in simplified terms. It strictly preserves all original source codes, examples, and knowledge points without any additions or modifications.

1. What is FakeLLM?

FakeLLM is a mock LLM class designed for testing purposes.

  • It simulates LLM calls without connecting to a real LLM API.
  • You predefine a list of responses, and FakeLLM returns them in order when called.
  • It’s useful for testing workflows (like agents) that rely on LLMs, without incurring API costs or waiting for real responses.

The example uses FakeListLLM (a subclass of FakeLLM) to simulate an LLM that guides an agent to calculate “2 + 2”.

2. Step 1: Import Required Modules

The code below imports all necessary LangChain classes—exactly as in the original documentation:

from langchain.llms.fake import FakeListLLM
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType

3. Step 2: Prepare Tools and Predefined Responses

Step 3.1: Load Tools

We load the python_repl tool (used to execute Python code) for the agent:

tools = load_tools(["python_repl"])

Step 3.2: Define Predefined Responses

We create a list of responses that FakeListLLM will return sequentially. These responses guide the agent to use the Python REPL and return the final answer:

responses = ["Action: Python REPL\nAction Input: print(2 + 2)", "Final Answer: 4"]

4. Step 3: Initialize FakeListLLM

Create an instance of FakeListLLM and pass the predefined responses list:

llm = FakeListLLM(responses=responses)

5. Step 4: Initialize the Agent

Combine the fake LLM, tools, and agent type to create an agent. The code is identical to the original:

agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)

6. Step 5: Run the Agent

Test the agent with the query “whats 2 + 2”. The fake LLM will return the predefined responses, guiding the agent to calculate the result.

Code:

agent.run("whats 2 + 2")

Output (exact as original, including formatting and color codes):

[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3mAction: Python REPL
Action Input: print(2 + 2)[0m
Observation: [36;1m[1;3m4
[0m
Thought:[32;1m[1;3mFinal Answer: 4[0m
[1m> Finished chain.[0m
'4'

Key Takeaway

  • FakeListLLM uses predefined responses to simulate LLM behavior.
  • It’s ideal for testing LLM-dependent workflows (like agents) quickly and cost-free.
  • The agent follows the fake LLM’s guided responses to complete the task (e.g., using Python REPL to calculate 2+2).
http://www.dtcms.com/a/581630.html

相关文章:

  • 文案写作网站网站策划需要具备什么
  • JavaSE知识分享——Object类小结
  • GitHub 热榜项目 - 日榜(2025-11-07)
  • 数字人技术:虚实交融的未来图景正在展开
  • 网站301检测烟台网站建设找企汇互联专业
  • TCP 的三次握手和四次挥手
  • 多平台视频下载工具的实现原理与技术实践, 免费下载视频下载工具
  • R MySQL 连接
  • FPGA——定点数的表示
  • PostgreSQL 子查询
  • AIStarter跨平台完工:Win/Mac/Linux一键部署Stable Diffusion
  • 李宏毅机器学习笔记42
  • 珠海市网络营销协会的官方网站制作网页网站用的是什么
  • SwinPaste: 一种基于Swin Transformer的RGB引导热成像图像超分辨率框架
  • 在ubtntu系统上安装redis踩坑
  • 什么是HTTPS?对网站有什么用?
  • 【ZeroRange WebRTC】TURN 在 WebRTC 中的角色与工作原理(深入指南)
  • 〈Java高并发核心编程·卷2〉(第2篇)—— Java内置锁与线程安全
  • 【全网首发】2025 华为OD 机试双机位A卷机考真题库清单(全真题库)含考点说明(OD上机考试双机位A卷)
  • AI研究-123 FSD V14 深度解析:纯视觉 SDF 与端到端 相比 V12 具体升级
  • python做网站好处陕西网站建设哪家好
  • 网站建设推销话术用模块做网站
  • Rust 练习册 :Macros与宏系统
  • 【Ruby】Mixins扩展方式之include,extend和prepend
  • 欧美 电台 网站模板4防伪查询网站
  • MTK外包面经
  • [linux] grep命令的使用
  • 前后端跨域问题解决
  • 通往AGI的模块化路径:一个可能的技术架构(同时解答微调与RAG之争)
  • cartographer ros 配置详解