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

让AI给你写代码(四)—— 初步利用LangChain Agent根据输入生成,保存,执行

要进一步提升智能编码助手的效率,我觉得需要做到两点
1) 进一步让主人聚焦于设计输入以及结果验证的循环
2) 进一步让智能编码助手聚焦于代码实现和程序流程(保存、打开,修订、执行、合并…)
正好接触到LLM的LangChain的框架,那么初步体验一把利用其Agent实现代码生成,保存与执行
LangChain的中文官网

参考借鉴链接 :阿里通义千问结合Langchain基于程序运行结果回答问题
链接中有一篇介绍,要求AI计算给定阶数(文中是10阶)斐波那契数列,代码生成和执行已经有了,所以我的诉求,看上去挺简单,增加一个保存有效代码的功能就OK了
初步改造如下,新增存盘的函数和相关调用:

#**新增存盘函数**
def saveFile(replyMessages: str):print('... to save the file ...')python_path="/home/cfets/eclipse-workspace/TestAI/testchain/"now_time = time.strftime('%Y-%m-%d_%H:%M:%S', time.localtime())i = random.randint(1, 100)code_file = python_path + "pyAITest_" + now_time + '_' + str(i) + ".py"# 保存至文件res_content=Falsetry:with open(code_file, 'w') as f:f.write(replyMessages)res_content =Trueexcept Exception as e:print('Error: %s' % e)return res_content@tool
def py_repl_tool(code: str):"""Returns the result of execution."""_, after = code.split("```py")realcode = code.split("```")[0]# **新增保存文件**if saveFile(realcode):print('file is saved ... ')py_repl=PythonREPL()return py_repl.run(realcode)if __name__ == '__main__':os.environ["DASHSCOPE_API_KEY"] = dashscope.api_keytools = [Tool(name = "python repl",func=py_repl_tool.run,description="useful for when you need to use python to answer a question."+"You should input python code in correct format and keep the logic as simple as possible. If you want to see the output of a value, you should print it out with `print(...)`.")]agent = initialize_agent(llm=tongyi.Tongyi(model_name="qwen-max",temperature=0.1), tools=tools,verbose=True,max_iterations=3,
)agent.run("What is the 10th fibonacci number?")#agent.run("How many letters in string: Helllomyyfrienddds?")

但很遗憾,测试的结果是,计算的结果与文章一致,10阶斐波那契数列,但保存的文件是空的
在这里插入图片描述

实际上,原代码中split不靠谱,按源代码realcode是空的,需要修改

@tool
def py_repl_tool(code: str):"""Returns the result of execution."""_, after = code.split("```py")# realcode = code.split("```")[0]   原文档的错误#修订后realcode = after.split("```")[0]#print('realcode:  %s' % realcode)# **新增保存文件**if saveFile(realcode):print('file is saved ... ')py_repl=PythonREPL()return py_repl.run(realcode)

修改后执行就OK
主程序执行结果:
在这里插入图片描述
与保存的程序执行结果一致
在这里插入图片描述
但是计算结果是34 这是9阶的斐波那契数列,而不是55——10阶的斐波那契数列,原链接中的结果
在这里插入图片描述
可以把这段代码cp下来算一下,fibonacci(10)=34 , fibonacci(11)才是55
考虑到原代码realcode实际上为空的问题,我觉得原先的agent是分别生成了代码,但没有执行代码(realcode是空,执行啥?)而是找了答案,但无法确认… 所以AI自动生成代码还是保存后检查执行的,这算是歪打正着吧。

最后一个问题,根据初步了解 agent相关tool生成结果跟description描述有直接关系,如果直接引用原来的描述

description="useful for when you need to use python to answer a question.
You should input python code in correct format and keep the logic as simple as possible.   
If you want to see the output of a value, you should print it out with `print(...)`."

agent生成结果会呈现不稳定,有的时候’‘’ py …‘’’ 有的时候 ‘’’ python …‘’’
那么就会报错,参考原文,整理出一个较为完整的描述(description,相当于需求或者设计描述)
完整代码如下:

import dashscope
import os
from langchain_community.llms import tongyi
from langchain.agents import initialize_agent
from langchain.agents.tools import Tool
from langchain.tools import tool
from langchain.utilities.python import PythonREPL
import re
import time
import randomdashscope.api_key="xxxxxx"  #请自行替换# 新增存盘函数
def saveFile(replyMessages: str):print('... to save the file ...')python_path="/home/cfets/eclipse-workspace/TestAI/testchain/"now_time = time.strftime('%Y-%m-%d_%H:%M:%S', time.localtime())i = random.randint(1, 100)code_file = python_path + "pyAITest_" + now_time + '_' + str(i) + ".py"# 保存至文件res_content=Falsetry:with open(code_file, 'w') as f:f.write(replyMessages)res_content =Trueexcept Exception as e:print('Error: %s' % e)return res_content@tool
def py_repl_tool(code: str):"""Returns the result of execution."""_, after = code.split("```python")  realcode = after.split("```")[0]   #修改了原代码错误py_repl=PythonREPL()#print('realcode:  %s' % realcode)# 新增保存文件if saveFile(realcode):print('file is saved ... ')return py_repl.run(realcode)if __name__ == '__main__':os.environ["DASHSCOPE_API_KEY"] = dashscope.api_keytools = [Tool(name = "python repl",func=py_repl_tool.run,description=""""useful for when you need to use python to answer a question. You should input python code in correct format and keep the logic as simple as possible. you should end the program with `print(...)` to print the output and return only python code in Markdown format, e.g.:```python....```""")     #修改了描述,指定了Markdown format]agent = initialize_agent(llm=tongyi.Tongyi(model_name="qwen-max",temperature=0.1), tools=tools,verbose=True,max_iterations=3,
)agent.run("What is the 10th fibonacci number?")

后续考虑几方面的进一步改进
对单一功能实现单元化设计,把多个功能集成串起来,包括前后台设计,形成较为复杂的程序, 再进一步开发本地的知识库/代码库, 同时再利用现有架构测试其他大模型。

最后,希望大家能不能帮我解决一个问题,就是在执行代码为空的时候 55到底是怎么得出来的,先行拜谢

http://www.lryc.cn/news/318369.html

相关文章:

  • Flutter does not exist
  • AIX上安装gcc和g++
  • js实现扫描线填色算法使用canvas展示
  • 考研模拟面试-题目【攻略】
  • Frostmourne - Elasticsearch源日志告警配置
  • GPT出现Too many requests in 1 hour. Try again later.
  • python爬虫实战——小红书
  • Linux信号机制
  • 区块链技术中的共识机制算法:以权益证明(PoS)为例
  • 19113133262(微信同号)【征稿进行时|见刊、检索快速稳定】2024年区块链、物联网与复合材料与国际学术会议 (ICBITC 2024)
  • Doris:使用表函数explode实现array字段列转行
  • 原生php单元测试示例
  • 计算机毕业设计-springboot+vue前后端分离电竞社交平台管理系统部分成果分享
  • Stable Diffusion 详解
  • Go函数全景:从基础到高阶的深度探索
  • 探秘Nutch:揭秘开源搜索引擎的工作原理与无限应用可能(一)
  • MySQL 数据库 下载地址 国内阿里云站点
  • 【25届秋招备战C++】算法篇-贪心算法(Greedy)
  • scrcpy远程投屏控制Android
  • 找机厅 洛谷 BFS
  • 软件无线电系列——模拟无线电、数字无线电、软件无线电
  • XSS_lab(level11-level18)
  • 【git】常用操作
  • 蓝桥杯第十一届电子类单片机组程序设计
  • Java中文乱码问题解析与解决方案
  • AIGC笔记--Maya提取和修改FBX动作文件
  • 【刷题训练】LeetCode125. 验证回文串
  • optee默认安全配置
  • Arcgis新建位置分配求解最佳商店位置
  • 【C++初阶】C++入门(上)