fix: "image/png" not in output["data"].

This commit is contained in:
刘棒棒 2023-11-30 16:05:56 +08:00
parent 68635ff4aa
commit b28111ab34
2 changed files with 76 additions and 47 deletions

View file

@ -17,6 +17,7 @@ from rich.syntax import Syntax
from metagpt.actions import Action
from metagpt.schema import Message
from metagpt.logs import logger
class ExecuteCode(ABC):
@ -90,11 +91,14 @@ class ExecutePyCode(ExecuteCode, Action):
if not outputs:
return parsed_output
for output in outputs:
for i, output in enumerate(outputs):
if output["output_type"] == "stream":
parsed_output += output["text"]
elif output["output_type"] == "display_data":
self.show_bytes_figure(output["data"]["image/png"], self.interaction)
if "image/png" in output["data"]:
self.show_bytes_figure(output["data"]["image/png"], self.interaction)
else:
logger.info(f"{i}th output['data'] from nbclient outputs dont have image/png, continue next output ...")
elif output["output_type"] == "execute_result":
parsed_output += output["data"]["text/plain"]
return parsed_output
@ -136,7 +140,6 @@ class ExecutePyCode(ExecuteCode, Action):
if isinstance(code, str):
return code, language
if isinstance(code, dict):
assert "code" in code
if "language" not in code:

View file

@ -1,57 +1,83 @@
import pytest
from metagpt.actions import ExecutePyCode
from metagpt.actions.execute_code import ExecutePyCode
from metagpt.schema import Message
@pytest.mark.asyncio
async def test_code_running():
pi = ExecutePyCode()
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!')")
output = await pi.run(code_msg)
assert output.state == "done"
# @pytest.mark.asyncio
# async def test_code_running():
# pi = ExecutePyCode()
# output = await pi.run("print('hello world!')")
# assert output[1] is True
# output = await pi.run({"code": "print('hello world!')", "language": "python"})
# assert output[1] is True
# code_msg = Message("print('hello world!')")
# output = await pi.run(code_msg)
# assert output[1] is True
# @pytest.mark.asyncio
# async def test_split_code_running():
# pi = ExecutePyCode()
# output = await pi.run("x=1\ny=2")
# output = await pi.run("z=x+y")
# output = await pi.run("assert z==3")
# assert output[1] is True
# @pytest.mark.asyncio
# async def test_execute_error():
# pi = ExecutePyCode()
# output = await pi.run("z=1/0")
# assert output[1] is False
# @pytest.mark.asyncio
# async def test_plotting_code():
# pi = ExecutePyCode()
# 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[1] is True
@pytest.mark.asyncio
async def test_split_code_running():
pi = ExecutePyCode()
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 = ExecutePyCode()
output = await pi.run("z=1/0")
assert output.state == "error"
@pytest.mark.asyncio
async def test_plotting_code():
pi = ExecutePyCode()
async def test_plotting_bug():
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')
# 显示图形
import seaborn as sns
import pandas as pd
from sklearn.datasets import load_iris
# Load the Iris dataset
iris_data = load_iris()
# Convert the loaded Iris dataset into a DataFrame for easier manipulation
iris_df = pd.DataFrame(iris_data['data'], columns=iris_data['feature_names'])
# Add a column for the target
iris_df['species'] = pd.Categorical.from_codes(iris_data['target'], iris_data['target_names'])
# Set the style of seaborn
sns.set(style='whitegrid')
# Create a pairplot of the iris dataset
plt.figure(figsize=(10, 8))
pairplot = sns.pairplot(iris_df, hue='species')
# Show the plot
plt.show()
"""
pi = ExecutePyCode()
output = await pi.run(code)
assert output.state == "done"
assert output[1] is True