AreesaAshfaq commited on
Commit
812e3d9
·
verified ·
1 Parent(s): 041c072

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -92
app.py CHANGED
@@ -10,102 +10,82 @@ import bs4
10
  import torch
11
  import getpass
12
 
13
-
14
  # Prompt the user to enter their Langchain API key
15
  api_key_langchain = st.text_input("Enter your LANGCHAIN_API_KEY", type="password")
16
 
17
- # Check if the API key has been provided
18
- if api_key_langchain:
19
- # Use the API key in your app
20
- st.write("LangChain API Key is set.")
21
- else:
22
- st.write("Please enter your LangChain API key.")
23
-
24
- # Initialize LangChain client (hypothetical example)
25
- #lc_client = Client(api_key=LANGCHAIN_API_KEY)
26
-
27
- #from langchain import SomeLangChainClass # Replace with the correct class
28
- #client = SomeLangChainClass(api_key=api_key_langchain)
29
- #lc = LangChain(api_key=api_key_langchain)
30
-
31
-
32
-
33
  # Prompt the user to enter their Groq API key
34
  api_key_Groq = st.text_input("Enter your Groq_API_KEY", type="password")
35
 
36
- # Check if the Groq API key has been provided
37
- if api_key_Groq:
38
- # Use the Groq API key in your app
39
- st.write("Groq API Key is set.")
40
  else:
41
- st.write("Please enter your Groq API key.")
42
-
43
-
44
-
45
- from langchain_groq import ChatGroq
46
-
47
- llm = ChatGroq(model="llama3-8b-8192", groq_api_key = api_key_Groq)
48
-
49
- # Define the embedding class
50
- class SentenceTransformerEmbedding:
51
- def __init__(self, model_name):
52
- self.model = SentenceTransformer(model_name)
53
-
54
- def embed_documents(self, texts):
55
- embeddings = self.model.encode(texts, convert_to_tensor=True)
56
- if isinstance(embeddings, torch.Tensor):
57
- return embeddings.cpu().detach().numpy().tolist() # Convert tensor to list
58
- return embeddings
59
-
60
- def embed_query(self, query):
61
- embedding = self.model.encode([query], convert_to_tensor=True)
62
- if isinstance(embedding, torch.Tensor):
63
- return embedding.cpu().detach().numpy().tolist()[0] # Convert tensor to list
64
- return embedding[0]
65
-
66
- # Initialize the embedding class
67
- embedding_model = SentenceTransformerEmbedding('all-MiniLM-L6-v2')
68
-
69
- # Load, chunk, and index the contents of the blog
70
- def load_data():
71
- loader = WebBaseLoader(
72
- web_paths=("https://lilianweng.github.io/posts/2023-06-23-agent/",),
73
- bs_kwargs=dict(
74
- parse_only=bs4.SoupStrainer(
75
- class_=("post-content", "post-title", "post-header")
76
- )
77
- ),
78
- )
79
- docs = loader.load()
80
- text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
81
- splits = text_splitter.split_documents(docs)
82
- vectorstore = Chroma.from_documents(documents=splits, embedding=embedding_model)
83
- return vectorstore
84
-
85
- vectorstore = load_data()
86
-
87
- # Streamlit UI
88
- st.title("Blog Retrieval and Question Answering")
89
-
90
- question = st.text_input("Enter your question:")
91
-
92
- if question:
93
- retriever = vectorstore.as_retriever()
94
- prompt = hub.pull("rlm/rag-prompt", api_key=api_key_langchain)
95
-
96
- def format_docs(docs):
97
- return "\n\n".join(doc.page_content for doc in docs)
98
-
99
- rag_chain = (
100
- {"context": retriever | format_docs, "question": RunnablePassthrough()}
101
- | prompt
102
- | llm # Replace with your LLM or appropriate function if needed
103
- | StrOutputParser()
104
- )
105
-
106
- # Example invocation
107
- try:
108
- result = rag_chain.invoke(question)
109
- st.write("Answer:", result)
110
- except Exception as e:
111
- st.error(f"An error occurred: {e}")
 
10
  import torch
11
  import getpass
12
 
 
13
  # Prompt the user to enter their Langchain API key
14
  api_key_langchain = st.text_input("Enter your LANGCHAIN_API_KEY", type="password")
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  # Prompt the user to enter their Groq API key
17
  api_key_Groq = st.text_input("Enter your Groq_API_KEY", type="password")
18
 
19
+ # Check if both API keys have been provided
20
+ if not api_key_langchain or not api_key_Groq:
21
+ st.write("Please enter both API keys.")
 
22
  else:
23
+ st.write("Both API keys are set.")
24
+
25
+ # Initialize the LLM with the provided Groq API key
26
+ from langchain_groq import ChatGroq
27
+ llm = ChatGroq(model="llama3-8b-8192", groq_api_key=api_key_Groq)
28
+
29
+ # Define the embedding class
30
+ class SentenceTransformerEmbedding:
31
+ def __init__(self, model_name):
32
+ self.model = SentenceTransformer(model_name)
33
+
34
+ def embed_documents(self, texts):
35
+ embeddings = self.model.encode(texts, convert_to_tensor=True)
36
+ if isinstance(embeddings, torch.Tensor):
37
+ return embeddings.cpu().detach().numpy().tolist() # Convert tensor to list
38
+ return embeddings
39
+
40
+ def embed_query(self, query):
41
+ embedding = self.model.encode([query], convert_to_tensor=True)
42
+ if isinstance(embedding, torch.Tensor):
43
+ return embedding.cpu().detach().numpy().tolist()[0] # Convert tensor to list
44
+ return embedding[0]
45
+
46
+ # Initialize the embedding class
47
+ embedding_model = SentenceTransformerEmbedding('all-MiniLM-L6-v2')
48
+
49
+ # Load, chunk, and index the contents of the blog
50
+ def load_data():
51
+ loader = WebBaseLoader(
52
+ web_paths=("https://lilianweng.github.io/posts/2023-06-23-agent/",),
53
+ bs_kwargs=dict(
54
+ parse_only=bs4.SoupStrainer(
55
+ class_=("post-content", "post-title", "post-header")
56
+ )
57
+ ),
58
+ )
59
+ docs = loader.load()
60
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
61
+ splits = text_splitter.split_documents(docs)
62
+ vectorstore = Chroma.from_documents(documents=splits, embedding=embedding_model)
63
+ return vectorstore
64
+
65
+ vectorstore = load_data()
66
+
67
+ # Streamlit UI
68
+ st.title("Blog Retrieval and Question Answering")
69
+
70
+ question = st.text_input("Enter your question:")
71
+
72
+ if question:
73
+ retriever = vectorstore.as_retriever()
74
+ prompt = hub.pull("rlm/rag-prompt", api_key=api_key_langchain)
75
+
76
+ def format_docs(docs):
77
+ return "\n\n".join(doc.page_content for doc in docs)
78
+
79
+ rag_chain = (
80
+ {"context": retriever | format_docs, "question": RunnablePassthrough()}
81
+ | prompt
82
+ | llm # Replace with your LLM or appropriate function if needed
83
+ | StrOutputParser()
84
+ )
85
+
86
+ # Example invocation
87
+ try:
88
+ result = rag_chain.invoke(question)
89
+ st.write("Answer:", result)
90
+ except Exception as e:
91
+ st.error(f"An error occurred: {e}")