-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatBotTest.py
More file actions
73 lines (63 loc) · 2.01 KB
/
chatBotTest.py
File metadata and controls
73 lines (63 loc) · 2.01 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
72
73
import faiss
import cohere
import mariadb
import numpy as np
# FAISS index path
FAISS_INDEX_PATH = "faiss_index.idx"
# MariaDB Connection
mariadb_config = {
"host": "172.31.30.137",
"user": "root",
"password": "H*W7]nD-C(4:#EfsV?MA5G$bQ",
"port": 3306,
"database": "hartnell_scraped_data"
}
# Cohere API Key
COHERE_API_KEY = "ekac8v5OlcEJ1TGGOFC30hzY73zwSqcQPfnv6hJN"
def generate_query_embedding(query):
"""Generate an embedding for the query using Cohere."""
co = cohere.Client(COHERE_API_KEY)
response = co.embed(
texts=[query],
model="embed-english-v3.0",
input_type="search_query"
)
return np.array(response.embeddings, dtype=np.float32)
def search_faiss(query_embedding, top_k=4):
"""Search FAISS index for the most relevant documents."""
index = faiss.read_index(FAISS_INDEX_PATH)
distances, indices = index.search(query_embedding, top_k)
return indices[0]
def fetch_results(indices):
"""Retrieve URLs from MariaDB based on FAISS results."""
try:
conn = mariadb.connect(**mariadb_config)
cursor = conn.cursor()
results = []
for idx in indices:
cursor.execute("SELECT url FROM scraped_data WHERE id = ?", (idx,))
row = cursor.fetchone()
if row:
results.append(row[0])
cursor.close()
conn.close()
return results
except mariadb.Error as e:
print(f"Error fetching results: {e}")
return []
def chatbot():
print("Hello! Ask me a question: ")
while True:
query = input("You: ")
if query.lower() == "exit":
break
query_embedding = generate_query_embedding(query).reshape(1, -1)
indices = search_faiss(query_embedding)
results = fetch_results(indices)
print("\nTop relevant sites to your query:")
for url in results:
print(f"- {url}")
print()
print("Any Other questions?")
if __name__ == "__main__":
chatbot()