This tutorial aims to guide you from scratch in mastering the skills to develop large model applications using Python. If the current content feels obscure, you can refer back to the earlier articles in this series to lower the learning difficulty.
1. Why Use the Python Interpreter?
ScenarioYou ask AI: “What is 123456789 × 987654321?” AI responds instantly: “121932631112635269” → You check with your phone calculator,and it is completely wrong → It is “guessing”, not “calculating”!
Human ApproachYouopen the calculator → press the keys → get the true value → tell the other party.
Python Interpreter ApproachEncapsulate the “Python calculator” as a tool → let AI click to run → get the true value → no more guessing.
2. What Is It?
| Term | Explanation | Life Analogy |
|---|---|---|
| Python REPL Tool | “Python Interactive Interpreter” | Likethe calculator on your phone, but can calculatedecimals, matrices, Fibonacci |
| create_python_agent | “Create an agent executor that can execute Python code” | Likeputting your hand inside the calculator, pressing the keys, and then taking out the result |
3. Step-by-Step Process
| Operation | Life Translation | Corresponding Code in This Section |
|---|---|---|
| “Define model… minimum temperature” | Let the brainfollow the steps obediently to calculate, without guessing | <span>temperature=0</span> |
| “Import Python REPL Tool” | Put the calculator in the toolbox | <span>from langchain_experimental.tools import PythonREPLTool</span> |
| “create_python_agent” | Combine hand + calculator + manual into a “brain” | <span>create_python_agent(llm, tool, …)</span> |
| “verbose=True” | Let you see the entire process of pressing the calculator keys | <span>verbose=True</span> |
| “handle_parsing_errors=True” | Press the wrong key → return the error → press again | Automatically retry, without crashing |
4. Code Example One
4.1. Code
# pip install langchain langchain-experimental langchain-deepseek
from langchain_experimental.tools import PythonREPLTool
from langchain_experimental.agents.agent_toolkits import create_python_agent
from langchain_deepseek import ChatDeepSeek
# ① Model (Brain)
llm = ChatDeepSeek(model="deepseek-chat", temperature=0)
# ② Tool (Python Calculator)
tool = PythonREPLTool()
# ③ Create Python Agent (Hand + Calculator + Manual)
agent = create_python_agent(
llm=llm,
tool=tool,
verbose=True,
execute_kwargs={"handle_parsing_errors": True}
)
# ④ Question Test
result = agent.invoke("What is 123456789 × 987654321?")
print("AI Final Answer:", result["output"])
4.2. Running Result (Verbose Print)
> Entering new AgentExecutor chain...
Python REPL can execute arbitrary code. Use with caution.
I need to calculate the product of 123456789 and 987654321. I can use Python to perform this multiplication.
Action: Python_REPL
Action Input: print(123456789 * 987654321)
Observation: 121932631112635269
Thought:I now know the final answer.
Final Answer: 121932631112635269
> Finished chain.
AI Final Answer: 121932631112635269
→ AI no longer guesses, real execution → real result!
5. More Complex Example
5.1. Question
“What is the 12th Fibonacci number starting from 1?”
5.2. Running Result (Verbose Print)
> Entering new AgentExecutor chain...
Python REPL can execute arbitrary code. Use with caution.
Thought: I need to find the 12th Fibonacci number starting from 1. The Fibonacci sequence typically starts with 0 and 1, but the question says "从 1 开始数" (starting from 1), so I'll consider the sequence as 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... where the first number is 1, second is 1, third is 2, and so on. So the 12th Fibonacci number would be the 12th term in this sequence.
I can write a Python function to calculate the nth Fibonacci number and then call it with n=12.
Action: Python_REPL
Action Input:
```python
def fibonacci(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
a, b = 1, 1
for i in range(2, n):
a, b = b, a + b
return b
print(fibonacci(12))
```Observation: 144
Thought:I now know the final answer.
Final Answer: 144
> Finished chain.
AI Final Answer: 144
→ AI writes function → runs → gets 144!