From 1802b75341bee7634595193c4a5784a1b84eb4a5 Mon Sep 17 00:00:00 2001 From: guanjihuan Date: Tue, 4 Nov 2025 16:50:37 +0800 Subject: [PATCH] update --- 2025.11.02_langchain/langchain_example.py | 5 ++ .../langchain_example_with_memory.py | 5 ++ .../langchain_example_with_tool.py | 5 ++ 2025.11.04_langchain_running_code/.env | 2 + .../langchain_running_code.py | 74 +++++++++++++++++++ 5 files changed, 91 insertions(+) create mode 100644 2025.11.04_langchain_running_code/.env create mode 100644 2025.11.04_langchain_running_code/langchain_running_code.py diff --git a/2025.11.02_langchain/langchain_example.py b/2025.11.02_langchain/langchain_example.py index 9b2c7ff..e8171f8 100644 --- a/2025.11.02_langchain/langchain_example.py +++ b/2025.11.02_langchain/langchain_example.py @@ -1,3 +1,8 @@ +""" +This code is supported by the website: https://www.guanjihuan.com +The newest version of this code is on the web page: https://www.guanjihuan.com/archives/47909 +""" + import langchain_openai from langchain_core.prompts import ChatPromptTemplate import dotenv diff --git a/2025.11.02_langchain/langchain_example_with_memory.py b/2025.11.02_langchain/langchain_example_with_memory.py index b79ddbd..896d3a0 100644 --- a/2025.11.02_langchain/langchain_example_with_memory.py +++ b/2025.11.02_langchain/langchain_example_with_memory.py @@ -1,3 +1,8 @@ +""" +This code is supported by the website: https://www.guanjihuan.com +The newest version of this code is on the web page: https://www.guanjihuan.com/archives/47909 +""" + import os from dotenv import load_dotenv from langchain_openai import ChatOpenAI diff --git a/2025.11.02_langchain/langchain_example_with_tool.py b/2025.11.02_langchain/langchain_example_with_tool.py index 3c7561f..4c49160 100644 --- a/2025.11.02_langchain/langchain_example_with_tool.py +++ b/2025.11.02_langchain/langchain_example_with_tool.py @@ -1,3 +1,8 @@ +""" +This code is supported by the website: https://www.guanjihuan.com +The newest version of this code is on the web page: https://www.guanjihuan.com/archives/47909 +""" + import os import dotenv from langchain_openai import ChatOpenAI diff --git a/2025.11.04_langchain_running_code/.env b/2025.11.04_langchain_running_code/.env new file mode 100644 index 0000000..6321e48 --- /dev/null +++ b/2025.11.04_langchain_running_code/.env @@ -0,0 +1,2 @@ +OPENAI_API_KEY=xxx +DASHSCOPE_BASE_URL=https://dashscope.aliyuncs.com/compatible-mode/v1 \ No newline at end of file diff --git a/2025.11.04_langchain_running_code/langchain_running_code.py b/2025.11.04_langchain_running_code/langchain_running_code.py new file mode 100644 index 0000000..a582720 --- /dev/null +++ b/2025.11.04_langchain_running_code/langchain_running_code.py @@ -0,0 +1,74 @@ +""" +This code is supported by the website: https://www.guanjihuan.com +The newest version of this code is on the web page: https://www.guanjihuan.com/archives/47925 +""" + +import os +import dotenv +from langchain_openai import ChatOpenAI +from langchain.agents import create_react_agent, AgentExecutor +from langchain_core.prompts import PromptTemplate +from langchain_experimental.tools import PythonREPLTool + +# 加载环境变量 +dotenv.load_dotenv() + +# 初始化 LLM +llm = ChatOpenAI( + api_key=os.getenv("OPENAI_API_KEY"), + base_url=os.getenv("DASHSCOPE_BASE_URL"), + model="qwen-plus", + temperature=0, +) + +# 使用官方 PythonREPLTool(⚠️ 注意:此工具可执行任意代码,仅用于安全环境) +tools = [PythonREPLTool()] # 直接实例化即可,它已是 Tool 类型 + +# 构建 ReAct Prompt +template = """你是一个严谨的 Python 编程助手,必须通过工具执行代码来验证逻辑,不能仅靠推理得出结论。 + +## 工具 +你可以使用以下工具: +{tools} + +## 规则 +- 每次只能执行一个 Action。 +- 所有 Python 代码必须通过工具执行,不得假设输出结果。 +- **Action Input 必须是纯 Python 代码,不要包含 Markdown 代码块符号(如 ``` 或 ```python)。** +- 如果测试失败(如 assert 报错),请根据错误信息修正代码并重新测试。 +- 最终答案必须基于工具返回的正确执行结果。 +- 输出必须严格遵循以下格式,不要添加额外内容: + +Question: <用户问题> +Thought: <你的思考> +Action: <工具名称> +Action Input: <纯 Python 代码(不要 Markdown 代码块)> +Observation: <工具返回结果> +...(重复以上步骤) +Final Answer: <最终答案,包含正确函数和简要说明> + +## 可用工具 +{tool_names} + +当前问题: {input_message} +{agent_scratchpad}""" + + +prompt = PromptTemplate.from_template(template) + +# 创建并执行 Agent +agent = create_react_agent(llm, tools, prompt) +executor = AgentExecutor( + agent=agent, + tools=tools, + verbose=True, + max_iterations=10, + handle_parsing_errors=True +) + +# 运行任务 +if __name__ == "__main__": + response = executor.invoke({ + "input_message": "请编写一个 Python 函数 fibonacci(n),返回第 n 个斐波那契数。测试通过后做代码的优化,并给出最终代码。" + }) + print(response["output"]) \ No newline at end of file