diff --git a/examples/confluence-data-retrieval-example/10_fetch_confluence_document.py b/examples/confluence-data-retrieval-example/10_fetch_confluence_document.py new file mode 100644 index 0000000000..e2047141ca --- /dev/null +++ b/examples/confluence-data-retrieval-example/10_fetch_confluence_document.py @@ -0,0 +1,37 @@ +# Copyright 2021-2024 VMware, Inc. +# SPDX-License-Identifier: Apache-2.0 +import logging +import os + +from langchain_community.document_loaders import ConfluenceLoader +from vdk.api.job_input import IJobInput + +log = logging.getLogger(__name__) + + +def fetch_confluence_document(url, token, document_id): + try: + # For more info regarding the LangChain ConfluenceLoader: + # https://python.langchain.com/docs/integrations/document_loaders/confluence + loader = ConfluenceLoader(url=url, token=token) + documents = loader.load(page_ids=[document_id]) + return documents[0] if documents else None + except Exception as e: + log.error(f"Error fetching document with ID {document_id} from Confluence: {e}") + return None + + +def run(job_input: IJobInput): + log.info(f"Starting job step {__name__}") + + confluence_url = os.environ.get("VDK_CONFLUENCE_URL") + token = os.environ.get("VDK_CONFLUENCE_TOKEN") + doc_id = os.environ.get("VDK_CONFLUENCE_DOC_ID") + + doc = fetch_confluence_document(confluence_url, token, doc_id) + + if doc: + log.info(f"Document with ID {doc_id} fetched successfully.") + print(doc.page_content) + else: + log.error(f"Failed to fetch the document with ID {doc_id}.") diff --git a/examples/confluence-data-retrieval-example/20_fetch_confluence_space.py b/examples/confluence-data-retrieval-example/20_fetch_confluence_space.py new file mode 100644 index 0000000000..cdd1a58e95 --- /dev/null +++ b/examples/confluence-data-retrieval-example/20_fetch_confluence_space.py @@ -0,0 +1,41 @@ +# Copyright 2021-2024 VMware, Inc. +# SPDX-License-Identifier: Apache-2.0 +import logging +import os + +from langchain_community.document_loaders import ConfluenceLoader +from vdk.api.job_input import IJobInput + +log = logging.getLogger(__name__) + + +def fetch_confluence_space(url, token, space_key): + try: + # For more info regarding the LangChain ConfluenceLoader: + # https://python.langchain.com/docs/integrations/document_loaders/confluence + loader = ConfluenceLoader(url=url, token=token) + documents = loader.load( + space_key=space_key, include_attachments=True, limit=20, max_pages=20 + ) + return documents + except Exception as e: + log.error(f"Error fetching documents from Confluence: {e}") + return None + + +def run(job_input: IJobInput): + log.info(f"Starting job step {__name__}") + + confluence_url = os.environ.get("VDK_CONFLUENCE_URL") + token = os.environ.get("VDK_CONFLUENCE_TOKEN") + space_key = os.environ.get("VDK_CONFLUENCE_SPACE_KEY") + + docs = fetch_confluence_space(confluence_url, token, space_key) + + if docs: + log.info(f"{len(docs)} documents fetched successfully.") + log.info("Printing the first 50 chars of each doc") + for i, doc in enumerate(docs): + print(f"{i}: {doc.page_content[:50]}") + else: + log.error(f"Failed to fetch any documents from the space with key {space_key}.") diff --git a/examples/confluence-data-retrieval-example/requirements.txt b/examples/confluence-data-retrieval-example/requirements.txt new file mode 100644 index 0000000000..8ef381f7a6 --- /dev/null +++ b/examples/confluence-data-retrieval-example/requirements.txt @@ -0,0 +1,7 @@ +# Python jobs can specify extra library dependencies in requirements.txt file. +# See https://pip.readthedocs.io/en/stable/user_guide/#requirements-files +# The file is optional and can be deleted if no extra library dependencies are necessary. + +atlassian-python-api +langchain_community +lxml