File size: 4,849 Bytes
8d8cebe
 
 
 
57fbc22
8d8cebe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57fbc22
8d8cebe
 
 
 
 
 
 
57fbc22
 
8d8cebe
 
 
 
8083b67
8d8cebe
57fbc22
 
 
 
 
8083b67
57fbc22
 
 
8d8cebe
8083b67
8d8cebe
57fbc22
 
 
 
8d8cebe
8083b67
8d8cebe
 
 
 
8083b67
8d8cebe
 
 
 
 
 
 
8083b67
8d8cebe
 
 
 
 
8083b67
8d8cebe
 
 
 
 
 
8083b67
 
8d8cebe
 
 
 
 
 
 
8083b67
 
 
 
8d8cebe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
{
 "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
}