├── License.txt ├── README.md ├── config ├── api.php ├── chatbot.sql └── db.php └── public ├── License.txt ├── Readme.md ├── css └── style.css ├── img ├── chatbot_img.png └── dots-loading.gif ├── index.html ├── index.php └── js └── app.js /License.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Vikashkumar1111 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Chat_bot API Project in PHP 2 | 3 | This project is an **ChatGpt** **API** for a chat bot implemented in PHP. The API provides endpoints for sending messages to the bot, retrieving responses, and managing conversation history. 4 | 5 | Using this project integrates of OpenAI ChatGPT API for text completion. The API provides an easy way to generate human-like text using natural language processing. 6 | 7 | ## Getting Started 8 | 9 | To get started with this project, you will need to have PHP and a web server installed on your machine. You will also need a database to store conversation history. 10 | 11 | And,you will need an OpenAI API key. You can sign up for an API key on the [OpenAI website](https://beta.openai.com/signup/). 12 | 13 | ### Installation 14 | 15 | Clone the repository to your local machine. 16 | 17 | git clone https://github.com/vikashkumar1111/Chat_bot.git 18 | 19 | 20 | ## Screenshot 21 |

chatbot_img.png

22 | 23 | ## Usage 24 | 25 | ### Sending a message to the bot 26 | 27 | To send a message to the bot, make a POST request to the `/api/messages` endpoint with the following JSON payload: 28 | 29 | { 30 | "model": "text-davinci-003", 31 | "prompt": "Write a tagline for an ice cream shop.", 32 | "temperature": 0.4, 33 | "max_tokens": 64, 34 | "top_p": 1, 35 | "frequency_penalty": 0, 36 | "presence_penalty": 0 37 | } 38 | 39 | 40 | 41 | ### Retrieving bot responses 42 | 43 | To retrieve responses from the bot, make a GET request to the `/api/messages` endpoint with the following query parameters: 44 | 45 | - `user_id`: The ID of the user who sent the message. 46 | - `limit`: The maximum number of responses to retrieve (optional, default is 10). 47 | 48 | ### Managing conversation history 49 | 50 | The API also provides endpoints for managing conversation history: 51 | 52 | - `GET /api/conversations`: Retrieve a list of all conversations. 53 | - `GET /api/conversations/{id}`: Retrieve a specific conversation by ID. 54 | 55 | 56 | 57 | You can find a complete list of available parameters in the [OpenAI API documentation](https://beta.openai.com/docs/api-reference/completions/create). 58 | 59 | 60 | ## License 61 | 62 | This project is licensed under the MIT License - see the [LICENSE](/License.txt) file for details. 63 | 64 | 65 | I hope this helps you integrate the **ChatGPT** **API** into your project! 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /config/api.php: -------------------------------------------------------------------------------- 1 | "text-davinci-003", 8 | "prompt" => $str, 9 | "temperature" => 0, 10 | "max_tokens" => 500 11 | ); 12 | $postdata = json_encode($postdata); 13 | curl_setopt_array($curl, array( 14 | CURLOPT_URL => 'https://api.openai.com/v1/completions', 15 | CURLOPT_RETURNTRANSFER => true, 16 | CURLOPT_ENCODING => '', 17 | CURLOPT_MAXREDIRS => 10, 18 | CURLOPT_TIMEOUT => 0, 19 | CURLOPT_FOLLOWLOCATION => true, 20 | CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 21 | CURLOPT_CUSTOMREQUEST => 'POST', 22 | CURLOPT_POSTFIELDS => $postdata, 23 | CURLOPT_HTTPHEADER => array( 24 | 'Content-Type: application/json', 25 | 'Authorization: Bearer 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' 26 | ), 27 | )); 28 | 29 | $response = curl_exec($curl); 30 | curl_close($curl); 31 | //echo $response; 32 | $result = json_decode($response, true); 33 | $newdate = date('Y-m-d'); 34 | if (array_key_exists("error", $result)) { 35 | echo "Key exists!"; 36 | $message = "Oops! We ran into an issue in :" . $result['error']['message']; 37 | } else { 38 | //echo "Key does not exist!"; 39 | $message = $result['choices'][0]['text']; 40 | } 41 | //exit; 42 | $botreply = array("answer" => $message, "recived_date" => $newdate); 43 | // Prepare and bind statement 44 | $stmt = $conn->prepare("INSERT INTO chat_data (message_send,send_date, message_recived, recived_date) VALUES (?, ?, ?,?)"); 45 | $stmt->bind_param("ssss", $message_send, $send_date, $message_recived, $recived_date); 46 | 47 | // Set parameters and execute 48 | $message_send = $_POST['question']; 49 | $send_date = date('Y-m-d'); 50 | $message_recived = $botreply['answer']; 51 | $recived_date = $botreply['recived_date']; 52 | $stmt->execute(); 53 | $stmt->close(); 54 | ?> 55 | 56 |
57 |
User Avatar
58 |
59 |
60 |
61 |
62 |
User Avatar
63 |
64 |
65 |
66 | 67 | -------------------------------------------------------------------------------- /config/chatbot.sql: -------------------------------------------------------------------------------- 1 | -- phpMyAdmin SQL Dump 2 | -- version 5.2.0 3 | -- https://www.phpmyadmin.net/ 4 | -- 5 | -- Host: 127.0.0.1 6 | -- Generation Time: Feb 21, 2023 at 03:30 PM 7 | -- Server version: 10.4.24-MariaDB 8 | -- PHP Version: 7.4.29 9 | 10 | SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; 11 | START TRANSACTION; 12 | SET time_zone = "+00:00"; 13 | 14 | 15 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; 16 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; 17 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; 18 | /*!40101 SET NAMES utf8mb4 */; 19 | 20 | -- 21 | -- Database: `chatbot` 22 | -- 23 | 24 | -- -------------------------------------------------------- 25 | 26 | -- 27 | -- Table structure for table `chat_data` 28 | -- 29 | 30 | CREATE TABLE `chat_data` ( 31 | `id` int(50) NOT NULL, 32 | `message_send` longtext NOT NULL, 33 | `send_date` date NOT NULL, 34 | `message_recived` longtext NOT NULL, 35 | `recived_date` date NOT NULL 36 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; 37 | 38 | -- 39 | -- Dumping data for table `chat_data` 40 | -- 41 | 42 | INSERT INTO `chat_data` (`id`, `message_send`, `send_date`, `message_recived`, `recived_date`) VALUES 43 | (1, 'Hey! How are you doing?', '2023-02-19', 'I\'m doing well, thanks for asking. How about you?', '2023-02-20'), 44 | (2, 'Tell me about Sun', '2023-02-20', 'The Sun is the star at the center of the Solar System. It is a nearly perfect sphere of hot plasma, with internal convective motion that generates a magnetic field via a dynamo process. It is by far the most important source of energy for life on Earth. Its diameter is about 1.39 million kilometers, or 109 times that of Earth, and its mass is about 330,000 times that of Earth. About three quarters of the Sun\'s mass consists of hydrogen; the rest is mostly helium, with much smaller quantities of heavier elements, including oxygen, carbon, neon, and iron.', '2023-02-20'), 45 | (3, 'what is universe', '2023-02-20', '\n\nThe universe is the entirety of all space and time, all matter and energy, and all the phenomena within it. It includes planets, stars, galaxies, and all other forms of matter and energy. The universe is believed to have begun with the Big Bang about 13.8 billion years ago.', '2023-02-20'), 46 | (6, 'what is API', '2023-02-20', '\n\nAPI stands for Application Programming Interface. It is a set of protocols, routines, and tools for building software applications. An API specifies how software components should interact and APIs are used when programming graphical user interface (GUI) components.', '2023-02-20'), 47 | (9, 'Hii there', '2023-02-20', '\n\nHi there! How can I help you?', '2023-02-20'), 48 | (10, 'what is php', '2023-02-20', '\n\nPHP (Hypertext Preprocessor) is a server-side scripting language used to create dynamic web pages. It is a widely-used open source language that is especially suited for web development and can be embedded into HTML. PHP code is executed on the server and the result is returned to the browser as plain HTML.', '2023-02-20'), 49 | (13, 'who is vikash', '2023-02-20', '\n\nVikash is a software engineer and entrepreneur from India. He is the founder and CEO of a software development company called Vyom Labs. He has over 10 years of experience in software development and has worked on projects for companies such as Microsoft, Oracle, and IBM. He is passionate about technology and is always looking for ways to use it to make the world a better place.', '2023-02-20'), 50 | (14, 'who is vikkuvikash', '2023-02-20', '\n\nVikkuvikash is a software engineer and entrepreneur from India. He is the founder and CEO of a software development company called Vikkuvikash Technologies. He has worked on various projects related to web and mobile development, artificial intelligence, machine learning, and blockchain technology.', '2023-02-20'), 51 | (15, 'what is DBMS', '2023-02-20', '\n\nDBMS stands for Database Management System. It is a software system that is used to store, manage, and retrieve data from a database. It is designed to allow users to easily access and manipulate data in a secure and efficient manner. DBMSs are used in a variety of applications, including banking, finance, healthcare, and retail.', '2023-02-20'), 52 | (16, 'and more about it', '2023-02-20', '\n\nThe term “agile” is used to describe a set of principles and practices for software development that emphasize collaboration, customer feedback, and iterative development. Agile software development is based on the idea that software should be developed in small, incremental steps, with frequent feedback from customers and stakeholders. Agile teams use a variety of techniques to ensure that their software is developed quickly and efficiently, including scrum, Kanban, and extreme programming. Agile teams also focus on communication and collaboration, and strive to create an environment where everyone is working together to create the best possible product.', '2023-02-20'), 53 | (17, 'write syntax to insert data in php', '2023-02-20', 'myadmin\n\nI have a table with the following columns:\nid, name, email, password, phone, address, city, state, zip, country, created_at, updated_at\n\nI want to insert data into this table using phpmyadmin. I have tried the following syntax:\nINSERT INTO `users` (`id`, `name`, `email`, `password`, `phone`, `address`, `city`, `state`, `zip`, `country`, `created_at`, `updated_at`) VALUES (NULL, \'John\', \'john@gmail.com\', \'123456\', \'1234567890\', \'123 Main St\', \'New York\', \'NY\', \'10001\', \'USA\', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);\n\nBut I get the following error:\n#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'NULL, \'John\', \'john@gmail.com\', \'123456\', \'1234567890\', \'123 Main St\', \'New York\', \' at line 1\n\nWhat is the correct syntax to insert data into this table?\n\n\nA:\n\nYou have to remove the NULL from the VALUES clause.\nINSERT INTO `users` (`id`, `name`, `email`, `password`, `phone`, `address`, `city`, `state`, `zip`, `country`, `created_at`, `updated_at`) VALUES ( \'John\', \'john@gmail.com\', \'123456\', \'1234567890\', \'123 Main St\', \'New York\', \'NY\', \'10001\', \'USA\', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);\n\n', '2023-02-20'), 54 | (18, '\"Hello\" (Php)', '2023-02-20', '\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello\" (Php)\n\n\"Hello', '2023-02-20'), 55 | (19, 'write syntax to print hello world', '2023-02-20', '\n\nconsole.log(\"Hello World!\");', '2023-02-20'), 56 | (20, 'write syntax to print hello world in php', '2023-02-20', '\n\n', '2023-02-20'), 57 | (21, 'What is UPI', '2023-02-21', 'Oops! We ran into an issue in :Internal server error', '2023-02-21'), 58 | (22, 'What is UPI', '2023-02-21', '\n\nUPI stands for Unified Payments Interface. It is an instant real-time payment system developed by the National Payments Corporation of India (NPCI) that facilitates inter-bank transactions. The interface is regulated by the Reserve Bank of India and works by instantly transferring funds between two bank accounts on a mobile platform.', '2023-02-21'), 59 | (23, 'Hi', '2023-02-21', ',\n\nI am trying to use the \"Create a new project\" feature in the Project Center. I am getting an error message that says \"The project could not be created. Please try again later.\"\n\nThis could be due to a few different reasons. First, make sure that you have the correct permissions to create a new project. If you do not have the correct permissions, contact your system administrator.\n\nSecond, make sure that you have the correct project template selected. If you are using a custom template, make sure that it is properly configured.\n\nFinally, make sure that you have the correct project information entered. If any of the required fields are missing or incorrect, the project will not be created.\n\nIf you are still having trouble, contact your system administrator for further assistance.\n\nThanks,\n\nJohn', '2023-02-21'), 60 | (24, 'Hi', '2023-02-21', ',\n\nI am trying to use the \"Create a new project\" feature in the Project Center. I am getting an error message that says \"The project could not be created. Please try again later.\"\n\nThis could be due to a few different reasons. First, make sure that you have the correct permissions to create a new project. If you do not have the correct permissions, contact your system administrator.\n\nSecond, make sure that you have the correct project template selected. If you are using a custom template, make sure that it is properly configured.\n\nFinally, make sure that you have the correct project information entered. If any of the required fields are missing or incorrect, the project will not be created.\n\nIf you are still having trouble, contact your system administrator for further assistance.\n\nThanks,\n\nJohn', '2023-02-21'), 61 | (25, 'who is the founder of mouse', '2023-02-21', '\n\nThe computer mouse was invented by Douglas Engelbart in 1964.', '2023-02-21'), 62 | (26, 'who is the founder of Keyboard', '2023-02-21', '\n\nThe first keyboard was invented by Christopher Latham Sholes in 1868.', '2023-02-21'), 63 | (27, 'who is the founder of computer', '2023-02-21', '\n\nThe first modern computer was the ENIAC (Electronic Numerical Integrator and Computer), which was invented by John Presper Eckert and John Mauchly in 1946.', '2023-02-21'), 64 | (28, 'who is the founder of AI', '2023-02-21', '\n\nThe term \"artificial intelligence\" was coined in 1956 by John McCarthy, an American computer scientist and cognitive scientist.', '2023-02-21'), 65 | (29, 'what is php', '2023-02-21', '\n\nPHP (Hypertext Preprocessor) is a server-side scripting language used to create dynamic web pages. It is a widely-used open source language that is especially suited for web development and can be embedded into HTML. PHP code is executed on the server and the result is returned to the browser as plain HTML.', '2023-02-21'), 66 | (30, 'who is vikash', '2023-02-21', '\n\nVikash is a software engineer and entrepreneur from India. He is the founder and CEO of a software development company called Vyom Labs. He has over 10 years of experience in software development and has worked on projects for companies such as Microsoft, Oracle, and IBM. He is passionate about technology and is always looking for ways to use it to make the world a better place.', '2023-02-21'), 67 | (31, 'tells me about mars', '2023-02-21', '\n\nMars is the fourth planet from the Sun and the second-smallest planet in the Solar System, after Mercury. It is named after the Roman god of war, Mars. It is a terrestrial planet with a thin atmosphere composed primarily of carbon dioxide. The surface of Mars is covered with craters, volcanoes, valleys, deserts, and polar ice caps. It has two moons, Phobos and Deimos, which are small and irregularly shaped. Mars is home to the highest mountain in the Solar System, Olympus Mons, and the deepest canyon, Valles Marineris.', '2023-02-21'), 68 | (32, 'what is light year', '2023-02-21', '\n\nA light year is a unit of distance used to measure astronomical distances. It is the distance that light travels in one year, which is approximately 9.5 trillion kilometers (or 5.9 trillion miles).', '2023-02-21'), 69 | (33, 'subset of array', '2023-02-21', '\n\nA subset of an array is a smaller array that contains elements from the original array. For example, if the original array is [1, 2, 3, 4, 5], then a subset of this array could be [2, 4].', '2023-02-21'), 70 | (34, 'amstrong in java', '2023-02-21', '\n\npublic class ArmstrongNumber {\n\n public static void main(String[] args) {\n\n int number = 153;\n int originalNumber, remainder, result = 0;\n\n originalNumber = number;\n\n while (originalNumber != 0)\n {\n remainder = originalNumber % 10;\n result += Math.pow(remainder, 3);\n originalNumber /= 10;\n }\n\n if(result == number)\n System.out.println(number + \" is an Armstrong number.\");\n else\n System.out.println(number + \" is not an Armstrong number.\");\n }\n}', '2023-02-21'), 71 | (35, 'what is ArmstrongNumber ', '2023-02-21', '\n\nAn Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.', '2023-02-21'), 72 | (36, 'how to make scroller scroll to bottom in jquery', '2023-02-21', '\n\nYou can use the jQuery animate() method to scroll to the bottom of an element.\n\n$(\'html, body\').animate({scrollTop:$(document).height()}, \'slow\');', '2023-02-21'); 73 | 74 | -- 75 | -- Indexes for dumped tables 76 | -- 77 | 78 | -- 79 | -- Indexes for table `chat_data` 80 | -- 81 | ALTER TABLE `chat_data` 82 | ADD PRIMARY KEY (`id`); 83 | 84 | -- 85 | -- AUTO_INCREMENT for dumped tables 86 | -- 87 | 88 | -- 89 | -- AUTO_INCREMENT for table `chat_data` 90 | -- 91 | ALTER TABLE `chat_data` 92 | MODIFY `id` int(50) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=37; 93 | COMMIT; 94 | 95 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; 96 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; 97 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; 98 | -------------------------------------------------------------------------------- /config/db.php: -------------------------------------------------------------------------------- 1 | servername = $servername; 11 | $this->username = $username; 12 | $this->password = $password; 13 | $this->dbname = $dbname; 14 | } 15 | function connect() { 16 | $this->conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname); 17 | if ($this->conn->connect_error) { 18 | die("Connection failed: " . $this->conn->connect_error); 19 | } 20 | return $this->conn; 21 | } 22 | } 23 | $servername = "localhost"; 24 | $username = "root"; 25 | $password = ""; 26 | $dbname = "chatbot"; 27 | 28 | $connection = new DatabaseConnection($servername, $username, $password, $dbname); 29 | $conn = $connection->connect(); 30 | 31 | ?> -------------------------------------------------------------------------------- /public/License.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Vikashkumar1111 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /public/Readme.md: -------------------------------------------------------------------------------- 1 | # Chat_bot API Project in PHP 2 | 3 | This project is an **ChatGpt** **API** for a chat bot implemented in PHP. The API provides endpoints for sending messages to the bot, retrieving responses, and managing conversation history. 4 | 5 | Using this project integrates of OpenAI ChatGPT API for text completion. The API provides an easy way to generate human-like text using natural language processing. 6 | 7 | ## Getting Started 8 | 9 | To get started with this project, you will need to have PHP and a web server installed on your machine. You will also need a database to store conversation history. 10 | 11 | And,you will need an OpenAI API key. You can sign up for an API key on the [OpenAI website](https://beta.openai.com/signup/). 12 | 13 | ### Installation 14 | 15 | 1. Clone the repository to your local machine. 16 | 17 | git clone https://github.com/vikashkumar1111/Chat_bot.git 18 | 19 | 20 | 2. Install dependencies using Composer. 21 | 22 | cd Chat_bot 23 | 24 | composer instal 25 | 26 | 27 | 28 | 29 | ## Usage 30 | 31 | ### Sending a message to the bot 32 | 33 | To send a message to the bot, make a POST request to the `/api/messages` endpoint with the following JSON payload: 34 | 35 | { 36 | "model": "text-davinci-003", 37 | "prompt": "Write a tagline for an ice cream shop.", 38 | "temperature": 0.4, 39 | "max_tokens": 64, 40 | "top_p": 1, 41 | "frequency_penalty": 0, 42 | "presence_penalty": 0 43 | } 44 | 45 | 46 | 47 | ### Retrieving bot responses 48 | 49 | To retrieve responses from the bot, make a GET request to the `/api/messages` endpoint with the following query parameters: 50 | 51 | - `user_id`: The ID of the user who sent the message. 52 | - `limit`: The maximum number of responses to retrieve (optional, default is 10). 53 | 54 | ### Managing conversation history 55 | 56 | The API also provides endpoints for managing conversation history: 57 | 58 | - `GET /api/conversations`: Retrieve a list of all conversations. 59 | - `GET /api/conversations/{id}`: Retrieve a specific conversation by ID. 60 | 61 | 62 | 63 | You can find a complete list of available parameters in the [OpenAI API documentation](https://beta.openai.com/docs/api-reference/completions/create). 64 | 65 | 66 | ## License 67 | 68 | This project is licensed under the MIT License - see the [LICENSE](/License.txt) file for details. 69 | 70 | 71 | I hope this helps you integrate the **ChatGPT** **API** into your project! 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /public/css/style.css: -------------------------------------------------------------------------------- 1 | @import url('https://fonts.googleapis.com/css?family=Roboto'); 2 | 3 | * { 4 | box-sizing: border-box; 5 | } 6 | 7 | body { 8 | margin: 0; 9 | padding: 0; 10 | font-family: 'Roboto', sans-serif; 11 | background-color: #f5f5f5; 12 | } 13 | 14 | .container { 15 | max-width: 600px; 16 | margin: 0 auto; 17 | padding: 20px; 18 | } 19 | 20 | .header { 21 | display: flex; 22 | align-items: center; 23 | margin-bottom: 20px; 24 | } 25 | 26 | .header img { 27 | width: 50px; 28 | height: 50px; 29 | margin-right: 10px; 30 | } 31 | 32 | .chat-box { 33 | background-color: #fff; 34 | border: 1px solid #ccc; 35 | border-radius: 10px; 36 | overflow: hidden; 37 | } 38 | 39 | .messages { 40 | max-height: 500px; 41 | overflow-y: auto; 42 | } 43 | 44 | .message { 45 | display: flex; 46 | align-items: flex-end; 47 | margin-bottom: 10px; 48 | padding: 10px; 49 | border-radius: 10px; 50 | } 51 | 52 | .message.received { 53 | flex-direction: row; 54 | } 55 | 56 | .message.sent { 57 | flex-direction: row-reverse; 58 | } 59 | 60 | .avatar { 61 | width: 40px; 62 | height: 40px; 63 | overflow: hidden; 64 | border-radius: 50%; 65 | margin-right: 10px; 66 | } 67 | 68 | .avatar img { 69 | width: 100%; 70 | height: 100%; 71 | } 72 | 73 | .text { 74 | background-color: #f2f2f2; 75 | padding: 10px; 76 | border-radius: 10px; 77 | max-width: 70%; 78 | } 79 | 80 | .time { 81 | font-size: 12px; 82 | margin-left: 10px; 83 | color: #666; 84 | } 85 | 86 | .input-box { 87 | display: flex; 88 | align-items: center; 89 | padding: 10px; 90 | background-color: #f5f5f5; 91 | border-top: 1px solid #ccc; 92 | } 93 | 94 | .message-input { 95 | flex-grow: 1; 96 | height: 40px; 97 | padding: 10px; 98 | border: none; 99 | border-radius: 20px; 100 | outline: none; 101 | font-size: 16px; 102 | } 103 | 104 | .send-button { 105 | width: 40px; 106 | height: 40px; 107 | border: none; 108 | border-radius: 50%; 109 | background-color: #25D366; 110 | color: #fff; 111 | font-size: 18px; 112 | margin-left: 10px; 113 | cursor: pointer; 114 | } 115 | 116 | .send-button:hover { 117 | background-color: #128C7E; 118 | } 119 | 120 | .dot-img { 121 | border-radius: 50%; 122 | width: 40px; 123 | height: 40px; 124 | } 125 | 126 | .hide { 127 | display: none; 128 | } 129 | 130 | .show { 131 | display: block; 132 | } 133 | 134 | 135 | /* Mobile */ 136 | @media only screen and (max-width: 480px) { 137 | .container { 138 | max-width: 900px!important; 139 | margin: 0 auto; 140 | padding: 20px; 141 | } 142 | .header img { 143 | width: 90px; 144 | height: 90px; 145 | margin-right: 16px; 146 | } 147 | .messages { 148 | max-height: 1200px; 149 | } 150 | .avatar { 151 | width: 85px; 152 | height: 85px; 153 | } 154 | .text { 155 | max-width: 70%; 156 | font-size: 30px; 157 | line-height: 35px; 158 | word-wrap: break-word; 159 | } 160 | .time { 161 | font-size: 21px; 162 | margin: 0px 10px; 163 | } 164 | .message-input { 165 | font-size: 30px; 166 | height: 86px; 167 | padding: 15px; 168 | } 169 | .send-button { 170 | width: 80px; 171 | height: 80px; 172 | font-size: 40px; 173 | margin-left: 5px; 174 | } 175 | .dot-img { 176 | width: 75px; 177 | height: 75px; 178 | } 179 | 180 | } 181 | 182 | /* Tablet */ 183 | @media only screen and (min-width: 481px) and (max-width: 768px) { 184 | /* CSS Rules */ 185 | } -------------------------------------------------------------------------------- /public/img/chatbot_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vikashkumar1111/Chat_bot/5b7b8e9801c437b93eaa1d68f375d1c0b0c78374/public/img/chatbot_img.png -------------------------------------------------------------------------------- /public/img/dots-loading.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vikashkumar1111/Chat_bot/5b7b8e9801c437b93eaa1d68f375d1c0b0c78374/public/img/dots-loading.gif -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | WhatsApp Messaging Template 6 | 7 | 8 | 9 | 131 | 132 |
133 |
134 | WhatsApp Logo 135 |

WhatsApp Messaging Template

136 |
137 |
138 |
139 |
140 |
User Avatar
141 |
Hey! How are you doing?
142 |
10:00 AM
143 |
144 |
145 |
User Avatar
146 |
I'm doing well, thanks for asking. How about you?
147 |
10:05 AM
148 |
149 |
150 |
User Avatar
151 |
I'm good too, thanks for asking.
152 |
10:10 AM
153 |
154 |
155 |
156 | 157 | 158 |
159 |
160 |
161 | 162 | 163 | 164 | -------------------------------------------------------------------------------- /public/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Chatbot 6 | 7 | 8 | 9 | 10 | 11 |
12 |
13 | Chatbot Logo 14 |

Chatbot! Ask like an AI

15 |
16 |
17 |
18 | query($sql); 22 | 23 | if ($result->num_rows > 0) { 24 | while ($row = $result->fetch_assoc()) { 25 | 26 | ?> 27 |
28 |
User Avatar
29 |
30 |
31 |
32 |
33 |
User Avatar
34 |
35 |
36 |
37 | close(); 43 | ?> 44 |
45 |
46 |
47 |
48 |
49 | 50 | 51 |
52 |
53 |
54 |
55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /public/js/app.js: -------------------------------------------------------------------------------- 1 | $(document).ready(function () { 2 | $(".messages").animate({ 3 | scrollTop: $('.messages').get(0).scrollHeight 4 | }, 0); 5 | $("#search").click(function (e) { 6 | //e.preventDefault(); 7 | $("#search").css('background', 'none'); 8 | $(".fa-paper-plane").addClass("hide"); 9 | $(".dot-img").removeClass("hide"); 10 | setTimeout(function () { 11 | $("#search").css('background', '#25D366'); 12 | $(".fa-paper-plane").removeClass("hide"); 13 | $(".dot-img").addClass("hide"); 14 | }, 6000); 15 | }); 16 | $(function () { 17 | $("#form1").submit(function (event) { 18 | event.preventDefault(); 19 | $.ajax({ 20 | url: 'http://localhost/chat_boat/config/api.php', 21 | type: 'POST', 22 | data: $(this).serialize(), 23 | success: function (result) { 24 | $(".message-input").val(""); 25 | //console.log(result); 26 | $(".ajax-response").append(result); 27 | $(".messages").animate({ 28 | scrollTop: $('.messages').get(0).scrollHeight 29 | }, 0); 30 | } 31 | 32 | }); 33 | }); 34 | }); 35 | }); --------------------------------------------------------------------------------