Skip to content

srikanth-kandi/myGenAIChatBot

Repository files navigation

ChatBot with OpenAI and LangChain

Main Handbook, This handbook

Prerequisites
  • Existing Open AI Account:

    • Check whether you have a limit to using the Open AI API

    • The below screenshot indicated it is expired by June 1. So, you need to create a new account.

      Free trail expired

    • Check your API key usage here

  • Creating Open AI Account with New Mail and New Phone Number:

    • Open https://openai.com

    • Click on Sign Up button

    • Choose your preffered Sign Up method

    • After logged in click here to see below options

      OpenAI Options

  • Create a Hugging Face Account

    • Open https://huggingface.co/

    • Click on Sign Up button

    • Enter your details and Click on Sign Up button

    • Verify your email address

  • Create a New Space in Hugging Face Account

    • Click on Profile icon top right

      Hugging face profile icon

    • Click on New Space

      Hugging face new space

    • Enter below details of your new space

      Hugging face new space details

  • Open the below provided Colab link

    Open In Colab

  • Copying Code to your Google Drive

    • On the top left corner of Google Colab Notebook you can find File, click on it

      Google Colab File Section

    • Click on Save a copy in Drive

      Google Colab Save a copy in Drive

    • If you are not logged in to your Google Account, please log into it.

    • Once you are successfully logged in a new Google Colab Notebook with the given code will be opened

Install the required packages
  • Click on the Play button to Install the Packages

    Google Colab pip install section

Importing the required packages
  • Click on the Play button to import the required things to build the application

    Google Colab import section

Get the OpenAI API Key and set it as environmental variable
Assigning the values for template, prompt, and memory
  • You can update the first line of the template with Prompts and Examples provided

  • Click on the Play button

    Google Colab template section

Initializing LLM Chain using OpenAI
  • Using ChatOpenAI method we are creating an LLM Chain

  • Click on the Play button

    Google Colab LLM Chain section

Define a function to generate the response for the question you ask
  • From the initialized llm_chain we will predict the response

  • Click on the Play button

    Google Colab LLM Chain Response

Create a Chat Interface using Gradio
  • We are creating the ChatInterface from gradio and providing a function get_text_response and also examples from Prompts and Examples provided

  • Check for other arguments here

  • Click on the Play button to create an interface

    Google Colab Gradio Chat Interface

Launch your ChatBot with Gradio App
  • Click on the Play button to launch the App

    Google Colab Gradio Chat Interface Launch

Now you can try asking questions in your ChatBot

If you are getting any errors
  • Keep print statements to identify the issue

  • To identify the error you are getting please add debug = True while launching the gradio app

    if __name__ == "__main__":
        demo.launch(debug = True)
    def get_text_response(user_message,history):
        try:
            response = llm_chain.predict(user_message = user_message)
        except Exception as e:
            print("Error:", e)
            try:
                print("Error:", e.error.message)
                response = "Failed to reply: " + e.error.message
            except Exception as e:
                response = "Failed to reply"
        return response

Publish your code to Hugging Face

Login to Hugging Face from Google Colab
  • Create a Hugging Face token and Copy

    • Login to Hugging Face https://huggingface.co/

    • Open https://huggingface.co/settings/tokens

    • Click on New token

    • Add a Name for the Token

    • Choose write Role for the Token

    • Click on Generate a token

    • Copy the Token

    • Click on Play button to enter Hugging Face Token

      Google Colab Hugging Face Token

    • Now paste the Hugging Face token in the textbox provided and click on Login

      Google Colab Hugging Face Token Login

Create Hugging Face API to push code from Google Colab
  • Click on the Play button to create API

    Google Colab Hugging Face API

Adding Hugging Face Repository ID
  • Copy Hugging Face Repository ID by opening the Hugging Face Repo Created

    Hugging Face Repo ID

  • Replace your Repo ID

    Replace Hugging Face Repo ID

  • Click on Play button to assign Hugging Face Repo ID

    Replace Hugging Face Repo ID Play Button

Add OPENAI_API_KEY in Hugging Face Repository secrets
  • Click on Settings Button

    Hugging Face Settings Button

  • Go to Variables and secrets section

    Hugging Face Variables and secrets section

  • Click on New secret

    Hugging Face New secret

  • Enter Name as OPENAI_API_KEY and Value as your OpenAI API Key

    Hugging Face Name and Value

Load Application and Requirement files
  • Click on the Play button to download files

    Google Colab Download Files

  • You can see downloaded files here

    Google Colab Downloaded Files

Editing the Application file
  • Click on the app.py file

    Google Colab Edit app.py

  • The file will be opened and you can edit the <Prompt> and <Example> as per Prompts and Examples and save using ⌃ + S or ⌘ + S

    Google Colab Modify app.py

Push your code to Hugging Face
  • Click on Play button

    Google Colab api upload_file

  • Now in your space, you should see it is Building

    Hugging Face Building

  • On Succesful build, you should see Running

    Hugging Face Running

  • If the Building fails, you will see a Runtime error with some errors below

    Hugging Face Runtime Error

  • Click on Logs to check more details about the error


Show some ❤️ by starring ⭐ this repository!

About

ChatBot with Open AI and LangChain

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published