ytrsoymr commited on
Commit
bf2dd99
·
verified ·
1 Parent(s): 062717b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -28
app.py CHANGED
@@ -1,44 +1,66 @@
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)
 
1
+ # app.py
2
 
3
  import os
4
+ from dotenv import load_dotenv
5
+ from tavily import TavilyClient
6
  from langchain_google_genai import ChatGoogleGenerativeAI
7
+ from langchain.chains import LLMChain
8
+ from langchain.prompts import PromptTemplate
9
 
10
+ # 1. Load environment variables from .env file (if present)
11
+ load_dotenv()
 
12
 
13
+ # 2. Set API keys from environment
14
+ GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
15
+ TAVILY_API_KEY = os.environ.get("TAVILY_API_KEY")
16
+
17
+ # 3. Initialize Gemini LLM
18
  llm = ChatGoogleGenerativeAI(
19
  model="models/gemini-1.5-flash",
20
+ google_api_key=GOOGLE_API_KEY
21
  )
22
 
23
+ # 4. Tavily Client
24
+ tavily_client = TavilyClient(api_key=TAVILY_API_KEY)
25
+
26
+ def extract_website_text(url):
27
+ result = tavily_client.extract(urls=url)
28
+ if result and "text" in result[0]:
29
+ return result[0]["text"]
30
+ return "Could not extract content from the URL."
31
+
32
+ # 5. Prompt Template
33
+ prompt = PromptTemplate(
34
+ input_variables=["website_content", "question"],
35
+ template="""
36
+ You are an intelligent assistant. Based on the following website content:
37
+
38
+ {website_content}
39
 
40
+ Answer the following question:
41
+ {question}
42
+ """
 
 
43
  )
44
 
45
+ # 6. LLM QA Chain
46
+ qa_chain = LLMChain(llm=llm, prompt=prompt)
47
+
48
+ # 7. Main Chat Function
49
+ def ask_from_website(url, question):
50
+ print(f"\n🔗 Extracting content from: {url}")
51
+ website_text = extract_website_text(url)
52
 
53
+ print(f"\n💬 Asking: {question}")
54
+ response = qa_chain.invoke({
55
+ "website_content": website_text,
56
+ "question": question
57
+ })
58
 
59
+ print("\n Answer:")
60
+ print(response["text"])
 
61
 
62
+ # 8. Run
63
  if __name__ == "__main__":
64
+ url = input("Enter the website URL: ")
65
+ question = input("What do you want to ask about this website? ")
66
+ ask_from_website(url, question)