AutoGen环境搭建与示例运行
AutoGen目前正火热
AutoGen是一个让LLM(大语言模型)相互聊天过程中解决任务的框架,可定制、可对话,并允许人类无缝参与。
直接上干货,了解AutoGen,并能运行。
AutoGen主要资源
参考博客:https://blog.csdn.net/techforward/article/details/133863795,了解主要介绍,下面直接给出所需步骤。
- 代码地址 https://github.com/microsoft/autogen.git
- 帮助文档:https://microsoft.github.io/autogen/docs/Getting-Started
运行环境搭建
- pip install pyautogen
- 修改autogen\test下的twoagent.py文件
- 代码修改如下:
#注释原代码config_list = 所在行
#config_list = config_list_from_json(env_or_file="OAI_CONFIG_LIST")
#加入如下语句,注意api_key后面的key为你要用的openai的key,默认是14天有效的那个。后面*号是因为太长了省略了,这个一定要用有效的,重要的东西重复一遍。
config_list = [{'model': 'gpt-3.5-turbo', 'api_key': 'eyJhbGciOiJSUzI1NiIsInR5c***'},]
如图所示:
运行代码
前提安装好python3.8以上版本。在AutoGen目录下执行:
- pip install pyautogen
- python test/twoagent.py
- 正常会输出如下内容:
user_proxy (to assistant):
Plot a chart of NVDA and TESLA stock price change YTD.
--------------------------------------------------------------------------------
assistant (to user_proxy):
To plot the chart of NVDA and TESLA stock price change Year-to-Date (YTD), we need to first collect the historical stock prices for both NVDA and TESLA. There are various APIs and libraries available to fetch stock prices. In this case, I will use the yfinance library to retrieve the stock prices.
Here is the Python code to accomplish this:
```python
# filename: stock_price_chart.py
import yfinance as yf
import matplotlib.pyplot as plt
# Define the ticker symbols for NVDA and TSLA
nvda = yf.Ticker("NVDA")
tsla = yf.Ticker("TSLA")
# Get the historical stock prices for YTD
nvda_hist = nvda.history(period="YTD")
tsla_hist = tsla.history(period="YTD")
# Plotting the stock price changes
fig, ax = plt.subplots()
ax.plot(nvda_hist.index, nvda_hist["Close"], label="NVDA")
ax.plot(tsla_hist.index, tsla_hist["Close"], label="TESLA")
ax.set_title("YTD Stock Price Change")
ax.set_xlabel("Date")
ax.set_ylabel("Stock Price")
ax.legend()
# Display the chart
plt.show()
Please make sure you have the following libraries installed:
- yfinance
- matplotlib
You can install the libraries using pip:
pip install yfinance matplotlib
Save the code in a file named stock_price_chart.py
, then execute it. The code will retrieve the historical stock prices for NVDA and TESLA for the current year and plot a chart showing the stock price changes.
Let me know if you need any further assistance!
Provide feedback to assistant. Press enter to skip and use auto-reply, or type ‘exit’ to end the conversation:
以上为输出内容;
- 直接回车结束代码
- 在目录下新建一个文件名称为:stock_price_chart.py
- 执行:pip install yfinance matplotlib
- 执行:python stock_price_chart.py
- 输出如下:可拉长显示,这样横坐标日期就没有重叠了。
总结:如果想尝试更多功能,可以继续运行里面的范例,最好的4的token,方便使用。
参考如下:
[1]: https://github.com/microsoft/autogen
[2]: https://microsoft.github.io/autogen/docs/Getting-Started