From 89336e4ddfbd64c89754d19f8abfe64816a62378 Mon Sep 17 00:00:00 2001
From: Marcus Schiesser <mail@marcusschiesser.de>
Date: Thu, 25 Jan 2024 18:09:19 +0700
Subject: [PATCH] feat: add deno jupyter examples (#428)

---
 examples/jupyter/.gitignore        |  1 +
 examples/jupyter/README.md         | 31 +++++++++++
 examples/jupyter/nodeparser.ipynb  | 82 +++++++++++++++++++++++++++++
 examples/jupyter/vectorIndex.ipynb | 83 ++++++++++++++++++++++++++++++
 4 files changed, 197 insertions(+)
 create mode 100644 examples/jupyter/.gitignore
 create mode 100644 examples/jupyter/README.md
 create mode 100644 examples/jupyter/nodeparser.ipynb
 create mode 100644 examples/jupyter/vectorIndex.ipynb

diff --git a/examples/jupyter/.gitignore b/examples/jupyter/.gitignore
new file mode 100644
index 000000000..87620ac7e
--- /dev/null
+++ b/examples/jupyter/.gitignore
@@ -0,0 +1 @@
+.ipynb_checkpoints/
diff --git a/examples/jupyter/README.md b/examples/jupyter/README.md
new file mode 100644
index 000000000..90a8d7e79
--- /dev/null
+++ b/examples/jupyter/README.md
@@ -0,0 +1,31 @@
+# Jupyter examples
+
+## Preparation
+
+1. Install Deno, e.g. on macOS:
+
+```
+brew install deno
+```
+
+2. Install Jupyter
+
+```
+pip3 install jupyterlab
+```
+
+3. Install Deno kernel
+
+```
+deno jupyter --unstable --install
+```
+
+4. Run Jupyter
+
+```
+jupyter lab
+```
+
+## Run examples
+
+Then you can open in Jupyter any of the examples in this directory.
diff --git a/examples/jupyter/nodeparser.ipynb b/examples/jupyter/nodeparser.ipynb
new file mode 100644
index 000000000..14df45b97
--- /dev/null
+++ b/examples/jupyter/nodeparser.ipynb
@@ -0,0 +1,82 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "id": "8be89595-8885-4d5e-b1da-1df04eda5c7a",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import {\n",
+    "  Document,\n",
+    "  SimpleNodeParser\n",
+    "} from \"npm:llamaindex\";"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "id": "65de03f9-455a-4c59-9089-093cb6998af7",
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "[\n",
+      "  TextNode {\n",
+      "    id_: \u001b[32m\"1b2ab25e-562a-4821-bdde-860bd23121c1\"\u001b[39m,\n",
+      "    metadata: {},\n",
+      "    excludedEmbedMetadataKeys: [],\n",
+      "    excludedLlmMetadataKeys: [],\n",
+      "    relationships: {\n",
+      "      SOURCE: {\n",
+      "        nodeId: \u001b[32m\"0cb8de0e-845f-4e73-a7bd-f04426aacfed\"\u001b[39m,\n",
+      "        metadata: {},\n",
+      "        hash: \u001b[32m\"jatVVXETDFjV2fV1/fbTrdpY6ZGnSYekq9m1X/Ff1qs=\"\u001b[39m\n",
+      "      }\n",
+      "    },\n",
+      "    hash: \u001b[32m\"zVyeDsfMwWH1CqK2269o5uzGWl/DpIWO4ZcVCuyENi4=\"\u001b[39m,\n",
+      "    text: \u001b[32m\"I am 10 years old. John is 20 years old.\"\u001b[39m,\n",
+      "    metadataSeparator: \u001b[32m\"\\n\"\u001b[39m\n",
+      "  }\n",
+      "]\n"
+     ]
+    }
+   ],
+   "source": [
+    "const nodeParser = new SimpleNodeParser();\n",
+    "const nodes = nodeParser.getNodesFromDocuments([\n",
+    "    new Document({ text: \"I am 10 years old. John is 20 years old.\" }),\n",
+    "]);\n",
+    "\n",
+    "console.log(nodes);"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "fc0ec12f-2062-47af-916d-7c77ca39433a",
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Deno",
+   "language": "typescript",
+   "name": "deno"
+  },
+  "language_info": {
+   "file_extension": ".ts",
+   "mimetype": "text/x.typescript",
+   "name": "typescript",
+   "nb_converter": "script",
+   "pygments_lexer": "typescript",
+   "version": "5.3.3"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
diff --git a/examples/jupyter/vectorIndex.ipynb b/examples/jupyter/vectorIndex.ipynb
new file mode 100644
index 000000000..a4f9aec87
--- /dev/null
+++ b/examples/jupyter/vectorIndex.ipynb
@@ -0,0 +1,83 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "id": "8be89595-8885-4d5e-b1da-1df04eda5c7a",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import {\n",
+    "  Document,\n",
+    "  VectorStoreIndex\n",
+    "} from \"npm:llamaindex\";"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 11,
+   "id": "65de03f9-455a-4c59-9089-093cb6998af7",
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "In college, the author studied subjects such as linear algebra and physics, but did not find them particularly interesting. They also slacked off and skipped lectures, leading to gaps in their knowledge. They had a negative experience with their English classes and became resentful and suspicious of higher education. They eventually dropped out of college and did not return until five years later to pick up their papers.\n"
+     ]
+    }
+   ],
+   "source": [
+    "// Create Document object with essay\n",
+    "const resp = await fetch('https://raw.githubusercontent.com/run-llama/LlamaIndexTS/main/packages/core/examples/abramov.txt');\n",
+    "const text = await resp.text();\n",
+    "const document = new Document({ text });\n",
+    "\n",
+    "// Split text and create embeddings. Store them in a VectorStoreIndex\n",
+    "const index = await VectorStoreIndex.fromDocuments([document]);\n",
+    "\n",
+    "// Query the index\n",
+    "const queryEngine = index.asQueryEngine();\n",
+    "const response = await queryEngine.query({\n",
+    "    query: \"What did the author do in college?\",\n",
+    "});\n",
+    "\n",
+    "// Output response\n",
+    "console.log(response.toString());"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "fc0ec12f-2062-47af-916d-7c77ca39433a",
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "68bdd292-5cf7-46e0-8646-51be1f070ad6",
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Deno",
+   "language": "typescript",
+   "name": "deno"
+  },
+  "language_info": {
+   "file_extension": ".ts",
+   "mimetype": "text/x.typescript",
+   "name": "typescript",
+   "nb_converter": "script",
+   "pygments_lexer": "typescript",
+   "version": "5.3.3"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
-- 
GitLab