背景简介
文章介绍了GPT模型的工作原理是基于自回归的语言建模,即通过对下一个字符的概率预测来生成连贯的文本。
GPT模型的预测机制
- 在生成每个token时,会基于前面的上下文计算所有可能token的概率分布
- 然后根据采样策略(如贪心搜索、束搜索或温度采样)选择下一个token
训练数据规模:以GPT-3为例,其训练数据包含数千亿个token,覆盖百科全书、技术文档、数学教材等各类文本。
数学知识的获取:GPT模型在训练过程中会接触到大量数学表达式,包括基础运算和复杂的数学推导。
局限性说明
- 这种能力仅限于模型在训练中见过的常见运算
- 对于更复杂或罕见的数学问题,模型可能给出错误答案
- 模型实际上并不理解数学原理,只是模仿了正确的表达模式
大数计算问题
当要求GPT计算超长数字相加时(如12311111111111111 + 999999988888888111),会出现问题:
- 训练数据限制:大语言模型在训练时接触的数字通常不超过10-15位
- 计算方式差异:人类计算会采用”竖式加法”逐位计算,模型可能试图一次性输出完整结果
解决方案
- 分步引导法
- 编程模式激活
- 工具调用法(最佳实践):让模型调用Python解释器
安装依赖
pip install --upgrade --quiet langchain-core langchain-experimental langchain-openai
代码实现
from langchain_experimental.utilities import PythonREPL
python_repl = PythonREPL()
result = python_repl.run("print(2 + 2)") # 将输出 4
完整示例代码
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_experimental.utilities import PythonREPL
from langchain_openai import ChatOpenAI
template = """Write some python code to solve the user's problem.
Return only python code in Markdown format, e.g.:
```python
....
```"""
prompt = ChatPromptTemplate.from_messages([("system", template), ("human", "{input}")])
model = ChatOpenAI(model="gpt-3.5-turbo")
def _sanitize_output(text: str):
_, after = text.split("```python")
result = after.split("```")[0]
print("---code---")
print(text)
print("---code---")
return result
chain = prompt | model | StrOutputParser() | _sanitize_output | PythonREPL().run
message = chain.invoke({"input": "whats 2 plus 2"})
print(f"message: {message}")
运行结果
---code---
```python
result = 2 + 2
print(result)
---code--- Python REPL can execute arbitrary code. Use with caution. message: 4
---
## 核心要点
| 项目 | 说明 |
|------|------|
| PythonREPL | LangChain实验性功能模块,提供安全的Python交互式解释环境 |
| 功能 | 支持执行模型生成的Python代码片段,捕获执行结果 |
| 适用场景 | LLM需要执行数学计算或数据处理时 |
| 安全提示 | 默认在受限环境中运行,防止危险操作 |