-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagentic_rag.py
More file actions
198 lines (161 loc) · 6.31 KB
/
agentic_rag.py
File metadata and controls
198 lines (161 loc) · 6.31 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import os
from datetime import datetime
import requests
from dotenv import load_dotenv
from client import get_opensearch_client
from crew import run_patent_analysis
from patent_search_tools import hybrid_search, iterative_search, semantic_search, keyword_search
def display_menu():
"""Display the main menu options"""
print("\n" + "=" * 60)
print(" PATENT INNOVATION PREDICTOR - AI for cancer detection in medical imaging")
print("=" * 60)
print("1. Run complete patent trend analysis and forecasting")
print("2. Search for specific patents")
print("3. Iterative patent exploration")
print("4. View system status")
print("5. Exit")
print("-" * 60)
return input("Select an option (1-5): ")
def run_complete_analysis():
"""Run the complete patent trend analysis using CrewAI agents"""
print("\nRunning comprehensive patent analysis...")
print("This may take several minutes depending on the data volume.")
research_area = input("Enter research area (default: AI for cancer detection in medical imaging): ")
if not research_area:
research_area = "AI for cancer detection in medical imaging"
model_name = input("Enter the Ollama model to use (default: llama2): ")
if not model_name:
model_name = "llama2"
print(f"\nAnalyzing patents for: {research_area}")
print(f"Using Ollama model: {model_name}")
print("Agents are now processing the data...\n")
try:
result = run_patent_analysis(research_area, model_name)
if not isinstance(result, str):
result = str(result)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"patent_analysis_{timestamp}.txt"
with open(filename, "w") as f:
f.write(result)
print(f"\nAnalysis completed and saved to {filename}")
print("\n" + "=" * 60)
print("ANALYSIS SUMMARY")
print("-" * 60)
print(result[:500] + "...\n")
except Exception as e:
print(f"Error during analysis: {e}")
def search_patents():
"""Search for specific patents in the database"""
print("\nPATENT SEARCH")
print("-" * 60)
query = input("Enter search query: ")
if not query:
print("Search query cannot be empty.")
return
search_type = input("Select search type (1: Keyword, 2: Semantic, 3: Hybrid) [3]: ")
if not search_type:
search_type = "3"
try:
results = []
if search_type == "1":
results = keyword_search(query)
elif search_type == "2":
results = semantic_search(query)
else:
results = hybrid_search(query)
# Display results
print(f"\nFound {len(results)} results for '{query}':")
print("-" * 60)
for i, hit in enumerate(results):
source = hit["_source"]
print(f"{i+1}. {source['title']}")
print(f" Score: {hit['_score']}")
print(f" Date: {source.get('publication_date', 'N/A')}")
print(f" Patent ID: {source.get('patent_id', 'N/A')}")
print(f" Abstract: {source['abstract'][:150]}...")
print("-" * 60)
except Exception as e:
print(f"Search error: {e}")
def iterative_exploration():
"""Perform iterative exploration of patents"""
print("\nITERATIVE PATENT EXPLORATION")
print("-" * 60)
query = input("Enter initial exploration query: ")
if not query:
print("Query cannot be empty.")
return
steps = input("Number of exploration steps (default: 3): ")
try:
steps = int(steps) if steps else 3
except:
steps = 3
print(f"\nExploring patents related to '{query}' with {steps} refinement steps...")
try:
results = iterative_search(query, refinement_steps=steps)
print(f"\nFound {len(results)} results through iterative exploration:")
print("-" * 60)
for i, hit in enumerate(results):
source = hit["_source"]
print(f"{i+1}. {source['title']}")
print(f" Date: {source.get('publication_date', 'N/A')}")
print(f" Patent ID: {source.get('patent_id', 'N/A')}")
print(f" Abstract: {source['abstract'][:150]}...")
print("-" * 60)
except Exception as e:
print(f"Exploration error: {e}")
def check_system_status():
"""Check the status of system components"""
print("\nSYSTEM STATUS")
print("-" * 60)
try:
client = get_opensearch_client("localhost", 9200)
indices = client.cat.indices(format="json")
print("✅ OpenSearch connection: OK")
print(f" Found {len(indices)} indices:")
for index in indices:
print(f" - {index['index']}: {index['docs.count']} documents")
except Exception as e:
print(f"❌ OpenSearch connection: Failed - {e}")
# Check Ollama API availability
try:
response = requests.get("http://localhost:11434/api/tags")
if response.status_code == 200:
models = response.json().get("models", [])
print("✅ Ollama connection: OK")
print(
f" Available models: {', '.join([m.get('name', 'unknown') for m in models])}"
)
else:
print(f"❌ Ollama connection: Failed (Status code: {response.status_code})")
except Exception as e:
print(f"❌ Ollama connection: Failed - {e}")
# Check embedding model
try:
from embedding import get_embedding
sample = get_embedding("test")
print(f"✅ Embedding model: OK (dimension: {len(sample)})")
except Exception as e:
print(f"❌ Embedding model: Failed - {e}")
print("\nSystem is ready for operation.")
def main():
"""Main application entry point"""
load_dotenv()
while True:
choice = display_menu()
if choice == "1":
run_complete_analysis()
elif choice == "2":
search_patents()
elif choice == "3":
iterative_exploration()
elif choice == "4":
check_system_status()
elif choice == "5":
print("\nExiting Patent Innovation Predictor. Goodbye!")
break
else:
print("\nInvalid option. Please select a number between 1 and 5.")
input("\nPress Enter to continue...")
if __name__ == "__main__":
main()