Spaces:
Sleeping
Sleeping
Update rag_agent.py
Browse files- rag_agent.py +22 -22
rag_agent.py
CHANGED
|
@@ -19,29 +19,31 @@ load_dotenv()
|
|
| 19 |
|
| 20 |
# Define paths
|
| 21 |
BNI_CSV_FILE = "bni_pearl_chapter.csv"
|
| 22 |
-
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
if not os.path.exists(BNI_DATA_PATH):
|
| 29 |
-
raise FileNotFoundError(f"❌ Error: The BNI data file {BNI_DATA_PATH} is missing.")
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# df = pd.read_csv(BNI_CSV_FILE)
|
| 34 |
-
# json_data = df.to_dict(orient="records")
|
| 35 |
-
|
| 36 |
-
# with open(BNI_JSON_FILE, "w") as file:
|
| 37 |
-
# json.dump(json_data, file, indent=4)
|
| 38 |
|
| 39 |
-
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# convert_csv_to_json()
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# 2️⃣ **Create a Vector Database for RAG**
|
| 47 |
vector_db = LanceDb(
|
|
@@ -53,8 +55,8 @@ vector_db = LanceDb(
|
|
| 53 |
|
| 54 |
# 3️⃣ **Create Knowledge Base from JSON**
|
| 55 |
knowledge_base = JSONKnowledgeBase(
|
| 56 |
-
file_path=
|
| 57 |
-
vector_db=vector_db, # Use vector DB for
|
| 58 |
)
|
| 59 |
|
| 60 |
# **Load Knowledge Base** (Run this once)
|
|
@@ -75,14 +77,12 @@ def recommend_bni_connections(company_data: str) -> str:
|
|
| 75 |
|
| 76 |
Args:
|
| 77 |
company_data (str): The user's company information.
|
| 78 |
-
|
| 79 |
Returns:
|
| 80 |
str: A list of relevant BNI members and how they can help.
|
| 81 |
"""
|
| 82 |
query = f"""
|
| 83 |
Given the following business details:
|
| 84 |
- **Company Information:** {company_data}
|
| 85 |
-
|
| 86 |
Search the BNI Pearl Chapter database and recommend **3-5 members** who can
|
| 87 |
help this business **through referrals, networking, or industry connections.**
|
| 88 |
Provide:
|
|
@@ -91,4 +91,4 @@ def recommend_bni_connections(company_data: str) -> str:
|
|
| 91 |
3. **How They Can Help**
|
| 92 |
"""
|
| 93 |
response = rag_agent.run(query)
|
| 94 |
-
return response.content # Return recommended BNI connections
|
|
|
|
| 19 |
|
| 20 |
# Define paths
|
| 21 |
BNI_CSV_FILE = "bni_pearl_chapter.csv"
|
| 22 |
+
BNI_JSON_FILE = "bni_pearl_chapter.json"
|
| 23 |
|
| 24 |
+
# 1️⃣ **Ensure JSON file exists by converting CSV to JSON**
|
| 25 |
+
def convert_csv_to_json():
|
| 26 |
+
"""
|
| 27 |
+
Converts the BNI CSV file into a structured JSON format.
|
| 28 |
+
"""
|
| 29 |
+
if not os.path.exists(BNI_CSV_FILE):
|
| 30 |
+
raise FileNotFoundError(f"❌ Error: The BNI CSV file {BNI_CSV_FILE} is missing.")
|
| 31 |
|
| 32 |
+
df = pd.read_csv(BNI_CSV_FILE)
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
if df.empty:
|
| 35 |
+
raise ValueError(f"❌ Error: {BNI_CSV_FILE} is empty!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
json_data = df.to_dict(orient="records")
|
| 38 |
|
| 39 |
+
with open(BNI_JSON_FILE, "w") as file:
|
| 40 |
+
json.dump(json_data, file, indent=4)
|
|
|
|
| 41 |
|
| 42 |
+
print(f"✅ Converted {BNI_CSV_FILE} to JSON format.")
|
| 43 |
+
|
| 44 |
+
# Run the conversion **only if JSON does not exist**
|
| 45 |
+
if not os.path.exists(BNI_JSON_FILE):
|
| 46 |
+
convert_csv_to_json()
|
| 47 |
|
| 48 |
# 2️⃣ **Create a Vector Database for RAG**
|
| 49 |
vector_db = LanceDb(
|
|
|
|
| 55 |
|
| 56 |
# 3️⃣ **Create Knowledge Base from JSON**
|
| 57 |
knowledge_base = JSONKnowledgeBase(
|
| 58 |
+
file_path=BNI_JSON_FILE, # Ensure the correct JSON file is used
|
| 59 |
+
vector_db=vector_db, # Use vector DB for efficient search
|
| 60 |
)
|
| 61 |
|
| 62 |
# **Load Knowledge Base** (Run this once)
|
|
|
|
| 77 |
|
| 78 |
Args:
|
| 79 |
company_data (str): The user's company information.
|
|
|
|
| 80 |
Returns:
|
| 81 |
str: A list of relevant BNI members and how they can help.
|
| 82 |
"""
|
| 83 |
query = f"""
|
| 84 |
Given the following business details:
|
| 85 |
- **Company Information:** {company_data}
|
|
|
|
| 86 |
Search the BNI Pearl Chapter database and recommend **3-5 members** who can
|
| 87 |
help this business **through referrals, networking, or industry connections.**
|
| 88 |
Provide:
|
|
|
|
| 91 |
3. **How They Can Help**
|
| 92 |
"""
|
| 93 |
response = rag_agent.run(query)
|
| 94 |
+
return response.content # Return recommended BNI connections
|