Class 29 - CHATBOT DEBUGGING IN VS CODE Notes from the AI Basic Course by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)
Class 29 - CHATBOT DEBUGGING IN VS CODE
Notes from the AI Basic Course by Irfan Malik & Dr Sheraz Naseer (Xeven Solutions)
{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 'entered_prompt' not in st.session_state:
st.session_state['entered_prompt'] = "" # Store the latest user input
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
# 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 = ""
# 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']):
Recommended by LinkedIn
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)
response = ai_response.content
return
# 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')
}
Your small gestures show, where you are going in life.
In life, Morals & Manners matters alot.
Debugger:
Bug means error
Variables are like temporary storage.
Divide code into tiny pieces.
Go line by line & then function by function, module by module/.
Don't cheat with yourself.
Programmer has strong logic building skills.
Q: How to develop logic building & critical thinking?
It develops, when you write code by yourself, stop copy pasting.
Don't be oversmart, be honest to yourself.
Programmer is like an artist.
Develop a skill, how you convert 100 lines to 10 lines, means smart coding.
When you think you have done alot of practice, it means you have just started.
#AI #artificialintelligence #datascience #irfanmalik #drsheraz #xevensolutions #openai #chatbot #streamlit #hamzanadeem