|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "14b2675c", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# How to connect Langchain to Oracle 26ai" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "id": "893d6647", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "!python -m pip install -U langchain-oracledb langchain-huggingface" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "code", |
| 21 | + "execution_count": null, |
| 22 | + "id": "245ac7b7", |
| 23 | + "metadata": { |
| 24 | + "vscode": { |
| 25 | + "languageId": "plaintext" |
| 26 | + } |
| 27 | + }, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "from langchain_oracledb.vectorstores import oraclevs\n", |
| 31 | + "from langchain_oracledb.vectorstores.oraclevs import OracleVS\n", |
| 32 | + "from langchain_community.vectorstores.utils import DistanceStrategy\n", |
| 33 | + "from langchain_core.documents import Document\n", |
| 34 | + "from langchain_huggingface import HuggingFaceEmbeddings" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": null, |
| 40 | + "id": "1c06de86", |
| 41 | + "metadata": { |
| 42 | + "vscode": { |
| 43 | + "languageId": "plaintext" |
| 44 | + } |
| 45 | + }, |
| 46 | + "outputs": [], |
| 47 | + "source": [ |
| 48 | + "import oracledb\n", |
| 49 | + "\n", |
| 50 | + "connection = oracledb.connect(\n", |
| 51 | + " user=\"ORACLE USERNAME\", \n", |
| 52 | + " password=\"ORACLE PASSWORD\", \n", |
| 53 | + " dsn=\"sample_low\",\n", |
| 54 | + " config_dir=\"FOLDER CONTAINING WALLET DATA\",\n", |
| 55 | + " wallet_location=\"FOLDER CONTAINING WALLET DATA\",\n", |
| 56 | + " wallet_password=\"WALLET PASSWORD\",\n", |
| 57 | + ")" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "code", |
| 62 | + "execution_count": null, |
| 63 | + "id": "a9f058c0", |
| 64 | + "metadata": { |
| 65 | + "vscode": { |
| 66 | + "languageId": "plaintext" |
| 67 | + } |
| 68 | + }, |
| 69 | + "outputs": [], |
| 70 | + "source": [ |
| 71 | + "documents_json_list = [\n", |
| 72 | + " {\n", |
| 73 | + " \"id\": \"creating-flows\",\n", |
| 74 | + " \"text\": \"Metaflow follows the dataflow paradigm which models a program as a directed graph of operations. This is a natural paradigm for expressing data processing pipelines, machine learning in particular. We call the graph of operations a flow. You define the operations, called steps, which are nodes of the graph and contain transitions to the next steps, which serve as edges. Metaflow sets some constraints on the structure of the graph. For starters, every flow needs a step called start and a step called end. An execution of the flow, which we call a run, starts at start. The run is successful if the final end step finishes successfully. What happens between start and end is up to you. You can construct the graph in between using an arbitrary combination of the following three types of transitions supported by Metaflow:\",\n", |
| 75 | + " \"link\": \"https://docs.metaflow.org/metaflow/basics\",\n", |
| 76 | + " },\n", |
| 77 | + " {\n", |
| 78 | + " \"id\": \"authoring-flows\",\n", |
| 79 | + " \"text\": \"With Metaflow, you might start with a simple stub, perhaps just a step to load data, and then gradually add more @steps, say, for data transformation, model training, and beyond, testing the flow at each iteration. To enable a smooth development experience, these iterations should run quickly, with minimal waiting - much like the familiar workflow in a notebook, where you build results one cell at a time.\",\n", |
| 80 | + " \"link\": \"https://docs.metaflow.org/metaflow/authoring-flows/introduction\",\n", |
| 81 | + " },\n", |
| 82 | + " {\n", |
| 83 | + " \"id\": \"running-flows\",\n", |
| 84 | + " \"text\": \"The Runner API allows you to start and manage Metaflow runs and other operations programmatically, for instance, to run flows in a script. The Runner class exposes a blocking API, which waits for operations to complete, as well as a non-blocking (asynchronous) APIs, prefixed with async which execute operations in the background. This document provides an overview of common patterns. For detailed API documentation, see the Runner API reference.\",\n", |
| 85 | + " \"link\": \"https://docs.metaflow.org/metaflow/managing-flows/runner\",\n", |
| 86 | + " }\n", |
| 87 | + "]" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "code", |
| 92 | + "execution_count": null, |
| 93 | + "id": "e1d67dd3", |
| 94 | + "metadata": { |
| 95 | + "vscode": { |
| 96 | + "languageId": "plaintext" |
| 97 | + } |
| 98 | + }, |
| 99 | + "outputs": [], |
| 100 | + "source": [ |
| 101 | + "documents = []\n", |
| 102 | + "\n", |
| 103 | + "for doc in documents_json_list:\n", |
| 104 | + " metadata = {\"id\": doc[\"id\"], \"link\": doc[\"link\"]}\n", |
| 105 | + " \n", |
| 106 | + " document = Document(\n", |
| 107 | + " page_content=doc[\"text\"], \n", |
| 108 | + " metadata=metadata\n", |
| 109 | + " )\n", |
| 110 | + "\n", |
| 111 | + " documents.append(document)" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "code", |
| 116 | + "execution_count": null, |
| 117 | + "id": "9560eb7a", |
| 118 | + "metadata": { |
| 119 | + "vscode": { |
| 120 | + "languageId": "plaintext" |
| 121 | + } |
| 122 | + }, |
| 123 | + "outputs": [], |
| 124 | + "source": [ |
| 125 | + "model = HuggingFaceEmbeddings(model_name=\"sentence-transformers/all-mpnet-base-v2\")" |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | + "cell_type": "code", |
| 130 | + "execution_count": null, |
| 131 | + "id": "a863751f", |
| 132 | + "metadata": { |
| 133 | + "vscode": { |
| 134 | + "languageId": "plaintext" |
| 135 | + } |
| 136 | + }, |
| 137 | + "outputs": [], |
| 138 | + "source": [ |
| 139 | + "vector_store = OracleVS.from_documents(\n", |
| 140 | + " documents,\n", |
| 141 | + " model,\n", |
| 142 | + " client=connection,\n", |
| 143 | + " table_name=\"Documents_COSINE\",\n", |
| 144 | + " distance_strategy=DistanceStrategy.COSINE,\n", |
| 145 | + ")" |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "cell_type": "code", |
| 150 | + "execution_count": null, |
| 151 | + "id": "9f5ef45d", |
| 152 | + "metadata": { |
| 153 | + "vscode": { |
| 154 | + "languageId": "plaintext" |
| 155 | + } |
| 156 | + }, |
| 157 | + "outputs": [], |
| 158 | + "source": [ |
| 159 | + "oraclevs.create_index(\n", |
| 160 | + " connection,\n", |
| 161 | + " vector_store,\n", |
| 162 | + " params={\n", |
| 163 | + " \"idx_name\": \"hnsw_idx2\",\n", |
| 164 | + " \"idx_type\": \"HNSW\",\n", |
| 165 | + " \"accuracy\": 97,\n", |
| 166 | + " \"parallel\": 16,\n", |
| 167 | + " },\n", |
| 168 | + ")" |
| 169 | + ] |
| 170 | + }, |
| 171 | + { |
| 172 | + "cell_type": "code", |
| 173 | + "execution_count": null, |
| 174 | + "id": "bd498a2e", |
| 175 | + "metadata": { |
| 176 | + "vscode": { |
| 177 | + "languageId": "plaintext" |
| 178 | + } |
| 179 | + }, |
| 180 | + "outputs": [], |
| 181 | + "source": [ |
| 182 | + "query = \"What is exposed by the Runner API?\"\n", |
| 183 | + "result = vector_store.similarity_search(query, 2)\n", |
| 184 | + "print(result)" |
| 185 | + ] |
| 186 | + } |
| 187 | + ], |
| 188 | + "metadata": { |
| 189 | + "language_info": { |
| 190 | + "name": "python" |
| 191 | + } |
| 192 | + }, |
| 193 | + "nbformat": 4, |
| 194 | + "nbformat_minor": 5 |
| 195 | +} |
0 commit comments