Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# chatbot_url_query.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 5 |
+
from langchain.retrievers import TavilyRetriever
|
| 6 |
+
from langchain.chains import RetrievalQA
|
| 7 |
+
|
| 8 |
+
# 1. Set API keys (Replace with your actual keys)
|
| 9 |
+
os.environ["GOOGLE_API_KEY"] = "your-google-api-key"
|
| 10 |
+
os.environ["TAVILY_API_KEY"] = "your-tavily-api-key"
|
| 11 |
+
|
| 12 |
+
# 2. Load Google Gemini model
|
| 13 |
+
llm = ChatGoogleGenerativeAI(
|
| 14 |
+
model="models/gemini-1.5-flash",
|
| 15 |
+
google_api_key=os.environ["GOOGLE_API_KEY"]
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# 3. Initialize Tavily Retriever (fetches data from web)
|
| 19 |
+
retriever = TavilyRetriever(k=3)
|
| 20 |
+
|
| 21 |
+
# 4. Create Retrieval QA Chain
|
| 22 |
+
qa_chain = RetrievalQA.from_chain_type(
|
| 23 |
+
llm=llm,
|
| 24 |
+
retriever=retriever,
|
| 25 |
+
return_source_documents=True
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# 5. Function to run the chatbot
|
| 29 |
+
def ask_web_query(url, question):
|
| 30 |
+
query = f"Given the content of the website {url}, answer the following: {question}"
|
| 31 |
+
response = qa_chain.invoke({"query": query})
|
| 32 |
+
|
| 33 |
+
print("\n✅ Answer:")
|
| 34 |
+
print(response["result"])
|
| 35 |
+
|
| 36 |
+
print("\n📄 Sources:")
|
| 37 |
+
for doc in response["source_documents"]:
|
| 38 |
+
print("-", doc.metadata.get("source", "Unknown"))
|
| 39 |
+
|
| 40 |
+
# 6. Run your chatbot
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
url = input("Enter website URL: ")
|
| 43 |
+
question = input("Ask your question about the website: ")
|
| 44 |
+
ask_web_query(url, question)
|