使用Python和OpenAI API探索GPT-3.5和GPT-4的应用

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 Web 应用程序是 GPT 模型的绝佳接口。然而,如果你想将人工智能纳入数据管道或软件中,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 消息类型

    聊天简介文档中记录了三种类型的消息:

  • system消息描述了人工智能助手的行为。对于数据科学用例来说,一条有用的系统消息是“您是了解数据科学的有用助手”。

  • user消息描述了您希望人工智能助手说的话。我们将在本教程中介绍用户消息的示例

  • 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}])
    

    检查 GPT 的响应是否正常

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

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

  • stop:API返回完整的模型输出

  • length:由于 max_tokens 参数或 token 限制导致模型输出不完整

  • 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 助手的响应

    在许多情况下,您会希望与人工智能进行更长时间的对话。也就是说,您向 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 结合起来。从一个或多个来源提取数据,然后将人工智能应用到其中,这是一种强大的工作流程。

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

    物联沃分享整理
    物联沃-IOTWORD物联网 » 使用Python和OpenAI API探索GPT-3.5和GPT-4的应用

    发表评论