Class 28 - CHATBOT USING OPENAI STREAMLIT Notes from the AI Basic Course by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)
Class 28 - CHATBOT USING OPENAI STREAMLIT
Notes from the AI Basic Course by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)
In this lecture, we will going to discuss Coding Rules.
When, you see someone else code, it will be difficult for you in first impression.
That time, give your brain time to absorb things, have some PATIENCE.
Chat-Bots are very valueable now-a-days.
Chat-bots are need of everyone.
Before that, chat-bots are used in customer service.
Human decisions are biased, but technology cann't be biased, because they have the accuracy.
Section storage is temporary, mostly on the client side, client side means the browser you are using at that time.
As a developer, keep in mind, these type of storage are risky that involves session storage, high risk of hacking attempts in this case, don't save sensitive storage on session side.
In this case, we are using OpenAI API Key for chatbot.
While building chatbots, keep the concept of anatomy of prompt in your mind.
Control the sensitivity in your prompt.
Keep concise tokens, otherwise you have to bear cost.
In LangChain, we have more control.
Goolge Slides Link:
Grasp the concepts, don't do ratta.
What can wo do with LangChain, LamaIndex here:
Purpose is to connect LLM with data source.
{from itertools import zip_longest
import streamlit as st
from streamlit_chat import message
from langchain.chat_models import ChatOpenAI
from langchain.schema import (
SystemMessage,
HumanMessage,
AIMessage
)
openapi_key = st.secrets["OPENAI_API_KEY"]
# Set streamlit page configuration
st.set_page_config(page_title="Hope to Skill ChatBot")
st.title("AI Mentor")
# Initialize session state variables
if 'generated' not in st.session_state:
st.session_state['generated'] = [] # Store AI generated responses
if 'past' not in st.session_state:
st.session_state['past'] = [] # Store past user inputs
if 'entered_prompt' not in st.session_state:
st.session_state['entered_prompt'] = "" # Store the latest user input
# Initialize the ChatOpenAI model
chat = ChatOpenAI(
temperature=0.5,
model_name="gpt-3.5-turbo",
openai_api_key=openapi_key,
max_tokens=100
)
def build_message_list():
"""
Build a list of messages including system, human and AI messages.
"""
# Start zipped_messages with the SystemMessage
zipped_messages = [SystemMessage(
# content="You are a helpful AI assistant talking with a human. If you do not know an answer, just say 'I don't know', do not make up an answer.")]
领英推荐
content = """your name is AI Mentor. You are an AI Technical Expert for Artificial Intelligence, here to guide and assist students with their AI-related questions and concerns. Please provide accurate and helpful information, and always maintain a polite and professional tone.
1. Greet the user politely ask user name and ask how you can assist them with AI-related queries.
2. Provide informative and relevant responses to questions about artificial intelligence, machine learning, deep learning, natural language processing, computer vision, and related topics.
3. you must Avoid discussing sensitive, offensive, or harmful content. Refrain from engaging in any form of discrimination, harassment, or inappropriate behavior.
4. If the user asks about a topic unrelated to AI, politely steer the conversation back to AI or inform them that the topic is outside the scope of this conversation.
5. Be patient and considerate when responding to user queries, and provide clear explanations.
6. If the user expresses gratitude or indicates the end of the conversation, respond with a polite farewell.
7. Do Not generate the long paragarphs in response. Maximum Words should be 100.
Remember, your primary goal is to assist and educate students in the field of Artificial Intelligence. Always prioritize their learning experience and well-being."""
)]
# Zip together the past and generated messages
for human_msg, ai_msg in zip_longest(st.session_state['past'], st.session_state['generated']):
if human_msg is not None:
zipped_messages.append(HumanMessage(
content=human_msg)) # Add user messages
if ai_msg is not None:
zipped_messages.append(
AIMessage(content=ai_msg)) # Add AI messages
return zipped_messages
def generate_response():
"""
Generate AI response using the ChatOpenAI model.
"""
# Build the list of messages
zipped_messages = build_message_list()
# Generate response using the chat model
ai_response = chat(zipped_messages)
return ai_response.content
# Define function to submit user input
def submit():
# Set entered_prompt to the current value of prompt_input
st.session_state.entered_prompt = st.session_state.prompt_input
# Clear prompt_input
st.session_state.prompt_input = ""
# Create a text input for user
st.text_input('YOU: ', key='prompt_input', on_change=submit)
if st.session_state.entered_prompt != "":
# Get user query
user_query = st.session_state.entered_prompt
# Append user query to past queries
st.session_state.past.append(user_query)
# Generate response
output = generate_response()
# Append AI response to generated responses
st.session_state.generated.append(output)
# Display the chat history
if st.session_state['generated']:
for i in range(len(st.session_state['generated'])-1, -1, -1):
# Display AI response
message(st.session_state["generated"][i], key=str(i))
# Display user message
message(st.session_state['past'][i],
is_user=True, key=str(i) + '_user')
}
#AI #artificialintelligence #datascience #irfanmalik #drsheraz #xevensolutions #openai #chatbot #streamlit #hamzanadeem