JustusI commited on
Commit
7d3360a
·
verified ·
1 Parent(s): 5c1f652

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -7
app.py CHANGED
@@ -31,7 +31,7 @@ def augment_prompt(query, vectordb):
31
  source_knowledge = "\n".join([x.page_content for x in results])
32
  augmented_prompt = f"""
33
  You are an AI assistant. Use the context provided below to answer the question as comprehensively as possible.
34
- If the answer is not contained within the context, respond with "I don't know".
35
 
36
  Context:
37
  {source_knowledge}
@@ -41,9 +41,9 @@ def augment_prompt(query, vectordb):
41
  return augmented_prompt
42
 
43
  # Function to handle chat with OpenAI
44
- def chat_with_openai(query, vectordb, openai_api_key):
45
- chat = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key=openai_api_key)
46
- augmented_query = augment_prompt(query, vectordb)
47
  prompt = HumanMessage(content=augmented_query)
48
  messages = [
49
  SystemMessage(content="You are a helpful assistant."),
@@ -52,6 +52,37 @@ def chat_with_openai(query, vectordb, openai_api_key):
52
  res = chat(messages)
53
  return res.content
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  # Streamlit UI
57
  st.title("Data Roles Company Finder Chatbot")
@@ -71,19 +102,69 @@ for message in st.session_state.messages:
71
  with st.chat_message(message["role"]):
72
  st.markdown(message["content"])
73
 
74
-
75
  # User input
76
  if prompt := st.chat_input("Enter your query"):
77
  st.session_state.messages.append({"role": "user", "content": prompt})
78
  with st.chat_message("user"):
79
  st.markdown(prompt)
80
 
 
 
 
 
 
 
 
 
81
  with st.chat_message("assistant"):
82
- openai_api_key = st.secrets["OPENAI_API_KEY"]
83
- response = chat_with_openai(prompt, vectordb, openai_api_key)
84
  st.markdown(response)
85
 
86
  st.session_state.messages.append({"role": "assistant", "content": response})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
  # # Query input
89
  # query = st.text_input("Enter your query", "")
 
31
  source_knowledge = "\n".join([x.page_content for x in results])
32
  augmented_prompt = f"""
33
  You are an AI assistant. Use the context provided below to answer the question as comprehensively as possible.
34
+ If the answer is not contained within the context, respond politely that you cannot provide that information.
35
 
36
  Context:
37
  {source_knowledge}
 
41
  return augmented_prompt
42
 
43
  # Function to handle chat with OpenAI
44
+ def chat_with_openai(query, vectordb, openai_api_key, search_results):
45
+ chat = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key=openai_api_key, timeout=30) # Increased timeout
46
+ augmented_query = augment_prompt(query, vectordb, search_results)
47
  prompt = HumanMessage(content=augmented_query)
48
  messages = [
49
  SystemMessage(content="You are a helpful assistant."),
 
52
  res = chat(messages)
53
  return res.content
54
 
55
+ # Function to perform web search
56
+ def perform_web_search(query):
57
+ headers = {
58
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}
59
+ search_results = ""
60
+
61
+ # Glassdoor search
62
+ glassdoor_url = f"https://www.glassdoor.com/Search/results.htm?keyword={query}"
63
+ response = requests.get(glassdoor_url, headers=headers)
64
+ if response.status_code == 200:
65
+ soup = BeautifulSoup(response.text, 'html.parser')
66
+ glassdoor_results = soup.find_all('div', {'class': 'jobContainer'})
67
+ for result in glassdoor_results[:5]: # limiting to first 3 results
68
+ title = result.find('a', {'class': 'jobInfoItem jobTitle'}).text.strip() if result.find('a', {'class': 'jobInfoItem jobTitle'}) else 'N/A'
69
+ company = result.find('div', {'class': 'jobInfoItem jobEmpolyerName'}).text.strip() if result.find('div', {'class': 'jobInfoItem jobEmpolyerName'}) else 'N/A'
70
+ location = result.find('span', {'class': 'subtle loc'}).text.strip() if result.find('span', {'class': 'subtle loc'}) else 'N/A'
71
+ search_results += f"Glassdoor Result: {title} at {company}, {location}\n"
72
+
73
+ # Indeed search
74
+ indeed_url = f"https://www.indeed.com/jobs?q={query}&limit=10"
75
+ response = requests.get(indeed_url, headers=headers)
76
+ if response.status_code == 200:
77
+ soup = BeautifulSoup(response.text, 'html.parser')
78
+ indeed_results = soup.find_all('div', {'class': 'jobsearch-SerpJobCard'})
79
+ for result in indeed_results[:5]: # limiting to first 3 results
80
+ title = result.find('h2', {'class': 'title'}).text.strip() if result.find('h2', {'class': 'title'}) else 'N/A'
81
+ company = result.find('span', {'class': 'company'}).text.strip() if result.find('span', {'class': 'company'}) else 'N/A'
82
+ location = result.find('span', {'class': 'location'}).text.strip() if result.find('span', {'class': 'location'}) else 'N/A'
83
+ search_results += f"Indeed Result: {title} at {company}, {location}\n"
84
+
85
+ return search_results
86
 
87
  # Streamlit UI
88
  st.title("Data Roles Company Finder Chatbot")
 
102
  with st.chat_message(message["role"]):
103
  st.markdown(message["content"])
104
 
 
105
  # User input
106
  if prompt := st.chat_input("Enter your query"):
107
  st.session_state.messages.append({"role": "user", "content": prompt})
108
  with st.chat_message("user"):
109
  st.markdown(prompt)
110
 
111
+ # Perform web search
112
+ search_results = perform_web_search(prompt)
113
+
114
+ # Chat with OpenAI
115
+ openai_api_key = st.secrets["OPENAI_API_KEY"]
116
+ response = chat_with_openai(prompt, vectordb, openai_api_key, search_results)
117
+
118
+ # Display assistant response
119
  with st.chat_message("assistant"):
 
 
120
  st.markdown(response)
121
 
122
  st.session_state.messages.append({"role": "assistant", "content": response})
123
+
124
+ # # Function to handle chat with OpenAI
125
+ # def chat_with_openai(query, vectordb, openai_api_key):
126
+ # chat = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key=openai_api_key)
127
+ # augmented_query = augment_prompt(query, vectordb)
128
+ # prompt = HumanMessage(content=augmented_query)
129
+ # messages = [
130
+ # SystemMessage(content="You are a helpful assistant."),
131
+ # prompt
132
+ # ]
133
+ # res = chat(messages)
134
+ # return res.content
135
+
136
+
137
+ # # Streamlit UI
138
+ # st.title("Data Roles Company Finder Chatbot")
139
+ # st.write("This app helps users find companies hiring for data roles, providing information such as job title, salary estimate, job description, company rating, and more.")
140
+
141
+ # # Load vector database
142
+ # zip_file_path = "chroma_db_compressed_.zip"
143
+ # extract_path = "./chroma_db_extracted"
144
+ # vectordb = load_vector_db(zip_file_path, extract_path)
145
+
146
+ # # Initialize session state for chat history
147
+ # if "messages" not in st.session_state:
148
+ # st.session_state.messages = []
149
+
150
+ # # Display chat history
151
+ # for message in st.session_state.messages:
152
+ # with st.chat_message(message["role"]):
153
+ # st.markdown(message["content"])
154
+
155
+
156
+ # # User input
157
+ # if prompt := st.chat_input("Enter your query"):
158
+ # st.session_state.messages.append({"role": "user", "content": prompt})
159
+ # with st.chat_message("user"):
160
+ # st.markdown(prompt)
161
+
162
+ # with st.chat_message("assistant"):
163
+ # openai_api_key = st.secrets["OPENAI_API_KEY"]
164
+ # response = chat_with_openai(prompt, vectordb, openai_api_key)
165
+ # st.markdown(response)
166
+
167
+ # st.session_state.messages.append({"role": "assistant", "content": response})
168
 
169
  # # Query input
170
  # query = st.text_input("Enter your query", "")