feat: add actions: plan, write_code_function, pycode_executor.

This commit is contained in:
刘棒棒 2023-11-21 18:38:41 +08:00
parent f57355c889
commit 89f1dce936
9 changed files with 335 additions and 0 deletions

View file

@ -0,0 +1,58 @@
import pytest
from metagpt.actions import PyCodeExecutor
from metagpt.schema import Message
@pytest.mark.asyncio
async def test_code_running():
pi = PyCodeExecutor()
output = await pi.run("print('hello world!')")
assert output.state == "done"
output = await pi.run({"code": "print('hello world!')", "language": "python"})
assert output.state == "done"
code_msg = Message("print('hello world!')")
setattr(code_msg, "language", "python")
output = await pi.run(code_msg)
assert output.state == "done"
@pytest.mark.asyncio
async def test_split_code_running():
pi = PyCodeExecutor()
output = await pi.run("x=1\ny=2")
output = await pi.run("z=x+y")
output = await pi.run("assert z==3")
assert output.state == "done"
@pytest.mark.asyncio
async def test_execute_error():
pi = PyCodeExecutor()
output = await pi.run("z=1/0")
assert output.state == "error"
@pytest.mark.asyncio
async def test_plotting_code():
pi = PyCodeExecutor()
code = """
import numpy as np
import matplotlib.pyplot as plt
# 生成随机数据
random_data = np.random.randn(1000) # 生成1000个符合标准正态分布的随机数
# 绘制直方图
plt.hist(random_data, bins=30, density=True, alpha=0.7, color='blue', edgecolor='black')
# 添加标题和标签
plt.title('Histogram of Random Data')
plt.xlabel('Value')
plt.ylabel('Frequency')
# 显示图形
plt.show()
"""
output = await pi.run(code)
assert output.state == "done"

View file

@ -0,0 +1,12 @@
import pytest
from metagpt.actions.plan import Plan
@pytest.mark.asyncio
async def test_plan():
p = Plan()
task_desc = """Heres some background information on Cyclistic, a bike-sharing company designing a marketing strategy aimed at converting casual riders into annual members: So far, Cyclistics marketing strategy has relied on building general awareness and engaging a wide range of consumers. group. One way to help achieve these goals is the flexibility of its pricing plans: one-way passes, full-day passes, and annual memberships. Customers who purchase a one-way or full-day pass are known as recreational riders. Customers purchasing an annual membership are Cyclistic members. I will provide you with a data sheet that records user behavior: '/Users/vicis/Downloads/202103-divvy-tripdata.csv"""
rsp = await p.run(task_desc, role="data analyst")
assert len(rsp.content) > 0
assert rsp.sent_from == "Plan"

View file

@ -0,0 +1,22 @@
import pytest
from metagpt.actions.write_code_v2 import WriteCode
@pytest.mark.asyncio
async def test_write_code():
coder = WriteCode()
code = await coder.run("Write a hello world code.")
assert "language" in code.content
assert "code" in code.content
print(code)
@pytest.mark.asyncio
async def test_write_code_by_list_prompt():
coder = WriteCode()
msg = ["a=[1,2,5,10,-10]", "写出求a中最大值的代码python"]
code = await coder.run(msg)
assert "language" in code.content
assert "code" in code.content
print(code)