HarshitX commited on
Commit
130951e
·
verified ·
1 Parent(s): d50d907

Upload 3 files

Browse files

**Imports and Environment Setup:**
The code imports necessary libraries such as os, streamlit, dotenv, and custom modules (ChatOpenAI and schema classes).
It loads environment variables using load_dotenv().
**Streamlight UI Configuration:**
The Streamlit app is configured with a centred layout and a header that says, “Hey! Let’s Chat…”
The st.set_page_config() function sets the page title.
**Chat Initialization:**
An instance of the ChatOpenAI class (presumably containing an OpenAI language model) is created with a temperature of 0.5.
If the session state doesn’t contain a flow_messages list, it initializes it with a system message: “You are a helpful Coding AI assistant.”
**Response Generation:**
The gen_openai_response() function takes a user input (question) and appends it to the flow_messages.
It then queries the chatbot model and appends the generated answer to the flow_messages.
The function returns the chatbot’s response.
**User Interaction:**
The Streamlit app displays a text input field where users can type questions.
When the “Ask the question” button is clicked, the response is shown under the “The Response is” subheader.
This code snippet sets up a basic conversational chatbot interface using Streamlit and leverages an OpenAI model for generating responses. Users can input questions, and the chatbot provides answers based on the model’s training.

Files changed (3) hide show
  1. .env +3 -0
  2. main.py +39 -0
  3. requirements.txt +5 -0
.env ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ """
2
+ OPENAI_API_KEY='sk-proj-JCOQNBOPtxXQIyI6PKONT3BlbkFJasLMfjZzZD4yjKYnwBka'
3
+ """
main.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## Conversational Q&A Chatbot
2
+ import os
3
+ import streamlit as st
4
+
5
+ from dotenv import load_dotenv
6
+ from langchain_community.chat_models import ChatOpenAI
7
+ from langchain.schema import HumanMessage, SystemMessage, AIMessage
8
+
9
+ load_dotenv()
10
+
11
+ # Streamlit UI
12
+ st.set_page_config(page_title="Conversational Q&A Chatbot", layout="centered")
13
+ st.header("Hey! Let's Chat...")
14
+
15
+ chat = ChatOpenAI(temperature=0.5)
16
+
17
+ if 'flow_messages' not in st.session_state:
18
+ st.session_state['flow_messages']=[
19
+ SystemMessage(content="You are a helpful Coding AI assistant.")
20
+ ]
21
+
22
+ # Functions to load OpenAI model and get responses
23
+ def gen_openai_response(question):
24
+ st.session_state["flow_messages"].append(HumanMessage(content=question))
25
+ answer = chat(st.session_state["flow_messages"])
26
+ st.session_state["flow_messages"].append(AIMessage(content=answer.content))
27
+ return answer.content
28
+
29
+ user_input= st.text_input("Input: ",key="input")
30
+ response= gen_openai_response(user_input)
31
+
32
+ submit=st.button("Ask the question")
33
+
34
+ if __name__ == "__main__":
35
+ # If ask button is clicked
36
+
37
+ if submit:
38
+ st.subheader("The Response is")
39
+ st.write(response)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ langchain
2
+ langchain_community
3
+ langchain_openai
4
+ python-dotenv
5
+ streamlit