Home  >  Article  >  Backend Development  >  Teach you how to use ChatGPT PHP to build an automatic question and answer system

Teach you how to use ChatGPT PHP to build an automatic question and answer system

WBOY
WBOYOriginal
2023-10-27 16:16:481443browse

教你如何使用ChatGPT PHP构建自动问答系统

Teach you how to use ChatGPT PHP to build an automatic question and answer system

Introduction:
With the continuous development of artificial intelligence technology, automatic question and answer systems have achieved success in various fields a wide range of applications. OpenAI's ChatGPT is a powerful generative model that can be used to build automatic question and answer systems. This article will introduce how to use PHP language to build an automatic question and answer system based on ChatGPT, and provide specific code examples for reference.

1. Introduction to ChatGPT
ChatGPT is an open text generation model released by OpenAI, which can use given text prompts to generate corresponding replies. This model is based on the GPT (Generative Pre-trained Transformer) architecture, uses massive Internet data for training, and can generate high-quality natural language text. ChatGPT can be used as a useful tool for the construction of automatic question answering systems.

2. Preparation work
Before starting to build an automatic question and answer system, we need to make some preparations.

  1. Install the PHP environment: First, make sure that the PHP environment has been installed on your computer. You can use the command line to run php -v to verify whether the installation is successful.
  2. Get the ChatGPT API key: In order to use ChatGPT, you need to get the OpenAI API key. Visit the OpenAI official website and register an account, and then follow the relevant instructions to obtain the API key.

3. Use ChatGPT API
After getting the API key, we can use PHP to call the ChatGPT API for text generation.

First, create a file named chatgpt.php in the project directory and write the following code:

<?php
$content = "请输入你的问题";
$request_data = array('prompt' => $content, 'max_tokens' => 50);

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://api.openai.com/v1/engines/davinci-codex/completions',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => http_build_query($request_data),
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/x-www-form-urlencoded',
        'Authorization: Bearer YOUR_API_KEY'
    ),
));

$response = curl_exec($curl);
curl_close($curl);

$data = json_decode($response, true);
$reply = $data['choices'][0]['text'];

echo $reply;
?>

In the code, $contentVariables store the user's questions or tips. We use array('prompt' => $content, 'max_tokens' => 50) to construct the request data. Among them, the prompt field is used to store the user's questions, and the max_tokens field defines the maximum length of the generated text.

You need to replace YOUR_API_KEY with the API key you obtained from the OpenAI official website.

4. Build an automatic question and answer system
We can already use the ChatGPT API to generate responses. Next, we can combine the front-end interface to build a complete automatic question and answer system.

Create a file named index.php in the project directory and write the following code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>ChatGPT 自动问答系统</title>
</head>
<body>
    <h1>ChatGPT 自动问答系统</h1>
    <form method="POST" action="index.php">
        <input type="text" name="question" placeholder="请输入你的问题">
        <input type="submit" value="提交">
    </form>
    <?php
    if($_POST['question']){
        $content = $_POST['question'];
        $request_data = array('prompt' => $content, 'max_tokens' => 50);

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://api.openai.com/v1/engines/davinci-codex/completions',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => http_build_query($request_data),
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/x-www-form-urlencoded',
                'Authorization: Bearer YOUR_API_KEY'
            ),
        ));

        $response = curl_exec($curl);
        curl_close($curl);

        $data = json_decode($response, true);
        $reply = $data['choices'][0]['text'];

        echo "<p><strong>问题:</strong>".$content."</p>";
        echo "<p><strong>回答:</strong>".$reply."</p>";
    }
    ?>
</body>
</html>

In the code, we use HTML to build a simple Forms allow users to enter questions in the input box and get answers by clicking the submit button. When the user submits a question, we pass the question to the ChatGPT API and display the returned answer on the page.

Similarly, you need to replace YOUR_API_KEY with the API key you obtained from the OpenAI official website.

5. Run the automatic question and answer system
After the preparation work is completed, we can run the automatic question and answer system locally.

Open the command line tool in the project directory and enter php -S localhost:8000 to start a local server.

Then, visit http://localhost:8000/index.php in the browser to see the interface of the automatic question and answer system. Enter the question and click the submit button, the system will generate the corresponding answer and display it on the page.

6. Summary
This article introduces how to use ChatGPT PHP to build an automatic question and answer system. By calling the ChatGPT API to obtain answers, combined with a simple front-end interface, a simple automatic question and answer system is implemented. I hope this article will be helpful to you in the process of building an automatic question and answer system.

The above is the detailed content of Teach you how to use ChatGPT PHP to build an automatic question and answer system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn