Introduction

Text analysis, the process of extracting meaningful insights and understanding from textual data, has undergone a remarkable evolution over the years. From traditional rule-based approaches to the advent of statistical methods and machine learning techniques, the field has witnessed significant advancements. However, the recent breakthroughs in large language models have revolutionized text analysis, enabling a new era of natural language understanding and generation. In the past, text analysis primarily relied on rule-based systems that employed handcrafted heuristics and patterns to extract information from text. While these approaches were effective for specific tasks, they often lacked the flexibility to handle the complexities and nuances of natural language. As data volumes increased exponentially, the need for more scalable and adaptable techniques became evident.

The introduction of statistical methods, such as bag-of-words and n-gram models, brought a significant shift to text analysis. These approaches allowed for more robust and automated processing of textual data by leveraging statistical patterns. However, they still faced limitations in capturing the contextual and semantic aspects of language. Machine learning techniques further propelled the evolution of text analysis. Supervised learning algorithms enabled the training of models on labeled data, empowering them to classify text, extract entities, and perform sentiment analysis. These approaches showcased promising results but were constrained by the availability of labeled data and the need for manual feature engineering.

Learning Objectives

  • Learn about Generative AI and LLMs
  • Learn about Open AI and the steps to access the API key
  • Learn about plotly, its feature, and its components
  • Building a chatbot app using plotly dash

What is Generative AI?

Generative AI refers to a branch of artificial intelligence focused on creating models and systems that have the ability to generate new and original content. These models are designed to understand patterns and structures within existing data and then use that knowledge to generate novel outputs that resemble the original data. In natural language processing, generative models can generate coherent and contextually relevant text, write stories, or even generate code. In computer vision, generative models can create new images, alter existing images, or generate realistic visual content. In the field of music, generative AI models can compose original pieces of music in different genres.

What are Large Language Models?

The Large language models refer to advanced artificial intelligence models that have been trained on vast amounts of textual data to understand and generate human-like text. These models leverage the power of deep learning and transformer architectures, enabling them to learn from vast amounts of text data. They capture the underlying structure of language, including contextual dependencies and semantic relationships, by employing self-attention mechanisms. As a result, they exhibit unprecedented capabilities in understanding, generating, and manipulating natural language. Large language models have been trained on diverse corpora, encompassing books, articles, websites, and even code repositories. This vast exposure to textual data equips them with a broad knowledge base and the ability to comprehend and generate coherent text across various domains and languages. They have demonstrated proficiency in tasks like language translation, text completion, question-answering, and even creative writing.

As we plan to build a Chat-GPT-like application. Let’s take a look at chat GPT and also understand the limitation with an example.

Query:
where was the recent G20 summit held?

OUTPUT from ChatGTP:
As an AI model with a knowledge cutoff in September 2021, I can provide 
information up until that date. The most recent G20 summit was held in Rome, 
Italy. However, please note that there may have been subsequent G20 summits 
after my knowledge cutoff.....

What is Plotly Dash?

Plotly Dash is an open-source Python framework that excels in building interactive analytical web applications. It is built on top of Flask, Plotly.js, and React.js, combining the strengths of these technologies. Dash simplifies the process of creating complex web applications by providing a user-friendly and intuitive framework that allows data scientists to integrate their models seamlessly. The framework’s interactive interface empowers users to make data-backed business decisions through visually compelling and customizable dashboards.

What sets Plotly Dash apart from other web development frameworks in Python is its exceptional UI components and visualization capabilities. Dash offers a vast array of interactive elements, including dropdowns, graphs, maps, and more, all powered by Plotly.js. This JavaScript library enables developers to build visually stunning and highly customizable dashboards effortlessly. With Dash, complex layouts and advanced visualizations can be incorporated seamlessly, enhancing the overall user experience.

Steps to Building Plotly Dash App

Here are the features that we will implement in the chatbot:

  • The app should have a text field for the user to type the query and a send button to submit the query
  • The AI’s response should be displayed below the send button
  • The app should track the chat history, which should be displayed at the bottom of the screen. 
  • Each of the answers should be time stamped
  • The app should have a counter to track the number of queries made by the user.

Let’s get started !!!

Step 1: Get the Open AI API Key

  • Sign up and create an account at https://platform.openai.com/signup
  • Navigate to https://platform.openai.com/account/api-keys
  • Click on Create new secret key, copy the key, and save it.

Step 2: Set up the API key and initialize the model engine

OPENAI_API_KEY = "YOUR_OPENAI_API_KEY"
openai.api_key = OPENAI_API_KEY

# Initialize the ChatGPT 
modelmodel_engine = 'text-davinci-003'

Step 2: Set up your development environment

Here are the steps to create a virtual development environment

# STEP 1: Open the terminal and install the library virtualenv
pip install virtualenv

# STEP 2: Create a new virtual environment
virtualenv venv_llm

# STEP 3: Activate the new virtual environment
venv\scripts\activate

# Install all the required dependent libraries
#!pip install os
#!pip install openai
#!pip install dash
#!pip install dash_bootstrap_components
#!pip install virtualenv


# Import all the required dependent libraries
import os
import pandas as pd
import openai
from datetime import datetime
from dash import Dash, dcc, html, Input, Output, State
import dash_bootstrap_components as dbc
from dash.exceptions import PreventUpdate

OPENAI_API_KEY = "YOUR_OPENAI_API_KEY"
openai.api_key = OPENAI_API_KEY
# Initialize the ChatGPT model
model_engine = "text-davinci-003"

Step 3: Build the User Interface (Front end)

We will build an application layout with a text area and a button. Also, we need two place holder one for displaying the response from the AI and the other for tracking chat history

# Instantiate the Dash app
app = Dash(__name__)

answer_list = []
df = pd.DataFrame()

app.layout = dbc.Container(
    [
        dbc.Row(
            [
                dbc.Col(
                    [
                        html.H1(
                            "CHAT WITH GENERATIVE AI",
                            style={
                                "text-align": "center",
                                "font-size": 30,
                                "font-weight": "bold",
                                "color": "#2E86C1",
                            },
                        ),
                    ]
                ),
            ]
        ),
        dbc.Row(
            [
                dcc.Textarea(
                    id="input-text",
                    style={"width": "50%", "height": 75, "margin-left": "350px"},
                )
            ]
        ),
        dbc.Row(
            [
                dbc.Button(
                    "SEND",
                    id="submit-button",
                    n_clicks=0,
                    style={
                        "backgroundColor": "blue",
                        "font-size": 12,
                        "font-weight": "bold",
                        "color": "white",
                        "margin-left": "350px",
                    },
                )
            ]
        ),
        html.Br(),
        dbc.Row(
            [
                dcc.Loading(
                    [
                        html.Div(
                            id="output-text",
                            style={
                                "margin-left": "350px",
                                "font-size": 17,
                                "font-weight": "bold",
                                "color": "Teal",
                                "width": "50%",
                            },
                        )
                    ],
                    type="default",
                )
            ]
        ),
        html.Br(),
        html.Br(),
        html.Div(
            id="output-chat-history",
            style={
                "text-align": "left",
                "margin-left": "350px",
                "font-size": 12,
                "font-weight": "bold",
                "color": "#2E86C1",
            },
        ),
        html.Div(
            id="output-text-track",
            style={
                "whiteSpace": "pre-line",
                "margin-left": "350px",
                "width": "50%",
                "margin-top": "10px",
            },
        ),
    ]
)#import csv

Step 4: Add interactivity with callbacks (Backend)

We will define a callback function that takes in the query as input, retrieves the response from the AI model, creates a logic for tracking chat history, and returns it to the UI.

@app.callback(
    [
        Output("output-text", "children"),
        Output("output-text-track", "children"),
        Output("output-chat-history", "children"),
    ],
    Input("submit-button", "n_clicks"),
    State("input-text", "value"),
)
def update_output(n_clicks, input_text):
    if n_clicks > 0:
        # Get the response from ChatGPT
        response = openai.Completion.create(
            engine=model_engine,
            prompt=f"{input_text}\n",
            max_tokens=4000,
            n=1,
            stop=None,
            temperature=0.7,
        )

        generated_text = response.choices[0].text
        answer_list_time = (
            f'\n{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}: {generated_text}\n'
        )
        answer_list.insert(0, answer_list_time)

        return (
            generated_text,
            answer_list,
            f"RECENT CHAT HISTORY ( {len(answer_list)} )",
        )

    else:
        raise PreventUpdate

Step 5: Run the application

  • Open the terminal, navigate to the project directory
  • Type the below command
<working directory>python app.py

Dash is running on http://127.0.0.1:8050/

 * Serving Flask app 'app'
 * Debug mode: on
  • Open the browser and type http:127.0.0.1/8050

Congratulations !! we have successfully implemented all the features that we had planned in the earlier section.

Real-world applications of apps driven by LLM

  • Language Translation and Interpretation: OpenAI’s LLM models can facilitate language translation and interpretation tasks. They can be used to build powerful translation systems that can translate text or speech between multiple languages accurately.
  • Customer Support and Chatbots: LLMs can be utilized in developing advanced natural language processing systems and chatbots. They can understand and generate human-like responses, allowing them to converse and provide information, support, or recommendations.
  • Writing Assistance: OpenAI’s language models can assist in content generation tasks, such as writing articles, essays, or product descriptions. They can provide suggestions, help with research, and even generate complete drafts based on given prompts or guidelines.

Conclusion

Building a Python chatbot using OpenAI has been fun and it shows the ease of building such apps. OpenAI’s advanced language model, combined with its API service, enables the creation of intelligent and interactive chatbots. Throughout this blog, we have covered the essential steps, from setting up an OpenAI account to implementing the chatbot using Python. By harnessing the power of natural language processing and machine learning, developers can create chatbots that understand and respond to user inputs with impressive accuracy. With OpenAI, the possibilities for creating sophisticated and effective chatbots are endless, paving the way for enhanced user engagement and exceptional conversational experiences.

I hope you liked the article and found it helpful.

FAQs

Q1: What are the other Python frameworks for building similar apps?
A1: Some common frameworks include Streamlit, Chainlit, and Langflow

Q2: Are there any restrictions to using Open AI API Key?
A1: As we have used the free version of the API key, the number of transactions/ queries will be limited. But this is good enough for learning purposes and it is not recommended for production purposes.

Q3: What more can be done with Open AI other than a chatbot? 
A3: There are many LLM models available from Open AI, some examples are document classification, advanced transformer models like GPT, code generation, text-to-images, and more. Applications can be built using these powerful model catering to specific domains/business case

Q4:What are the applications of generative AI?
A4Generative models have a wide range of applications across different domains for example Image generation, natural language processing, and music composition to note a few.

Q5: Where can I find the learning materials for generative AI?
A5Online Courses platforms like Coursera, Udemy, and edX and OpenAI’s documentation and blogs are good places to get started

Key Takeaways

  • Plotly Dash uses a declarative syntax, making it easy to build complex web applications without extensive knowledge of web development.
  • Dash seamlessly integrates with Python and leverages its extensive ecosystem of libraries and tools.
  • OpenAI offers access to state-of-the-art generative AI models, such as GPT-3, which can understand, generate, and respond to human language.
  • OpenAI’s models are highly flexible and customizable. Developers can fine-tune the models based on specific requirements, adapting them to specific domains or applications.

References

https://dash-bootstrap-components.opensource.faculty.ai/

https://plotly.com/dash/

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *