File size: 1,459 Bytes
3b7f713
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from tavily import TavilyClient
import os
from typing import List, Dict


def grant_search_tool(query: str) -> List[Dict]:
    """
    Rzeczywiste wyszukiwanie naborów w internecie przy użyciu Tavily.
    """
    api_key = os.environ.get("TAVILY_API_KEY")
    if not api_key:
        return [
            {
                "title": "Brak klucza Tavily",
                "description": "Dodaj TAVILY_API_KEY",
                "url": "",
            }
        ]

    try:
        client = TavilyClient(api_key=api_key)
        response = client.search(
            query=query + " dotacje unijne PARP NCBR",
            search_depth="advanced",
            max_results=10,
        )

        structured_results = []
        for r in response.get("results", []):
            structured_results.append(
                {
                    "title": r.get("title", "Znaleziony dokument programowy"),
                    "description": r.get("content", ""),
                    "url": r.get("url", ""),
                }
            )

        return (
            structured_results
            if structured_results
            else [
                {
                    "title": "Brak precyzyjnych wyników",
                    "description": "Rozszerz zapytanie",
                    "url": "",
                }
            ]
        )
    except Exception as e:
        return [{"title": "Błąd wyszukiwania Tavily", "description": str(e), "url": ""}]