Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,7 @@ from langchain_core.runnables import RunnablePassthrough
|
|
8 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
9 |
import bs4
|
10 |
import torch
|
|
|
11 |
|
12 |
# Prompt the user to enter their Langchain API key
|
13 |
api_key_langchain = st.text_input("Enter your LANGCHAIN_API_KEY", type="password")
|
@@ -17,7 +18,7 @@ api_key_Groq = st.text_input("Enter your Groq_API_KEY", type="password")
|
|
17 |
|
18 |
# Check if both API keys have been provided
|
19 |
if not api_key_langchain or not api_key_Groq:
|
20 |
-
st.write("Please enter both API keys
|
21 |
else:
|
22 |
st.write("Both API keys are set.")
|
23 |
|
@@ -46,11 +47,13 @@ else:
|
|
46 |
embedding_model = SentenceTransformerEmbedding('all-MiniLM-L6-v2')
|
47 |
|
48 |
# Load, chunk, and index the contents of the blog
|
49 |
-
def load_data(
|
50 |
loader = WebBaseLoader(
|
51 |
-
web_paths=(
|
52 |
bs_kwargs=dict(
|
53 |
-
parse_only=bs4.SoupStrainer(
|
|
|
|
|
54 |
),
|
55 |
)
|
56 |
docs = loader.load()
|
@@ -59,34 +62,30 @@ else:
|
|
59 |
vectorstore = Chroma.from_documents(documents=splits, embedding=embedding_model)
|
60 |
return vectorstore
|
61 |
|
62 |
-
|
63 |
-
st.title("URL Retrieval and Question Answering")
|
64 |
-
|
65 |
-
# Input URL from user
|
66 |
-
url = st.text_input("Enter the URL:")
|
67 |
|
68 |
-
|
69 |
-
|
70 |
|
71 |
-
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
|
77 |
-
|
78 |
-
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
8 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
9 |
import bs4
|
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")
|
|
|
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 |
|
|
|
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()
|
|
|
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}")
|