thrag commited on
Commit
8083b67
1 Parent(s): 57fbc22

Got conversational agent working with OpenAI

Browse files
Files changed (2) hide show
  1. app.ipynb +105 -9
  2. vector-stores-query.ipynb +13 -27
app.ipynb CHANGED
@@ -78,15 +78,15 @@
78
  },
79
  {
80
  "cell_type": "code",
81
- "execution_count": 3,
82
  "metadata": {},
83
  "outputs": [
84
  {
85
- "name": "stderr",
86
  "output_type": "stream",
87
  "text": [
88
- "c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\langchain\\vectorstores\\pinecone.py:59: UserWarning: Passing in `embedding` as a Callable is deprecated. Please pass in an Embeddings object instead.\n",
89
- " warnings.warn(\n"
90
  ]
91
  }
92
  ],
@@ -95,24 +95,120 @@
95
  "\n",
96
  "course = \"Roman History\"\n",
97
  "if course == \"Roman History\":\n",
98
- " print(f\"Using course: {}\")\n",
99
  " pinecone.init(api_key=os.environ[\"PINECONE_API_KEY_2\"], environment=os.environ[\"PINECONE_API_ENV_2\"])\n",
100
  " index = pinecone.Index(\"rag-demo-1-history-rome\")\n",
101
- " vector_store = Pinecone(index, embeddings.embed_query, \"text\")\n",
102
- " "
 
 
 
 
 
 
 
 
103
  ]
104
  },
105
  {
106
  "cell_type": "code",
107
- "execution_count": 5,
108
  "metadata": {},
109
- "outputs": [],
 
 
 
 
 
 
 
 
 
 
 
110
  "source": [
111
  "llm = OpenAI(temperature=0)\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  "qa = RetrievalQA.from_chain_type(llm=llm, chain_type=\"stuff\", retriever=vector_store.as_retriever())\n",
113
  "\n",
114
  "query = input(\"Who was ceasar and why whas he noteable?\")\n",
115
  "answer = qa.run(query)\n",
 
 
 
 
 
116
  "# print(answer)\n",
117
  "# print(\"\\nWhat else can I help you with:\")\n",
118
  "\n",
 
78
  },
79
  {
80
  "cell_type": "code",
81
+ "execution_count": 9,
82
  "metadata": {},
83
  "outputs": [
84
  {
85
+ "name": "stdout",
86
  "output_type": "stream",
87
  "text": [
88
+ "Using course: Roman History\n",
89
+ "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"
90
  ]
91
  }
92
  ],
 
95
  "\n",
96
  "course = \"Roman History\"\n",
97
  "if course == \"Roman History\":\n",
98
+ " print(f\"Using course: {course}\")\n",
99
  " pinecone.init(api_key=os.environ[\"PINECONE_API_KEY_2\"], environment=os.environ[\"PINECONE_API_ENV_2\"])\n",
100
  " index = pinecone.Index(\"rag-demo-1-history-rome\")\n",
101
+ " vector_store = Pinecone(index, embeddings, \"text\")\n",
102
+ " \n",
103
+ "query = \"When was Ceasar born?\"\n",
104
+ "\n",
105
+ "result = vector_store.similarity_search(\n",
106
+ " query, # our search query\n",
107
+ " k=1 # return 3 most relevant docs\n",
108
+ ")\n",
109
+ "\n",
110
+ "print(result[0])"
111
  ]
112
  },
113
  {
114
  "cell_type": "code",
115
+ "execution_count": 6,
116
  "metadata": {},
117
+ "outputs": [
118
+ {
119
+ "data": {
120
+ "text/plain": [
121
+ "'\\n\\nHow are you?'"
122
+ ]
123
+ },
124
+ "execution_count": 6,
125
+ "metadata": {},
126
+ "output_type": "execute_result"
127
+ }
128
+ ],
129
  "source": [
130
  "llm = OpenAI(temperature=0)\n",
131
+ "llm.predict(\"Hello there\")"
132
+ ]
133
+ },
134
+ {
135
+ "cell_type": "markdown",
136
+ "metadata": {},
137
+ "source": []
138
+ },
139
+ {
140
+ "cell_type": "code",
141
+ "execution_count": 21,
142
+ "metadata": {},
143
+ "outputs": [],
144
+ "source": [
145
+ "from langchain.memory import ConversationBufferMemory\n",
146
+ "from langchain.chains import ConversationalRetrievalChain\n",
147
+ "\n",
148
+ "memory = ConversationBufferMemory(memory_key=\"chat_history\", return_messages= True)\n",
149
+ "chain = ConversationalRetrievalChain.from_llm(llm, retriever= vector_store.as_retriever(), memory= memory)"
150
+ ]
151
+ },
152
+ {
153
+ "cell_type": "code",
154
+ "execution_count": 26,
155
+ "metadata": {},
156
+ "outputs": [
157
+ {
158
+ "data": {
159
+ "text/plain": [
160
+ "\" I don't know.\""
161
+ ]
162
+ },
163
+ "execution_count": 26,
164
+ "metadata": {},
165
+ "output_type": "execute_result"
166
+ }
167
+ ],
168
+ "source": [
169
+ "query = \"\"\" \n",
170
+ " SYSTEM:\n",
171
+ " You are a helpful teacher who is teaching a class of 10 year olds. \n",
172
+ " Your answers must only come from the context provided to you in the question.\n",
173
+ " If you don't know the answer then say so. \n",
174
+ " The answers should be at least 40 words or longer\n",
175
+ " \n",
176
+ " QUESTION:\n",
177
+ " What is moby dick?\n",
178
+ " \n",
179
+ "\"\"\"\n",
180
+ "chain.run({'question': query})"
181
+ ]
182
+ },
183
+ {
184
+ "cell_type": "code",
185
+ "execution_count": 5,
186
+ "metadata": {},
187
+ "outputs": [
188
+ {
189
+ "ename": "KeyboardInterrupt",
190
+ "evalue": "Interrupted by user",
191
+ "output_type": "error",
192
+ "traceback": [
193
+ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
194
+ "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
195
+ "\u001b[1;32mc:\\sc\\ai\\rag-demo-1\\app.ipynb Cell 5\u001b[0m line \u001b[0;36m4\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/app.ipynb#W4sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m llm \u001b[39m=\u001b[39m OpenAI(temperature\u001b[39m=\u001b[39m\u001b[39m0\u001b[39m)\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/app.ipynb#W4sZmlsZQ%3D%3D?line=1'>2</a>\u001b[0m qa \u001b[39m=\u001b[39m RetrievalQA\u001b[39m.\u001b[39mfrom_chain_type(llm\u001b[39m=\u001b[39mllm, chain_type\u001b[39m=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mstuff\u001b[39m\u001b[39m\"\u001b[39m, retriever\u001b[39m=\u001b[39mvector_store\u001b[39m.\u001b[39mas_retriever())\n\u001b[1;32m----> <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/app.ipynb#W4sZmlsZQ%3D%3D?line=3'>4</a>\u001b[0m query \u001b[39m=\u001b[39m \u001b[39minput\u001b[39;49m(\u001b[39m\"\u001b[39;49m\u001b[39mWho was ceasar and why whas he noteable?\u001b[39;49m\u001b[39m\"\u001b[39;49m)\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/app.ipynb#W4sZmlsZQ%3D%3D?line=4'>5</a>\u001b[0m answer \u001b[39m=\u001b[39m qa\u001b[39m.\u001b[39mrun(query)\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/app.ipynb#W4sZmlsZQ%3D%3D?line=5'>6</a>\u001b[0m \u001b[39m# print(answer)\u001b[39;00m\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/app.ipynb#W4sZmlsZQ%3D%3D?line=6'>7</a>\u001b[0m \u001b[39m# print(\"\\nWhat else can I help you with:\")\u001b[39;00m\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/app.ipynb#W4sZmlsZQ%3D%3D?line=7'>8</a>\u001b[0m \n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/app.ipynb#W4sZmlsZQ%3D%3D?line=11'>12</a>\u001b[0m \u001b[39m# print(answer)\u001b[39;00m\n\u001b[0;32m <a href='vscode-notebook-cell:/c%3A/sc/ai/rag-demo-1/app.ipynb#W4sZmlsZQ%3D%3D?line=12'>13</a>\u001b[0m \u001b[39m# print(\"\\nWhat else can I help you with:\")\u001b[39;00m\n",
196
+ "File \u001b[1;32mc:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\ipykernel\\kernelbase.py:1202\u001b[0m, in \u001b[0;36mKernel.raw_input\u001b[1;34m(self, prompt)\u001b[0m\n\u001b[0;32m 1200\u001b[0m msg \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mraw_input was called, but this frontend does not support input requests.\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m 1201\u001b[0m \u001b[39mraise\u001b[39;00m StdinNotImplementedError(msg)\n\u001b[1;32m-> 1202\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_input_request(\n\u001b[0;32m 1203\u001b[0m \u001b[39mstr\u001b[39;49m(prompt),\n\u001b[0;32m 1204\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_parent_ident[\u001b[39m\"\u001b[39;49m\u001b[39mshell\u001b[39;49m\u001b[39m\"\u001b[39;49m],\n\u001b[0;32m 1205\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mget_parent(\u001b[39m\"\u001b[39;49m\u001b[39mshell\u001b[39;49m\u001b[39m\"\u001b[39;49m),\n\u001b[0;32m 1206\u001b[0m password\u001b[39m=\u001b[39;49m\u001b[39mFalse\u001b[39;49;00m,\n\u001b[0;32m 1207\u001b[0m )\n",
197
+ "File \u001b[1;32mc:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\ipykernel\\kernelbase.py:1245\u001b[0m, in \u001b[0;36mKernel._input_request\u001b[1;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[0;32m 1242\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mKeyboardInterrupt\u001b[39;00m:\n\u001b[0;32m 1243\u001b[0m \u001b[39m# re-raise KeyboardInterrupt, to truncate traceback\u001b[39;00m\n\u001b[0;32m 1244\u001b[0m msg \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mInterrupted by user\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m-> 1245\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mKeyboardInterrupt\u001b[39;00m(msg) \u001b[39mfrom\u001b[39;00m \u001b[39mNone\u001b[39;00m\n\u001b[0;32m 1246\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mException\u001b[39;00m:\n\u001b[0;32m 1247\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mlog\u001b[39m.\u001b[39mwarning(\u001b[39m\"\u001b[39m\u001b[39mInvalid Message:\u001b[39m\u001b[39m\"\u001b[39m, exc_info\u001b[39m=\u001b[39m\u001b[39mTrue\u001b[39;00m)\n",
198
+ "\u001b[1;31mKeyboardInterrupt\u001b[0m: Interrupted by user"
199
+ ]
200
+ }
201
+ ],
202
+ "source": [
203
  "qa = RetrievalQA.from_chain_type(llm=llm, chain_type=\"stuff\", retriever=vector_store.as_retriever())\n",
204
  "\n",
205
  "query = input(\"Who was ceasar and why whas he noteable?\")\n",
206
  "answer = qa.run(query)\n",
207
+ "\n",
208
+ "\n",
209
+ "qa = RetrievalQA.from_chain_type(llm, chain_type=\"stuff\", retriever=retriever)\n",
210
+ "response = qa.run(query)\n",
211
+ "\n",
212
  "# print(answer)\n",
213
  "# print(\"\\nWhat else can I help you with:\")\n",
214
  "\n",
vector-stores-query.ipynb CHANGED
@@ -61,41 +61,31 @@
61
  },
62
  {
63
  "cell_type": "code",
64
- "execution_count": 20,
65
  "metadata": {},
66
  "outputs": [
67
  {
68
  "name": "stdout",
69
  "output_type": "stream",
70
  "text": [
71
- "Using index: rag-demo-1-history-rome\n"
72
- ]
73
- },
74
- {
75
- "name": "stderr",
76
- "output_type": "stream",
77
- "text": [
78
- "c:\\sc\\ai\\rag-demo-1\\.venv\\lib\\site-packages\\langchain\\vectorstores\\pinecone.py:59: UserWarning: Passing in `embedding` as a Callable is deprecated. Please pass in an Embeddings object instead.\n",
79
- " warnings.warn(\n"
80
  ]
81
  }
82
  ],
83
  "source": [
84
- "text_field = \"text\"\n",
85
  "\n",
86
  "pinecone.list_indexes()\n",
87
  "index_name = pinecone.list_indexes()[0]\n",
88
  "print(f\"Using found index: {index_name}\")\n",
89
  "\n",
90
  "index = pinecone.Index(index_name)\n",
91
- "vectorstore = Pinecone(\n",
92
- " index, embeddings.embed_query, text_field\n",
93
- ")"
94
  ]
95
  },
96
  {
97
  "cell_type": "code",
98
- "execution_count": 23,
99
  "metadata": {},
100
  "outputs": [],
101
  "source": [
@@ -103,25 +93,21 @@
103
  "\n",
104
  "result = vectorstore.similarity_search(\n",
105
  " query, # our search query\n",
106
- " k=3 # return 3 most relevant docs\n",
107
  ")"
108
  ]
109
  },
110
  {
111
  "cell_type": "code",
112
- "execution_count": 24,
113
  "metadata": {},
114
  "outputs": [
115
  {
116
  "name": "stdout",
117
  "output_type": "stream",
118
  "text": [
119
- "3\n",
120
- "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",
121
- "##\n",
122
- "page_content='13 After the adoption his full name was Caius Julius Caesar Octavianus.\\nAlthough he was known as Caesar by his contemporaries, it is more convenient\\nto refer to him henceforth as Octavian, to distinguish him from his adoptive\\nfather.\\n\\n[187]\\n\\n\\x0c222\\n\\nA History of Rome to 565 A. D.\\n\\nCaesars murder, was about to leave Italy to join Brutus when he\\nheard of the changed situation in Rome and returned to assume\\nthe leadership of the republican party. Antony left Rome for\\nthe Cisalpine province early in December, 44 B. C., and Cicero\\ninduced the Senate to enter into a coalition with Octavian against\\nhim.\\nIn his Philippic Orations he gave full vent to his bitter\\nhatred of Antony and so aroused the latters undying enmity.' 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",
123
- "##\n",
124
- "page_content='[180]\\n\\nIV. THE DICTATORSHIP OF JULIUS CAESAR: 4644 B. C.\\n\\n213\\n\\n46, when he celebrated his triumph over the Gauls and his other\\nnon-Roman enemies. He assumed it again after Munda in the\\nfollowing year.' 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"
125
  ]
126
  }
127
  ],
@@ -129,10 +115,10 @@
129
  "print(len(result))\n",
130
  "\n",
131
  "print(result[0])\n",
132
- "print('##')\n",
133
- "print(result[1])\n",
134
- "print('##')\n",
135
- "print(result[2])"
136
  ]
137
  }
138
  ],
 
61
  },
62
  {
63
  "cell_type": "code",
64
+ "execution_count": 25,
65
  "metadata": {},
66
  "outputs": [
67
  {
68
  "name": "stdout",
69
  "output_type": "stream",
70
  "text": [
71
+ "Using found index: rag-demo-1-history-rome\n"
 
 
 
 
 
 
 
 
72
  ]
73
  }
74
  ],
75
  "source": [
76
+ "# text_field = \"text\"\n",
77
  "\n",
78
  "pinecone.list_indexes()\n",
79
  "index_name = pinecone.list_indexes()[0]\n",
80
  "print(f\"Using found index: {index_name}\")\n",
81
  "\n",
82
  "index = pinecone.Index(index_name)\n",
83
+ "vectorstore = Pinecone(index, embeddings.embed_query, \"text\")"
 
 
84
  ]
85
  },
86
  {
87
  "cell_type": "code",
88
+ "execution_count": 28,
89
  "metadata": {},
90
  "outputs": [],
91
  "source": [
 
93
  "\n",
94
  "result = vectorstore.similarity_search(\n",
95
  " query, # our search query\n",
96
+ " k=1 # return 3 most relevant docs\n",
97
  ")"
98
  ]
99
  },
100
  {
101
  "cell_type": "code",
102
+ "execution_count": 29,
103
  "metadata": {},
104
  "outputs": [
105
  {
106
  "name": "stdout",
107
  "output_type": "stream",
108
  "text": [
109
+ "1\n",
110
+ "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"
 
 
 
 
111
  ]
112
  }
113
  ],
 
115
  "print(len(result))\n",
116
  "\n",
117
  "print(result[0])\n",
118
+ "# print('##')\n",
119
+ "# print(result[1])\n",
120
+ "# print('##')\n",
121
+ "# print(result[2])"
122
  ]
123
  }
124
  ],