-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeb_to_db.py
More file actions
71 lines (41 loc) · 1.98 KB
/
Web_to_db.py
File metadata and controls
71 lines (41 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from KeyLoader import load_genai_key
from FetchWeb import fetch_website_content
from TextProcessor import *
from langchain.prompts import ChatPromptTemplate
import google.generativeai as genai
PROMPT_TEMPLATE = """
Answer the question based on the following context and common sense only, context:
{context}
---
Answer the question based on the above context in detail in format of an FAQ answer only in a way that it can be directly displayed on website also simply return "error 101" if the context has critical info missing : {question}
"""
url = "https://www.brooklinen.com/products/down-pillow?variant=8364502979&galopt=true"
genai_api_key = load_genai_key()
print(f"Fetching content from {url}...")
website_text = fetch_website_content(url)
cleaned_text = clean_text(website_text)
chunks = chunk_text(cleaned_text)
document_embeddings = compute_embeddings(chunks)
model_name = "sentence-transformers/all-mpnet-base-v2"
embedding_function = HuggingFaceEmbeddings(model_name=model_name)
print("Enter your questions (type 'exit' to stop):")
while True:
query_text = input("Question: ")
if query_text.lower() == 'exit':
break
query_embedding = embedding_function.embed_query(query_text)
results = similarity_search(query_embedding, document_embeddings, chunks, k=3)
context_texts = []
for doc, score in results:
temp = doc.page_content + "\n" + doc.metadata.get("source", "Unknown Source")
context_texts.append(temp)
context_text = "\n\n---\n\n".join(context_texts)
prompt_template = ChatPromptTemplate.from_template(PROMPT_TEMPLATE)
prompt = prompt_template.format(context=context_text, question=query_text)
# print("Generated Prompt:")
# print(prompt)
genai.configure(api_key=genai_api_key)
model = genai.GenerativeModel("gemini-1.5-flash")
response = model.generate_content(prompt)
print("AI Response:")
print(response.text)