File size: 1,170 Bytes
1971b24
ad5d74e
51f2c5d
1971b24
 
 
 
 
 
 
ad5d74e
1971b24
 
ad5d74e
 
 
1971b24
 
ad5d74e
1971b24
 
 
ad5d74e
1971b24
 
 
 
 
ad5d74e
 
 
 
 
 
 
1971b24
 
ad5d74e
1971b24
ad5d74e
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
from agno.tools import tool
from .product_search import AmazonProductSearch, FilterModel, ProductItem
from .config import settings

from typing import Optional
import json
from dotenv import load_dotenv

load_dotenv()


@tool
def fetch_products(
    query: str,
    limit: int = settings.SEARCH_LIMIT,
    filters: Optional[FilterModel] = None,
) -> list[dict]:
    """
    Fetch products from Amazon based on query and filters

    Args:
        query (str): Search query
        limit (int, optional): Number of products to return. Must be between 2-10. Defaults to 6.
        filters (FilterModel, optional): Filters to apply. Defaults to None.

    Returns:
        list[dict]: List of products dictionary items matching the query and filters
    """
    limit = max(2, min(limit, 10))  # Ensure limit is between 2 and 10

    product_searcher = AmazonProductSearch(
        rapid_api_key=settings.RAPIDAPI_KEY, rerank_model=settings.RERANKING_MODEL
    )

    products: list[ProductItem] = product_searcher.fetch_products(
        query=query,
        filters=filters,
        limit=limit
    )
    return json.dumps([product.model_dump() for product in products])