要进一步提升智能编码助手的效率,我觉得需要做到两点
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=False
try:
with open(code_file, 'w') as f:
f.write(replyMessages)
res_content =True
except 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_key
tools = [
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 random
dashscope.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=False
try:
with open(code_file, 'w') as f:
f.write(replyMessages)
res_content =True
except 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_key
tools = [
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到底是怎么得出来的,先行拜谢