autogen/notebook/autogen_agentchat_auto_feedback_from_code_execution.ipynb

1002 lines
288 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://colab.research.google.com/github/microsoft/FLAML/blob/main/notebook/autogen_agentchat_auto_feedback_from_code_execution.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Auto Generated Agent Chat: Task Solving with Code Generation, Execution & Debugging\n",
"\n",
"FLAML offers an experimental feature of interactive LLM agents, which can be used to solve various tasks with human or automatic feedback, including tasks that require using tools via code.\n",
"Please find documentation about this feature [here](https://microsoft.github.io/FLAML/docs/Use-Cases/Auto-Generation#agents-experimental).\n",
"\n",
"In this notebook, we demonstrate how to use `AssistantAgent` and `UserProxyAgent` to write code and execute the code. Here `AssistantAgent` is an LLM-based agent that can write Python code (in a Python coding block) for a user to execute for a given task. `UserProxyAgent` is an agent which serves as a proxy for the human user to execute the code written by `AssistantAgent`, or automatically execute the code. Depending on the setting of `human_input_mode` and `max_consecutive_auto_reply`, the `UserProxyAgent` either solicits feedback from the human user or returns auto-feedback based on the result of code execution (success or failure and corresponding outputs) to `AssistantAgent`. `AssistantAgent` will debug the code and suggest new code if the result contains error. The two agents keep communicating to each other until the task is done.\n",
"\n",
"## Requirements\n",
"\n",
"FLAML requires `Python>=3.8`. To run this notebook example, please install flaml with the [autogen] option:\n",
"```bash\n",
"pip install flaml[autogen]\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"execution": {
"iopub.execute_input": "2023-02-13T23:40:52.317406Z",
"iopub.status.busy": "2023-02-13T23:40:52.316561Z",
"iopub.status.idle": "2023-02-13T23:40:52.321193Z",
"shell.execute_reply": "2023-02-13T23:40:52.320628Z"
}
},
"outputs": [],
"source": [
"# %pip install flaml[autogen]~=2.0.0rc4"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Set your API Endpoint\n",
"\n",
"The [`config_list_from_json`](https://microsoft.github.io/FLAML/docs/reference/autogen/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file.\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from flaml import oai\n",
"\n",
"config_list = oai.config_list_from_json(\n",
" \"OAI_CONFIG_LIST\",\n",
" filter_dict={\n",
" \"model\": [\"gpt-4\", \"gpt4\", \"gpt-4-32k\", \"gpt-4-32k-0314\"],\n",
" },\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"It first looks for environment variable \"OAI_CONFIG_LIST\" which needs to be a valid json string. If that variable is not found, it then looks for a json file named \"OAI_CONFIG_LIST\". It filters the configs by models (you can filter by other keys as well). Only the gpt-4 models are kept in the list based on the filter condition.\n",
"\n",
"The config list looks like the following:\n",
"```python\n",
"config_list = [\n",
" {\n",
" 'model': 'gpt-4',\n",
" 'api_key': '<your OpenAI API key here>',\n",
" },\n",
" {\n",
" 'model': 'gpt-4',\n",
" 'api_key': '<your Azure OpenAI API key here>',\n",
" 'api_base': '<your Azure OpenAI API base here>',\n",
" 'api_type': 'azure',\n",
" 'api_version': '2023-06-01-preview',\n",
" },\n",
" {\n",
" 'model': 'gpt-4-32k',\n",
" 'api_key': '<your Azure OpenAI API key here>',\n",
" 'api_base': '<your Azure OpenAI API base here>',\n",
" 'api_type': 'azure',\n",
" 'api_version': '2023-06-01-preview',\n",
" },\n",
"]\n",
"```\n",
"\n",
"If you open this notebook in colab, you can upload your files by clicking the file icon on the left panel and then choose \"upload file\" icon.\n",
"\n",
"You can set the value of config_list in other ways you prefer, e.g., loading from a YAML file."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example Task: Check Stock Price Change\n",
"\n",
"In the example below, let's see how to use the agents in FLAML to write a python script and execute the script. This process involves constructing a `AssistantAgent` to serve as the assistant, along with a `UserProxyAgent` that acts as a proxy for the human user. In this example demonstrated below, when constructing the `UserProxyAgent`, we select the `human_input_mode` to \"NEVER\". This means that the `UserProxyAgent` will not solicit feedback from the human user. It stops replying when the limit defined by `max_consecutive_auto_reply` is reached, or when `is_termination_msg()` returns true for the received message."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"user_proxy (to assistant):\n",
"\n",
"What date is today? Compare the year-to-date gain for META and TESLA.\n",
"\n",
"--------------------------------------------------------------------------------\n",
"assistant (to user_proxy):\n",
"\n",
"# filename: stock_comparison.py\n",
"```python\n",
"import datetime\n",
"import yfinance as yf\n",
"\n",
"# Get today's date\n",
"today = datetime.date.today()\n",
"print(\"Today's date:\", today)\n",
"\n",
"# Get the start of the year\n",
"start_of_year = datetime.date(today.year, 1, 1)\n",
"\n",
"# Download the historical data for META and TESLA\n",
"meta_data = yf.download('META', start=start_of_year, end=today)\n",
"tesla_data = yf.download('TSLA', start=start_of_year, end=today)\n",
"\n",
"# Calculate the year-to-date gain for META and TESLA\n",
"meta_gain = (meta_data['Close'][-1] - meta_data['Close'][0]) / meta_data['Close'][0]\n",
"tesla_gain = (tesla_data['Close'][-1] - tesla_data['Close'][0]) / tesla_data['Close'][0]\n",
"\n",
"# Print the year-to-date gain for META and TESLA\n",
"print(\"Year-to-date gain for META:\", round(meta_gain * 100, 2), \"%\")\n",
"print(\"Year-to-date gain for TESLA:\", round(tesla_gain * 100, 2), \"%\")\n",
"```\n",
"Please save this code in a file named `stock_comparison.py` and run it. This script will print today's date and compare the year-to-date gain for META and TESLA. It uses the `yfinance` library to download the historical data for the stocks. If you haven't installed `yfinance`, please install it by running `pip install yfinance`.\n",
"\n",
"--------------------------------------------------------------------------------\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"user_proxy (to assistant):\n",
"\n",
"exitcode: 0 (execution succeeded)\n",
"Code output: \n",
"Today's date: 2023-07-26\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"Year-to-date gain for META: 136.07 %\n",
"Year-to-date gain for TESLA: 145.4 %\n",
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"assistant (to user_proxy):\n",
"\n",
"Great! The code executed successfully. Today's date is July 26, 2023. The year-to-date gain for META is 136.07%, and the year-to-date gain for TESLA is 145.4%. Please let me know if you need any further assistance.\n",
"\n",
"TERMINATE\n",
"\n",
"--------------------------------------------------------------------------------\n"
]
}
],
"source": [
"from flaml.autogen.agentchat import AssistantAgent, UserProxyAgent\n",
"\n",
"# create an AssistantAgent named \"assistant\"\n",
"assistant = AssistantAgent(\n",
" name=\"assistant\",\n",
" oai_config={\n",
" \"seed\": 42, # seed for caching and reproducibility\n",
" \"config_list\": config_list, # a list of OpenAI API configurations\n",
" \"temperature\": 0, # temperature for sampling\n",
" }, # configuration for flaml.oai, an enhanced inference API compatible with OpenAI API\n",
")\n",
"# create a UserProxyAgent instance named \"user_proxy\"\n",
"user_proxy = UserProxyAgent(\n",
" name=\"user_proxy\",\n",
" human_input_mode=\"NEVER\",\n",
" max_consecutive_auto_reply=10,\n",
" is_termination_msg=lambda x: x.get(\"content\", \"\").rstrip().endswith(\"TERMINATE\"),\n",
" code_execution_config={\n",
" \"work_dir\": \"coding\",\n",
" \"use_docker\": False, # set to True or image name like \"python:3\" to use docker\n",
" },\n",
")\n",
"# the assistant receives a message from the user_proxy, which contains the task description\n",
"user_proxy.initiate_chat(\n",
" assistant,\n",
" message=\"\"\"What date is today? Compare the year-to-date gain for META and TESLA.\"\"\",\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"The example above involves code execution. In FLAML, code execution is triggered automatically by the `UserProxyAgent` when it detects an executable code block in a received message and no human user input is provided. This process occurs in a designated working directory, using a Docker container by default. Unless a specific directory is specified, FLAML defaults to the `flaml/autogen/extensions` directory. Users have the option to specify a different working directory by setting the `work_dir` argument when constructing a new instance of the `UserProxyAgent`.\n",
"\n",
"The whole chat is auto generated."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example Task: Plot Chart"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"user_proxy (to assistant):\n",
"\n",
"Plot a chart of their stock price change YTD and save to stock_price_ytd.png.\n",
"\n",
"--------------------------------------------------------------------------------\n",
"assistant (to user_proxy):\n",
"\n",
"# filename: stock_price_chart.py\n",
"```python\n",
"import datetime\n",
"import yfinance as yf\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Get today's date\n",
"today = datetime.date.today()\n",
"\n",
"# Get the start of the year\n",
"start_of_year = datetime.date(today.year, 1, 1)\n",
"\n",
"# Download the historical data for META and TESLA\n",
"meta_data = yf.download('META', start=start_of_year, end=today)\n",
"tesla_data = yf.download('TSLA', start=start_of_year, end=today)\n",
"\n",
"# Plot the stock price change YTD for META and TESLA\n",
"plt.figure(figsize=(10, 6))\n",
"plt.plot(meta_data['Close'], label='META')\n",
"plt.plot(tesla_data['Close'], label='TSLA')\n",
"plt.xlabel('Date')\n",
"plt.ylabel('Stock Price')\n",
"plt.title('Stock Price Change YTD for META and TESLA')\n",
"plt.legend()\n",
"\n",
"# Save the chart to stock_price_ytd.png\n",
"plt.savefig('stock_price_ytd.png')\n",
"plt.show()\n",
"```\n",
"\n",
"Please save this code in a file named `stock_price_chart.py` and run it. This script will plot the stock price change YTD for META and TESLA and save the chart to a file named `stock_price_ytd.png`. It uses the `matplotlib` library to create the chart. If you haven't installed `matplotlib`, please install it by running `pip install matplotlib`.\n",
"\n",
"--------------------------------------------------------------------------------\n",
"user_proxy (to assistant):\n",
"\n",
"exitcode: 0 (execution succeeded)\n",
"Code output: \n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"Figure(1000x600)\n",
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"assistant (to user_proxy):\n",
"\n",
"Great! The code executed successfully, and the chart of the stock price change YTD for META and TESLA has been saved to a file named `stock_price_ytd.png`. You can now view the chart in the saved file. If you need any further assistance, please let me know.\n",
"\n",
"TERMINATE\n",
"\n",
"--------------------------------------------------------------------------------\n"
]
}
],
"source": [
"# followup of the previous question\n",
"user_proxy.send(\n",
" recipient=assistant,\n",
" message=\"\"\"Plot a chart of their stock price change YTD and save to stock_price_ytd.png.\"\"\",\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's display the generated figure."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA+gAAAJYCAYAAADxHswlAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8pXeV/AAAACXBIWXMAAA9hAAAPYQGoP6dpAADddklEQVR4nOzdd3xUVfrH8c+k94RAQiCEAKH3KiBSlaIIolgAUUGxYkH9qYu61lV23bUXbAgqIC6IKyBSpCNFegfpNQkQSCE9mfv742YmhPQ6Cfm+X6+85s4t5z4zhCTPnHOeYzEMw0BEREREREREHMrJ0QGIiIiIiIiIiBJ0ERERERERkUpBCbqIiIiIiIhIJaAEXURERERERKQSUIIuIiIiIiIiUgkoQRcRERERERGpBJSgi4iIiIiIiFQCStBFREREREREKgEl6CIiIiIiIiKVgBJ0ERERERERkUpACbqIiIiIiIhIJaAEXURERERERKQSUIIuIiIiIiIiUgkoQRcRERERERGpBJSgi4iIiIiIiFQCStBFREREREREKgEl6CIiIiIiIiKVgBJ0ERERERERkUpACbqIiIiIiIhIJaAEXURERERERKQSUIIuIiIiIiIiUgkoQRcRERERERGpBJSgi4iIiIiIiFQCStBFREREREREKgEl6CIiIiIiIiKVgBJ0ERERERERkUpACbqIiIiIiIhIJaAEXURERERERKQSUIIuIiIiIiIiUgkoQRcRERERERGpBJSgi4iIiIiIiFQCStBFREREREREKgEl6CIiIiIiIiKVgBJ0ERERERERkUpACbqIiIiIiIhIJaAEXURERERERKQSUIIuIiIiIiIiUgkoQRcRERERERGpBJSgi4iIiIiIiFQCStBFREREREREKgEl6CIiIiIiIiKVgBJ0ERERERERkUpACbqIiIiIiIhIJaAEXURERERERKQSUIIuIiIiIiIiUgkoQRcRERERERGpBJSgi4iIiIiIiFQCStBFREREREREKgEl6CIiIiIiIiKVgBJ0EZEqYuXKlVgsFubMmeOQ+0+bNg2LxcKxY8cccv+i6NOnD61bt3Z0GNXWpUuXGDduHCEhIVgsFiZMmODokOQKFouF1157zdFhiIhIPpSgi4gUYNeuXdx+++2Eh4fj4eFBaGgo/fv35+OPP85x3ttvv83//vc/xwRZBMeOHcNisdi/nJ2dqV+/Prfeeivbt293dHiFio+P5/XXX6ddu3b4+Pjg6elJ69ateeGFFzhz5oyjw6sQ6enptGnThoiICJKTk3MdP3bsGF5eXtxxxx05/q0L+lq5cmWu7w1XV1dq1arFtddey4svvsiJEyeKHOPbb7/NtGnTePTRR/n++++55557yvItyKVBgwZYLBZuuOGGPI9/9dVX9te1efNm+/7XXnutwPclKiqKPn36FOk9vDzZ3bdvHxaLBQ8PD2JjY8v1tZeX4r5u279BXl+DBg3K0fbatWu58cYbCQ0NxcPDg/r16zNkyBBmzpyZ4zyLxcLjjz9e5Jg/++wzLBYLXbt2LfXrFxFxNBdHByAiUlmtW7eOvn37Ur9+fR588EFCQkI4efIkGzZs4MMPP+SJJ56wn/v2229z++23M2zYMMcFXAQjR47kpptuIjMzk3379jF58mR+++03NmzYQPv27Qu89p577mHEiBG4u7tXTLBZjhw5wg033MCJEye44447eOihh3Bzc2Pnzp1MmTKFn3/+mb/++qtCY3IEV1dXvvzyS3r06MGbb77J22+/neP4448/jpubGx999BG33HJLjmPfffcdS5cu5fvvv8+xv0WLFvZk3/a9YbVauXjxIps2beKDDz7gww8/ZMqUKYwYMaLQGJcvX063bt149dVXS/lqi87Dw4MVK1YQFRVFSEhIjmMzZszAw8ODlJSUPK+dPHkyPj4+ufYHBATw0ksvMW7cOPu+TZs28dFHH/Hiiy/SokUL+/62bdvat6dPn05ISAgXL15kzpw5Oa6vKkryutu3b8+zzz6bq626devat2fPns1dd91F+/bteeqpp6hRowZHjx5l9erVfPXVV4waNarEMc+YMYMGDRrw559/cujQIRo3blzitkREHM4QEZE83XTTTUZQUJBx8eLFXMeio6NzPPf29jbuu+++co1nxYoVBmDMnj272NcePXrUAIx///vfOfbPmzfPAIyHHnoo32svXbpU7PuVlfT0dKNdu3aGl5eXsWbNmlzH4+LijBdffNH+vHfv3karVq0qMsQK9+ijjxqurq7G7t277fvmzJljAMZnn32W5zXjx4838vuVn9/3hmEYxrFjx4ymTZsabm5uxvbt2wuNrWHDhsbgwYOL+EoKl56ebqSmpuZ7PDw83Lj++usNPz8/44MPPshx7OTJk4aTk5MxfPhwAzA2bdpkP/bqq68agHHu3LkixzJ79mwDMFasWJHncavVajRo0MB45plnjFtvvdXo06dPkduuSIDx6quvFvn8wl53eHh4kf7NW7ZsabRq1SrPf88rf54Cxvjx44sU35EjRwzAmDt3rhEUFGS89tprRbpORKSy0hB3EZF8HD58mFatWhEQEJDrWHBwsH3bYrGQmJjIt99+ax/aOWbMGPvxbdu2ceONN+Ln54ePjw/XX389GzZsyNVmbGwsTz/9NA0aNMDd3Z169epx7733cv78+XxjTE1N5eabb8bf359169YV+zX269cPgKNHjwLZ88xXrVrFY489RnBwMPXq1ctx7Mo56L/99hu9e/fG19cXPz8/unTpkmvI6saNGxk0aBD+/v54eXnRu3dv/vjjj0Lj++mnn9ixYwcvvfQS1113Xa7jfn5+vPXWW7n27927l759++Ll5UVoaCjvvPNOjuNpaWm88sordOrUCX9/f7y9venZsycrVqzIcZ5t+Pd//vMfvvzySyIiInB3d6dLly5s2rQp131nz55Ny5Yt8fDwoHXr1vz888+MGTOGBg0a5DjParXywQcf0KpVKzw8PKhduzYPP/wwFy9eLPQ9mTRpErVq1eKRRx7BMAwuXbrEhAkT6N69O4888kih1xdHeHg406ZNIy0tLdd7eDlbfYSjR4/y66+/2v8f2L5Xzp49ywMPPEDt2rXx8PCgXbt2fPvttznauPy9/uCDD+zv9d69ewuM0cPDg9tuuy3X99wPP/xAjRo1GDhwYMlefDH98ccfHDt2jBEjRjBixAhWr17NqVOninTtzp07GTNmDI0aNcLDw4OQkBDuv/9+YmJicpxnG5p/6NAhxowZQ0BAAP7+/owdO5akpKQc56ampvL0008TFBSEr68vQ4cOLXI85eHw4cN06dIFNze3XMcu/3laXDNmzKBGjRoMHjyY22+/nRkzZpQmTBERh9MQdxGRfISHh7N+/Xp2795dYOGx77//nnHjxnHNNdfw0EMPARAREQHAnj176NmzJ35+fjz//PO4urryxRdf0KdPH1atWmWfM3np0iV69uzJvn37uP/+++nYsSPnz59n3rx5nDp1ilq1auW6b3JyMrfccgubN2/m999/p0uXLsV+jYcPHwagZs2aOfY/9thjBAUF8corr5CYmJjv9dOmTeP++++nVatWTJw4kYCAALZt28aiRYvsQ1aXL1/OjTfeSKdOnXj11VdxcnJi6tSp9OvXjzVr1nDNNdfk2/68efMAijWX+eLFiwwaNIjbbruNO++8kzlz5vDCCy/Qpk0bbrzxRsCc0/71118zcuRIHnzwQRISEpgyZQoDBw7kzz//zDXcf+bMmSQkJPDwww9jsVh45513uO222zhy5Aiurq4A/Prrr9x11120adOGSZMmcfHiRR544AFCQ0Nzxfjwww8zbdo0xo4dy5NPPsnRo0f55JNP2LZtG3/88Ye9zbz4+/vz0Ucfcccdd/D111+zd+9eoqOj+e2337BYLEV+n4qqe/fuREREsHTp0nzPadGiBd9//z1PP/009erVsw93DgoKIjk5mT59+nDo0CEef/xxGjZsyOzZsxkzZgyxsbE89dRTOdqaOnUqKSkpPPTQQ7i7uxMYGFhojKNGjWLAgAEcPnzY/n9v5syZ3H7
"text/plain": [
"<IPython.core.display.Image object>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import Image\n",
"\n",
"Image(filename='coding/stock_price_ytd.png')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use a Different Code Execution Environment\n",
"\n",
"The code execution happened in a separate process, so the plot is not directly displayed in the notebook. Is it possible to change the code execution environment into IPython?\n",
"\n",
"Yes! In the following we demonstrate how to extend the `UserProxyAgent` to use a different code execution environment."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from typing import Dict, Union\n",
"from IPython import get_ipython\n",
"\n",
"class IPythonUserProxyAgent(UserProxyAgent):\n",
" def __init__(self, name: str, **kwargs):\n",
" super().__init__(name, **kwargs)\n",
" self._ipython = get_ipython()\n",
"\n",
" def generate_init_message(self, *args, **kwargs) -> Union[str, Dict]:\n",
" return super().generate_init_message(*args, **kwargs) + \"\"\"\n",
"If you suggest code, the code will be executed in IPython.\"\"\"\n",
"\n",
" def run_code(self, code, **kwargs):\n",
" result = self._ipython.run_cell(code)\n",
" log = str(result.result)\n",
" exitcode = 0 if result.success else 1\n",
" if result.error_before_exec is not None:\n",
" log += f\"\\n{result.error_before_exec}\"\n",
" exitcode = 1\n",
" if result.error_in_exec is not None:\n",
" log += f\"\\n{result.error_in_exec}\"\n",
" exitcode = 1\n",
" return exitcode, bytes(log, \"utf-8\"), None"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The implementation overrides three functions in `UserProxyAgent`:\n",
"* constructor. We get the ipython instance as the code execution environment.\n",
"* `generate_init_message`. We generate a modified initial message to send to the assistant agent, by adding the info that the execution will be performed in IPython.\n",
"* `run_code`. We execute the code with the ipython instance.\n",
"\n",
"With the new `IPythonUserProxyAgent`, we are able to run the code within the current notebook environment and display plot directly."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ipython_user_proxy (to assistant):\n",
"\n",
"Plot a chart of META and TESLA stock price change YTD\n",
"If you suggest code, the code will be executed in IPython.\n",
"\n",
"--------------------------------------------------------------------------------\n",
"assistant (to ipython_user_proxy):\n",
"\n",
"Sure, we can use the `yfinance` library in Python to download the stock price data and `matplotlib` to plot the data. If you don't have `yfinance` installed, you can install it using pip:\n",
"\n",
"```python\n",
"!pip install yfinance\n",
"```\n",
"\n",
"Here is the Python code to plot the YTD stock price change for META (Facebook) and TESLA.\n",
"\n",
"```python\n",
"# Python code\n",
"import yfinance as yf\n",
"import matplotlib.pyplot as plt\n",
"from datetime import datetime\n",
"\n",
"# Define the ticker symbol\n",
"tickerSymbols = ['META', 'TSLA']\n",
"\n",
"# Get data on this ticker\n",
"start_date = datetime(datetime.now().year, 1, 1)\n",
"end_date = datetime.now()\n",
"\n",
"# Fetch the data\n",
"data = yf.download(tickerSymbols, start=start_date, end=end_date)\n",
"\n",
"# Plot the close prices\n",
"plt.figure(figsize=(14,7))\n",
"plt.plot(data['Close'])\n",
"plt.title('YTD Stock Price Change for META and TESLA')\n",
"plt.xlabel('Date')\n",
"plt.ylabel('Price (USD)')\n",
"plt.legend(tickerSymbols)\n",
"plt.grid(True)\n",
"plt.show()\n",
"```\n",
"\n",
"This code will plot the closing prices of META and TESLA stocks from the start of this year to the current date. The prices are in USD.\n",
"\n",
"--------------------------------------------------------------------------------\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Defaulting to user installation because normal site-packages is not writeable\n",
"Requirement already satisfied: yfinance in /home/vscode/.local/lib/python3.9/site-packages (0.2.26)\n",
"Requirement already satisfied: pandas>=1.3.0 in /home/vscode/.local/lib/python3.9/site-packages (from yfinance) (2.0.3)\n",
"Requirement already satisfied: numpy>=1.16.5 in /home/vscode/.local/lib/python3.9/site-packages (from yfinance) (1.25.1)\n",
"Requirement already satisfied: multitasking>=0.0.7 in /home/vscode/.local/lib/python3.9/site-packages (from yfinance) (0.0.11)\n",
"Requirement already satisfied: frozendict>=2.3.4 in /home/vscode/.local/lib/python3.9/site-packages (from yfinance) (2.3.8)\n",
"Requirement already satisfied: requests>=2.31 in /usr/local/lib/python3.9/site-packages (from yfinance) (2.31.0)\n",
"Requirement already satisfied: html5lib>=1.1 in /home/vscode/.local/lib/python3.9/site-packages (from yfinance) (1.1)\n",
"Requirement already satisfied: appdirs>=1.4.4 in /home/vscode/.local/lib/python3.9/site-packages (from yfinance) (1.4.4)\n",
"Requirement already satisfied: lxml>=4.9.1 in /home/vscode/.local/lib/python3.9/site-packages (from yfinance) (4.9.3)\n",
"Requirement already satisfied: pytz>=2022.5 in /home/vscode/.local/lib/python3.9/site-packages (from yfinance) (2023.3)\n",
"Requirement already satisfied: beautifulsoup4>=4.11.1 in /home/vscode/.local/lib/python3.9/site-packages (from yfinance) (4.12.2)\n",
"Requirement already satisfied: soupsieve>1.2 in /home/vscode/.local/lib/python3.9/site-packages (from beautifulsoup4>=4.11.1->yfinance) (2.4.1)\n",
"Requirement already satisfied: six>=1.9 in /usr/local/lib/python3.9/site-packages (from html5lib>=1.1->yfinance) (1.16.0)\n",
"Requirement already satisfied: webencodings in /home/vscode/.local/lib/python3.9/site-packages (from html5lib>=1.1->yfinance) (0.5.1)\n",
"Requirement already satisfied: python-dateutil>=2.8.2 in /home/vscode/.local/lib/python3.9/site-packages (from pandas>=1.3.0->yfinance) (2.8.2)\n",
"Requirement already satisfied: tzdata>=2022.1 in /home/vscode/.local/lib/python3.9/site-packages (from pandas>=1.3.0->yfinance) (2023.3)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.9/site-packages (from requests>=2.31->yfinance) (2023.5.7)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.9/site-packages (from requests>=2.31->yfinance) (2.0.3)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.9/site-packages (from requests>=2.31->yfinance) (3.4)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.9/site-packages (from requests>=2.31->yfinance) (3.2.0)\n",
"\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.0.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.2.1\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
"[*********************100%***********************] 2 of 2 completed\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABJEAAAJwCAYAAAA5hvCvAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8pXeV/AAAACXBIWXMAAA9hAAAPYQGoP6dpAAEAAElEQVR4nOzdd3hU1dbH8e9Mei+QECAhQOi9VymigogodsECKrYr99r1tfd6r/3aReAKiCJWVBDpvfdeQkloCZDeZ877xyEjMYEkZJKZJL/P8+SZM6fsvc7kJJrF3mtbDMMwEBEREREREREROQerqwMQERERERERERH3pySSiIiIiIiIiIiUSkkkEREREREREREplZJIIiIiIiIiIiJSKiWRRERERERERESkVEoiiYiIiIiIiIhIqZREEhERERERERGRUimJJCIiIiIiIiIipVISSURERERERERESqUkkoiISDU2cOBA2rVr57L+GzduzJgxY1zWf2kWLFiAxWLhu+++c3UolSojI4OxY8cSFRWFxWLhgQcecHVI8jcWi4Xnn3/e1WGIiIhUiJJIIiLidoYOHUpYWBjHjh0rdiw1NZX69etTr149LBZLqV8TJ04EKLLP09OT8PBwunbtyv3338+2bdvKHFteXh7vvfcenTt3Jjg4mNDQUNq2bctdd93Fjh07HOctW7aM559/npSUlIp+HJVmzJgxRT6X4OBgOnbsyFtvvUVubq6rwyvVggULuPrqq4mKisLb25vIyEiGDx/O999/7+rQqtyrr77KxIkTuffee/nqq6+45ZZbKrW/xo0bY7FYuPjii0s8/vnnnzueqzVr1jj2P//88+f8eT169CgDBw4s08/2mQmZ7du3Y7FY8PX1deufuXMp730Xfg9K+rr00kuLtL1kyRKGDh1Kw4YN8fX1pVGjRgwfPpypU6cWOc9isTBu3Lgyx/zRRx9hsVjo2bNnhe9fRESqB09XByAiIvJ3H330Ee3atePBBx8s9kfOk08+SXJyMk8//TRxcXGO/b/99htff/0177zzDnXr1nXs79Onj2P7kksu4dZbb8UwDFJTU9m4cSOTJk3io48+4o033uChhx4qNbZrrrmG33//nZEjR3LnnXeSn5/Pjh07mDlzJn369KFVq1aAmUR64YUXGDNmDKGhoRX8RCqPj48PX3zxBQApKSnMmDGDRx55hNWrVzNt2rRSr9+5cydWa9X/m9Rzzz3Hiy++SPPmzbn77ruJjY3lxIkT/Pbbb1xzzTVMmTKFUaNGVXlcrjJv3jx69erFc889V2V9+vr6Mn/+fI4ePUpUVFSRY1OmTMHX15ecnJwSr/34448JDAwstj80NJSnnnqKsWPHOvatXr2a999/nyeffJLWrVs79nfo0MGxPXnyZKKiojh16hTfffddkeuri/O5706dOvHwww8Xa6tBgwaO7enTp3PDDTfQqVMn7r//fsLCwoiPj2fRokV8/vnnFfo5mTJlCo0bN2bVqlXs2bOHZs2anXdbIiJSTRgiIiJu6I033jAAY/bs2Y59q1atMqxWq/HYY48VO//f//63ARjx8fEltgcY9913X7H9ycnJRu/evQ3A+PXXX88Z06pVqwzAeOWVV4odKygoMJKTk8scj7MMGDDAaNu27XldO3r0aCMgIKDIPpvNZnTr1s0AjMTExBKvs9vtRlZW1nn16QzTp083AOPaa6818vLyih2fNWuW8csvvxiGYRjz5883AGP69OlVHWaVatKkiTFs2DCntZefn2/k5uae9XhsbKxx0UUXGcHBwca7775b5NihQ4cMq9VqXHPNNQZgrF692nHsueeeMwAjKSmpzLEUfr/nz59f4nG73W40btzYeOihh4yrrrrKGDhwYJnbrkqA8dxzz5X5/NLuOzY2tkzf8zZt2hht27Yt8ft57NixYjGW9HuyJPv27TMA4/vvvzciIiKM559/vkzXiYhI9abpbCIi4pYeeughOnTowD/+8Q9ycnKw2Wzcc889xMbGOnW0RZ06dZg2bRqenp688sor5zx37969APTt27fYMQ8PD+rUqQOYU3YeffRRAJo0aeKYYrJ//34ACgoKeOmll4iLi8PHx4fGjRvz5JNPljiF7Pfff2fAgAEEBQURHBxM9+7di43O+rs//vgDf39/Ro4cSUFBQamfwZmsVisDBw4EcMTbuHFjLr/8cmbPnk23bt3w8/Pj008/dRz7e02klJQUHnzwQRo3boyPjw/R0dHceuutJCcnO87Jzc3lueeeo1mzZvj4+BATE8Njjz1Wpml0zzzzDOHh4Xz55Zd4eXkVOz5kyBAuv/zyIvvsdjuvvPIK0dHR+Pr6ctFFF7Fnz54i5yxevJjrrruORo0aOWJ68MEHyc7OLnLemDFjCAwMJDExkREjRhAYGEhERASPPPIINputyLknTpzglltucUx9HD16NBs3biwy1bLQjh07uPbaawkPD8fX15du3brx888/n/OzKKz5FB8fz6+//lrsWTt+/Dh33HEH9erVw9fXl44dOzJp0qQibezfvx+LxcJ//vMf3n33XcdzWdo0T19fX66++upiz+PXX39NWFgYQ4YMOef1zrJ06VL279/PjTfeyI033siiRYtISEgo07WbNm1izJgxNG3aFF9fX6Kiorj99ts5ceJEkfMKp+Ht2bPHMbowJCSE2267jaysrCLn5ubm8uCDDxIREUFQUBBXXHFFmeOpDHv37qV79+54e3sXOxYZGXne7U6ZMoWwsDCGDRvGtddey5QpUyoSpoiIVBOaziYiIm7J09OTzz77jD59+vDSSy8RGRnJunXrmDVrFv7+/k7tq1GjRgwYMID58+eTlpZGcHBwiefFxsYC5h9Pffv2xdOz5P+MXn311ezatavY9LqIiAgAxo4dy6RJk7j22mt5+OGHWblyJa+99hrbt2/nhx9+cLQzceJEbr/9dtq2bcsTTzxBaGgo69evZ9asWWedgjJz5kyuvfZabrjhBr788ks8PDzK/XkUJssKk2JgTlsbOXIkd999N3feeSctW7Ys8dqMjAz69evH9u3buf322+nSpQvJycn8/PPPJCQkULduXex2O1dccQVLlizhrrvuonXr1mzevJl33nmHXbt28eOPP541tt27d7Njxw5uv/12goKCynxPr7/+OlarlUceeYTU1FTefPNNbrrpJlauXOk4Z/r06WRlZXHvvfdSp04dVq1axQcffEBCQgLTp08v0p7NZmPIkCH07NmT//znP/z555+89dZbxMXFce+99wJm4mr48OGsWrWKe++9l1atWvHTTz8xevToYvFt3bqVvn370rBhQ/7v//6PgIAAvv32W0aMGMGMGTO46qqrSryv1q1b89VXX/Hggw8SHR3tmNoUERFBdnY2AwcOZM+ePYwbN44mTZowffp0xowZQ0pKCvfff3+RtiZMmEBOTg533XUXPj4+hIeHl/q5jho1isGDB7N3717H9NKpU6dy7bXXlpjgK3Ty5Mli+zw9Pc9r6ueUKVOIi4uje/futGvXDn9/f77++mtHIvdc5syZw759+7jtttuIiopi69atfPbZZ2zdupUVK1ZgsViKnH/99dfTpEkTXnvtNdatW8cXX3xBZGQkb7zxhuOcsWPHMnnyZEaNGkWfPn2YN28ew4YNK/d9lUV+fn6R5GyhgIAA/Pz8APP31ty5c0lISCA6OtppfU+ZMoWrr74ab29vRo4cyccff8zq1avp3r270/oQERE35OqhUCIiIucybtw4w8vLywgMDDRGjhx51vPOdzpbofvvv98AjI0bN571HLvdbgwYMMAAjHr16hkjR440PvzwQ+PAgQNljmfDhg0GYIwdO7bI/kceecQAjHnz5hmGYRgpKSlGUFCQ0bNnTyM7O7tYHIXOnM42Y8YMw8vLy7jzzjsNm8121vsoVDidLSkpyUhKSjL27NljvPrqq4bFYjE6dOjgOC82NtYAjFmzZhVrIzY21hg9erTj/bPPPuuY4vJ3hXF/9dVXhtVqNRYvXlzk+CeffGIAxtKlS88a808//WQAxjvvvFPq/RnGX9PZWrduXWQ6z3v
"text/plain": [
"<Figure size 1400x700 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"ipython_user_proxy (to assistant):\n",
"\n",
"exitcode: 0 (execution succeeded)\n",
"Code output: \n",
"None\n",
"None\n",
"\n",
"--------------------------------------------------------------------------------\n",
"assistant (to ipython_user_proxy):\n",
"\n",
"It seems like the code executed successfully but didn't produce any output. This could be due to the fact that the IPython environment you're using might not support matplotlib's interactive mode. \n",
"\n",
"Let's try a different approach using pandas' built-in plotting function, which should work in any environment. \n",
"\n",
"```python\n",
"# Python code\n",
"import yfinance as yf\n",
"import pandas as pd\n",
"from datetime import datetime\n",
"\n",
"# Define the ticker symbol\n",
"tickerSymbols = ['META', 'TSLA']\n",
"\n",
"# Get data on this ticker\n",
"start_date = datetime(datetime.now().year, 1, 1)\n",
"end_date = datetime.now()\n",
"\n",
"# Fetch the data\n",
"data = yf.download(tickerSymbols, start=start_date, end=end_date)\n",
"\n",
"# Plot the close prices\n",
"data['Close'].plot(title='YTD Stock Price Change for META and TESLA', figsize=(14,7), grid=True)\n",
"```\n",
"\n",
"This code does the same thing as the previous one, but uses pandas' built-in plot function instead of matplotlib. The plot should appear directly in your IPython environment.\n",
"\n",
"--------------------------------------------------------------------------------\n",
"[*********************100%***********************] 2 of 2 completed\n"
]
},
{
"data": {
"text/plain": [
"<Axes: title={'center': 'YTD Stock Price Change for META and TESLA'}, xlabel='Date'>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "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
"text/plain": [
"<Figure size 1400x700 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"ipython_user_proxy (to assistant):\n",
"\n",
"exitcode: 0 (execution succeeded)\n",
"Code output: \n",
"Axes(0.125,0.2;0.775x0.68)\n",
"\n",
"--------------------------------------------------------------------------------\n",
"assistant (to ipython_user_proxy):\n",
"\n",
"Great! The code executed successfully. The output `Axes(0.125,0.2;0.775x0.68)` is just a representation of the plot object. The actual plot should be displayed in your IPython environment. \n",
"\n",
"The plot should show the YTD stock price change for META and TESLA. The x-axis represents the date and the y-axis represents the closing price in USD. \n",
"\n",
"If you can see the plot and it meets your requirements, then we are done here. \n",
"\n",
"TERMINATE\n",
"\n",
"--------------------------------------------------------------------------------\n"
]
}
],
"source": [
"ipy_user = IPythonUserProxyAgent(\n",
" \"ipython_user_proxy\",\n",
" human_input_mode=\"NEVER\",\n",
" max_consecutive_auto_reply=10,\n",
" is_termination_msg=lambda x: x.get(\"content\", \"\").rstrip().endswith(\"TERMINATE\") or x.get(\"content\", \"\").rstrip().endswith('\"TERMINATE\".'),\n",
")\n",
"assistant.reset()\n",
"# the assistant receives a message from the user, which contains the task description\n",
"ipy_user.initiate_chat(\n",
" assistant,\n",
" message=\"\"\"Plot a chart of META and TESLA stock price change YTD\"\"\",\n",
")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.17"
},
"vscode": {
"interpreter": {
"hash": "949777d72b0d2535278d3dc13498b2535136f6dfe0678499012e853ee9abcab1"
}
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {
"2d910cfd2d2a4fc49fc30fbbdc5576a7": {
"model_module": "@jupyter-widgets/base",
"model_module_version": "2.0.0",
"model_name": "LayoutModel",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "2.0.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "2.0.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border_bottom": null,
"border_left": null,
"border_right": null,
"border_top": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"454146d0f7224f038689031002906e6f": {
"model_module": "@jupyter-widgets/controls",
"model_module_version": "2.0.0",
"model_name": "HBoxModel",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "2.0.0",
"_model_name": "HBoxModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "2.0.0",
"_view_name": "HBoxView",
"box_style": "",
"children": [
"IPY_MODEL_e4ae2b6f5a974fd4bafb6abb9d12ff26",
"IPY_MODEL_577e1e3cc4db4942b0883577b3b52755",
"IPY_MODEL_b40bdfb1ac1d4cffb7cefcb870c64d45"
],
"layout": "IPY_MODEL_dc83c7bff2f241309537a8119dfc7555",
"tabbable": null,
"tooltip": null
}
},
"577e1e3cc4db4942b0883577b3b52755": {
"model_module": "@jupyter-widgets/controls",
"model_module_version": "2.0.0",
"model_name": "FloatProgressModel",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "2.0.0",
"_model_name": "FloatProgressModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "2.0.0",
"_view_name": "ProgressView",
"bar_style": "success",
"description": "",
"description_allow_html": false,
"layout": "IPY_MODEL_2d910cfd2d2a4fc49fc30fbbdc5576a7",
"max": 1,
"min": 0,
"orientation": "horizontal",
"style": "IPY_MODEL_74a6ba0c3cbc4051be0a83e152fe1e62",
"tabbable": null,
"tooltip": null,
"value": 1
}
},
"6086462a12d54bafa59d3c4566f06cb2": {
"model_module": "@jupyter-widgets/base",
"model_module_version": "2.0.0",
"model_name": "LayoutModel",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "2.0.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "2.0.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border_bottom": null,
"border_left": null,
"border_right": null,
"border_top": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"74a6ba0c3cbc4051be0a83e152fe1e62": {
"model_module": "@jupyter-widgets/controls",
"model_module_version": "2.0.0",
"model_name": "ProgressStyleModel",
"state": {
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "2.0.0",
"_model_name": "ProgressStyleModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "2.0.0",
"_view_name": "StyleView",
"bar_color": null,
"description_width": ""
}
},
"7d3f3d9e15894d05a4d188ff4f466554": {
"model_module": "@jupyter-widgets/controls",
"model_module_version": "2.0.0",
"model_name": "HTMLStyleModel",
"state": {
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "2.0.0",
"_model_name": "HTMLStyleModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "2.0.0",
"_view_name": "StyleView",
"background": null,
"description_width": "",
"font_size": null,
"text_color": null
}
},
"b40bdfb1ac1d4cffb7cefcb870c64d45": {
"model_module": "@jupyter-widgets/controls",
"model_module_version": "2.0.0",
"model_name": "HTMLModel",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "2.0.0",
"_model_name": "HTMLModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "2.0.0",
"_view_name": "HTMLView",
"description": "",
"description_allow_html": false,
"layout": "IPY_MODEL_f1355871cc6f4dd4b50d9df5af20e5c8",
"placeholder": "",
"style": "IPY_MODEL_ca245376fd9f4354af6b2befe4af4466",
"tabbable": null,
"tooltip": null,
"value": " 1/1 [00:00&lt;00:00, 44.69it/s]"
}
},
"ca245376fd9f4354af6b2befe4af4466": {
"model_module": "@jupyter-widgets/controls",
"model_module_version": "2.0.0",
"model_name": "HTMLStyleModel",
"state": {
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "2.0.0",
"_model_name": "HTMLStyleModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "2.0.0",
"_view_name": "StyleView",
"background": null,
"description_width": "",
"font_size": null,
"text_color": null
}
},
"dc83c7bff2f241309537a8119dfc7555": {
"model_module": "@jupyter-widgets/base",
"model_module_version": "2.0.0",
"model_name": "LayoutModel",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "2.0.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "2.0.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border_bottom": null,
"border_left": null,
"border_right": null,
"border_top": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
},
"e4ae2b6f5a974fd4bafb6abb9d12ff26": {
"model_module": "@jupyter-widgets/controls",
"model_module_version": "2.0.0",
"model_name": "HTMLModel",
"state": {
"_dom_classes": [],
"_model_module": "@jupyter-widgets/controls",
"_model_module_version": "2.0.0",
"_model_name": "HTMLModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/controls",
"_view_module_version": "2.0.0",
"_view_name": "HTMLView",
"description": "",
"description_allow_html": false,
"layout": "IPY_MODEL_6086462a12d54bafa59d3c4566f06cb2",
"placeholder": "",
"style": "IPY_MODEL_7d3f3d9e15894d05a4d188ff4f466554",
"tabbable": null,
"tooltip": null,
"value": "100%"
}
},
"f1355871cc6f4dd4b50d9df5af20e5c8": {
"model_module": "@jupyter-widgets/base",
"model_module_version": "2.0.0",
"model_name": "LayoutModel",
"state": {
"_model_module": "@jupyter-widgets/base",
"_model_module_version": "2.0.0",
"_model_name": "LayoutModel",
"_view_count": null,
"_view_module": "@jupyter-widgets/base",
"_view_module_version": "2.0.0",
"_view_name": "LayoutView",
"align_content": null,
"align_items": null,
"align_self": null,
"border_bottom": null,
"border_left": null,
"border_right": null,
"border_top": null,
"bottom": null,
"display": null,
"flex": null,
"flex_flow": null,
"grid_area": null,
"grid_auto_columns": null,
"grid_auto_flow": null,
"grid_auto_rows": null,
"grid_column": null,
"grid_gap": null,
"grid_row": null,
"grid_template_areas": null,
"grid_template_columns": null,
"grid_template_rows": null,
"height": null,
"justify_content": null,
"justify_items": null,
"left": null,
"margin": null,
"max_height": null,
"max_width": null,
"min_height": null,
"min_width": null,
"object_fit": null,
"object_position": null,
"order": null,
"overflow": null,
"padding": null,
"right": null,
"top": null,
"visibility": null,
"width": null
}
}
},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}