Skip to content

Notion

Notion tool spec.

NotionToolSpec #

Bases: BaseToolSpec

Notion tool spec.

Currently a simple wrapper around the data loader. TODO: add more methods to the Notion spec.

Source code in llama-index-integrations/tools/llama-index-tools-notion/llama_index/tools/notion/base.py
 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
class NotionToolSpec(BaseToolSpec):
    """Notion tool spec.

    Currently a simple wrapper around the data loader.
    TODO: add more methods to the Notion spec.

    """

    spec_functions = ["load_data", "search_data"]

    def __init__(self, integration_token: Optional[str] = None) -> None:
        """Initialize with parameters."""
        self.reader = NotionPageReader(integration_token=integration_token)

    def get_fn_schema_from_fn_name(
        self, fn_name: str, spec_functions: Optional[List[SPEC_FUNCTION_TYPE]] = None
    ) -> Optional[Type[BaseModel]]:
        """Return map from function name."""
        if fn_name == "load_data":
            return NotionLoadDataSchema
        elif fn_name == "search_data":
            return NotionSearchDataSchema
        else:
            raise ValueError(f"Invalid function name: {fn_name}")

    def load_data(
        self, page_ids: Optional[List[str]] = None, database_id: Optional[str] = None
    ) -> str:
        """Loads content from a set of page ids or a database id.

        Don't use this endpoint if you don't know the page ids or database id.

        """
        page_ids = page_ids or []
        docs = self.reader.load_data(page_ids=page_ids, database_id=database_id)
        return "\n".join([doc.get_content() for doc in docs])

    def search_data(
        self,
        query: str,
        direction: Optional[str] = None,
        timestamp: Optional[str] = None,
        value: Optional[str] = None,
        property: Optional[str] = None,
        page_size: int = 100,
    ) -> str:
        """Search a list of relevant pages.

        Contains metadata for each page (but not the page content).

        """
        payload: Dict[str, Any] = {
            "query": query,
            "page_size": page_size,
        }
        if direction is not None or timestamp is not None:
            payload["sort"] = {}
            if direction is not None:
                payload["sort"]["direction"] = direction
            if timestamp is not None:
                payload["sort"]["timestamp"] = timestamp

        if value is not None or property is not None:
            payload["filter"] = {}
            if value is not None:
                payload["filter"]["value"] = value
            if property is not None:
                payload["filter"]["property"] = property

        response = requests.post(SEARCH_URL, json=payload, headers=self.reader.headers)
        response_json = response.json()
        return response_json["results"]

get_fn_schema_from_fn_name #

get_fn_schema_from_fn_name(fn_name: str, spec_functions: Optional[List[SPEC_FUNCTION_TYPE]] = None) -> Optional[Type[BaseModel]]

Return map from function name.

Source code in llama-index-integrations/tools/llama-index-tools-notion/llama_index/tools/notion/base.py
45
46
47
48
49
50
51
52
53
54
def get_fn_schema_from_fn_name(
    self, fn_name: str, spec_functions: Optional[List[SPEC_FUNCTION_TYPE]] = None
) -> Optional[Type[BaseModel]]:
    """Return map from function name."""
    if fn_name == "load_data":
        return NotionLoadDataSchema
    elif fn_name == "search_data":
        return NotionSearchDataSchema
    else:
        raise ValueError(f"Invalid function name: {fn_name}")

load_data #

load_data(page_ids: Optional[List[str]] = None, database_id: Optional[str] = None) -> str

Loads content from a set of page ids or a database id.

Don't use this endpoint if you don't know the page ids or database id.

Source code in llama-index-integrations/tools/llama-index-tools-notion/llama_index/tools/notion/base.py
56
57
58
59
60
61
62
63
64
65
66
def load_data(
    self, page_ids: Optional[List[str]] = None, database_id: Optional[str] = None
) -> str:
    """Loads content from a set of page ids or a database id.

    Don't use this endpoint if you don't know the page ids or database id.

    """
    page_ids = page_ids or []
    docs = self.reader.load_data(page_ids=page_ids, database_id=database_id)
    return "\n".join([doc.get_content() for doc in docs])

search_data #

search_data(query: str, direction: Optional[str] = None, timestamp: Optional[str] = None, value: Optional[str] = None, property: Optional[str] = None, page_size: int = 100) -> str

Search a list of relevant pages.

Contains metadata for each page (but not the page content).

Source code in llama-index-integrations/tools/llama-index-tools-notion/llama_index/tools/notion/base.py
 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
def search_data(
    self,
    query: str,
    direction: Optional[str] = None,
    timestamp: Optional[str] = None,
    value: Optional[str] = None,
    property: Optional[str] = None,
    page_size: int = 100,
) -> str:
    """Search a list of relevant pages.

    Contains metadata for each page (but not the page content).

    """
    payload: Dict[str, Any] = {
        "query": query,
        "page_size": page_size,
    }
    if direction is not None or timestamp is not None:
        payload["sort"] = {}
        if direction is not None:
            payload["sort"]["direction"] = direction
        if timestamp is not None:
            payload["sort"]["timestamp"] = timestamp

    if value is not None or property is not None:
        payload["filter"] = {}
        if value is not None:
            payload["filter"]["value"] = value
        if property is not None:
            payload["filter"]["property"] = property

    response = requests.post(SEARCH_URL, json=payload, headers=self.reader.headers)
    response_json = response.json()
    return response_json["results"]