赞
踩
ChatGPT 是一种用于生成文本的尖端大型语言模型。它已经改变了我们编写几乎所有类型文本的方式,从像这样的教程到自动生成的产品描述、Bing 的搜索引擎结果,以及ChatGPT for Data Science 备忘单中描述的数十种数据用例。
对于交互式使用,ChatGPT 的 Web 界面是理想的选择。但是,OpenAI(ChatGPT 背后的公司)也有一个应用程序编程接口 (API),可让您使用代码与 ChatGPT 及其其他模型进行交互。
在本教程中,您将学习如何使用openai
Python 包以编程方式与 ChatGPT 进行对话。
请注意,OpenAI 使用 GPT API 是收费的。(有时会向新用户提供免费积分,但谁获得积分以及这笔交易将持续多长时间并不透明。)它的成本为 0.002 美元/1000 个代币,其中 1000 个代币约等于 750 个单词。运行本教程中的所有示例一次成本应该低于 2 美分(但如果您重新运行任务,则每次都会收费)。
ChatGPT 网络应用程序是 GPT 模型的绝佳界面。但是,如果你想将 AI 包含到数据管道或软件中,API 更合适。数据从业者的一些可能用例包括:
要使用该 API,您需要使用 OpenAI 创建一个开发者帐户。您需要准备好您的电子邮件地址、电话号码以及借记卡或信用卡详细信息。
按着这些次序:
秘钥需要保密!否则,其他人可以使用它来访问 API,您将为此付费。以下步骤描述了如何使用 DataCamp Workspace 安全地存储您的密钥。如果您使用的是其他平台,请查看该平台的文档。您也可以向 ChatGPT 寻求建议。这是一个建议的提示:
> 您是 IT 安全专家。您正在与数据科学家交谈。解释安全存储用于 API 访问的私钥的最佳实践。
要通过 API 使用 GPT,您需要导入os
和openai
Python 包。
如果您使用的是 Jupyter Notebook(如 DataCamp Workspace),从IPython.display
.
一个示例还使用 yfinance 包来检索股票价格。
-
-
# Import the os package import os # Import the openai package import openai # From the IPython.display package, import display and Markdown from IPython.display import display, Markdown # Import yfinance as yf import yfinance as yf
另一个设置任务是将您刚刚创建的环境变量放在 openai 包可以看到的地方。
-
-
# Set openai.api_key to the OPENAI environment variable openai.api_key = os.environ["OPENAI"]
调用 OpenAI API 并获取聊天响应的代码模式如下:
-
-
response = openai.ChatCompletion.create( model="MODEL_NAME", messages=[{"role": "system", "content": 'SPECIFY HOW THE AI ASSISTANT SHOULD BEHAVE'}, {"role": "user", "content": 'SPECIFY WANT YOU WANT THE AI ASSISTANT TO SAY'} ])
这里有几件事情要解包。
模型名称列在开发人员文档的“模型概述”页面中。在本教程中,您将使用gpt-3.5-turbo
,这是 ChatGPT 使用的具有公共 API 访问权限的最新模型。(当它变得广泛可用时,您会想要切换到gpt-4
.)
Chat 文档简介中记录了三种类型的消息:
system
消息描述了 AI 助手的行为。数据科学用例的有用系统消息是“您是了解数据科学的乐于助人的助手”。user
消息描述您希望 AI 助手说什么。我们将在本教程中介绍用户消息的示例assistant
消息描述对话中先前的响应。我们将在后面的任务中介绍如何进行交互式对话第一条消息应该是系统消息。附加消息应该在用户和助手之间交替。
生成示例数据集对于针对不同的数据场景测试您的代码或向其他人演示代码非常有用。要从 GPT 获得有用的响应,您需要精确并指定数据集的详细信息,包括:
下面是创建数据集的示例用户消息。
Create a small dataset about total sales over the last year. The format of the dataset should be a data frame with 12 rows and 2 columns. The columns should be called "month" and "total_sales_usd". The "month" column should contain the shortened forms of month names from "Jan" to "Dec". The "total_sales_usd" column should contain random numeric values taken from a normal distribution with mean 100000 and standard deviation 5000. Provide Python code to generate the dataset, then provide the output in the format of a markdown table.
让我们将此消息包含在前面的 Code Pattern 中。
-
-
# Define the system message system_msg = 'You are a helpful assistant who understands data science.' # Define the user message user_msg = 'Create a small dataset about total sales over the last year. The format of the dataset should be a data frame with 12 rows and 2 columns. The columns should be called "month" and "total_sales_usd". The "month" column should contain the shortened forms of month names from "Jan" to "Dec". The "total_sales_usd" column should contain random numeric values taken from a normal distribution with mean 100000 and standard deviation 5000. Provide Python code to generate the dataset, then provide the output in the format of a markdown table.' # Create a dataset using GPT response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "system", "content": system_msg}, {"role": "user", "content": user_msg}])
API 调用是“有风险的”,因为问题可能发生在您的笔记本之外,例如互联网连接问题,或者服务器向您发送数据时出现问题,或者因为您用完了 API 信用。您应该检查您得到的响应是否正常。
GPT 模型返回具有四个值之一的状态代码,记录在聊天文档的响应格式部分中。
stop
:API 返回完整的模型输出length
: 由于 max_tokens 参数或令牌限制,模型输出不完整content_filter
:由于我们的内容过滤器中的标记而省略了内容null
: API 响应仍在进行中或不完整GPT API 以 JSON 格式向 Python 发送数据,因此响应变量包含深度嵌套的列表和字典。工作起来有点痛苦!
对于名为 的响应变量response
,状态代码存储在以下位置。
response["choices"][0]["finish_reason"]
隐藏在响应变量中的是我们要求 GPT 生成的文本。幸运的是,它总是在同一个地方。
response["choices"][0]["message"]["content"]
可以像往常一样使用 打印响应内容print(content)
,但它是 Markdown 内容,Jupyter 笔记本可以通过display(Markdown(content))
Here's the Python code to generate the dataset: import numpy as np import pandas as pd # Set random seed for reproducibility np.random.seed(42) # Generate random sales data sales_data = np.random.normal(loc=100000, scale=5000, size=12) # Create month abbreviation list month_abbr = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] # Create dataframe sales_df = pd.DataFrame({'month': month_abbr, 'total_sales_usd': sales_data}) # Print dataframe print(sales_df) And here's the output in markdown format: | month | total_sales_usd | |-------|----------------| | Jan | 98728.961189 | | Feb | 106931.030292 | | Mar | 101599.514152 | | Apr | 97644.534384 | | May | 103013.191014 | | Jun | 102781.514665 | | Jul | 100157.741173 | | Aug | 104849.281004 | | Sep | 100007.081991 | | Oct | 94080.272682 | | Nov | 96240.993328 | | Dec | 104719.371461 |
您需要编写大量重复的样板代码来完成这三件简单的事情。有一个包装函数来抽象掉无聊的部分是很有用的。这样,我们就可以专注于数据科学用例。
希望 OpenAI 会改进他们的 Python 包的接口,以便内置这种东西。同时,请随意在您自己的代码中使用它。
该函数有两个参数。
system
:包含系统消息的字符串。user_assistant
: 一个字符串数组,用于交替显示用户消息和助理消息。返回值是生成的内容。
def chat(system, user_assistant): assert isinstance(system, str), "`system` should be a string" assert isinstance(user_assistant, list), "`user_assistant` should be a list" system_msg = [{"role": "system", "content": system}] user_assistant_msgs = [ {"role": "assistant", "content": user_assistant[i]} if i % 2 else {"role": "user", "content": user_assistant[i]} for i in range(len(user_assistant))] msgs = system_msg + user_assistant_msgs response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=msgs) status_code = response["choices"][0]["finish_reason"] assert status_code == "stop", f"The status code was {status_code}." return response["choices"][0]["message"]["content"]
此函数的示例用法是
response_fn_test = chat("You are a machine learning expert.",["Explain what a neural network is."]) display(Markdown(response_fn_test))
A neural network is a type of machine learning model that is inspired by the architecture of the human brain. It consists of layers of interconnected processing units, called neurons, that work together to process and analyze data. Each neuron receives input from other neurons or from external sources, processes that input using a mathematical function, and then produces an output that is passed on to other neurons in the network. The structure and behavior of a neural network can be adjusted by changing the weights and biases of the connections between neurons. During the training process, the network learns to recognize patterns and make predictions based on the input it receives. Neural networks are often used for tasks such as image classification, speech recognition, and natural language processing, and have been shown to be highly effective at solving complex problems that are difficult to solve with traditional rule-based programming methods.
在许多情况下,您会希望与 AI 进行更长时间的对话。也就是说,您向 GPT 发送一个提示,得到回复,然后发送另一个提示以继续聊天。在这种情况下,您需要在第二次调用 API 时包含 GPT 的先前响应,以便 GPT 具有完整的上下文。这将提高响应的准确性并提高整个对话的一致性。
为了重用 GPT 的消息,您从响应中检索它,然后将其传递到新的聊天调用中。
让我们尝试从先前生成的数据集中计算销售列的平均值。chat()
请注意,因为我们第一次没有使用该功能,所以我们必须使用更长的子集代码来获取先前的响应文本。如果使用chat()
,代码会更简单。
# Assign the content from the response in Task 1 to assistant_msg assistant_msg = response["choices"][0]["message"]["content"] # Define a new user message user_msg2 = 'Using the dataset you just created, write code to calculate the mean of the `total_sales_usd` column. Also include the result of the calculation.' # Create an array of user and assistant messages user_assistant_msgs = [user_msg, assistant_msg, user_msg2] # Get GPT to perform the request response_calc = chat(system_msg, user_assistant_msgs) # Display the generated content display(Markdown(response_calc))
Sure! Here's the code to calculate the mean of the `total_sales_usd` column: ```python mean_sales = sales_df['total_sales_usd'].mean() print("Mean sales: $", round(mean_sales, 2)) ``` And here's the output of this code: ``` Mean sales: $ 100077.57 ``` Therefore, the mean of total sales over the last year is about $100,077.57.
通过 Web 界面使用 API 的一个巨大优势是您可以将 GPT 与其他 API 结合使用。从一个或多个来源提取数据,然后对其应用 AI 是一种强大的工作流程。
在这里,我们将使用 weather2 包 ( PyPI )获取天气预报,并使用 GPT 来提出活动的想法。
# Import the weather2 package import weather # Get the forecast for Miami location = "Miami" forecast = weather.forecast(location) # Pull out forecast data for midday tomorrow fcast = forecast.tomorrow["12:00"] # Create a prompt user_msg_weather = f"In {location} at midday tomorrow, the temperature is forecast to be {fcast.temp}, the wind speed is forecast to be {fcast.wind.speed} m/s, and the amount of precipitation is forecast to be {fcast.precip}. Make a list of suitable leisure activities." # Call GPT response_activities = chat("You are a travel guide.", [user_msg_weather]) display(Markdown(response_activities))
With mild temperatures and calm winds, Miami is the perfect place for leisure activities. Here are some suggestions:
1. Visit Miami's beaches and soak up some sun or take a dip in the ocean!
2. Explore Miami's art scene with a visit to the Perez Art Museum Miami or the Wynwood Walls.
3. Take a stroll along the famous Ocean Drive and enjoy the colorful Art Deco architecture.
4. Head to Little Havana to experience the Cuban culture and delicious cuisine.
5. Enjoy a scenic walk or bike ride through one of Miami's many parks, such as Bayfront Park or South Pointe Park.
6. Visit the Miami Seaquarium and see some incredible marine life up close.
7. Take a boat tour to see the stunning Miami skyline from the water.
8. Shopping enthusiasts can explore the many high-end boutiques and outdoor shopping malls, such as Lincoln Road Mall.
9. Foodies can venture to one of the many food festivals happening throughout the year.
10. Finally, there are plenty of nightclubs and live music venues to keep the night going.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。