微信扫码
与创始人交个朋友
我要投稿
直接看一个例子吧:
模型 | llama3 |
prompt (核心描述) | 你是一个智能助手,可以通过拆解任务来解决问题,建议你通过执行Python代码来实现任务,如果代码成功执行后任务仍未解决,分析问题,重新审视你的假设,收集你需要的额外信息,并考虑尝试不同的方法。当你找到答案时,仔细验证答案。如果可能的话,在你的回复中包含可验证的证据。当一切都完成后,回复结束 |
任务 | 绘制NVDA和特斯拉股价本年迄今变化图。 将绘图保存到名为plot.png的文件中 |
角色 | assistant:大模型助手; user_proxy:用户代理 |
接下来先通过user_proxy对assistant发起提问,然后开始整个任务的执行:
首先user_proxy发起提问:
user_proxy (to assistant):
绘制NVDA和特斯拉股价本年迄今变化图。将绘图保存到名为plot.png的文件中
assistant回复:
assistant (to user_proxy):
To achieve this task, we'll need to collect the stock price data for NVDA (NVIDIA) and TSLA (Tesla) for this year and then use a Python script to plot the data.
First, let's collect the data using the `yfinance` library. Here's the code to get the data:
```python
# filename: get_stock_data.py
import yfinance as yf
start_date = '2023-01-01'
end_date = '2023-12-31'
nvda_data = yf.download('NVDA', start=start_date, end=end_date)
tsla_data = yf.download('TSLA', start=start_date, end=end_date)
print(nvda_data.head())
print(tsla_data.head())
```
Please execute this code to get the stock price data for NVDA and TSLA for this year. The `print` statements will output the first few rows of the data.
Once you've executed the code, please provide the output, and I'll proceed to create the plot.
(Note: If you don't have the `yfinance` library installed, you can install it using `pip install yfinance`)
然后assistant开始执行:
Provide feedback to assistant. Press enter to skip and use auto-reply, or type 'exit' to end the conversation:
>>>>>>>> NO HUMAN INPUT RECEIVED.
>>>>>>>> USING AUTO REPLY...
>>>>>>>> EXECUTING CODE BLOCK (inferred language is python)...
user_proxy (to assistant):
exitcode: 1 (execution failed)
Code output: Traceback (most recent call last):
File "/Users/lvlin/code/agent_test/demo/autogen_demo/coding_local/get_stock_data.py", line 2, in <module>
import yfinance as yf
ModuleNotFoundError: No module named 'yfinance'
这里比较厉害了,assistant发现我环境没有yfinance,程序并没有终止,这时assistant再次向user_proxy交流,说明你环境缺几个包需要安装一下:
assistant (to user_proxy):
It looks like the `yfinance` library is not installed. Let's fix that!
Here's the updated code to install the `yfinance` library and then collect the stock price data:
```python
# filename: get_stock_data.py
import os
# Install yfinance library if not already installed
os.system('pip install yfinance')
import yfinance as yf
start_date = '2023-01-01'
end_date = '2023-12-31'
nvda_data = yf.download('NVDA', start=start_date, end=end_date)
tsla_data = yf.download('TSLA', start=start_date, end=end_date)
print(nvda_data.head())
print(tsla_data.head())
```
Please execute this code to install the `yfinance` library and get the stock price data for NVDA and TSLA for this year.
(Note: If you're using a virtual environment, make sure to activate it before running the code)
当然这里有一些小问题,比如模型把时间搞错了,我让它绘制的是今年的趋势,它识别成2023了,这个可以先忽略,我们可以在prompt告诉它今天的时间,模型就可以正确识别时间了;
然后assistant继续执行
Collecting yfinanceUsing cached yfinance-0.2.40-py2.py3-none-any.whl.metadata (11 kB) XXXX开始安装缺的包
安装完以后,assistant再次完善代码,加上了绘图相关代码
assistant (to user_proxy):
Now that we have the stock price data, let's create the plot.
Here's the updated code to plot the data:
```python
# filename: plot_stock_prices.py
import yfinance as yf
import matplotlib.pyplot as plt
start_date = '2023-01-01'
end_date = '2023-12-31'
nvda_data = yf.download('NVDA', start=start_date, end=end_date)
tsla_data = yf.download('TSLA', start=start_date, end=end_date)
plt.figure(figsize=(12, 6))
plt.plot(nvda_data.index, nvda_data['Close'], label='NVDA')
plt.plot(tsla_data.index, tsla_data['Close'], label='TSLA')
plt.xlabel('Date')
plt.ylabel('Stock Price (USD)')
plt.title('NVDA and TSLA Stock Prices')
plt.legend()
plt.savefig('plot.png')
plt.show()
然后再次执行,yf.download超时了,assistant进行了代码优化
assistant (to user_proxy):
It looks like the execution timed out. Let's try to optimize the code to make it more efficient.
Here's the updated code:
```python
# filename: plot_stock_prices.py
import yfinance as yf
import matplotlib.pyplot as plt
start_date = '2023-01-01'
end_date = '2023-12-31'
nvda_data = yf.download('NVDA', start=start_date, end=end_date, progress=False)
tsla_data = yf.download('TSLA', start=start_date, end=end_date, progress=False)
plt.figure(figsize=(12, 6))
plt.plot(nvda_data.index, nvda_data['Close'], label='NVDA')
plt.plot(tsla_data.index, tsla_data['Close'], label='TSLA')
plt.xlabel('Date')
plt.ylabel('Stock Price (USD)')
plt.title('NVDA and TSLA Stock Prices')
plt.legend()
plt.savefig('plot.png')
plt.close()# Close the figure to free up memory
```
I added the `progress=False` parameter to `yf.download()` to disable the progress bar, which can slow down the execution. I also added `plt.close()` to close the figure after saving it to free up memory.
Please execute this code again to generate the plot and save it to a file named `plot.png`.
If you still encounter issues, please let me know!
最后在反复调整之后,最终画出了股票趋势图
整个过程我们可以设置是否人为介入,如果设置为NEVER,就是模型自己反复在调整自己的策略,规划、反思、执行;如果设置了人为介入,可以在模型需要我们反馈时进行矫正或者提一些建议思路,模型都会听取进行改正。
虽然例子很简单,这个场景的代码人工写可能比模型快很多,但还是挺震撼的,整个过程没有人为干涉全部是模型一点一点自己实现的,让我们看到了大模型agent的一些潜力,未来可期
后面有机会再和大家分享一下agent在实际场景中的一些使用情况
53AI,企业落地应用大模型首选服务商
产品:大模型应用平台+智能体定制开发+落地咨询服务
承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业
2024-05-28
2024-04-26
2024-08-21
2024-08-13
2024-04-11
2024-07-09
2024-07-18
2024-10-25
2024-07-01
2024-06-16