{ "cells": [ { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loading settings from ../../env/ai.json\n" ] } ], "source": [ "import os\n", "import json\n", "\n", "# If the file does not exist it'll default to the manual setting see below\n", "filePathToSettingsFile = '../../env/ai.json'\n", "\n", "# Is there a settings file? \n", "if os.path.exists(filePathToSettingsFile):\n", " # Yes there is so load settings from there\n", " \n", " print(f'Loading settings from {filePathToSettingsFile}')\n", " f = open(filePathToSettingsFile)\n", " settingsJson = json.load(f)\n", " del f\n", "\n", " for key in settingsJson:\n", " os.environ[key] = settingsJson[key]\n", " \n", " del settingsJson\n", "else: \n", " # Set variables manually\n", " \n", " print('Setting variables manually as there is not ai.json settings file')\n", "\n", " # Update the variables below with your own settings\n", " os.environ['REQUESTS_CA_BUNDLE'] = '../../env/ZCert.pem' \n", " os.environ['HUGGING_FACE_API_KEY'] = 'Get here: https://huggingface.co/settings/tokens'\n", " os.environ['OPENAI_API_KEY'] = 'Get here: https://platform.openai.com/account/api-keys'\n", " os.environ[\"SERPAPI_API_KEY\"] = 'serpapi KEY, Get here: https://serpapi.com/manage-api-key' " ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "from langchain.vectorstores import Pinecone\n", "from langchain.embeddings import OpenAIEmbeddings\n", "import pinecone\n", "\n", "embeddings = OpenAIEmbeddings(openai_api_key=os.environ['OPENAI_API_KEY'])\n", "pinecone.init(api_key=os.environ['PINECONE_API_KEY_2'], environment=os.environ['PINECONE_API_ENV_2'])" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using found index: rag-demo-1-history-rome\n" ] } ], "source": [ "# text_field = \"text\"\n", "\n", "pinecone.list_indexes()\n", "index_name = pinecone.list_indexes()[0]\n", "print(f\"Using found index: {index_name}\")\n", "\n", "index = pinecone.Index(index_name)\n", "vectorstore = Pinecone(index, embeddings.embed_query, \"text\")" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "query = \"When was Ceasar born?\"\n", "\n", "result = vectorstore.similarity_search(\n", " query, # our search query\n", " k=1 # return 3 most relevant docs\n", ")" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "page_content='The situation in Rome. While Pompey was adding to his\\nmilitary reputation in the East he was regarded with jealous\\nand anxious eyes not only by the Senate but also by the other\\nchampions of the popular party, Crassus who found his wealth\\nno match for Pompeys military achievements, and Caius Julius\\nCaesar who was rapidly coming to be one of the leading figures in\\nRoman public life. Caesar was born in 100 B. C., of the patrician [162]\\ngens of the Julii, but since his aunt was the wife of Marius,\\nand he himself had married the daughter of Cinna, his lot was\\ncast with the Populares. As a young man he had distinguished\\nhimself by refusing to divorce his wife at Sullas behest, whereat\\nSulla was with difficulty induced to spare his life, saying that he\\nsaw in him many a Marius. For the time being Caesar judged it\\nprudent to withdraw from Rome to Rhodes. While in the East\\nhe was captured by pirates, and after being ransomed, fulfilled' metadata={'source': '..\\\\rag-demo-1-data\\\\history-roman\\\\3. A History of Rome to 565 A. D. author Arthur Edward Romilly Boak.pdf.txt'}\n" ] } ], "source": [ "print(len(result))\n", "\n", "print(result[0])\n", "# print('##')\n", "# print(result[1])\n", "# print('##')\n", "# print(result[2])" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "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.10.6" } }, "nbformat": 4, "nbformat_minor": 2 }