Tuesday 19 March 2024

Building a Chatbot Using the Google PaLM API and FrontEnd with Flask

Pathway Language Model (PaLM)

It is a powerful  LLM for Generative AI by Google.

Let us use this to make ChatBot.

Step 1:

Go to https://makersuite.google.com/app/apikey

Login using your Google A/C 

Get palm_api_key and store in safe place

Create a New directory "PALMCHAT"in your Hard Drive.

Below PALMCHAT create another sub directory "template"

Step 2: [Backend- Server]

Now in Visual Studio Code, Open Folder PALMCHAT

Create the file .env and put the following contents

OPENAI_API_KEY = "palm_api_key"

Create another file server.py andd add the following code:

from flask import Flask, render_template, request
import google.generativeai as palm
import os
from dotenv import load_dotenv
load_dotenv()
palm_api_key = os.environ["OPENAI_API_KEY"]

palm.configure(api_key="AIzaSyBNI2rLwpVKs3mI4fG5dZ5OVzIga0-iPy8")

app = Flask(__name__)

@app.route("/")
def home():
    return render_template("index.html")

@app.route("/chatbot", methods=["POST"])
def chatbot():
    user_input = request.form["message"]
   
    models = [m for m in palm.list_models() if 'generateText' in m.supported_generation_methods]
    model = models[0].name
   
    prompt =f"User: { user_input} \n AUCSE PaLM Bot:"

    response = palm.generate_text(
        model = model,
        prompt = prompt,
        stop_sequences=None,
        temperature=0,
        max_output_tokens=100
    )

    bot_response = response.result

    chat_history = []
    chat_history.append(f"Use: {user_input} \n AUCSE Palm BOT {bot_response}")

    return render_template(
        "chatbot.html",
        user_input=user_input,
        bot_response=bot_response,
        chat_history=chat_history
    )
if __name__ == "__main__":
    app.run(debug=True)


 In terminal 

pip install flask
pip install dotenv
pip install google.generativeai

Step 3: [Front End- index, chatbot.htmls files ]

Under the subdirectory create files index.html with the following contents

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AUCSE PaLM Bot</title>
</head>
<body>
    <h1>AUCSE PaLM BOT</h1>
    <p>AUCSE PaLM Bot: Welcome to our AUCSE ! </p>
    <form method="post" action="{{url_for('chatbot')}}" class="chat-input">
        <input type="text" id="message" name="message" placeholder="type your msg...">
        <button type="submit">Send</button>    
    </form>
</body>
</html>

create anotherfile chatbot.html with the following code (same as above with little modification with adding after <h1> tag <p>{{bot_response}}<p>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AUCSE Chat Bot</title>
</head>
<body>
    <h1>AUCSE PaLM BOT</h1>
    <p>{{bot_response}}</p>
     <form method="post" action="{{url_for('chatbot')}}" class="chat-input">
        <input type="text" id="message" name="message" placeholder="type your msg...">
        <button type="submit">Send</button>
    </form>
</body>
</html>

Now run server.py by clicking the top right button play(run) . You will get screen similar according to your environment

If you click the above image, you will see the contents clearly. You can see the server running,
with line Running on http://127.0.0.1:5000. If you move your mouse, place on that, and CTRL + CLICK, you will get browser window similar as shown below:
If you enter question 
What is AWS?
You will get screen as shown below:

Wow ! It is so Simple to generate a basic PalM based Chat bot with google Generative Ai and Flask in Python. 

You can modify model, parameters like temperature, max_number_tokens, etc., as per your will and requirements. You can add features.
You can beautify with css in html files for stunning look and feel. Deep dive in to PaLM!! Have greate Fun and Enjoy! Sky is even no Limit?!
















No comments:

Post a Comment

Making Prompts for Profile Web Site

  Prompt: Can you create prompt to craft better draft in a given topic. Response: Sure! Could you please specify the topic for which you...