Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
37 |
-
if api_key_Groq:
|
38 |
-
|
39 |
-
st.write("Groq API Key is set.")
|
40 |
else:
|
41 |
-
st.write("
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
self
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
)
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
st.
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
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}")
|
|
|
|