From 7378ec7a90add24fa011c1bd95d736488d12981d Mon Sep 17 00:00:00 2001 From: Pranav Aurora Date: Thu, 5 Oct 2023 14:22:20 -0700 Subject: [PATCH] vertex AI notebook --- .../notebook.ipynb | 258 +++++++++--------- 1 file changed, 129 insertions(+), 129 deletions(-) diff --git a/notebooks/genai-app-with-singlestore-and-llamaindex/notebook.ipynb b/notebooks/genai-app-with-singlestore-and-llamaindex/notebook.ipynb index 5b5da769..f50c5976 100644 --- a/notebooks/genai-app-with-singlestore-and-llamaindex/notebook.ipynb +++ b/notebooks/genai-app-with-singlestore-and-llamaindex/notebook.ipynb @@ -1,18 +1,4 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "provenance": [] - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "language_info": { - "name": "python" - } - }, "cells": [ { "cell_type": "markdown", @@ -32,6 +18,7 @@ }, { "cell_type": "markdown", + "metadata": {}, "source": [ "# Building a Generative AI Application with LlamaIndex and SingleStore\n", "\n", @@ -50,15 +37,14 @@ "- Basic knowledge of Python programming.\n", "- Understanding of SQL databases.\n", "- Familiarity with generative AI concepts would be beneficial." - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "Let's first install the necessary packages." - ], - "metadata": {} + ] }, { "cell_type": "code", @@ -66,104 +52,107 @@ "metadata": {}, "outputs": [], "source": [ - "!pip install llama-index --quiet\n", - "!pip install langchain --quiet\n", - "!pip install llama-hub --quiet\n", - "!pip install singlestoredb --quiet" + "%pip install llama-index --quiet\n", + "%pip install langchain --quiet\n", + "%pip install llama-hub --quiet\n", + "%pip install singlestoredb --quiet" ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "Then, let's set our OpenAI API Key. Note: the API keys used in this notebook are placeholders and invalid." - ], - "metadata": {} + ] }, { "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], "source": [ "import os\n", "os.environ[\"OPENAI_API_KEY\"] = \"sk-xxx\"" - ], - "metadata": {}, - "execution_count": 2, - "outputs": [] + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "Next, we'll import the SingleStore vectorstore from Langchain." - ], - "metadata": {} + ] }, { "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], "source": [ "from langchain.vectorstores import SingleStoreDB" - ], - "metadata": {}, - "execution_count": 3, - "outputs": [] + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "After importing SingleStore, we can ingest the docs for LlamaIndex into a new table. This takes three steps:\n", "\n", "1. Load raw HTML data using WebBaseLoader\n", "2. Chunk the text.\n", "3. Embed or vectorize the chunked text, then ingest it into SingleStore." - ], - "metadata": {} + ] }, { "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], "source": [ "from langchain.document_loaders import WebBaseLoader\n", "\n", "loader = WebBaseLoader(\"https://gpt-index.readthedocs.io/en/latest/\")\n", "data = loader.load()" - ], - "metadata": {}, - "execution_count": 4, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], "source": [ "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", "\n", "text_splitter = RecursiveCharacterTextSplitter(chunk_size = 500, chunk_overlap = 0)\n", "all_splits = text_splitter.split_documents(data)" - ], - "metadata": {}, - "execution_count": 5, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], "source": [ "from langchain.embeddings import OpenAIEmbeddings\n", "os.environ[\"SINGLESTOREDB_URL\"] = \"admin:password@svc-56441794-b2ba-46ad-bc0b-c3d5810a45f4-dml.aws-oregon-3.svc.singlestore.com:3306/demo\"\n", "\n", "# vectorstore = SingleStoreDB.from_documents(documents=all_splits, embedding=OpenAIEmbeddings())\n", "vectorstore = SingleStoreDB(embedding=OpenAIEmbeddings())" - ], - "metadata": {}, - "execution_count": 6, - "outputs": [] + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "Now, we'll use Llama Index to retrieve and query from SingleStore using the SingleStoreReader, a lightweight embedding lookup tool for SingleStore databases ingested with content and vector data.\n", "\n", "Note that the full SingleStore vectorstore integration with Llama Index for ingesting and indexing is coming soon!" - ], - "metadata": {} + ] }, { "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], "source": [ "from llama_index import download_loader\n", "\n", @@ -180,13 +169,11 @@ " content_field=\"content\",\n", " vector_field=\"vector\"\n", ")" - ], - "metadata": {}, - "execution_count": 7, - "outputs": [] + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "Let's test it out. This function takes a natural language query as input, then does the following:\n", "\n", @@ -194,11 +181,13 @@ "2. Ingest the documents into a Llama Index list index, a data structure that returns all documents into the context.\n", "3. Initialize the index as a Llama Index query engine, which uses the `gpt-3.5-turbo` OpenAI LLM by default to understand the query and provided context, then generate a response.\n", "4. Returns the response." - ], - "metadata": {} + ] }, { "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], "source": [ "import json\n", "\n", @@ -216,51 +205,51 @@ "\n", " response = query_engine.query(query)\n", " return response" - ], - "metadata": {}, - "execution_count": 8, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], "source": [ "print(ask_llamaindex_docs(\"What is Llama Index?\"))" - ], - "metadata": {}, - "execution_count": 9, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], "source": [ "print(ask_llamaindex_docs(\"What are data indexes in Llama Index?\"))" - ], - "metadata": {}, - "execution_count": 10, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], "source": [ "print(ask_llamaindex_docs(\"What are query engines in Llama Index?\"))" - ], - "metadata": {}, - "execution_count": 11, - "outputs": [] + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "# Tips and Tricks\n", "\n", "## Chat engines\n", "\n", "Chat with your data conversationally with Llama Index Chat Engines, which allow for follow-ups and further questions." - ], - "metadata": {} + ] }, { "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], "source": [ "query = \"What is Llama Index?\"\n", "\n", @@ -273,13 +262,11 @@ "chat_engine = index.as_chat_engine(chat_mode='context')\n", "\n", "chat_engine.chat_repl()" - ], - "metadata": {}, - "execution_count": 12, - "outputs": [] + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "## Finetune Embeddings\n", "\n", @@ -288,31 +275,33 @@ "1. Split your data into train and validation datasets.\n", "2. Generate synthetic QA embedding pairs.\n", "3. Finetune your model using Llama Index." - ], - "metadata": {} + ] }, { "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], "source": [ "# Your data goes here\n", "train_dataset = generate_qa_embedding_pairs(train_nodes)\n", "val_dataset = generate_qa_embedding_pairs(val_nodes)" - ], - "metadata": {}, - "execution_count": 13, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], "source": [ "from llama_index.finetuning import SentenceTransformersFinetuneEngine" - ], - "metadata": {}, - "execution_count": 14, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], "source": [ "finetune_engine = SentenceTransformersFinetuneEngine(\n", " train_dataset,\n", @@ -320,38 +309,36 @@ " model_output_path=\"test_model\",\n", " val_dataset=val_dataset,\n", ")" - ], - "metadata": {}, - "execution_count": 15, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], "source": [ "finetune_engine.finetune()" - ], - "metadata": {}, - "execution_count": 16, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], "source": [ "embed_model = finetune_engine.get_finetuned_model()" - ], - "metadata": {}, - "execution_count": 17, - "outputs": [] + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "****" - ], - "metadata": {} + ] }, { "cell_type": "markdown", + "metadata": {}, "source": [ "## Data Agents\n", "\n", @@ -362,22 +349,24 @@ "- Calling any external service API in a structured fashion, and processing the response + storing it for later.\n", "\n", "We'll create a simple agent with access to the `ask_llamaindex_docs` function we created earlier as a tool." - ], - "metadata": {} + ] }, { "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], "source": [ "from llama_index.llms import OpenAI\n", "from llama_index.agent import ReActAgent\n", "from llama_index.tools import QueryEngineTool, ToolMetadata" - ], - "metadata": {}, - "execution_count": 18, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], "source": [ "llamaindex_docs_tool = QueryEngineTool(\n", " query_engine=index.as_query_engine(),\n", @@ -386,46 +375,43 @@ " description=\"Provides access to the docs for Llama Index, a library for ingesting, indexing, and querying data for LLMs.\"\n", " )\n", ")" - ], - "metadata": {}, - "execution_count": 19, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], "source": [ "agent = ReActAgent.from_tools([llamaindex_docs_tool], verbose=True)" - ], - "metadata": {}, - "execution_count": 20, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], "source": [ "agent.reset()" - ], - "metadata": {}, - "execution_count": 21, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], "source": [ "agent.chat(\"What is Llama Index?\")" - ], - "metadata": {}, - "execution_count": 22, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], "source": [ "agent.chat(\"Tell me about it's capabiltiies\")" - ], - "metadata": {}, - "execution_count": 23, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -436,5 +422,19 @@ "
" ] } - ] + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + }, + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 0 }