Spaces:
Sleeping
Sleeping
File size: 13,572 Bytes
28295d6 |
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
from typing import List, Dict, Optional
from langchain.tools import StructuredTool
import json
# Now import from payments module
from src.payments.agent import process_purchase_request
# Load datasets
with open("data/supplements.json", "r") as f:
SUPPLEMENTS = json.load(f)
with open("data/prescription_drugs.json", "r") as f:
PRESCRIPTION_DRUGS = json.load(f)
# Combine both datasets for unified searching
ALL_PRODUCTS = {**SUPPLEMENTS, **PRESCRIPTION_DRUGS}
# Supplement search and retrieval functions
def search_supplements_by_condition(query: str) -> List[Dict]:
"""
Search for supplements that are recommended for a specific health condition,
well-being goal, or enhancement purpose.
Args:
query: The condition, goal, or purpose to search supplements for
(e.g., 'hair_loss', 'cognitive_function', 'muscle_growth')
Returns:
A list of supplements that match the query
"""
matching_supplements = []
query = query.lower()
for supp_id, supp_data in SUPPLEMENTS.items():
# Check conditions
conditions = [c.lower() for c in supp_data.get("conditions", [])]
# Check benefits/enhancements
benefits = [b.lower() for b in supp_data.get("benefits", [])]
# Check categories
categories = [cat.lower() for cat in supp_data.get("categories", [])]
# Match against all fields
if (query in conditions or
query in benefits or
query in categories or
query in supp_data["name"].lower() or
query in supp_data["description"].lower()):
matching_supplements.append({
"id": supp_id,
"name": supp_data["name"],
"description": supp_data["description"],
"link": supp_data["affiliate_link"],
"conditions": supp_data.get("conditions", []),
"benefits": supp_data.get("benefits", []),
"categories": supp_data.get("categories", []),
"disclaimers": supp_data.get("disclaimers", [])
})
return matching_supplements
def search_supplements_by_name(name: str) -> Optional[Dict]:
"""
Search for a supplement by its name.
Args:
name: The name of the supplement to search for (e.g., 'Vitamin D3', 'Magnesium')
Returns:
Details of the supplement if found, None otherwise
"""
name = name.lower()
for supp_id, supp_data in SUPPLEMENTS.items():
if name in supp_data["name"].lower():
return {
"id": supp_id,
"name": supp_data["name"],
"description": supp_data["description"],
"link": supp_data["affiliate_link"],
"conditions": supp_data.get("conditions", []),
"benefits": supp_data.get("benefits", []),
"categories": supp_data.get("categories", []),
"disclaimers": supp_data.get("disclaimers", [])
}
return None
def get_supplement_details(supplement_id: str) -> Optional[Dict]:
"""
Get detailed information about a specific supplement.
Args:
supplement_id: The ID of the supplement to retrieve
Returns:
Detailed information about the supplement or None if not found
"""
if supplement_id in SUPPLEMENTS:
supp_data = SUPPLEMENTS[supplement_id]
return {
"id": supplement_id,
"name": supp_data["name"],
"description": supp_data["description"],
"link": supp_data["affiliate_link"],
"conditions": supp_data.get("conditions", []),
"disclaimers": supp_data.get("disclaimers", [])
}
return None
def list_all_supplements() -> List[Dict]:
"""
List all available supplements in the database.
Returns:
A list of all supplements with their basic information
"""
return [
{
"id": supp_id,
"name": supp_data["name"],
"description": supp_data["description"],
"conditions": supp_data.get("conditions", [])
}
for supp_id, supp_data in SUPPLEMENTS.items()
]
# Prescription drug search and retrieval functions
def search_prescription_drugs_by_condition(query: str) -> List[Dict]:
"""
Search for prescription drugs that are used for a specific health condition,
well-being goal, or therapeutic purpose.
Args:
query: The condition, goal, or purpose to search drugs for
(e.g., 'hypertension', 'depression', 'hair_loss')
Returns:
A list of prescription drugs that match the query
"""
matching_drugs = []
query = query.lower()
for drug_id, drug_data in PRESCRIPTION_DRUGS.items():
# Check conditions
conditions = [c.lower() for c in drug_data.get("conditions", [])]
# Check benefits
benefits = [b.lower() for b in drug_data.get("benefits", [])]
# Check categories
categories = [cat.lower() for cat in drug_data.get("categories", [])]
# Match against all fields
if (query in conditions or
query in benefits or
query in categories or
query in drug_data["name"].lower() or
query in drug_data["description"].lower()):
matching_drugs.append({
"id": drug_id,
"name": drug_data["name"],
"description": drug_data["description"],
"link": drug_data["affiliate_link"],
"conditions": drug_data.get("conditions", []),
"benefits": drug_data.get("benefits", []),
"categories": drug_data.get("categories", []),
"disclaimers": drug_data.get("disclaimers", []),
"requires_prescription": True
})
return matching_drugs
def search_prescription_drugs_by_name(name: str) -> Optional[Dict]:
"""
Search for a prescription drug by its name.
Args:
name: The name of the drug to search for (e.g., 'Metformin', 'Atorvastatin')
Returns:
Details of the prescription drug if found, None otherwise
"""
name = name.lower()
for drug_id, drug_data in PRESCRIPTION_DRUGS.items():
if name in drug_data["name"].lower():
return {
"id": drug_id,
"name": drug_data["name"],
"description": drug_data["description"],
"link": drug_data["affiliate_link"],
"conditions": drug_data.get("conditions", []),
"benefits": drug_data.get("benefits", []),
"categories": drug_data.get("categories", []),
"disclaimers": drug_data.get("disclaimers", []),
"requires_prescription": True
}
return None
def get_prescription_drug_details(drug_id: str) -> Optional[Dict]:
"""
Get detailed information about a specific prescription drug.
Args:
drug_id: The ID of the prescription drug to retrieve
Returns:
Detailed information about the drug or None if not found
"""
if drug_id in PRESCRIPTION_DRUGS:
drug_data = PRESCRIPTION_DRUGS[drug_id]
return {
"id": drug_id,
"name": drug_data["name"],
"description": drug_data["description"],
"link": drug_data["affiliate_link"],
"conditions": drug_data.get("conditions", []),
"benefits": drug_data.get("benefits", []),
"categories": drug_data.get("categories", []),
"disclaimers": drug_data.get("disclaimers", []),
"requires_prescription": True
}
return None
def list_all_prescription_drugs() -> List[Dict]:
"""
List all available prescription drugs in the database.
Returns:
A list of all prescription drugs with their basic information
"""
return [
{
"id": drug_id,
"name": drug_data["name"],
"description": drug_data["description"],
"conditions": drug_data.get("conditions", []),
"requires_prescription": True
}
for drug_id, drug_data in PRESCRIPTION_DRUGS.items()
]
# Combined search functions
def search_all_products_by_condition(query: str) -> List[Dict]:
"""
Search for both supplements and prescription drugs that are recommended for a specific health condition,
well-being goal, or enhancement purpose.
Args:
query: The condition, goal, or purpose to search for
(e.g., 'hair_loss', 'cognitive_function', 'depression')
Returns:
A list of supplements and prescription drugs that match the query
"""
supplements = search_supplements_by_condition(query)
prescription_drugs = search_prescription_drugs_by_condition(query)
return supplements + prescription_drugs
def search_all_products_by_name(name: str) -> List[Dict]:
"""
Search for both supplements and prescription drugs by name.
Args:
name: The name to search for (e.g., 'Vitamin D3', 'Metformin')
Returns:
List of products matching the name query
"""
results = []
supplement = search_supplements_by_name(name)
if supplement:
results.append(supplement)
prescription = search_prescription_drugs_by_name(name)
if prescription:
results.append(prescription)
return results
def get_product_details(product_id: str) -> Optional[Dict]:
"""
Get detailed information about a specific product (supplement or prescription drug).
Args:
product_id: The ID of the product to retrieve
Returns:
Detailed information about the product or None if not found
"""
supplement = get_supplement_details(product_id)
if supplement:
return supplement
prescription = get_prescription_drug_details(product_id)
if prescription:
return prescription
return None
def list_all_products() -> List[Dict]:
"""
List all available products (supplements and prescription drugs) in the database.
Returns:
A list of all products with their basic information
"""
supplements = list_all_supplements()
prescription_drugs = list_all_prescription_drugs()
return supplements + prescription_drugs
def create_payment_link(request: str) -> str:
"""
Create a payment link for products and consultations based on a natural language request.
Args:
request: A string describing what products and quantities to purchase
(e.g., "I want to buy 1 tretinoin and 1 consultation")
Returns:
The payment link URL
"""
# Process the request and get payment link
response = process_purchase_request(request)
#response['messages'][0].content
return response
# Create and export Langchain tools
def get_tools():
"""Return all the tools needed for the agent"""
return [
StructuredTool.from_function(search_supplements_by_condition),
StructuredTool.from_function(search_prescription_drugs_by_condition),
StructuredTool.from_function(search_all_products_by_condition),
StructuredTool.from_function(search_supplements_by_name),
StructuredTool.from_function(search_prescription_drugs_by_name),
StructuredTool.from_function(search_all_products_by_name),
StructuredTool.from_function(get_supplement_details),
StructuredTool.from_function(get_prescription_drug_details),
StructuredTool.from_function(get_product_details),
StructuredTool.from_function(list_all_supplements),
StructuredTool.from_function(list_all_prescription_drugs),
StructuredTool.from_function(list_all_products),
StructuredTool.from_function(create_payment_link)
]
# Export data for use in main.py
def get_supplements():
return SUPPLEMENTS
def get_prescription_drugs():
return PRESCRIPTION_DRUGS
def get_available_products() -> Dict:
"""
Get a list of all available products in the catalog.
Returns:
A dictionary containing available products categorized by type
"""
# Load the products catalog
with open("src/payments/products.json", "r") as f:
products_catalog = json.load(f)
# Separate into supplements, prescription drugs, and services
supplements = []
prescription_drugs = []
services = []
for product_id in products_catalog.keys():
if product_id == "NP_consultation":
services.append(product_id) # Add consultation as a service
elif product_id in PRESCRIPTION_DRUGS:
prescription_drugs.append(product_id)
elif product_id in SUPPLEMENTS:
supplements.append(product_id)
return {
"supplements": supplements,
"prescription_drugs": prescription_drugs,
"services": services,
"all_products": list(products_catalog.keys())
}
if __name__ == "__main__":
sample_request = '1 Finasteride consultation, 1 Minoxidil consultation'
result = process_purchase_request(sample_request)
print(result)
breakpoint()
asd = 1
# You can test with other examples
# result2 = process_purchase_request("I want to buy 3 items of product_i and 2 of product_k")
# print(result2)
|