GiantAnalytics commited on
Commit
4ec2c90
·
verified ·
1 Parent(s): 2b9ab72

Create rag_agent.py

Browse files
Files changed (1) hide show
  1. rag_agent.py +86 -0
rag_agent.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import pandas as pd
4
+ import faiss
5
+ import numpy as np
6
+ from phi.agent import Agent
7
+ from phi.model.openai import OpenAIChat
8
+ from phi.embedder.openai import OpenAIEmbedder
9
+ from phi.vectordb.lancedb import LanceDb, SearchType
10
+ from phi.knowledge.json import JSONKnowledgeBase
11
+
12
+ # Load environment variables (API keys, etc.)
13
+ from dotenv import load_dotenv
14
+ load_dotenv()
15
+
16
+ #####################################################################################
17
+ # BNI RAG Agent (Auto-RAG) #
18
+ #####################################################################################
19
+
20
+ # Define paths
21
+ BNI_CSV_FILE = "bni_pearl_chapter.csv"
22
+ BNI_JSON_FILE = "bni_pearl_chapter.json"
23
+
24
+ # 1️⃣ **Convert CSV to JSON for structured retrieval**
25
+ def convert_csv_to_json():
26
+ df = pd.read_csv(BNI_CSV_FILE)
27
+ json_data = df.to_dict(orient="records")
28
+
29
+ with open(BNI_JSON_FILE, "w") as file:
30
+ json.dump(json_data, file, indent=4)
31
+
32
+ print(f"✅ Converted {BNI_CSV_FILE} to JSON format.")
33
+
34
+ # Run the conversion **only if JSON does not exist**
35
+ if not os.path.exists(BNI_JSON_FILE):
36
+ convert_csv_to_json()
37
+
38
+ # 2️⃣ **Create a Vector Database for RAG**
39
+ vector_db = LanceDb(
40
+ table_name="bni_pearl",
41
+ uri="tmp/lancedb",
42
+ search_type=SearchType.vector,
43
+ embedder=OpenAIEmbedder(model="text-embedding-3-small"),
44
+ )
45
+
46
+ # 3️⃣ **Create Knowledge Base from JSON**
47
+ knowledge_base = JSONKnowledgeBase(
48
+ file_path=BNI_JSON_FILE,
49
+ vector_db=vector_db, # Use vector DB for faster search
50
+ )
51
+
52
+ # **Load Knowledge Base** (Run this once)
53
+ knowledge_base.load()
54
+
55
+ # 4️⃣ **Define the RAG Agent**
56
+ rag_agent = Agent(
57
+ model=OpenAIChat(id="gpt-4o"),
58
+ knowledge=knowledge_base, # Attach the Knowledge Base
59
+ show_tool_calls=True,
60
+ markdown=True,
61
+ )
62
+
63
+ # 5️⃣ **Define Function to Recommend BNI Members**
64
+ def recommend_bni_connections(company_data: str) -> str:
65
+ """
66
+ Retrieves BNI Pearl Chapter members who can help based on the user's company details.
67
+
68
+ Args:
69
+ company_data (str): The user's company information.
70
+
71
+ Returns:
72
+ str: A list of relevant BNI members and how they can help.
73
+ """
74
+ query = f"""
75
+ Given the following business details:
76
+ - **Company Information:** {company_data}
77
+
78
+ Search the BNI Pearl Chapter database and recommend **3-5 members** who can
79
+ help this business **through referrals, networking, or industry connections.**
80
+ Provide:
81
+ 1. **Member Name**
82
+ 2. **Company & Industry**
83
+ 3. **How They Can Help**
84
+ """
85
+ response = rag_agent.run(query)
86
+ return response.content # Return recommended BNI connections