Spaces:
Sleeping
Sleeping
| # chatbot_url_query.py | |
| import os | |
| from langchain_google_genai import ChatGoogleGenerativeAI | |
| from langchain.retrievers import TavilyRetriever | |
| from langchain.chains import RetrievalQA | |
| # 1. Set API keys (Replace with your actual keys) | |
| os.environ["GOOGLE_API_KEY"] = "your-google-api-key" | |
| os.environ["TAVILY_API_KEY"] = "your-tavily-api-key" | |
| # 2. Load Google Gemini model | |
| llm = ChatGoogleGenerativeAI( | |
| model="models/gemini-1.5-flash", | |
| google_api_key=os.environ["GOOGLE_API_KEY"] | |
| ) | |
| # 3. Initialize Tavily Retriever (fetches data from web) | |
| retriever = TavilyRetriever(k=3) | |
| # 4. Create Retrieval QA Chain | |
| qa_chain = RetrievalQA.from_chain_type( | |
| llm=llm, | |
| retriever=retriever, | |
| return_source_documents=True | |
| ) | |
| # 5. Function to run the chatbot | |
| def ask_web_query(url, question): | |
| query = f"Given the content of the website {url}, answer the following: {question}" | |
| response = qa_chain.invoke({"query": query}) | |
| print("\n✅ Answer:") | |
| print(response["result"]) | |
| print("\n📄 Sources:") | |
| for doc in response["source_documents"]: | |
| print("-", doc.metadata.get("source", "Unknown")) | |
| # 6. Run your chatbot | |
| if __name__ == "__main__": | |
| url = input("Enter website URL: ") | |
| question = input("Ask your question about the website: ") | |
| ask_web_query(url, question) | |