在 Python 中通过 OpenAI API 使用 GPT-3.5 和 GPT-4Using GPT-3.5 and GPT-4 via the OpenAI API in Python

ChatGPT 是一种用于生成文本的尖端大型语言模型。它已经改变了我们编写几乎所有类型文本的方式,从像这样的教程到自动生成的产品描述、Bing 的搜索引擎结果,以及ChatGPT for Data Science 备忘单中描述的数十种数据用例。

对于交互式使用,ChatGPT 的 Web 界面是理想的选择。但是,OpenAI(ChatGPT 背后的公司)也有一个应用程序编程接口 (API),可让您使用代码与 ChatGPT 及其其他模型进行交互。

在本教程中,您将学习如何使用openaiPython 包以编程方式与 ChatGPT 进行对话。

请注意,OpenAI 使用 GPT API 是收费的。(有时会向新用户提供免费积分,但谁获得积分以及这笔交易将持续多长时间并不透明。)它的成本为 0.002 美元/1000 个代币,其中 1000 个代币约等于 750 个单词。运行本教程中的所有示例一次成本应该低于 2 美分(但如果您重新运行任务,则每次都会收费)。

什么时候应该使用 API 而不是 Web 界面?

ChatGPT 网络应用程序是 GPT 模型的绝佳界面。但是,如果你想将 AI 包含到数据管道或软件中,API 更合适。数据从业者的一些可能用例包括:

  • 从数据库或其他 API 中提取数据,然后要求 GPT 对其进行汇总或生成有关它的报告
  • 在仪表板中嵌入 GPT 功能以自动提供结果的文本摘要
  • 为您的数据集市提供自然语言界面
  • 通过学术( PyPI、Conda )包引入期刊论文进行研究,并让 GPT 总结结果

设置 OpenAI 开发者帐户

要使用该 API,您需要使用 OpenAI 创建一个开发者帐户。您需要准备好您的电子邮件地址、电话号码以及借记卡或信用卡详细信息。

按着这些次序:

  1. 转到API 注册页面。
  2. 创建您的帐户(您需要提供您的电子邮件地址和电话号码)。
  3. 转到API 密钥页面。
  4. 创建一个新的密钥。
  5. 复制此密钥。(如果丢失,请删除密钥并创建一个新密钥。)
  6. 转到“付款方式”页面。
  7. 点击“添加付款方式”并填写您的银行卡详细信息。

安全地存储您的帐户凭据

秘钥需要保密!否则,其他人可以使用它来访问 API,您将为此付费。以下步骤描述了如何使用 DataCamp Workspace 安全地存储您的密钥。如果您使用的是其他平台,请查看该平台的文档。您也可以向 ChatGPT 寻求建议。这是一个建议的提示:

> 您是 IT 安全专家。您正在与数据科学家交谈。解释安全存储用于 API 访问的私钥的最佳实践。

  1. 在您的工作区中,单击“集成”
  2. 单击“创建集成”加号按钮
  3. 选择“环境变量”集成
  4. 在“名称”字段中,键入“OPENAI”。在“值”字段中,粘贴您的密钥
  5. 单击“创建”,并连接新的集成

设置 Python

要通过 API 使用 GPT,您需要导入osopenaiPython 包。

如果您使用的是 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"]

API调用GPT的Code Pattern

调用 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 的 OpenAI API 模型名称

模型名称列在开发人员文档的“模型概述”页面中。在本教程中,您将使用gpt-3.5-turbo,这是 ChatGPT 使用的具有公共 API 访问权限的最新模型。(当它变得广泛可用时,您会想要切换到gpt-4.)

OpenAI API GPT 消息类型

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.
由 DATACAMP 工作区提供支持

让我们将此消息包含在前面的 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}])
由 DATACAMP 工作区提供支持

检查 GPT 的响应是否正常

API 调用是“有风险的”,因为问题可能发生在您的笔记本之外,例如互联网连接问题,或者服务器向您发送数据时出现问题,或者因为您用完了 API 信用。您应该检查您得到的响应是否正常。

GPT 模型返回具有四个值之一的状态代码,记录在聊天文档的响应格式部分中。

  • stop:API 返回完整的模型输出
  • length: 由于 max_tokens 参数或令牌限制,模型输出不完整
  • content_filter:由于我们的内容过滤器中的标记而省略了内容
  • null: API 响应仍在进行中或不完整

GPT API 以 JSON 格式向 Python 发送数据,因此响应变量包含深度嵌套的列表和字典。工作起来有点痛苦!

对于名为 的响应变量response,状态代码存储在以下位置。

response["choices"][0]["finish_reason"]

提取AI助手的消息

隐藏在响应变量中的是我们要求 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 |

使用辅助函数调用 GPT

您需要编写大量重复的样板代码来完成这三件简单的事情。有一个包装函数来抽象掉无聊的部分是很有用的。这样,我们就可以专注于数据科学用例。

希望 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 助手的回复

在许多情况下,您会希望与 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.

在管道中使用 GPT

通过 Web 界面使用 API 的一个巨大优势是您可以将 GPT 与其他 API 结合使用。从一个或多个来源提取数据,然后对其应用 AI 是一种强大的工作流程。

将 GPT 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.

你可能感兴趣的:(人工智能,python,emacs,flask,plotly)