How I created my first Chatbot ?
- Python
- Streamlit
- Tableau APIs
- OpenAI (Model gpt-4o-mini )
- openai : to call ChatGPT LLM
- streamlit : this is our UI developer
- os
- sys
- pandas : for data processing
- matplotlib.pyplot : for plotting the charts
- User Asks Question from UI
- Send the question to ChatGPT along with the dataset schema
- Inform GPT through prompt that code should be generated in Python
- Capture the ChatGPT response
- Execute the code by performing small cleanup on the code received from GPT
- Display code output on the UI
import openaiimport streamlit as stimport osimport sys # for commandline aurgumentsimport pandas as pdimport 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'
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 askedQuestions : {v_userQuestions}"""#print(prompt)return promptdef 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 dataprompt = callPrompt(v_userQuestions,columns) # create a prompt with list of columnsresponse = 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 openAIResponseopenAIResponse = callOPenAI(v_userQuestions)#st.write(openAIResponse) # print python code on UI for validation purposeopenAIResponse = openAIResponse.strip() # leading and trailing whitespaces# Remove triple backticks and 'python' if presentopenAIResponse = 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
No comments:
Post a Comment