AI(Artificial Intelligence): AI can be defined as the tasks done by machines with the intelligence that a human can possess. As the human trains the machine with large amount of data, machines can perform tasks like humans in predicting, responding, decision making etc.,
Branches or Domains of AI :
Machine Learning
Deep Learning
Robotics
Expert Systems
Fuzzy Logic
Natural Language Processing
NLP (Natural Language Processing) : NLP is a way for humans to communicate with machines. NLP can be simply defined as a way where machines can understand the human languages with the help of programming. Different applications of NLP are chatbot, sentiment analysis, advertisement matching etc., NLP has become part of our lives as we are frequently using the voice assistants like Alexa, google assistant and Siri etc., where we ask a question and then we get the appropriate reply from the machine. This particular text is sent and after going through the below steps processed text comes as output:
Let us consider an example where you are asking google for a suggestion on movies . “ Hey google can you suggest any Telugu movie that released in 2018, with good rating”. First let us imagine we have sent a text to machine, it involves steps like :
Segmentation : The process of dividing sentences into composite sentences is called segmentation.
2. Tokenization : The process of dividing sentences into composite words is called tokenization.
3. Stemming : The process of obtaining the stem word of a word. It removes the suffixes or prefixes in the words and bring it to its root word. In the above example the word released will be converted to release.
4. Lemmatization : The process of obtaining the root word of a word is known as lemmatization. It is also similar to stemming and it is advanced version for stemming. For example it consider the words ate, eating into eat.
5. stop words : The words that don’t have any impact on the given sentences can be considered as stop words. In the above example hey, can, you, any, that, in will come under the category of stop words.
6. Part of Speech (POS) tagging : The process of obtaining the part of speech for a word.(whether the word is noun or pronoun or verb).
7. Dependency parsing : Dependency parsing is used to check if the words are in correct order grammatically and to phrase them in the right order.
8. Named Identity Recognition : The process of obtaining the word into subcategories. (whether the word is a person or location or organization). From the above example they consider the output is about google is a organization, Telugu is a language and 2018 is a year.
9. Syntax Tree : Syntax tree is used to arrange how a sentence can be arranged in correct sense.
10. Chunking : Words are combined into sentences. So all the words that are formed will be created into a sentence after syntax tree.
CHATBOT is one of the important applications that uses the NLP to give better response to the humans. Chatbots are widely used in various industries like Customer support, Healthcare, E-commerce, Human Resources etc., Now we are creating a chatbot using chatGPT for building a conversation between chatbot and end user.
pip install openai
import openai
openai python library is installed and imported as it acts as the interface between the user and openai models.
pip install gradio
import gradio
Gradio is a python library and it provides an API to create UI components such as input fields, sliders, and buttons, and it supports various types of input and output.
openai.api_key = "key"
API key is generated from our account and will be used in the above statement as the key is required to authenticate and authorize access to the OpenAI API.
messages = [{"role": "user", "content": "You are PCOS expert"}]
A message is provided in the format of a conversation to start the interaction between user and chatbot. So that chatbot will give answers to the users questions relevant to PCOS.
def CustomChatGPT(user_input):
messages.append({"role": "user", "content": user_input})
response = openai.ChatCompletion.create(
model = "gpt-3.5-turbo",
messages = messages
)
ChatGPT_reply = response["choices"][0]["message"]["content"]
messages.append({"role": "assistant", "content": ChatGPT_reply})
return ChatGPT_reply
demo = gradio.Interface(fn=CustomChatGPT, inputs = "text", outputs = "text", title = "PCOS expert doctor")
demo.launch(share=True)
We are defining CustomChatGPT function to accept user_input as its parameter, where CustomChatGPT adds the message given by user to its message list. ‘ChatCompletion.create’ sends the message list to OpenAi API to generate response.The response from the API is stored in the ‘response’ variable. To extract the generated response text, ChatGPT_reply = response[“choices”][0][“message”][“content”] is used.
Note : response[“choices”][0] is used to access the first choice from the API response.
Then, a new message is created representing the assistant’s response, with the role set as “assistant” and the content set as ChatGPT_reply. This assistant message is appended to the messages list.
Gradio interface for the CustomChatGPT function, allowing users to interact with the chatbot model in a user-friendly way.
If we want to share your Gradio interface with others, you can use the ‘share =True’ parameter when launching the ‘demo’ interface. This allows you to create a publicly accessible URL that others can use to access and interact with your chatbot.
Finally, the ChatGPT_reply is returned as the output of the function, which can be used to display or process the generated response.
Conclusion : Natural Language Processing plays a key role to build chatbots and it receives the text or commands from the human and then analyzing the text to give right response automatically and can even make a conversation with humans.