diff --git a/learn/generation/langchain/handbook/08-langchain-retrieval-agent.ipynb b/learn/generation/langchain/handbook/08-langchain-retrieval-agent.ipynb index fefea3b1..ac63e0e9 100644 --- a/learn/generation/langchain/handbook/08-langchain-retrieval-agent.ipynb +++ b/learn/generation/langchain/handbook/08-langchain-retrieval-agent.ipynb @@ -1,1291 +1,1242 @@ { - "cells": [ - { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "TXC2wBpCU9f7" - }, - "source": [ - "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pinecone-io/examples/blob/master/learn/generation/langchain/handbook/08-langchain-retrieval-agent.ipynb) [![Open nbviewer](https://raw.githubusercontent.com/pinecone-io/examples/master/assets/nbviewer-shield.svg)](https://nbviewer.org/github/pinecone-io/examples/blob/master/learn/generation/langchain/handbook/08-langchain-retrieval-agent.ipynb)" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "bhWwrfbbVGOA" - }, - "source": [ - "#### [LangChain Handbook](https://pinecone.io/learn/langchain)\n", - "\n", - "# Retrieval Agents\n", - "\n", - "We've seen in previous chapters how powerful [retrieval augmentation](https://www.pinecone.io/learn/langchain-retrieval-augmentation/) and [conversational agents](https://www.pinecone.io/learn/langchain-agents/) can be. They become even more impressive when we begin using them together.\n", - "\n", - "Conversational agents can struggle with data freshness, knowledge about specific domains, or accessing internal documentation. By coupling agents with retrieval augmentation tools we no longer have these problems.\n", - "\n", - "One the other side, using \"naive\" retrieval augmentation without the use of an agent means we will retrieve contexts with *every* query. Again, this isn't always ideal as not every query requires access to external knowledge.\n", - "\n", - "Merging these methods gives us the best of both worlds. In this notebook we'll learn how to do this.\n", - "\n", - "To begin, we must install the prerequisite libraries that we will be using in this notebook." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "pva9ehKXUpU2", - "outputId": "3bbcf2dd-1889-412f-d45a-f56945ac4f2f" - }, - "outputs": [], - "source": [ - "!pip install -qU \\\n", - " openai==1.6.1 \\\n", - " pinecone-client==3.1.0 \\\n", - " langchain==0.1.1 \\\n", - " langchain-community==0.0.13 \\\n", - " tiktoken==0.5.2 \\\n", - " datasets==2.12.0" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "ZTgrOQziXUto" - }, - "source": [ - "## Building the Knowledge Base" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "qNyRsz0ZXXaq" - }, - "source": [ - "We start by constructing our knowledge base. We'll use a mostly prepared dataset called **S**tanford **Qu**estion-**A**nswering **D**ataset (SQuAD) hosted on Hugging Face *Datasets*. We download it like so:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "laSDMjqQXuj-", - "outputId": "5272df99-eb4b-4ec2-c513-504e067be2b6" - }, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "79d95a944b44423d9a2dd0082cdd80b0", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "Downloading readme: 0%| | 0.00/7.83k [00:00\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
idtitlecontextquestionanswers
05733be284776f41900661182University_of_Notre_DameArchitecturally, the school has a Catholic cha...To whom did the Virgin Mary allegedly appear i...{'text': ['Saint Bernadette Soubirous'], 'answ...
15733be284776f4190066117fUniversity_of_Notre_DameArchitecturally, the school has a Catholic cha...What is in front of the Notre Dame Main Building?{'text': ['a copper statue of Christ'], 'answe...
25733be284776f41900661180University_of_Notre_DameArchitecturally, the school has a Catholic cha...The Basilica of the Sacred heart at Notre Dame...{'text': ['the Main Building'], 'answer_start'...
35733be284776f41900661181University_of_Notre_DameArchitecturally, the school has a Catholic cha...What is the Grotto at Notre Dame?{'text': ['a Marian place of prayer and reflec...
45733be284776f4190066117eUniversity_of_Notre_DameArchitecturally, the school has a Catholic cha...What sits on top of the Main Building at Notre...{'text': ['a golden statue of the Virgin Mary'...
\n", - "" - ], - "text/plain": [ - " id title \\\n", - "0 5733be284776f41900661182 University_of_Notre_Dame \n", - "1 5733be284776f4190066117f University_of_Notre_Dame \n", - "2 5733be284776f41900661180 University_of_Notre_Dame \n", - "3 5733be284776f41900661181 University_of_Notre_Dame \n", - "4 5733be284776f4190066117e University_of_Notre_Dame \n", - "\n", - " context \\\n", - "0 Architecturally, the school has a Catholic cha... \n", - "1 Architecturally, the school has a Catholic cha... \n", - "2 Architecturally, the school has a Catholic cha... \n", - "3 Architecturally, the school has a Catholic cha... \n", - "4 Architecturally, the school has a Catholic cha... \n", - "\n", - " question \\\n", - "0 To whom did the Virgin Mary allegedly appear i... \n", - "1 What is in front of the Notre Dame Main Building? \n", - "2 The Basilica of the Sacred heart at Notre Dame... \n", - "3 What is the Grotto at Notre Dame? \n", - "4 What sits on top of the Main Building at Notre... \n", - "\n", - " answers \n", - "0 {'text': ['Saint Bernadette Soubirous'], 'answ... \n", - "1 {'text': ['a copper statue of Christ'], 'answe... \n", - "2 {'text': ['the Main Building'], 'answer_start'... \n", - "3 {'text': ['a Marian place of prayer and reflec... \n", - "4 {'text': ['a golden statue of the Virgin Mary'... " - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "data = data.to_pandas()\n", - "data.head()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 528 - }, - "id": "er2nM4vsYD4R", - "outputId": "d58b5640-be25-495a-b90c-585940c851bf" - }, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
idtitlecontextquestionanswers
05733be284776f41900661182University_of_Notre_DameArchitecturally, the school has a Catholic cha...To whom did the Virgin Mary allegedly appear i...{'text': ['Saint Bernadette Soubirous'], 'answ...
55733bf84d058e614000b61beUniversity_of_Notre_DameAs at most other universities, Notre Dame's st...When did the Scholastic Magazine of Notre dame...{'text': ['September 1876'], 'answer_start': [...
105733bed24776f41900661188University_of_Notre_DameThe university is the major seat of the Congre...Where is the headquarters of the Congregation ...{'text': ['Rome'], 'answer_start': [119]}
155733a6424776f41900660f51University_of_Notre_DameThe College of Engineering was established in ...How many BS level degrees are offered in the C...{'text': ['eight'], 'answer_start': [487]}
205733a70c4776f41900660f64University_of_Notre_DameAll of Notre Dame's undergraduate students are...What entity provides help with the management ...{'text': ['Learning Resource Center'], 'answer...
\n", - "
" - ], - "text/plain": [ - " id title \\\n", - "0 5733be284776f41900661182 University_of_Notre_Dame \n", - "5 5733bf84d058e614000b61be University_of_Notre_Dame \n", - "10 5733bed24776f41900661188 University_of_Notre_Dame \n", - "15 5733a6424776f41900660f51 University_of_Notre_Dame \n", - "20 5733a70c4776f41900660f64 University_of_Notre_Dame \n", - "\n", - " context \\\n", - "0 Architecturally, the school has a Catholic cha... \n", - "5 As at most other universities, Notre Dame's st... \n", - "10 The university is the major seat of the Congre... \n", - "15 The College of Engineering was established in ... \n", - "20 All of Notre Dame's undergraduate students are... \n", - "\n", - " question \\\n", - "0 To whom did the Virgin Mary allegedly appear i... \n", - "5 When did the Scholastic Magazine of Notre dame... \n", - "10 Where is the headquarters of the Congregation ... \n", - "15 How many BS level degrees are offered in the C... \n", - "20 What entity provides help with the management ... \n", - "\n", - " answers \n", - "0 {'text': ['Saint Bernadette Soubirous'], 'answ... \n", - "5 {'text': ['September 1876'], 'answer_start': [... \n", - "10 {'text': ['Rome'], 'answer_start': [119]} \n", - "15 {'text': ['eight'], 'answer_start': [487]} \n", - "20 {'text': ['Learning Resource Center'], 'answer... " - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "data.drop_duplicates(subset='context', keep='first', inplace=True)\n", - "data.head()" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "B2_Pt7N6Zg2X" - }, - "source": [ - "### Initialize the Embedding Model and Vector DB" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "bGoS84KYZnSK" - }, - "source": [ - "We'll be using OpenAI's `text-embedding-ada-002` model initialize via LangChain and the Pinecone vector DB. We start by initializing the embedding model, for this we need an [OpenAI API key](https://platform.openai.com/).\n", - "\n", - "*(Note that OpenAI is a paid service and so running the remainder of this notebook may incur some small cost)*" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "U57x2_87YSpb", - "outputId": "cf4c99af-24b7-4bf5-97ea-71091c8fc2ce" - }, - "outputs": [], - "source": [ - "import os\n", - "from getpass import getpass\n", - "from langchain.embeddings.openai import OpenAIEmbeddings\n", - "\n", - "# get API key from top-right dropdown on OpenAI website\n", - "OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\") or getpass(\"Enter your OpenAI API key: \")\n", - "model_name = 'text-embedding-ada-002'\n", - "\n", - "embed = OpenAIEmbeddings(\n", - " model=model_name,\n", - " openai_api_key=OPENAI_API_KEY\n", - ")" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "JQTfOTR6aBRS" - }, - "source": [ - "Now we create our vector DB to store our vectors. For this we need to get a [free Pinecone API key](https://app.pinecone.io) \u2014 the API key can be found in the \"API Keys\" button found in the left navbar of the Pinecone dashboard." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [], - "source": [ - "from pinecone import Pinecone\n", - "\n", - "# initialize connection to pinecone (get API key at app.pinecone.io)\n", - "api_key = os.getenv(\"PINECONE_API_KEY\") or getpass(\"Enter your Pinecone API key: \")\n", - "\n", - "# configure client\n", - "pc = Pinecone(api_key=api_key)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we setup our index specification, this allows us to define the cloud provider and region where we want to deploy our index. You can find a list of all [available providers and regions here](https://docs.pinecone.io/docs/projects)." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [], - "source": [ - "from pinecone import ServerlessSpec\n", - "\n", - "spec = ServerlessSpec(\n", - " cloud=\"aws\", region=\"us-east-1\"\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Creating an index, we set `dimension` equal to to dimensionality of Ada-002 (`1536`), and use a `metric` also compatible with Ada-002 (this can be either `cosine` or `dotproduct`). We also pass our `spec` to index initialization." - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "C3wrG-9yaJel", - "outputId": "842bf46f-fd0f-4322-8d90-a7fd769b7687" - }, - "outputs": [ - { - "data": { - "text/plain": [ - "{'dimension': 1536,\n", - " 'index_fullness': 0.0,\n", - " 'namespaces': {},\n", - " 'total_vector_count': 0}" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "import time\n", - "\n", - "index_name = \"langchain-retrieval-agent\"\n", - "existing_indexes = [\n", - " index_info[\"name\"] for index_info in pc.list_indexes()\n", - "]\n", - "\n", - "# check if index already exists (it shouldn't if this is first time)\n", - "if index_name not in existing_indexes:\n", - " # if does not exist, create index\n", - " pc.create_index(\n", - " index_name,\n", - " dimension=1536, # dimensionality of ada 002\n", - " metric='dotproduct',\n", - " spec=spec\n", - " )\n", - " # wait for index to be initialized\n", - " while not pc.describe_index(index_name).status['ready']:\n", - " time.sleep(1)\n", - "\n", - "# connect to index\n", - "index = pc.Index(index_name)\n", - "time.sleep(1)\n", - "# view index stats\n", - "index.describe_index_stats()" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "AD5IGOoLaVx7" - }, - "source": [ - "We should see that the new Pinecone index has a `total_vector_count` of `0`, as we haven't added any vectors yet.\n", - "\n", - "## Indexing\n", - "\n", - "We can perform the indexing task using the LangChain vector store object. But for now it is much faster to do it via the Pinecone python client directly. We will do this in batches of `100` or more." - ] + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "TXC2wBpCU9f7" + }, + "source": [ + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pinecone-io/examples/blob/main/learn/generation/langchain/handbook/08-langchain-retrieval-agent.ipynb) [![Open nbviewer](https://raw.githubusercontent.com/pinecone-io/examples/main/assets/nbviewer-shield.svg)](https://nbviewer.org/github/pinecone-io/examples/blob/main/learn/generation/langchain/handbook/08-langchain-retrieval-agent.ipynb)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "bhWwrfbbVGOA" + }, + "source": [ + "#### [LangChain Handbook](https://www.pinecone.io/learn/series/langchain/)\n", + "\n", + "# Retrieval Agents\n", + "\n", + "We've seen in previous chapters how powerful [retrieval augmentation](https://www.pinecone.io/learn/series/langchain/langchain-retrieval-augmentation/) and [conversational agents](https://www.pinecone.io/learn/series/langchain/langchain-agents/) can be. They become even more impressive when we begin using them together.\n", + "\n", + "Conversational agents can struggle with data freshness, knowledge about specific domains, or accessing internal documentation. By coupling agents with retrieval augmentation tools we no longer have these problems.\n", + "\n", + "One the other side, using \"naive\" retrieval augmentation without the use of an agent means we will retrieve contexts with *every* query. Again, this isn't always ideal as not every query requires access to external knowledge.\n", + "\n", + "Merging these methods gives us the best of both worlds. In this notebook we'll learn how to do this.\n", + "\n", + "To begin, we must install the prerequisite libraries that we will be using in this notebook." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 49, - "referenced_widgets": [ - "321ebcd192fc41e0a7a2ba9c0e7b8556", - "7c898f43b39545879013df760aa4f6f1", - "1b5fe655986842a6afd4a2eb2c0f47fc", - "76872ca737b64616aeacc90f2f7fc655", - "0679733cce4f4b5eb25f8f56dc7ebe22", - "71551fc297b54fb4a21d787d117e01a1", - "b90283522431447b925b226431bb4fa6", - "a03d372e8b4f456e8663e54c818e0fe9", - "6fc773ccbc594fddbc1f2f0adaa96186", - "07b47869f727455fb5ba67e99767c04c", - "4b2e12a33584489690aaf69f51cd4a3b" - ] - }, - "id": "AhDcbRGTaWPi", - "outputId": "dca5a4d0-5d19-4542-c26c-3073bfa13f2a" - }, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "862314956bfc40af91a2f2e8d68a0fdd", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - " 0%| | 0/189 [00:00\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
idtitlecontextquestionanswers
05733be284776f41900661182University_of_Notre_DameArchitecturally, the school has a Catholic cha...To whom did the Virgin Mary allegedly appear i...{'text': ['Saint Bernadette Soubirous'], 'answ...
15733be284776f4190066117fUniversity_of_Notre_DameArchitecturally, the school has a Catholic cha...What is in front of the Notre Dame Main Building?{'text': ['a copper statue of Christ'], 'answe...
25733be284776f41900661180University_of_Notre_DameArchitecturally, the school has a Catholic cha...The Basilica of the Sacred heart at Notre Dame...{'text': ['the Main Building'], 'answer_start'...
35733be284776f41900661181University_of_Notre_DameArchitecturally, the school has a Catholic cha...What is the Grotto at Notre Dame?{'text': ['a Marian place of prayer and reflec...
45733be284776f4190066117eUniversity_of_Notre_DameArchitecturally, the school has a Catholic cha...What sits on top of the Main Building at Notre...{'text': ['a golden statue of the Virgin Mary'...
\n", + "" ], - "source": [ - "from langchain.vectorstores import Pinecone\n", - "\n", - "text_field = \"text\" # the metadata field that contains our text\n", - "\n", - "# initialize the vector store object\n", - "vectorstore = Pinecone(\n", - " index, embed.embed_query, text_field\n", - ")" + "text/plain": [ + " id title \\\n", + "0 5733be284776f41900661182 University_of_Notre_Dame \n", + "1 5733be284776f4190066117f University_of_Notre_Dame \n", + "2 5733be284776f41900661180 University_of_Notre_Dame \n", + "3 5733be284776f41900661181 University_of_Notre_Dame \n", + "4 5733be284776f4190066117e University_of_Notre_Dame \n", + "\n", + " context \\\n", + "0 Architecturally, the school has a Catholic cha... \n", + "1 Architecturally, the school has a Catholic cha... \n", + "2 Architecturally, the school has a Catholic cha... \n", + "3 Architecturally, the school has a Catholic cha... \n", + "4 Architecturally, the school has a Catholic cha... \n", + "\n", + " question \\\n", + "0 To whom did the Virgin Mary allegedly appear i... \n", + "1 What is in front of the Notre Dame Main Building? \n", + "2 The Basilica of the Sacred heart at Notre Dame... \n", + "3 What is the Grotto at Notre Dame? \n", + "4 What sits on top of the Main Building at Notre... \n", + "\n", + " answers \n", + "0 {'text': ['Saint Bernadette Soubirous'], 'answ... \n", + "1 {'text': ['a copper statue of Christ'], 'answe... \n", + "2 {'text': ['the Main Building'], 'answer_start'... \n", + "3 {'text': ['a Marian place of prayer and reflec... \n", + "4 {'text': ['a golden statue of the Virgin Mary'... " ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = data.to_pandas()\n", + "data.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 528 }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "3K3xRthWcXzW" - }, - "source": [ - "As in previous examples, we can use the `similarity_search` method to do a pure semantic search (without the generation component)." - ] + "execution": { + "iopub.execute_input": "2026-10-08T21:58:52.151092Z", + "iopub.status.busy": "2026-10-08T21:58:52.151031Z", + "iopub.status.idle": "2026-10-08T21:58:52.178530Z", + "shell.execute_reply": "2026-10-08T21:58:52.178014Z" }, + "id": "er2nM4vsYD4R", + "outputId": "d58b5640-be25-495a-b90c-585940c851bf" + }, + "outputs": [ { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "uITMZtzschJF", - "outputId": "88fae02f-6f82-47d7-a153-21b3e0615709" - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[Document(page_content=\"In 1919 Father James Burns became president of Notre Dame, and in three years he produced an academic revolution that brought the school up to national standards by adopting the elective system and moving away from the university's traditional scholastic and classical emphasis. By contrast, the Jesuit colleges, bastions of academic conservatism, were reluctant to move to a system of electives. Their graduates were shut out of Harvard Law School for that reason. Notre Dame continued to grow over the years, adding more colleges, programs, and sports teams. By 1921, with the addition of the College of Commerce, Notre Dame had grown from a small college to a university with five colleges and a professional law school. The university continued to expand and add new residence halls and buildings with each subsequent president.\", metadata={'title': 'University_of_Notre_Dame'}),\n", - " Document(page_content='The College of Engineering was established in 1920, however, early courses in civil and mechanical engineering were a part of the College of Science since the 1870s. Today the college, housed in the Fitzpatrick, Cushing, and Stinson-Remick Halls of Engineering, includes five departments of study \u2013 aerospace and mechanical engineering, chemical and biomolecular engineering, civil engineering and geological sciences, computer science and engineering, and electrical engineering \u2013 with eight B.S. degrees offered. Additionally, the college offers five-year dual degree programs with the Colleges of Arts and Letters and of Business awarding additional B.A. and Master of Business Administration (MBA) degrees, respectively.', metadata={'title': 'University_of_Notre_Dame'}),\n", - " Document(page_content='Since 2005, Notre Dame has been led by John I. Jenkins, C.S.C., the 17th president of the university. Jenkins took over the position from Malloy on July 1, 2005. In his inaugural address, Jenkins described his goals of making the university a leader in research that recognizes ethics and building the connection between faith and studies. During his tenure, Notre Dame has increased its endowment, enlarged its student body, and undergone many construction projects on campus, including Compton Family Ice Arena, a new architecture hall, additional residence halls, and the Campus Crossroads, a $400m enhancement and expansion of Notre Dame Stadium.', metadata={'title': 'University_of_Notre_Dame'})]" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
idtitlecontextquestionanswers
05733be284776f41900661182University_of_Notre_DameArchitecturally, the school has a Catholic cha...To whom did the Virgin Mary allegedly appear i...{'text': ['Saint Bernadette Soubirous'], 'answ...
55733bf84d058e614000b61beUniversity_of_Notre_DameAs at most other universities, Notre Dame's st...When did the Scholastic Magazine of Notre dame...{'text': ['September 1876'], 'answer_start': [...
105733bed24776f41900661188University_of_Notre_DameThe university is the major seat of the Congre...Where is the headquarters of the Congregation ...{'text': ['Rome'], 'answer_start': [119]}
155733a6424776f41900660f51University_of_Notre_DameThe College of Engineering was established in ...How many BS level degrees are offered in the C...{'text': ['eight'], 'answer_start': [487]}
205733a70c4776f41900660f64University_of_Notre_DameAll of Notre Dame's undergraduate students are...What entity provides help with the management ...{'text': ['Learning Resource Center'], 'answer...
\n", + "
" ], - "source": [ - "query = \"when was the college of engineering in the University of Notre Dame established?\"\n", - "\n", - "vectorstore.similarity_search(\n", - " query, # our search query\n", - " k=3 # return 3 most relevant docs\n", - ")" + "text/plain": [ + " id title \\\n", + "0 5733be284776f41900661182 University_of_Notre_Dame \n", + "5 5733bf84d058e614000b61be University_of_Notre_Dame \n", + "10 5733bed24776f41900661188 University_of_Notre_Dame \n", + "15 5733a6424776f41900660f51 University_of_Notre_Dame \n", + "20 5733a70c4776f41900660f64 University_of_Notre_Dame \n", + "\n", + " context \\\n", + "0 Architecturally, the school has a Catholic cha... \n", + "5 As at most other universities, Notre Dame's st... \n", + "10 The university is the major seat of the Congre... \n", + "15 The College of Engineering was established in ... \n", + "20 All of Notre Dame's undergraduate students are... \n", + "\n", + " question \\\n", + "0 To whom did the Virgin Mary allegedly appear i... \n", + "5 When did the Scholastic Magazine of Notre dame... \n", + "10 Where is the headquarters of the Congregation ... \n", + "15 How many BS level degrees are offered in the C... \n", + "20 What entity provides help with the management ... \n", + "\n", + " answers \n", + "0 {'text': ['Saint Bernadette Soubirous'], 'answ... \n", + "5 {'text': ['September 1876'], 'answer_start': [... \n", + "10 {'text': ['Rome'], 'answer_start': [119]} \n", + "15 {'text': ['eight'], 'answer_start': [487]} \n", + "20 {'text': ['Learning Resource Center'], 'answer... " ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data.drop_duplicates(subset=\"context\", keep=\"first\", inplace=True)\n", + "data.head()" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "B2_Pt7N6Zg2X" + }, + "source": [ + "### Initialize the Embedding Model and Vector DB" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "bGoS84KYZnSK" + }, + "source": [ + "We'll be using OpenAI's `text-embedding-3-small` model initialized via LangChain and the Pinecone vector DB. We start by initializing the embedding model, for this we need an [OpenAI API key](https://platform.openai.com/).\n", + "\n", + "*(Note that OpenAI is a paid service and so running the remainder of this notebook may incur some small cost)*" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "-zGF6YsgczqT" - }, - "source": [ - "Looks like we're getting good results. Let's take a look at how we can begin integrating this into a conversational agent." - ] + "execution": { + "iopub.execute_input": "2026-10-08T21:58:52.179632Z", + "iopub.status.busy": "2026-10-08T21:58:52.179569Z", + "iopub.status.idle": "2026-10-08T21:58:52.183702Z", + "shell.execute_reply": "2026-10-08T21:58:52.183350Z" }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "tFsIOm73dcOI" - }, - "source": [ - "## Initializing the Conversational Agent" - ] + "id": "U57x2_87YSpb", + "outputId": "cf4c99af-24b7-4bf5-97ea-71091c8fc2ce" + }, + "outputs": [], + "source": [ + "# get API key from platform.openai.com/api-keys\n", + "OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\") or getpass(\"Enter your OpenAI API key: \")\n", + "model_name = \"text-embedding-3-small\"\n", + "\n", + "embed = OpenAIEmbeddings(model=model_name, openai_api_key=OPENAI_API_KEY)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "JQTfOTR6aBRS" + }, + "source": [ + "Now we create our vector DB to store our vectors. For this we need a [free Pinecone API key](https://app.pinecone.io), which you can create on the **API keys** page of the Pinecone console." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "execution": { + "iopub.execute_input": "2026-10-08T21:58:52.184737Z", + "iopub.status.busy": "2026-10-08T21:58:52.184683Z", + "iopub.status.idle": "2026-10-08T21:58:52.186493Z", + "shell.execute_reply": "2026-10-08T21:58:52.186100Z" + } + }, + "outputs": [], + "source": [ + "# initialize connection to pinecone (get API key at app.pinecone.io)\n", + "api_key = os.getenv(\"PINECONE_API_KEY\") or getpass(\"Enter your Pinecone API key: \")\n", + "\n", + "# configure client\n", + "pc = Pinecone(api_key=api_key)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we set up our index specification, which defines the cloud provider and region where we want to deploy our index. You can find a list of all [available providers and regions here](https://docs.pinecone.io/guides/index-data/create-an-index#cloud-regions)." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "execution": { + "iopub.execute_input": "2026-10-08T21:58:52.187247Z", + "iopub.status.busy": "2026-10-08T21:58:52.187199Z", + "iopub.status.idle": "2026-10-08T21:58:52.188651Z", + "shell.execute_reply": "2026-10-08T21:58:52.188282Z" + } + }, + "outputs": [], + "source": [ + "spec = ServerlessSpec(cloud=\"aws\", region=\"us-east-1\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Creating an index, we set `dimension` equal to the dimensionality of `text-embedding-3-small` (`1536`), and use a `metric` also compatible with it (`cosine`). We also pass our `spec` to index initialization." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "XMv6TXWkdfNR" - }, - "source": [ - "Our conversational agent needs a Chat LLM, conversational memory, and a `RetrievalQA` chain to initialize. We create these using:" - ] + "execution": { + "iopub.execute_input": "2026-10-08T21:58:52.189578Z", + "iopub.status.busy": "2026-10-08T21:58:52.189502Z", + "iopub.status.idle": "2026-10-08T21:58:59.017206Z", + "shell.execute_reply": "2026-10-08T21:58:59.016248Z" }, + "id": "C3wrG-9yaJel", + "outputId": "842bf46f-fd0f-4322-8d90-a7fd769b7687" + }, + "outputs": [ { - "cell_type": "code", - "execution_count": 13, - "metadata": { - "id": "zMRs9Klic5-Y" - }, - "outputs": [], - "source": [ - "from langchain.chat_models import ChatOpenAI\n", - "from langchain.chains.conversation.memory import ConversationBufferWindowMemory\n", - "from langchain.chains import RetrievalQA\n", - "\n", - "# chat completion llm\n", - "llm = ChatOpenAI(\n", - " openai_api_key=OPENAI_API_KEY,\n", - " model_name='gpt-3.5-turbo',\n", - " temperature=0.0\n", - ")\n", - "# conversational memory\n", - "conversational_memory = ConversationBufferWindowMemory(\n", - " memory_key='chat_history',\n", - " k=5,\n", - " return_messages=True\n", - ")\n", - "# retrieval qa chain\n", - "qa = RetrievalQA.from_chain_type(\n", - " llm=llm,\n", - " chain_type=\"stuff\",\n", - " retriever=vectorstore.as_retriever()\n", - ")" + "data": { + "text/plain": [ + "{'dimension': 1536,\n", + " 'index_fullness': 0.0,\n", + " 'metric': 'cosine',\n", + " 'namespaces': {},\n", + " 'total_vector_count': 0,\n", + " 'vector_type': 'dense'}" ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "index_name = \"langchain-retrieval-agent\"\n", + "\n", + "# check if index already exists (it shouldn't if this is first time)\n", + "if not pc.has_index(index_name):\n", + " # if does not exist, create index\n", + " pc.create_index(\n", + " index_name,\n", + " dimension=1536, # dimensionality of text-embedding-3-small\n", + " metric=\"cosine\",\n", + " spec=spec,\n", + " )\n", + "\n", + "# connect to index\n", + "index = pc.Index(index_name)\n", + "# view index stats\n", + "index.describe_index_stats()" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "AD5IGOoLaVx7" + }, + "source": [ + "We should see that the new Pinecone index has a `total_vector_count` of `0`, as we haven't added any vectors yet.\n", + "\n", + "## Indexing\n", + "\n", + "We can perform the indexing task using the LangChain vector store object. But for now it is much faster to do it via the Pinecone python client directly. We will do this in batches of `100` or more." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 49, + "referenced_widgets": [ + "321ebcd192fc41e0a7a2ba9c0e7b8556", + "7c898f43b39545879013df760aa4f6f1", + "1b5fe655986842a6afd4a2eb2c0f47fc", + "76872ca737b64616aeacc90f2f7fc655", + "0679733cce4f4b5eb25f8f56dc7ebe22", + "71551fc297b54fb4a21d787d117e01a1", + "b90283522431447b925b226431bb4fa6", + "a03d372e8b4f456e8663e54c818e0fe9", + "6fc773ccbc594fddbc1f2f0adaa96186", + "07b47869f727455fb5ba67e99767c04c", + "4b2e12a33584489690aaf69f51cd4a3b" + ] }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "-ySfWyZLdboX" - }, - "source": [ - "Using these we can generate an answer using the `run` method:" - ] + "execution": { + "iopub.execute_input": "2026-10-08T21:58:59.019789Z", + "iopub.status.busy": "2026-10-08T21:58:59.019497Z", + "iopub.status.idle": "2026-10-08T22:03:46.011541Z", + "shell.execute_reply": "2026-10-08T22:03:46.010432Z" }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 35 - }, - "id": "LaYSq0V-dxHw", - "outputId": "a85730a3-ab15-49b6-8aba-6da774fd3e3f" - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'The College of Engineering at the University of Notre Dame was established in 1920.'" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "qa.run(query)" - ] + "id": "AhDcbRGTaWPi", + "outputId": "dca5a4d0-5d19-4542-c26c-3073bfa13f2a" + }, + "outputs": [], + "source": [ + "batch_size = 100\n", + "\n", + "for i in tqdm(range(0, len(data), batch_size)):\n", + " # get end of batch\n", + " i_end = min(len(data), i + batch_size)\n", + " batch = data.iloc[i:i_end]\n", + " # first get metadata fields for this record\n", + " metadatas = [\n", + " {\"title\": record[\"title\"], \"text\": record[\"context\"]}\n", + " for j, record in batch.iterrows()\n", + " ]\n", + " # get the list of contexts / documents\n", + " documents = batch[\"context\"].tolist()\n", + " # create document embeddings\n", + " embeds = embed.embed_documents(documents)\n", + " # get IDs\n", + " ids = batch[\"id\"].tolist()\n", + " # add everything to pinecone\n", + " index.upsert(vectors=list(zip(ids, embeds, metadatas)))" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "jDUnLdy1b7G1" + }, + "source": [ + "We've indexed everything, now we can check the number of vectors in our index like so:" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "DtSXR5RXdyU0" - }, - "source": [ - "But this isn't yet ready for our conversational agent. For that we need to convert this retrieval chain into a tool. We do that like so:" - ] + "execution": { + "iopub.execute_input": "2026-10-08T22:03:46.015444Z", + "iopub.status.busy": "2026-10-08T22:03:46.015223Z", + "iopub.status.idle": "2026-10-08T22:03:46.142773Z", + "shell.execute_reply": "2026-10-08T22:03:46.141612Z" }, + "id": "SiccGZKAb_Qo", + "outputId": "5cafd8c8-771f-46d0-e261-12c9b5b1b058" + }, + "outputs": [ { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "id": "FwCYrS4duqBW" - }, - "outputs": [], - "source": [ - "from langchain.agents import Tool\n", - "\n", - "tools = [\n", - " Tool(\n", - " name='Knowledge Base',\n", - " func=qa.run,\n", - " description=(\n", - " 'use this tool when answering general knowledge queries to get '\n", - " 'more information about the topic'\n", - " )\n", - " )\n", - "]" + "data": { + "text/plain": [ + "{'dimension': 1536,\n", + " 'index_fullness': 0.0,\n", + " 'metric': 'cosine',\n", + " 'namespaces': {'': {'vector_count': 18891}},\n", + " 'total_vector_count': 18891,\n", + " 'vector_type': 'dense'}" ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "index.describe_index_stats()" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "b-3oolT5cCR8" + }, + "source": [ + "## Creating a Vector Store and Querying" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "DcZ12U06cCH5" + }, + "source": [ + "Now that we've build our index we can switch back over to LangChain. We start by initializing a vector store using the same index we just built. We do that like so:" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "execution": { + "iopub.execute_input": "2026-10-08T22:03:46.144701Z", + "iopub.status.busy": "2026-10-08T22:03:46.144546Z", + "iopub.status.idle": "2026-10-08T22:03:46.148562Z", + "shell.execute_reply": "2026-10-08T22:03:46.147408Z" }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "wXi_0ipTvM_l" - }, - "source": [ - "Now we can initialize the agent like so:" - ] + "id": "0MBJ477-cFNw" + }, + "outputs": [], + "source": [ + "text_field = \"text\" # the metadata field that contains our text\n", + "\n", + "# initialize the vector store object\n", + "vectorstore = PineconeVectorStore(index=index, embedding=embed, text_key=text_field)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "3K3xRthWcXzW" + }, + "source": [ + "As in previous examples, we can use the `similarity_search` method to do a pure semantic search (without the generation component)." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": { - "id": "JaKTzPUEvOoy" - }, - "outputs": [], - "source": [ - "from langchain.agents import initialize_agent\n", - "\n", - "agent = initialize_agent(\n", - " agent='chat-conversational-react-description',\n", - " tools=tools,\n", - " llm=llm,\n", - " verbose=True,\n", - " max_iterations=3,\n", - " early_stopping_method='generate',\n", - " memory=conversational_memory\n", - ")" - ] + "execution": { + "iopub.execute_input": "2026-10-08T22:03:46.150600Z", + "iopub.status.busy": "2026-10-08T22:03:46.150436Z", + "iopub.status.idle": "2026-10-08T22:03:46.524641Z", + "shell.execute_reply": "2026-10-08T22:03:46.523935Z" }, + "id": "uITMZtzschJF", + "outputId": "88fae02f-6f82-47d7-a153-21b3e0615709" + }, + "outputs": [ { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "WbXl-AzVvszB" - }, - "source": [ - "With that our retrieval augmented conversational agent is ready and we can begin using it." + "data": { + "text/plain": [ + "[Document(id='57338724d058e614000b5c9f', metadata={'title': 'University_of_Notre_Dame'}, page_content=\"In 1919 Father James Burns became president of Notre Dame, and in three years he produced an academic revolution that brought the school up to national standards by adopting the elective system and moving away from the university's traditional scholastic and classical emphasis. By contrast, the Jesuit colleges, bastions of academic conservatism, were reluctant to move to a system of electives. Their graduates were shut out of Harvard Law School for that reason. Notre Dame continued to grow over the years, adding more colleges, programs, and sports teams. By 1921, with the addition of the College of Commerce, Notre Dame had grown from a small college to a university with five colleges and a professional law school. The university continued to expand and add new residence halls and buildings with each subsequent president.\"),\n", + " Document(id='573393e1d058e614000b5dc2', metadata={'title': 'University_of_Notre_Dame'}, page_content='Hesburgh is also credited with transforming the face of Notre Dame by making it a coeducational institution. In the mid-1960s Notre Dame and Saint Mary\\'s College developed a co-exchange program whereby several hundred students took classes not offered at their home institution, an arrangement that added undergraduate women to a campus that already had a few women in the graduate schools. After extensive debate, merging with St. Mary\\'s was rejected, primarily because of the differential in faculty qualifications and pay scales. \"In American college education,\" explained the Rev. Charles E. Sheedy, C.S.C., Notre Dame\\'s Dean of Arts and Letters, \"certain features formerly considered advantageous and enviable are now seen as anachronistic and out of place.... In this environment of diversity, the integration of the sexes is a normal and expected aspect, replacing separatism.\" Thomas Blantz, C.S.C., Notre Dame\\'s Vice President of Student Affairs, added that coeducation \"opened up a whole other pool of very bright students.\" Two of the male residence halls were converted for the newly admitted female students that first year, while two others were converted for the next school year. In 1971 Mary Ann Proctor became the first female undergraduate; she transferred from St. Mary\\'s College. In 1972 the first woman to graduate was Angela Sienko, who earned a bachelor\\'s degree in marketing.'),\n", + " Document(id='5733a6424776f41900660f51', metadata={'title': 'University_of_Notre_Dame'}, page_content='The College of Engineering was established in 1920, however, early courses in civil and mechanical engineering were a part of the College of Science since the 1870s. Today the college, housed in the Fitzpatrick, Cushing, and Stinson-Remick Halls of Engineering, includes five departments of study – aerospace and mechanical engineering, chemical and biomolecular engineering, civil engineering and geological sciences, computer science and engineering, and electrical engineering – with eight B.S. degrees offered. Additionally, the college offers five-year dual degree programs with the Colleges of Arts and Letters and of Business awarding additional B.A. and Master of Business Administration (MBA) degrees, respectively.')]" ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "query = (\n", + " \"when was the college of engineering in the University of Notre Dame established?\"\n", + ")\n", + "\n", + "vectorstore.similarity_search(\n", + " query, # our search query\n", + " k=3, # return 3 most relevant docs\n", + ")" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "-zGF6YsgczqT" + }, + "source": [ + "Looks like we're getting good results. Let's take a look at how we can begin integrating this into a conversational agent." + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "tFsIOm73dcOI" + }, + "source": [ + "## Initializing the Conversational Agent" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "XMv6TXWkdfNR" + }, + "source": [ + "Our conversational agent needs a chat LLM, a tool that retrieves context from our knowledge base, and conversational memory. We start with the LLM and the retrieval tool:" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "execution": { + "iopub.execute_input": "2026-10-08T22:03:46.526930Z", + "iopub.status.busy": "2026-10-08T22:03:46.526787Z", + "iopub.status.idle": "2026-10-08T22:03:46.822669Z", + "shell.execute_reply": "2026-10-08T22:03:46.821798Z" }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "IlxUBWKcvzeP" - }, - "source": [ - "### Using the Conversational Agent" - ] + "id": "zMRs9Klic5-Y" + }, + "outputs": [], + "source": [ + "# chat completion llm\n", + "llm = ChatOpenAI(\n", + " openai_api_key=OPENAI_API_KEY, model_name=\"gpt-4o-mini\", temperature=0.0\n", + ")\n", + "\n", + "\n", + "# retrieval tool\n", + "@tool\n", + "def knowledge_base(query: str) -> str:\n", + " \"\"\"Use this tool when answering general knowledge queries to get more information about the topic.\"\"\"\n", + " docs = vectorstore.similarity_search(query, k=3)\n", + " return \"\\n\\n\".join(doc.page_content for doc in docs)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "-ySfWyZLdboX" + }, + "source": [ + "The agent decides when to call the tool, and the tool's docstring tells it what the tool is for. We can call the tool ourselves to see what context it returns:" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "ZZapCP4Pv2kz" - }, - "source": [ - "To make queries we simply call the `agent` directly." - ] + "execution": { + "iopub.execute_input": "2026-10-08T22:03:46.824727Z", + "iopub.status.busy": "2026-10-08T22:03:46.824567Z", + "iopub.status.idle": "2026-10-08T22:03:47.125606Z", + "shell.execute_reply": "2026-10-08T22:03:47.124612Z" }, + "id": "LaYSq0V-dxHw", + "outputId": "a85730a3-ab15-49b6-8aba-6da774fd3e3f" + }, + "outputs": [ { - "cell_type": "code", - "execution_count": 17, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "RJoAhy76vzAB", - "outputId": "31ae7589-e66a-4835-d1d8-555bb59c962c" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3m{\n", - " \"action\": \"Knowledge Base\",\n", - " \"action_input\": \"When was the College of Engineering in the University of Notre Dame established?\"\n", - "}\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3mThe College of Engineering at the University of Notre Dame was established in 1920.\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m{\n", - " \"action\": \"Final Answer\",\n", - " \"action_input\": \"The College of Engineering at the University of Notre Dame was established in 1920.\"\n", - "}\u001b[0m\n", - "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - }, - { - "data": { - "text/plain": [ - "{'input': 'when was the college of engineering in the University of Notre Dame established?',\n", - " 'chat_history': [],\n", - " 'output': 'The College of Engineering at the University of Notre Dame was established in 1920.'}" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "agent(query)" - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "In 1919 Father James Burns became president of Notre Dame, and in three years he produced an academic revolution that brought the school up to national standards by adopting the elective system and moving away from the university's traditional scholastic and classical emphasis. By contrast, the Jesuit colleges, bastions of academic conservatism, were reluctant to move to a system of electives. Their graduates were shut out of Harvard Law School for that reason. Notre Dame continued to grow over the years, adding more colleges, programs, and sports teams. By 1921, with the addition of the College of Commerce, Notre Dame had grown from a small college to a university with five colleges and a professional law school. The university continued to expand and add new residence halls and buildings with each subsequent president.\n", + "\n", + "Hesburgh is also credited with transforming the face of Notre Dame by making it a coeducational institution. In the mid-1960s Notre Dame and Saint Mary's College developed a co-exchange program whereby several hundred students took classes not offered at their home institution, an arrangement that added undergraduate women to a campus that already had a few women in the graduate schools. After extensive debate, merging with St. Mary's was rejected, primarily because of the differential in faculty qualifications and pay scales. \"In American college education,\" explained the Rev. Charles E. Sheedy, C.S.C., Notre Dame's Dean of Arts and Letters, \"certain features formerly considered advantageous and enviable are now seen as anachronistic and out of place.... In this environment of diversity, the integration of the sexes is a normal and expected aspect, replacing separatism.\" Thomas Blantz, C.S.C., Notre Dame's Vice President of Student Affairs, added that coeducation \"opened up a whole other pool of very bright students.\" Two of the male residence halls were converted for the newly admitted female students that first year, while two others were converted for the next school year. In 1971 Mary Ann Proctor became the first female undergraduate; she transferred from St. Mary's College. In 1972 the first woman to graduate was Angela Sienko, who earned a bachelor's degree in marketing.\n", + "\n", + "The College of Engineering was established in 1920, however, early courses in civil and mechanical engineering were a part of the College of Science since the 1870s. Today the college, housed in the Fitzpatrick, Cushing, and Stinson-Remick Halls of Engineering, includes five departments of study – aerospace and mechanical engineering, chemical and biomolecular engineering, civil engineering and geological sciences, computer science and engineering, and electrical engineering – with eight B.S. degrees offered. Additionally, the college offers five-year dual degree programs with the Colleges of Arts and Letters and of Business awarding additional B.A. and Master of Business Administration (MBA) degrees, respectively.\n" + ] + } + ], + "source": [ + "print(knowledge_base.invoke(query))" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "DtSXR5RXdyU0" + }, + "source": [ + "Now we can initialize the agent. The `InMemorySaver` checkpointer gives the agent conversational memory: every call that uses the same `thread_id` sees the previous messages in that conversation." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "execution": { + "iopub.execute_input": "2026-10-08T22:03:47.127844Z", + "iopub.status.busy": "2026-10-08T22:03:47.127624Z", + "iopub.status.idle": "2026-10-08T22:03:47.135197Z", + "shell.execute_reply": "2026-10-08T22:03:47.134111Z" }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "YcMqa9Va2hU6" - }, - "source": [ - "Looks great, now what if we ask it a non-general knowledge question?" - ] + "id": "FwCYrS4duqBW" + }, + "outputs": [], + "source": [ + "agent = create_agent(\n", + " model=llm,\n", + " tools=[knowledge_base],\n", + " system_prompt=\"You are a helpful assistant. Use the knowledge base tool for general knowledge questions.\",\n", + " checkpointer=InMemorySaver(),\n", + ")\n", + "\n", + "config = {\"configurable\": {\"thread_id\": \"retrieval-agent-demo\"}}" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "wXi_0ipTvM_l" + }, + "source": [ + "We'll also define a small helper that sends a message to the agent and prints every message from that turn, including any tool calls, so we can see when the agent uses the knowledge base:" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "execution": { + "iopub.execute_input": "2026-10-08T22:03:47.137005Z", + "iopub.status.busy": "2026-10-08T22:03:47.136870Z", + "iopub.status.idle": "2026-10-08T22:03:47.139730Z", + "shell.execute_reply": "2026-10-08T22:03:47.139149Z" }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "85vipqC02deV", - "outputId": "345c724e-eaea-4a20-9445-99794bc743fe" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3m{\n", - " \"action\": \"Final Answer\",\n", - " \"action_input\": \"The product of 2 multiplied by 7 is 14.\"\n", - "}\u001b[0m\n", - "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - }, - { - "data": { - "text/plain": [ - "{'input': 'what is 2 * 7?',\n", - " 'chat_history': [HumanMessage(content='when was the college of engineering in the University of Notre Dame established?'),\n", - " AIMessage(content='The College of Engineering at the University of Notre Dame was established in 1920.')],\n", - " 'output': 'The product of 2 multiplied by 7 is 14.'}" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "agent(\"what is 2 * 7?\")" - ] + "id": "JaKTzPUEvOoy" + }, + "outputs": [], + "source": [ + "def chat(message: str):\n", + " result = agent.invoke(\n", + " {\"messages\": [{\"role\": \"user\", \"content\": message}]},\n", + " config=config,\n", + " )\n", + " messages = result[\"messages\"]\n", + " # this turn starts at the last user message\n", + " start = max(i for i, m in enumerate(messages) if m.type == \"human\")\n", + " for m in messages[start:]:\n", + " m.pretty_print()" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "WbXl-AzVvszB" + }, + "source": [ + "With that our retrieval augmented conversational agent is ready and we can begin using it." + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "IlxUBWKcvzeP" + }, + "source": [ + "### Using the Conversational Agent" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "ZZapCP4Pv2kz" + }, + "source": [ + "To make queries we call our `chat` helper." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "gR_b0IN32rQ9" - }, - "source": [ - "Perfect, the agent is able to recognize that it doesn't need to refer to it's general knowledge tool for that question. Let's try some more questions." - ] + "execution": { + "iopub.execute_input": "2026-10-08T22:03:47.141432Z", + "iopub.status.busy": "2026-10-08T22:03:47.141309Z", + "iopub.status.idle": "2026-10-08T22:03:48.804294Z", + "shell.execute_reply": "2026-10-08T22:03:48.803602Z" }, + "id": "RJoAhy76vzAB", + "outputId": "31ae7589-e66a-4835-d1d8-555bb59c962c" + }, + "outputs": [ { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "mQeicHTj2pmY", - "outputId": "9810647d-0846-4ca8-ae2e-1b249ca4ad3d" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3m{\n", - " \"action\": \"Knowledge Base\",\n", - " \"action_input\": \"University of Notre Dame\"\n", - "}\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3mThe University of Notre Dame is a Catholic research university located in South Bend, Indiana, in the United States. It is known for its strong academic programs, including undergraduate colleges in Arts and Letters, Science, Engineering, Business, and the Architecture School. The university also has a graduate program with over 50 master's, doctoral, and professional degree programs. Notre Dame is recognized as one of the top universities in the United States and has a strong alumni network. It is also known for its iconic landmarks, such as the Golden Dome and the Basilica. The university is committed to research and has various institutes dedicated to different fields of study. Notre Dame is also home to the Notre Dame Global Adaptation Index, which ranks countries based on their vulnerability to climate change.\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m{\n", - " \"action\": \"Final Answer\",\n", - " \"action_input\": \"The University of Notre Dame is a Catholic research university located in South Bend, Indiana. It offers strong academic programs in various fields, including Arts and Letters, Science, Engineering, Business, and Architecture. Notre Dame is known for its academic excellence, iconic landmarks like the Golden Dome and the Basilica, and its commitment to research. It is also home to the Notre Dame Global Adaptation Index, which ranks countries based on their vulnerability to climate change.\"\n", - "}\u001b[0m\n", - "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - }, - { - "data": { - "text/plain": [ - "{'input': 'can you tell me some facts about the University of Notre Dame?',\n", - " 'chat_history': [HumanMessage(content='when was the college of engineering in the University of Notre Dame established?'),\n", - " AIMessage(content='The College of Engineering at the University of Notre Dame was established in 1920.'),\n", - " HumanMessage(content='what is 2 * 7?'),\n", - " AIMessage(content='The product of 2 multiplied by 7 is 14.')],\n", - " 'output': 'The University of Notre Dame is a Catholic research university located in South Bend, Indiana. It offers strong academic programs in various fields, including Arts and Letters, Science, Engineering, Business, and Architecture. Notre Dame is known for its academic excellence, iconic landmarks like the Golden Dome and the Basilica, and its commitment to research. It is also home to the Notre Dame Global Adaptation Index, which ranks countries based on their vulnerability to climate change.'}" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "agent(\"can you tell me some facts about the University of Notre Dame?\")" - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "================================\u001b[1m Human Message \u001b[0m=================================\n", + "\n", + "when was the college of engineering in the University of Notre Dame established?\n", + "==================================\u001b[1m Ai Message \u001b[0m==================================\n", + "Tool Calls:\n", + " knowledge_base (call_ut03wg2ptYJDqkPUveHHm7vb)\n", + " Call ID: call_ut03wg2ptYJDqkPUveHHm7vb\n", + " Args:\n", + " query: College of Engineering University of Notre Dame establishment date\n", + "=================================\u001b[1m Tool Message \u001b[0m=================================\n", + "Name: knowledge_base\n", + "\n", + "In 1919 Father James Burns became president of Notre Dame, and in three years he produced an academic revolution that brought the school up to national standards by adopting the elective system and moving away from the university's traditional scholastic and classical emphasis. By contrast, the Jesuit colleges, bastions of academic conservatism, were reluctant to move to a system of electives. Their graduates were shut out of Harvard Law School for that reason. Notre Dame continued to grow over the years, adding more colleges, programs, and sports teams. By 1921, with the addition of the College of Commerce, Notre Dame had grown from a small college to a university with five colleges and a professional law school. The university continued to expand and add new residence halls and buildings with each subsequent president.\n", + "\n", + "Hesburgh is also credited with transforming the face of Notre Dame by making it a coeducational institution. In the mid-1960s Notre Dame and Saint Mary's College developed a co-exchange program whereby several hundred students took classes not offered at their home institution, an arrangement that added undergraduate women to a campus that already had a few women in the graduate schools. After extensive debate, merging with St. Mary's was rejected, primarily because of the differential in faculty qualifications and pay scales. \"In American college education,\" explained the Rev. Charles E. Sheedy, C.S.C., Notre Dame's Dean of Arts and Letters, \"certain features formerly considered advantageous and enviable are now seen as anachronistic and out of place.... In this environment of diversity, the integration of the sexes is a normal and expected aspect, replacing separatism.\" Thomas Blantz, C.S.C., Notre Dame's Vice President of Student Affairs, added that coeducation \"opened up a whole other pool of very bright students.\" Two of the male residence halls were converted for the newly admitted female students that first year, while two others were converted for the next school year. In 1971 Mary Ann Proctor became the first female undergraduate; she transferred from St. Mary's College. In 1972 the first woman to graduate was Angela Sienko, who earned a bachelor's degree in marketing.\n", + "\n", + "Since the construction of its oldest buildings, the university's physical plant has grown substantially. Over the years 29 residence halls have been built to accommodate students and each has been constructed with its own chapel. Many academic building were added together with a system of libraries, the most prominent of which is the Theodore Hesburgh Library, built in 1963 and today containing almost 4 million books. Since 2004, several buildings have been added, including the DeBartolo Performing Arts Center, the Guglielmino Complex, and the Jordan Hall of Science. Additionally, a new residence for men, Duncan Hall, was begun on March 8, 2007, and began accepting residents for the Fall 2008 semester. Ryan Hall was completed and began housing undergraduate women in the fall of 2009. A new engineering building, Stinson-Remick Hall, a new combination Center for Social Concerns/Institute for Church Life building, Geddes Hall, and a law school addition have recently been completed as well. Additionally the new hockey arena opened in the fall of 2011. The Stayer Center for Executive Education, which houses the Mendoza College of Business Executive Education Department opened in March 2013 just South of the Mendoza College of Business building. Because of its long athletic tradition, the university features also many building dedicated to sport. The most famous is Notre Dame Stadium, home of the Fighting Irish football team; it has been renovated several times and today it can hold more than 80 thousand people. Prominent venues include also the Edmund P. Joyce Center, with indoor basketball and volleyball courts, and the Compton Family Ice Arena, a two-rink facility dedicated to hockey. Also, there are many outdoor fields, as the Frank Eck Stadium for baseball.\n", + "==================================\u001b[1m Ai Message \u001b[0m==================================\n", + "\n", + "The College of Engineering at the University of Notre Dame was established in 1920.\n" + ] + } + ], + "source": [ + "chat(query)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "YcMqa9Va2hU6" + }, + "source": [ + "Looks great, now what if we ask it a non-general knowledge question?" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "G93vLXso3B5Z", - "outputId": "cb345147-5630-4a5a-bb9f-242ad91d9968" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3m{\n", - " \"action\": \"Final Answer\",\n", - " \"action_input\": \"The University of Notre Dame is a Catholic research university located in South Bend, Indiana. It offers strong academic programs and is known for its iconic landmarks and commitment to research.\"\n", - "}\u001b[0m\n", - "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - }, - { - "data": { - "text/plain": [ - "{'input': 'can you summarize these facts in two short sentences',\n", - " 'chat_history': [HumanMessage(content='when was the college of engineering in the University of Notre Dame established?'),\n", - " AIMessage(content='The College of Engineering at the University of Notre Dame was established in 1920.'),\n", - " HumanMessage(content='what is 2 * 7?'),\n", - " AIMessage(content='The product of 2 multiplied by 7 is 14.'),\n", - " HumanMessage(content='can you tell me some facts about the University of Notre Dame?'),\n", - " AIMessage(content='The University of Notre Dame is a Catholic research university located in South Bend, Indiana. It offers strong academic programs in various fields, including Arts and Letters, Science, Engineering, Business, and Architecture. Notre Dame is known for its academic excellence, iconic landmarks like the Golden Dome and the Basilica, and its commitment to research. It is also home to the Notre Dame Global Adaptation Index, which ranks countries based on their vulnerability to climate change.')],\n", - " 'output': 'The University of Notre Dame is a Catholic research university located in South Bend, Indiana. It offers strong academic programs and is known for its iconic landmarks and commitment to research.'}" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "agent(\"can you summarize these facts in two short sentences\")" - ] + "execution": { + "iopub.execute_input": "2026-10-08T22:03:48.806953Z", + "iopub.status.busy": "2026-10-08T22:03:48.806763Z", + "iopub.status.idle": "2026-10-08T22:03:49.515792Z", + "shell.execute_reply": "2026-10-08T22:03:49.514723Z" }, + "id": "85vipqC02deV", + "outputId": "345c724e-eaea-4a20-9445-99794bc743fe" + }, + "outputs": [ { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "PWivmw9F3bCw" - }, - "source": [ - "Looks great! We're also able to ask questions that refer to previous interactions in the conversation and the agent is able to refer to the conversation history to as a source of information.\n", - "\n", - "That's all for this example of building a retrieval augmented conversational agent with OpenAI and Pinecone (the OP stack) and LangChain.\n", - "\n", - "Once finished, we delete the Pinecone index to save resources:" - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "================================\u001b[1m Human Message \u001b[0m=================================\n", + "\n", + "what is 2 * 7?\n", + "==================================\u001b[1m Ai Message \u001b[0m==================================\n", + "\n", + "2 * 7 equals 14.\n" + ] + } + ], + "source": [ + "chat(\"what is 2 * 7?\")" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "gR_b0IN32rQ9" + }, + "source": [ + "The agent recognizes that it doesn't need to use its knowledge base tool for that question. Let's try some more questions." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "id": "Pa1whr8V3Wfm" - }, - "outputs": [], - "source": [ - "pc.delete_index(index_name)" - ] + "execution": { + "iopub.execute_input": "2026-10-08T22:03:49.518292Z", + "iopub.status.busy": "2026-10-08T22:03:49.518098Z", + "iopub.status.idle": "2026-10-08T22:03:53.644871Z", + "shell.execute_reply": "2026-10-08T22:03:53.644025Z" }, + "id": "mQeicHTj2pmY", + "outputId": "9810647d-0846-4ca8-ae2e-1b249ca4ad3d" + }, + "outputs": [ { - "attachments": {}, - "cell_type": "markdown", - "metadata": { - "id": "Ykg5TYA033yR" - }, - "source": [ - "---" - ] + "name": "stdout", + "output_type": "stream", + "text": [ + "================================\u001b[1m Human Message \u001b[0m=================================\n", + "\n", + "can you tell me some facts about the University of Notre Dame?\n", + "==================================\u001b[1m Ai Message \u001b[0m==================================\n", + "Tool Calls:\n", + " knowledge_base (call_cuZmeF4NjJgKnEzIZrtFpRsu)\n", + " Call ID: call_cuZmeF4NjJgKnEzIZrtFpRsu\n", + " Args:\n", + " query: facts about the University of Notre Dame\n", + "=================================\u001b[1m Tool Message \u001b[0m=================================\n", + "Name: knowledge_base\n", + "\n", + "Besides its prominence in sports, Notre Dame is also a large, four-year, highly residential research University, and is consistently ranked among the top twenty universities in the United States and as a major global university. The undergraduate component of the university is organized into four colleges (Arts and Letters, Science, Engineering, Business) and the Architecture School. The latter is known for teaching New Classical Architecture and for awarding the globally renowned annual Driehaus Architecture Prize. Notre Dame's graduate program has more than 50 master's, doctoral and professional degree programs offered by the five schools, with the addition of the Notre Dame Law School and a MD-PhD program offered in combination with IU medical School. It maintains a system of libraries, cultural venues, artistic and scientific museums, including Hesburgh Library and the Snite Museum of Art. Over 80% of the university's 8,000 undergraduates live on campus in one of 29 single-sex residence halls, each with its own traditions, legacies, events and intramural sports teams. The university counts approximately 120,000 alumni, considered among the strongest alumni networks among U.S. colleges.\n", + "\n", + "Notre Dame is known for its competitive admissions, with the incoming class enrolling in fall 2015 admitting 3,577 from a pool of 18,156 (19.7%). The academic profile of the enrolled class continues to rate among the top 10 to 15 in the nation for national research universities. The university practices a non-restrictive early action policy that allows admitted students to consider admission to Notre Dame as well as any other colleges to which they were accepted. 1,400 of the 3,577 (39.1%) were admitted under the early action plan. Admitted students came from 1,311 high schools and the average student traveled more than 750 miles to Notre Dame, making it arguably the most representative university in the United States. While all entering students begin in the College of the First Year of Studies, 25% have indicated they plan to study in the liberal arts or social sciences, 24% in engineering, 24% in business, 24% in science, and 3% in architecture.\n", + "\n", + "The University of Notre Dame du Lac (or simply Notre Dame /ˌnoʊtərˈdeɪm/ NOH-tər-DAYM) is a Catholic research university located adjacent to South Bend, Indiana, in the United States. In French, Notre Dame du Lac means \"Our Lady of the Lake\" and refers to the university's patron saint, the Virgin Mary. The main campus covers 1,250 acres in a suburban setting and it contains a number of recognizable landmarks, such as the Golden Dome, the \"Word of Life\" mural (commonly known as Touchdown Jesus), and the Basilica.\n", + "==================================\u001b[1m Ai Message \u001b[0m==================================\n", + "\n", + "Here are some facts about the University of Notre Dame:\n", + "\n", + "1. **Type of Institution**: Notre Dame is a large, four-year, highly residential research university, consistently ranked among the top twenty universities in the United States.\n", + "\n", + "2. **Colleges and Programs**: The undergraduate component is organized into four colleges: Arts and Letters, Science, Engineering, and Business, along with a School of Architecture known for New Classical Architecture.\n", + "\n", + "3. **Graduate Programs**: Notre Dame offers more than 50 master's, doctoral, and professional degree programs, including a law school and a combined MD-PhD program with Indiana University School of Medicine.\n", + "\n", + "4. **Campus Life**: Over 80% of the university's approximately 8,000 undergraduates live on campus in one of 29 single-sex residence halls, each with its own traditions and events.\n", + "\n", + "5. **Alumni Network**: The university has around 120,000 alumni, known for having one of the strongest alumni networks among U.S. colleges.\n", + "\n", + "6. **Admissions**: Notre Dame has competitive admissions, with a 19.7% acceptance rate for the incoming class in fall 2015. The university practices a non-restrictive early action policy.\n", + "\n", + "7. **Campus Size and Landmarks**: The main campus covers 1,250 acres and features recognizable landmarks such as the Golden Dome, the \"Word of Life\" mural (often referred to as Touchdown Jesus), and the Basilica.\n", + "\n", + "8. **Patron Saint**: The university's name, \"Notre Dame du Lac,\" translates to \"Our Lady of the Lake\" in French, honoring the Virgin Mary as its patron saint.\n", + "\n", + "These facts highlight the university's academic reputation, campus life, and cultural significance.\n" + ] } - ], - "metadata": { + ], + "source": [ + "chat(\"can you tell me some facts about the University of Notre Dame?\")" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { "colab": { - "provenance": [] + "base_uri": "https://localhost:8080/" }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" + "execution": { + "iopub.execute_input": "2026-10-08T22:03:53.646977Z", + "iopub.status.busy": "2026-10-08T22:03:53.646802Z", + "iopub.status.idle": "2026-10-08T22:03:54.552904Z", + "shell.execute_reply": "2026-10-08T22:03:54.551962Z" }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.12" + "id": "G93vLXso3B5Z", + "outputId": "cb345147-5630-4a5a-bb9f-242ad91d9968" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "================================\u001b[1m Human Message \u001b[0m=================================\n", + "\n", + "can you summarize these facts in two short sentences\n", + "==================================\u001b[1m Ai Message \u001b[0m==================================\n", + "\n", + "The University of Notre Dame is a prestigious Catholic research university located in South Bend, Indiana, known for its strong academic programs across various colleges and a vibrant campus life with 29 residence halls. It has a competitive admissions process, a large and active alumni network, and notable landmarks such as the Golden Dome and the Basilica.\n" + ] } + ], + "source": [ + "chat(\"can you summarize these facts in two short sentences\")" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "PWivmw9F3bCw" + }, + "source": [ + "Looks great! We're also able to ask questions that refer to previous interactions in the conversation and the agent is able to refer to the conversation history to as a source of information.\n", + "\n", + "That's all for this example of building a retrieval augmented conversational agent with OpenAI and Pinecone (the OP stack) and LangChain.\n", + "\n", + "Once finished, we delete the Pinecone index to save resources:" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "execution": { + "iopub.execute_input": "2026-10-08T22:03:54.554950Z", + "iopub.status.busy": "2026-10-08T22:03:54.554596Z", + "iopub.status.idle": "2026-10-08T22:04:00.033983Z", + "shell.execute_reply": "2026-10-08T22:04:00.032920Z" + }, + "id": "Pa1whr8V3Wfm" + }, + "outputs": [], + "source": [ + "pc.delete_index(index_name)" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": { + "id": "Ykg5TYA033yR" + }, + "source": [ + "---" + ] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" }, - "nbformat": 4, - "nbformat_minor": 0 -} \ No newline at end of file + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.13" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}