Contents
- Introduction
- Stunning chatbots enabled by GPT-3
- Incorporating GPT-3 into your site with simple PHP and JavaScript
- Teaching your model factual data that it doesn’t know about in the form provided by OpenAI
- A full web app where users interact with your custom-trained GPT-3 chatbot using their own API key
1. Introduction
Developed by OpenAI, GPT-3 is a Machine Learning model specialized in language processing. Given an input text, GPT-3 outputs new text. Depending on the input and exact flavor of GPT-3 used, the output text will conform to a task such as replying to a question asked in the input, or completing the input with additional data, or translating the input from one language to another, or summarizing the input, or inferring sentiments, or even crazier things such as writing a piece of computer code from an indication given in the input. Among many other applications!
Trained with billions of parameters and huge loads of text corpora, GPT-3 is one of the largest models available out there. But among all language processing models, I find GPT-3 especially enticing because of these features:
- It runs online, so I don’t need to download anything to the computer or server where I want to use it. I can use it just by including calls to the GPT-3 API in my source code.
- The above includes the possibility of writing web code that will call the GPT-3 API, so you can include the power of this tool in your web pages. We will see here how to achieve this with simple PHP and JavaScript.
- Despite not having a grasp on the executables (because it all runs online), GPT-3 is very customizable. This allows you to program chatbots that behave in ways you can tune, or even more interesting, that "know" about a particular topic you teach them. We will in fact see here one of two possible ways to train your GPT-3 models ad hoc for your goals.
- GPT-3 is very easy to work with, for example in Python or as shown here in JavaScript and PHP.
I recently tested GPT-3’s capabilities and potential in assisting science education and research by acting as an always-available bot that can answer the questions of students or researchers. Part of these tests involved teaching GPT-3 some pieces of data, on which one could then ask questions.
The results were impressive, despite many limitations mainly because the model of course doesn’t really understand what it reads and writes… it’s just a statistical model that synthesizes text that is gramatically correct but may or may not be factually accurate. You can know more about these tests I made in these recent articles:
GPT-3-like models with extended training could be the future 24/7 tutors for biology students
A Note on GPT-3 and Its (Obviously Null) "Thinking" Capabilities
Testing GPT-3 on Elementary Physics Unveils Some Important Problems
Importantly, as I show in some examples especially in this one, by setting GPT-3’s parameters correctly and by training it with ad hoc content, it proved very "smart" especially for information retrieval using natural language about factual topics. We will exploit this here to make a web page that knows about a specific topic you teach GPT-3 about.
GPT-3-like models with extended training could be the future 24/7 tutors for biology students
By setting GPT-3’s parameters correctly and by training it with ad hoc content, it proved very "smart" especially for information retrieval using natural language about factual topics. We will exploit this here to make a web page that knows about a specific topic you teach GPT-3 about.
2. Stunning chatbots allowed by GPT-3
In this article we will see how to build a simple chatbot that knows about a specific topic that you provide as prompt. Users can naturally chat with the bot through a web page, asking whatever they want. When the questions refer to the topic you informed the chatbot with, then it will reply based on that content. The best thing is that you don’t need to ask the questions in a very structured way, as you’d need for a regular question-answer-matched chatbot to understand you. Rather, you can retrieve the information in a natural way. Besides, the responses depend on the context; so for example you can talk about a person by name and then refer to him or her with the corresponding articles.
As an example, let’s compare the outputs of a regular GPT-3 chatbot when asked about me to those of a GPT-3 chatbot informed with a short bio about me and some of my projects. The former will make up stuff, or in the best case just not provide any answers, while the second will be more accurate in its answers, at least when you ask it about information that I provided it.
Let me show you first this short conversation with the custom-trained GPT-3 chatbot. I achieve this in a way called "few-shot learning" by the OpenAI people; it essentially consists in preceding the questions of the prompt (to be sent to the GPT-3 API) with a block of text that contains the relevant information. This is not very efficient because it is limited in the amount of information you can pass, and also because it consumes many tokens each time you send the prompt. But it’s extremely simple and practical.
You see in bold my questions; the rest is the chatbot’s answers and I write some lines with comments in-between:
Visitor: who is luciano abriata? Assistant: Luciano Abriata is a biotechnologist, doctor in chemistry, artist and content creator.
This answer was taken textually from the provided training paragraph.
Visitor: what has he created? Assistant: Luciano has created a website called moleculARweb, which is a website for education and outreach in chemistry and structural biology through augmented reality (AR) content.
The information is right, and although it is provided in the training paragraph, it doesn’t match textually any part of it. GPT-3 might have reprocessed it, or maybe it already knows about it because our website has been online since 2020.
Visitor: that looks cool! Is there a publication on molecularweb? Assistant: Yes! The website is featured in a publication in the Journal of Chemical Education, 2021: https://pubs.acs.org/doi/10.1021/acs.jchemed.1c00179
Perfect, and the link is correct. This piece of information is provided in the training paragraph, not exactly like that but it got it.

Visitor: do i need any special hardware to use molecularweb? Assistant: No, you don’t need any special hardware. All you need is a web browser and a device with a camera, like a smartphone, tablet, or computer.
Again correct information provided by the chatbot, and again this is not given in the training exactly in this way -but the information is there.
Visitor: do you mean I can run it on my phone? Assistant: Yes! You can run it on your phone, tablet, or computer. Visitor: how do I use it? Assistant: You can use it by going to the website and following the instructions.
Here the answer was correct, but it was weird to see that the chatbot added a second question! I’m stunned that the question would be quite natural in a real conversation… by this time the user knows quite a bit about the website, so it would be natural that (s)he wants to use it.
Visitor: what’s the url? Assistant: The url is https://molecularweb.epfl.ch
Perfect, and this data is provided in the training but not exactly in this way. URL is not even mentioned, yet GPT-3 "understands" what the user is looking for.
Visitor: cool thanks. I will try it out.
(no answer)
Now let’s see what happens if we ask the same questions to an untrained GPT-3 chatbot. All parameters are exactly the same as above, except that I don’t apply any few-shot learning procedure here; that is, I don’t add any additional data to the prompts.
Visitor: who is luciano abriata?
(no answer)
But if I ask it about a more famous Luciano, it does know!
Visitor: Visitor: who is luciano pavarotti? Assistant: Luciano Pavarotti was an Italian operatic tenor who also had a successful career as a crossover artist. He was one of the most commercially successful tenors of all time.
Let’s keep asking:
Visitor: do you know moleculARweb? Assistant: I do not know moleculARweb.
Visitor: Is there any website for chemistry education using augmented reality? Assistant: I do not know of any websites for chemistry education using augmented reality.
Visitor: do you know google? Assistant: Yes, I know of Google.
Visitor: what is google? Assistant: Google is a multinational technology company specializing in Internet-related services and products. These include online advertising technologies, search, cloud computing, software, and hardware.
I think that’s enough to see that our chatbot clearly knows about many things, in fact everything that the standard as-provided GPT-3 knows, but it just doesn’t know anything about things it never saw.
Read on to discover how you can insert your own assistant inside your own webpage and fine-tune it to know about any given subject -for example about you and your work so that it can behave as a secretary who chats with the visitors of your site.
3. Incorporating GPT-3 into your site with simple PHP and JavaScript
You need a few things first: a server that allows PHP and JavaScript, an API key, and some PHP libraries that you will couple to JavaScript code.
i) A server that can run PHP and allows JavaScript code
The easiest solution is to use a hosting service that natively provides PHP runtimes and allows JavaScript. I use Altervista, whose basic free package already allows both! You don’t even need to install PHP!
But you do need to activate it (it’s a free feature as of March 2022). I’m using PHP version 8, and it was important to enable all connections without restrictions (otherwise it won’t connect to the OpenAI API).

ii) An API key from OpenAI
People in most countries can get a free API with some credit precharged on it, at no cost, to experiment with the system. Check the official OpenAI site at https://beta.openai.com/signup
Critical: Don’t give away your API key (either personally or because you left it exposed in your JavaScript code!) because its use will burn your credits!
The examples I present here require that the user introduces his/her own key. The web app passes the key to the PHP wrapper that calls the GPT-3 API (note that the key doesn’t get stored, so feel free to try my code at no risk!).
More about this point throughout the rest of the article.
iii) A PHP library to connect to OpenAI’s GPT-3, and a way to use this library from your web page’s HTML+JavaScript code
OpenAI doesn’t support PHP natively, but there’s a dedicated community of developers who wrote libraries to call the GPT-3 API over PHP (and from code in other languages too):
I tried a couple of the available PHP libraries and decided for this one:
But I had to do a few modifications on the main file, called OpenAI.php. You can get the final file as I used it here:
http://lucianoabriata.altervista.org/tests/gpt-3/OpenAI-PHP-library-as-used.txt
Some of small modifications I did were needed to make it work. One of the small modifications was to enable passing the user’s API key programmatically, instead of having a fixed one. In this way, users of your app do not spend tokens from your account! The bad side of this is that they need to get a key themselves. An intermediate solution would be to let your own key hard-coded in the PHP file as intended in the original OpenAI.php file, and then charge your users when they utilize your app.
There’s another file you need, which connects your HTML/JavaScript file to the core PHP file that calls the GPT-3 API. This is a short PHP file with this contents:
<?php
//Based on tutorials and scripts at:
// https://github.com/karamusluk/OpenAI-GPT-3-API-Wrapper-for-PHP-8/blob/master/OpenAI.php
// https://githubhelp.com/karamusluk/OpenAI-GPT-3-API-Wrapper-for-PHP-8
//Thanks to this for hints about connecting PHP and JavaScript:
// https://stackoverflow.com/questions/15757750/how-can-i-call-php-functions-by-javascript
require_once "./OpenAI-PHP-library-as-used.php";
$apikey = $_GET["apikey"];
$instance = new OpenAIownapikey($apikey);
$prompt = $_GET["prompt"];
$instance->setDefaultEngine("text-davinci-002"); // by default it is davinci
$res = $instance->complete(
$prompt,
100,
[
"stop" => ["n"],
"temperature" => 0,
"frequency_penalty" => 0,
"presence_penalty" => 0,
"max_tokens" => 100,
"top_p" => 1
]
);
echo $res;
?>
Interfacing PHP and JavaScript to run GPT-3 at OpenAI
Your JavaScript code simply needs to make an asynchronous call to the complete function of the PHP file above, which is defined (in the PHP file above) on an instance object that carries the text prompt and parameters you want to pass.
This is how you make the asynchronous call in JavaScript (using JQuery):
var chatbotprocessinput = function(){
var apikey = "Bearer <API KEY>"
var theprompt = "(define prompt)"
$.ajax({
url: "phpdescribedfileabove.php?prompt=" + theprompt + "&apikey=" + apikey
}).done(function(data) {
console.log(data) //data has the prompt plus GPT-3's output
});
}
If you inspect the source code of the example I show you later on in this article, you’ll see it’s a bit more complicated. That’s because my web app cleans the output (in data) to remove the input and other things; it also reformats the text to show the user and chatbot names in bold, and it keeps the whole set of inputs and outputs (growing as the user chats with the bot) in an internal variable so that GPT-3 gets context about the conversation each time it is executed.
4. Teaching your model factual data that it doesn’t know about in the form provided by OpenAI
In other words: teaching GPT-3 what it needs to know in order to better fulfill your goals
As I anticipated above, there are two main ways to "train" your GPT-3 model with ad hoc data. One, used here and also introduced above, is called "few-shot learning". Few-shot learning is VERY simple: just extend your prompt (that is, the input with the questions for GPT-3) with a few paragraphs of relevant information.
In the example we saw above (and that you can play with, see below in section 3), where the user would ask the chatbot about me because it is supposed to answer for me, I fed it two paragraphs:
PARAGRAPH 1: Hello, welcome to Luciano Abriata’s website. I’m here representing Luciano online. I know a lot about him -I’m an OpenAI GPT-3 model extended with texts written by Luciano. Feel free to ask any questions. Dr. Luciano A. Abriata is a biotechnologist, doctor in chemistry, artist and content creator. On science subjects, Luciano has experience in the experimental and computational aspects of structural biology, biophysics, protein biotechnology, molecular visualization through augmented and virtual realities, and science education using modern technologies. Luciano Abriata was born in Rosario, Argentina, in 1981. He studied biotechnology and chemistry in Argentina, and then moved to Switzerland where he currently works in two laboratories at the Ecole Polytechnique Federale de Lausanne (EPFL). He works at EPFL’s Laboratory for Biomolecular Modeling, and at EPFL’s Protein Production and Structure Core Facility. He currently works on web-based methods to achieve commodity augmented reality and virtual reality tools for viewing and manipulating molecular structures immersively. He also works in collaboration with multiple groups on molecular modeling, simulation, and nuclear magnetic resonance (NMR) spectroscopy applied to biological systems.
PARAGRAPH 2: Article title: MoleculARweb: a website for chemistry and structural biology education through interactive augmented reality out of the box in commodity devices (Journal of Chemical Education, 2021: https://pubs.acs.org/doi/10.1021/acs.jchemed.1c00179). Text: moleculARweb (https://molecularweb.epfl.ch) began as a website for education and outreach in chemistry and structural biology through augmented reality (AR) content that runs in the web browsers of regular devices like smartphones, tablets, and computers. Here we present two evolutions of moleculARweb’s Virtual Modeling Kits (VMK), tools where users can build and view molecules, and explore their mechanics, in 3D AR by handling the molecules in full 3D with custom-printed cube markers (VMK 2.0) or by moving around a simulated scene with mouse or touch gestures (VMK 3.0). Upon simulation the molecules experience visually realistic torsions, clashes, and hydrogen-bonding interactions that the user can manually switch on and off to explore their effects. Moreover, by manually tuning a fictitious temperature the users can accelerate conformational transitions or ‘freeze’ specific conformations for careful inspection in 3D. Even some phase transitions and separations can be simulated. We here showcase these and other features of the new VMKs connecting them to possible specific applications to teaching and self-learning of concepts from general, organic, biological and physical chemistry; and in assisting with small tasks in molecular modelling for research. Last, in a short discussion section we overview what future developments are needed for the ‘dream tool’ for the future of chemistry education and work."
Each time the user asks something to my chatbot, my webpage is not sending just the question but it is actually concatenating the two paragraphs of information, plus any previous questions and answers, to the new question. Does, GPT-3’s output is based on that data, if it finds some relevant content in it (you can still ask it about any other thing and it might still reply).
As I advanced earlier, "training" your GPT-3-based chatbot by "few-shot learning" is not very efficient because it is limited in the amount of information you can pass, and also because it consumes many tokens each time you send the prompt. But it’s extremely simple and practical, as you saw in the examples above and will see in section 3 below.
The alternative to the quick and simple few-shot learning is to execute what OpenAI’s people call "fine-tuning". This is a more stable procedure in which you train your GPT-3 model only once and then store this training in a file for further use later on. I haven’t experimented with fine-tuning yet, but you can consult OpenAI’s site about this here:
5. A full web app where users interact with your custom-trained GPT-3 chatbot using their own API key
(Or your API key if you prefer, just include it in your PHP file so that it is not exposed -and even then, notice that your users will consume your credits.)
You can easily write a web app that implements a GPT-3-based chatbot. You can also try my examples right away once you get an API key from OpenAI.

Example with few-shot learning: http://lucianoabriata.altervista.org/tests/gpt-3/TDSarticle-example-with-prompt.html
Example without few-shot learning: http://lucianoabriata.altervista.org/tests/gpt-3/TDSarticle-example-without-prompt.html
And you can
A quick look at the code:

Bonus: Advocating for web programming
You noticed all this is prepared for the web. In fact, this article is part of a series where I put forward solutions and futuristic tools that rely on web Programming -with all the advantages this entails, starting with the seamless cross-device and cross-OS compatibility.
Here are some highlights, and you can check more on my profile:
A free online tool for Principal Components Analysis with full graphical output
Exquisite hand and finger tracking in web browsers with MediaPipe’s machine learning models
Obtaining historical and real-time crypto data with very simple web programming
Websites for Statistics and Data Analysis on Every Device
Live Display of Cryptocurrency Data in a VR Environment on the Web
The Definitive Procedure for Aligning Two Sets of 3D Points with the Kabsch Algorithm
Read Public Messages from the Ethereum Network with Simple Web Programming
www.lucianoabriata.com I write and photoshoot about everything that lies in my broad sphere of interests: nature, science, technology, programming, etc. Become a Medium member to access all its stories (affiliate links of the platform for which I get small revenues without cost to you) and subscribe to get my new stories by email. To consult about small jobs check my services page here. You can contact me here.