如何连接到ChatGPT API

闪电发卡2年前ChatGPT2314

image.png

由于其独特、几乎准确且类似人类的响应,聊天 GPT 如今在互联网上引起了太多讨论。本文讨论如何通过Python代码连接Chat GPT API。

第 1 步:获取 OpenAI API 的 API 密钥

要获取 OpenAI API 的 API 密钥,您需要在 OpenAI 网站上注册 OpenAI 帐户。拥有帐户后,您可以按照以下步骤创建 API 密钥:

  • 在OpenAI 网站上登录您的 OpenAI 帐户

  • 单击页面右上角的“查看 API 密钥”按钮。

  • 单击“创建 API 密钥”按钮生成新的 API 密钥。


连接到聊天 GPT API


  • 生成 API 密钥后,您可以复制它并在代码中使用它来通过 OpenAI API 进行身份验证。

第2步:下载OpenAi库

要通过 Python 中的 OpenAI API 连接到 GPT-3,您需要通过运行以下命令来安装 openai 库:

pip install --upgrade openai



步骤 3:创建 Python 代码以连接 Chat GPT

您可以使用开放的 AI 库连接到 Chat GPT 并生成文本。以下是如何使用开放 AI 库通过 GPT-3 生成文本的示例:

import openai,os,sysopenai.api_key = os.environ['api_key']messages = [
        {"role": "system", "content": "You are a helpful assistant."},]while True:
    message = input("You: ")
    if message:
        messages.append(
                {"role": "user", "content": message},
        )
        chat_completion = openai.ChatCompletion.create(
                model="gpt-3.5-turbo",
                messages=messages
        )
    answer = chat_completion.choices[0].message.content
    print(f"ChatGPT: {answer}")
    messages.append({"role": "assistant", "content": answer})

第4步:通过代码与聊天GPT交互

导出 OpenAI API 密钥

export api_key=xxxxxxxxxxx

访问聊天 GPT API



注意:本文使用新的 ChatGPT API 引擎。我们可以从“ OpenAI ChatGPT API 等待列表”页面请求 Chat GPT API 

使用 PHP 代码连接到 ChatGPT API

要连接到 ChatGPT 的 OpenAI API,需要拥有 API 密钥并向 API 端点发送 POST 请求。可以使用 PHP 中的curl 库来实现这一点。以下是如何使用 PHP 连接到 ChatGPT API 的示例:

安装php-curl

composer require ext-curl

PHP代码:

将以下 PHP 脚本中的 API_KEY 变量替换为您的实际 OpenAI API 密钥:

<?php

// Replace this with your actual API key
$API_KEY = 'your_api_key_here';

$url = 'https://api.openai.com/v1/engines/gpt-3.5-turbo/completions';

$headers = [
    'Content-Type: application/json',
    'Authorization: Bearer ' . $API_KEY,
];

// Replace this prompt with your question
$question = 'What is the capital city of France?';

$data = [
    'prompt' => $question,
    'max_tokens' => 50,
    'n' => 1,
    'stop' => ['\n'],
];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
} else {
    $decoded_response = json_decode($response, true);

    if (isset($decoded_response['choices'])) {
        $answer = $decoded_response['choices'][0]['text'];
        echo 'Answer: ' . $answer;
    } else {
        echo "An error occurred while processing the API response.\n";
        echo "Response: " . $response . "\n";
    }
}

curl_close($ch);

?>

将 $question 变量替换为您的问题,代码将将该问题发送到 GPT-3.5-turbo API。响应将由 Chat GPT API 进行解码并打印答案。


闪电发卡ChatGPT产品推荐:

ChatGPT独享账号:https://www.chatgptzh.com/post/86.html

ChatGPT Plus共享账号:https://www.chatgptzh.com/post/319.html

ChatGPT Plus独享账号(购买充值代充订阅):https://www.chatgptzh.com/post/306.html

ChatGPT APIKey购买充值(直连+转发):https://www.chatgptzh.com/post/305.html

ChatGPT Plus国内镜像逆向版:https://www.chatgptzh.com/post/312.html

ChatGPT国内版(AIChat):https://www.chatgptzh.com/post/318.html


相关文章

在云服务器上搭建个人版ChatGPT及后端Spring Boot集成ChatGPT

在云服务器上搭建个人版ChatGPT及后端Spring Boot集成ChatGPT

一、国内服务器上搭建chat GPT首先,你需要准备以下东西:1、一台可以访问公网的Linux云服务器,最低配置1核2G即可(当然,有钱可以任性,买最高配置)2、ChatGPT的密钥3、开源的仿Cha...

如何在向 ChatGPT 发送 API 请求之前计算令牌

如何在向 ChatGPT 发送 API 请求之前计算令牌

在向 ChatGPT 发送 API 请求之前,了解令牌的计算方式非常重要。令牌是直接影响 API 限制和成本的文本片段。因此,您需要了解令牌的确切数量并管理成本,同时遵守 API 限制。如何在向 Ch...

一文读懂 ChatGPT API 接入指南

一文读懂 ChatGPT API 接入指南

最近 ChatGPT 突然爆火。抱着好奇的心态我也去官网注册账号体验了一下,因为网站人数太多,一时半会竟然注册不了,不过最终还是成功注册了。还没注册的朋友们可以参考一下这篇教程 https:...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。