Friday, 7 February 2025

 How I created my first Chatbot ?

In my current project, Chatbot development initiative was started by our Project Manager and it opened up an opportunity for all of us to learn about it. I also started exploring about it and with the guidance and help from my project manager, YouTube, online blogs / documents and ChatGPT, I was able to develop my first little chatbot that leveraged ChatGPT LLM.

I would like to share what all components I have used and how I developed it.

Use Case : Tableau Admin Assistant, that answers questions on Licenses, Users, Data sources, Jobs, etc.

Tools & Libraries
  1. Python
  2. Streamlit
  3. Tableau APIs
  4. OpenAI (Model gpt-4o-mini )
Python Libraries
  • openai : to call ChatGPT LLM
  • streamlit : this is our UI developer
  • os 
  • sys
  • pandas : for data processing
  • matplotlib.pyplot : for plotting the charts

Flow chart of Chatbot
  1. User Asks Question from UI
  2. Send the question to ChatGPT along with the dataset schema
  3. Inform GPT through prompt that code should be generated in Python
  4. Capture the ChatGPT response
  5. Execute the code by performing small cleanup on the code received from GPT
  6. Display code output on the UI
Coding

Import the below libraries

import openai
import streamlit as st
import os
import sys # for commandline aurguments
import pandas as pd
import matplotlib.pyplot as plt

Create a dropdown on the UI for the user to select the subject on which question needs to be asked 

v_Query = st.selectbox('Query',['Select','Licenses','Datasources','Groups','Users']) 

 Based upon what subject user has selected, I will be deciding my source file that holds the subject information

if v_Query == 'Select':

    st.write("Please select subject to query from the above dropdown.")

elif v_Query == 'Licenses':

    File = "Usage.txt"

    data = pd.read_csv(File)

    columns = data.columns.tolist()

Set openai api_key

openai.api_key = 'place_your_open_ai_api_key_here'

Once question is received, execute the below code. 

if v_userQuestions == '':
    st.write("How can I help you?")
else:

    def callPrompt(v_userQuestions,v_dataSample):
        prompt = f"""
            I have this dataset with the columns {columns}.
            
            Generate python code based upon the question asked
            
            Questions : {v_userQuestions}
                  """
        #print(prompt)
        
        return prompt

    def callOPenAI(v_userQuestions):

        v_dataSample = data.head(100).to_string(index=False) # I'm loading the sample data here
        #print(v_dataSample)
        #prompt = callPrompt(v_userQuestions,v_dataSample) # create a prompt with sample data
        prompt = callPrompt(v_userQuestions,columns) # create a prompt with list of columns
        
        response = openai.ChatCompletion.create(model = 'gpt-4o-mini'
                                    ,messages = [
                                                  {"role" : "system", "content" : "you are an intelligent assistant to generate python code using the columns provided. Use python only. My data frame variable is data. Convert Timestamp column to date before applying in the filter. Store final output always in FinalResult variable. Always import pandas libray "}
                                                , {"role":"user", "content":prompt}
                                                ]
                                    , max_tokens = 500
                                    ,temperature=0)
                                    
        #print(response["choices"][0]["message"]["content"])
        print(response)
        openAIResponse = response["choices"][0]["message"]["content"]
        return openAIResponse
        
        
    openAIResponse = callOPenAI(v_userQuestions)
    #st.write(openAIResponse) # print python code on UI for validation purpose

    openAIResponse = openAIResponse.strip() # leading and trailing whitespaces
    # Remove triple backticks and 'python' if present
    openAIResponse = openAIResponse.replace("'''python", "").replace("```python", "")
    openAIResponse = openAIResponse.replace("'''", "").replace("```", "")
    openAIResponse = openAIResponse.strip()

    execResult = {}
    #st.pyplot(exec(openAIResponse))
    exec(openAIResponse,{'data':data},execResult) # pass data as a global scope. Follow the systax. exec returns none. To store its output, use local variable example execResults.
    result = execResult['FinalResult']
    st.write(result)

Important Functions

ChatCompletion.create : This is an OpenAI function that takes model, message, max_tokens, temperature as arguments.

  • message are sent in the form of role and content. Role defines who is speaking. Content is the question or the text.
  • max_tokens limits gpt response to number of tokens. Change depends on numbers of tokens.
  • temperature ranges from 0 to 1. 0 means model will be non-creative. Greater than 0 means model starts getting creative.


     # model = select the model within the GPT versions like GPT4/3 etc

    # message = The chat completion function requires a list of messages, each with a role ("system", "user", or "assistant") and content.
    # A list of dictionaries where each dictionary represents a message in the conversation.
    # role: Defines who is speaking. It can be "system" (to set context), "user" (the input from the user), or "assistant" (the model's response).
    # content: The actual text content of the message.
    # max_tokens = max number of token you want to limit in this request. OpenAI will not go beyond the max limits as charge depends on tokens consumed.
    # temperature = can range from 0 to 1. 0 means model will not be creative. with temperatur greater that 0, model starts being creative towards the answers.
    # n = numbers of outputs model can send in the reponse
   
Reading the Response : ChatGPT sends response in the json format. Based upon the number of answers we have requested ChatGPT, it sends multiple answers. 
To retrieve the first answer follow the syntax, response["choices"][0]["message"]["content"]

Chatbot Screens





Thank you for reading !!!

 

No comments:

Post a Comment