We write a program for teaching English using the OpenAI API
I want to describe the process of creating an application that works with the OpenAI API. The idea for the app came from a need to improve my English. As they say, if you have a hammer, then all problems look like nails. Since I am a programmer, I decided to write an Artificial Intelligence that will replace the English teacher.
The scenario of interaction with the application is as follows: the student sets the topic of the lesson, the AI teacher provides a list of basic words on the topic. The student can ask the meaning of words that are not familiar to him, the AI teacher provides the meanings of words and examples of their use. Then the AI teacher enters into a dialogue with the student on a given topic, the student can answer questions and ask their own questions. The AI teacher also finds errors in the student’s answers.
Initially, I thought about creating a full-fledged web service where anyone can register, link their card and start learning with an AI teacher. However, in the end, I decided to start with a prototype: a console application in Rust, with which it would be possible to conduct a dialogue from the terminal. But then I cooled off this idea a bit, since the implementation of a full-fledged service requires opening a legal entity, concluding an agreement with a bank to process customer cards, renting a cloud for a server, developing a web version of an AI teacher, and investing in an advertising campaign.
Of course, I can perform all these tasks, but the creation of such a service will be quite long and significant investments are required. And to do it on your knee and lose your time, and not be able to compete with similar startups from the USA, which have sufficient funds for advertising and development, is not interesting.
Let’s return to the subtleties of the implementation of this application. In order to get a list of the main words on a topic, you need to perform a “completions” request to the OpenAI API. This query returns a logical completion of the text that you pass to it, for example, a list of basic words on the topic of the lesson.
As a result, by trial and error, I got a request that the program sends, it looks like this:
Give list of words linked with the subject "_TOPIC_". Words should comma separated. Only words.
Where _TOPIC_ is the topic of the lesson with the AI teacher. The result of this query will be a comma-separated list of words on the topic you need.
Further, according to the logic of the application, it was necessary to give an explanation for the words that the student does not know. The following request came up:
Give answer what is "_WORD_" from the field "_TOPIC_".
Where _WORD_ is the word that needs explanation, _TOPIC_ is the topic of the lesson. In response, you will receive 1-2 sentences explaining the given word.
For clarity, it is necessary to generate examples of the use of this word. I got the following two requests:
Give sentence with word "_WORD_" in field "_TOPIC_".
Give rude sentence with word "_WORD_" in field "_TOPIC_".
For each of them, I received two different sentences that demonstrate the use of the word. The first is of a general nature, the second is a bit with aggression, for a change, so to speak.
Next, the application should enter into a dialogue with the student. One of the easiest ways is to use a word list related to the topic of the lesson and ask a question using it. I got the following request to the OpenAI API:
Give _QUESTION_TYPE_. About "_TOPIC_". Use word "_WORD_". Do not use word "_TOPIC_".
Where _QUESTION_TYPE_ is the question type, which is randomly selected from the list:
Follow up question
Forced choice question
This allows you to generate always different questions even when you go through a lesson on the same topic multiple times.
Moreover, in this query, I added “Do not use word “_TOPIC_”” to the end, so that in each question from the teacher there would be no word mentioning the topic of the lesson, otherwise all sentences would be the same. And it looks very unnatural.
The next task is to determine if there are errors in the student’s answer. To do this, the OpenAI API has an “edits” request. It allows you to take a piece of text and change it into it. There are two parameters: the source text and what needs to be done with it. As a result, when I called this function, I passed the text of the student’s answer and the following line:
Fix grammar and spelling.
At the output, I received a line with the corrected text. If it did not coincide with what the student wrote, then I thought that he was mistaken, and deduced the corrected version.
In the case when the student ignores the question of the AI teacher and writes his own question in response, or when answering it, he also adds his own, then I generate an answer to him through the OpenAI API “completions” with the following line:
Give _SENTENCE_TYPE_ answer on text: _STUDENT_QUESTION_.
Where _STUDENT_QUESTION_ is the student’s question text and _SENTENCE_TYPE_ is the affirmative sentence type, which is randomly selected from the following list:
Thus, even for the same questions, the AI teacher always gives a different answer, which introduces diversity during communication.
The code of the AI teacher console program can be viewed here. The code of the library working with the OpenAI API can be viewed here.
And finally, an example of interaction with this program, which replaces the English teacher:
Please enter a topic of the lesson:
words of lesson:
Do you know all these words? Say: Yes or write unknown words comma separated:
A bellhop is a hotel employee who assists guests with their luggage and other needs. They are usually the first person a guest sees when they arrive at the hotel. Example 1: The bellhop carried our luggage to our room in the hotel. Example 2: “Hey bellhop, why are you so slow? Hurry up and get my bags to my room!”
Valet is a service offered by some hotels where a guest can have their car parked and retrieved by a hotel employee. Example 1: The hotel valet was very helpful in parking our car. Example 2: “If you can’t do your job as a valet in this hotel, then you should find another job.”
What type of establishment typically offers a variety of food and beverages for customers to enjoy?
I think it is restraunt.
I think it is a restaurant. < grammar fixes
What is the process for check-in at a lodging establishment?
You need show a passport and student card. Is it correct?
You need to show a passport and student card. Is it correct? < grammar fixes
Yes, it is correct that you need to show a passport and student card in order to prove your identity and student status. Depending on the situation, you may need to provide additional documents such as a birth certificate or proof of address.
What kind of lodging would you prefer?
I prefer room with toulet and shower. If I visit hotel with wife then I prefer one big bad for two.
I prefer room with toilet and shower. If I visit hotel with wife then I prefer one big bad for two. < grammar fixes
What is the average wait time at the reception desk?
Usually it could take not more 5 minutes.
Usually it could take no more than 5 minutes. < grammar fixes
What services does a concierge provide?
You could ask a concierge call you at 7:00 am to wake you tommorow.
You could ask a concierge to call you at 7:00 am to wake you tomorrow. < grammar fixes
What kind of accommodation can you find when you travel?
When I travel I usualy take apartmant in hotel.
When I travel I usually take an apartment in a hotel. < grammar fixes
Do you have a swimming pool at your accommodation?
No We do not have swimming pool at the hotel.
No, we do not have a swimming pool at the hotel. < grammar fixes