totallysaber commited on
Commit
c6e3f99
·
verified ·
1 Parent(s): 3d64e75

Update app.py

Browse files

new version not using a env file

Files changed (1) hide show
  1. app.py +63 -64
app.py CHANGED
@@ -1,64 +1,63 @@
1
- import os
2
- import streamlit as st
3
- from langchain_openai import OpenAI
4
- from langchain.chains import RetrievalQA
5
- from langchain_openai import OpenAIEmbeddings
6
- from langchain_community.vectorstores import Chroma
7
- from dotenv import load_dotenv
8
- from langchain_core.messages import HumanMessage, AIMessage
9
-
10
- def run_query(query, chat_history, k, temperature,ai_key):
11
- load_dotenv()
12
- openai_key = ai_key
13
- persist_directory = 'db'
14
-
15
- db = Chroma(persist_directory=persist_directory, embedding_function=OpenAIEmbeddings()) # access db
16
- retriever = db.as_retriever(search_kwargs={"k": k}) # kwargs determines how many docs it uses
17
-
18
- llm = OpenAI(api_key=openai_key, max_tokens=1500, temperature=temperature) # api key self explanatory. max_tokens provides how long of a response
19
- # we get from the llm (do note the llm has a cap of 4097.) and temperature provides a scale form 0.0 to 1.0 of how much freedom
20
- # the llm should take in its response (how closely it should adhere to docs vs how freely)
21
-
22
- # Perform similarity search
23
- search_results = retriever.get_relevant_documents(query)
24
-
25
- # Extract texts from the retrieved documents
26
- context = "\n".join([doc.page_content for doc in search_results])
27
-
28
- # Combine the context with the query
29
- full_query = f"{context}\n\n{query}"
30
-
31
- # Get response from LLM
32
- llm_response = llm(full_query)
33
-
34
- # Store the interaction in chat history
35
- chat_history.append((HumanMessage(content=query), AIMessage(content=llm_response)))
36
-
37
- return llm_response
38
-
39
- def main():
40
- st.title("EmeraldEnergy™️ HVAC Technician Assistant")
41
- api_key = st.text_input("Enter your API key:")
42
- chat_history = []
43
-
44
- role = 'Pretend I am an HVAC technician installing a Mitsubishi heat pump. I do not have access to the manual. Help me install this using your knowledge of the mitsubishi heatpump. Please consider the section under instruction of troubleshooting. If you can not help me, then provide generalized guidance. Please ask for which model of the product we are using at the end. \n'
45
-
46
- query = st.text_area("Enter your query:", height=100)
47
-
48
- k = st.slider("Number of documents to retrieve (k):", min_value=1, max_value=4, value=4)
49
-
50
- temperature = st.slider("Temperature:", min_value=0.0, max_value=1.0, value=0.2)
51
-
52
- if st.button("Submit"):
53
- full_query = role + query
54
- result = run_query(full_query, chat_history, k, temperature,api_key)
55
- st.write(f"AI: {result}")
56
-
57
- # Display chat history
58
- st.write("### Chat History")
59
- for human_msg, ai_msg in chat_history:
60
- st.write(f"**Human:** {query}")
61
- st.write(f"**AI:** {ai_msg.content}")
62
-
63
- if __name__ == "__main__":
64
- main()
 
1
+ import os
2
+ import streamlit as st
3
+ from langchain_openai import OpenAI
4
+ from langchain.chains import RetrievalQA
5
+ from langchain_openai import OpenAIEmbeddings
6
+ from langchain_community.vectorstores import Chroma
7
+ from dotenv import load_dotenv
8
+ from langchain_core.messages import HumanMessage, AIMessage
9
+
10
+ def run_query(query, chat_history, k, temperature,openai_api_key):
11
+ load_dotenv()
12
+ persist_directory = 'db'
13
+
14
+ db = Chroma(persist_directory=persist_directory, embedding_function=OpenAIEmbeddings()) # access db
15
+ retriever = db.as_retriever(search_kwargs={"k": k}) # kwargs determines how many docs it uses
16
+
17
+ llm = OpenAI(api_key=openai_api_key, max_tokens=1500, temperature=temperature) # api key self explanatory. max_tokens provides how long of a response
18
+ # we get from the llm (do note the llm has a cap of 4097.) and temperature provides a scale form 0.0 to 1.0 of how much freedom
19
+ # the llm should take in its response (how closely it should adhere to docs vs how freely)
20
+
21
+ # Perform similarity search
22
+ search_results = retriever.get_relevant_documents(query)
23
+
24
+ # Extract texts from the retrieved documents
25
+ context = "\n".join([doc.page_content for doc in search_results])
26
+
27
+ # Combine the context with the query
28
+ full_query = f"{context}\n\n{query}"
29
+
30
+ # Get response from LLM
31
+ llm_response = llm(full_query)
32
+
33
+ # Store the interaction in chat history
34
+ chat_history.append((HumanMessage(content=query), AIMessage(content=llm_response)))
35
+
36
+ return llm_response
37
+
38
+ def main():
39
+ st.title("EmeraldEnergy™️ HVAC Technician Assistant")
40
+ api_key = st.text_input("Enter your API key:")
41
+ chat_history = []
42
+
43
+ role = 'Pretend I am an HVAC technician installing a Mitsubishi heat pump. I do not have access to the manual. Help me install this using your knowledge of the mitsubishi heatpump. Please consider the section under instruction of troubleshooting. If you can not help me, then provide generalized guidance. Please ask for which model of the product we are using at the end. \n'
44
+
45
+ query = st.text_area("Enter your query:", height=100)
46
+
47
+ k = st.slider("Number of documents to retrieve (k):", min_value=1, max_value=4, value=4)
48
+
49
+ temperature = st.slider("Temperature:", min_value=0.0, max_value=1.0, value=0.2)
50
+
51
+ if st.button("Submit"):
52
+ full_query = role + query
53
+ result = run_query(full_query, chat_history, k, temperature,api_key)
54
+ st.write(f"AI: {result}")
55
+
56
+ # Display chat history
57
+ st.write("### Chat History")
58
+ for human_msg, ai_msg in chat_history:
59
+ st.write(f"**Human:** {query}")
60
+ st.write(f"**AI:** {ai_msg.content}")
61
+
62
+ if __name__ == "__main__":
63
+ main()