{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Document summary\n", "\n", "Document summary is an AI-powered assistant that helps you create a summary for one or a maximum of 100 documents in .pdf format. You can upload PDF files to Cognite Data Fusion (CDF) and use the Document summary API to create a document summary." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "trusted": true }, "outputs": [], "source": [ "from cognite.client import CogniteClient\n", "\n", "# Instantiate Cognite SDK client:\n", "client = CogniteClient()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 1. Upload PDF\n", "\n", "You can upload a PDF file to CDF one of the following ways:\n", "\n", "* Go to **_CDF_** > **_Industrial tools_** > **_Canvas_** and drag your PDF file to the canvas or upload existing files by selecting **_+ Add data_**. \n", " If you don't have a good file to upload, try this [test file](./well_report.pdf).\n", "\n", "* Go to **_CDF_** > **_Industrial tools_** > **_Data explorer_** > **_Files_** and select **_Upload_**.\n", "\n", "* Use the Python code." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "trusted": true }, "outputs": [], "source": [ "response1 = client.files.upload(path=\"./well_report.pdf\")\n", "document_id = response1.id\n", "print(document_id)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 2. Processing\n", "\n", "Once you've uploaded the file, wait for it to pass through the RAG pipeline. You can use the Document status API to poll the status." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "trusted": true }, "outputs": [], "source": [ "import time\n", "\n", "status_path = f\"/api/v1/projects/{client.config.project}/documents/status\"\n", "\n", "body = {\n", " \"items\": [\n", " {\n", " \"id\": document_id\n", " }\n", " ]\n", "}\n", "\n", "while True:\n", " response2 = client.post(status_path, json=body, headers={\"cdf-version\": \"alpha\"}).json()\n", "\n", " status = response2[\"items\"][0][\"semanticsearch\"][\"status\"]\n", " print(f\"status: {status}\")\n", "\n", " if status in {\"waiting\", \"progress\"}:\n", " time.sleep(5)\n", " continue\n", "\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 3. Create summary\n", "\n", "Once the document is fully indexed, create the document summary with the Python code." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "trusted": true }, "outputs": [], "source": [ "import json\n", "\n", "ask_path = f\"/api/v1/projects/{client.config.project}/ai/tools/documents/summarize\"\n", "\n", "body = {\n", " \"items\": [\n", " {\n", " \"id\": document_id\n", " }\n", " ]\n", "}\n", "\n", "response3 = client.post(ask_path, json=body, headers={\"cdf-version\": \"beta\"}).json()\n", "\n", "print(json.dumps(response3, indent=2))" ] } ], "metadata": { "kernelspec": { "display_name": "Python (Pyodide)", "language": "python", "name": "python" }, "language_info": { "codemirror_mode": { "name": "python", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8" } }, "nbformat": 4, "nbformat_minor": 4 }