GenAIIPython 的生成式 AI 工具
GenAI 是一个 IPython 扩展,可以帮助快速诊断错误和异常。
%pip install genai
%load_ext genai
通过从IPython中提取上下文,将其与OpenAI的大型语言模型混合在一起,并赋予你在所有Jupyter环境中生成代码单元并在发生异常后获得建议的能力,包括 IPython、JupyterLab、Jupyter Notebook和Noteable。
在 notebook 启动时,genai 注册为自定义 IPython 异常处理程序。当用户遇到错误时,genai 会自动将错误发送到 OpenAI 的 GPT-3.5-turbo 模型。
通过将 OpenAI 的新 ChatCompletion 端点与 IPython 的执行上下文连接起来,genai 可以推荐量身定制的建议来解锁用户。
在 notebook 内部工作自然是用户与系统之间的对话。交互式计算的这种对话方面非常适合邀请 GPT 加入他们的对话。
特性:
-
%%assist
magic command 从自然语言生成代码 - 自定义异常建议
In [1]: %load_ext genai In [2]: import pandas as pd In [3]: df = pd.DataFrame(dict(col1=['a', 'b', 'c']), index=['first', 'second', 'third']) In [4]: df.sort_values() --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[4], line 1 ----> 1 df.sort_values() File ~/.pyenv/versions/3.9.9/lib/python3.9/site-packages/pandas/util/_decorators.py:331, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs) 325 if len(args) > num_allow_args: 326 warnings.warn( 327 msg.format(arguments=_format_argument_list(allow_args)), 328 FutureWarning, 329 stacklevel=find_stack_level(), 330 ) --> 331 return func(*args, **kwargs) TypeError: sort_values() missing 1 required positional argument: 'by'
示例:
In [1]: %load_ext genai In [2]: %%assist ...: ...: # Pull census data ...: 'What would a data analyst do? 🤔' In [3]: # generated with %%assist ...: # Pull census data ...: # To pull census data we can use the `requests` library to send a GET request to the appropriate API endpoint. ...: # First, import the requests module ...: import requests ...: ...: # Define the URL endpoint to the Census API ...: url = "https://api.census.gov/data/2019/pep/population" ...: ...: # Define the parameters needed for the API request, such as dataset and variables requested ...: params = { ...: "get": "POP", ...: "for": "state:*", ...: } ...: ...: # Send a GET request to the Census API endpoint with the parameters ...: response = requests.get(url, params=params) ...: ...: # Access the response content ...: content = response.content ...: ...: # The Census data is now stored in the `content` variable and can be processed or saved elsewhere. The user can modify the `params` variable to request different data or specify a different API endpoint. In [6]: content Out[6]: b'[["POP","state"],\n["4903185","01"],\n["731545","02"],\n["7278717","04"],\n["3017804","05"],\n["39512223","06"],\n["5758736","08"],\n["973764","10"],\n["705749","11"],\n["3565287","09"],\n["21477737","12"],\n["10617423","13"],\n["1787065","16"],\n["1415872","15"],\n["12671821","17"],\n["6732219","18"],\n["3155070","19"],\n["2913314","20"],\n["4467673","21"],\n["4648794","22"],\n["1344212","23"],\n["6045680","24"],\n["6892503","25"],\n["9986857","26"],\n["5639632","27"],\n["2976149","28"],\n["6137428","29"],\n["1068778","30"],\n["1934408","31"],\n["3080156","32"],\n["1359711","33"],\n["8882190","34"],\n["2096829","35"],\n["19453561","36"],\n["10488084","37"],\n["762062","38"],\n["11689100","39"],\n["3956971","40"],\n["4217737","41"],\n["12801989","42"],\n["1059361","44"],\n["5148714","45"],\n["884659","46"],\n["6829174","47"],\n["28995881","48"],\n["623989","50"],\n["3205958","49"],\n["8535519","51"],\n["7614893","53"],\n["1792147","54"],\n["5822434","55"],\n["578759","56"],\n["3193694","72"]]'
评论