thrag commited on
Commit
f7bc6a1
·
1 Parent(s): 8083b67

Setting up UI for chat

Browse files
Files changed (1) hide show
  1. app.ipynb +29 -72
app.ipynb CHANGED
@@ -78,7 +78,7 @@
78
  },
79
  {
80
  "cell_type": "code",
81
- "execution_count": 9,
82
  "metadata": {},
83
  "outputs": [
84
  {
@@ -86,6 +86,8 @@
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
  }
@@ -93,20 +95,29 @@
93
  "source": [
94
  "embeddings = OpenAIEmbeddings()\n",
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
  },
@@ -127,18 +138,19 @@
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": [
@@ -151,16 +163,16 @@
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
  }
@@ -174,67 +186,12 @@
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",
215
- "# while True:\n",
216
- "# query = input(\"Who was ceasar and why whas he notable?\")\n",
217
- "# answer = qa.run(query)\n",
218
- "# print(answer)\n",
219
- "# print(\"\\nWhat else can I help you with:\")"
220
- ]
221
- },
222
- {
223
- "cell_type": "code",
224
- "execution_count": null,
225
- "metadata": {},
226
- "outputs": [],
227
- "source": [
228
- "qa = RetrievalQA.from_chain_type(llm=OpenAI(temperature=0), chain_type=\"stuff\", retriever=vector_store.as_retriever())\n",
229
- "\n",
230
- "print(\"Connector development help bot. What do you want to know?\")\n",
231
- "while True:\n",
232
- " query = input(\"\")\n",
233
- " answer = qa.run(query)\n",
234
- " print(answer)\n",
235
- " print(\"\\nWhat else can I help you with:\")"
236
- ]
237
- },
238
  {
239
  "cell_type": "code",
240
  "execution_count": 3,
 
78
  },
79
  {
80
  "cell_type": "code",
81
+ "execution_count": 30,
82
  "metadata": {},
83
  "outputs": [
84
  {
 
86
  "output_type": "stream",
87
  "text": [
88
  "Using course: Roman History\n",
89
+ "Using index: rag-demo-1-history-rome\n",
90
+ "Test questions: When was Ceasar born?\n",
91
  "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"
92
  ]
93
  }
 
95
  "source": [
96
  "embeddings = OpenAIEmbeddings()\n",
97
  "\n",
98
+ "\n",
99
  "course = \"Roman History\"\n",
100
  "if course == \"Roman History\":\n",
101
  " print(f\"Using course: {course}\")\n",
102
+ " apiKeyKey = \"PINECONE_API_KEY_2\"\n",
103
+ " apiEnvKey = \"PINECONE_API_ENV_2\"\n",
104
+ " testQuestion = \"When was Ceasar born?\"\n",
105
+ "else:\n",
106
+ " print(f\"Using course: {course}\")\n",
107
+ " apiKeyKey = \"PINECONE_API_KEY\"\n",
108
+ " apiEnvKey = \"PINECONE_API_ENV\"\n",
109
+ " testQuestion = \"What is Moby Dick?\"\n",
110
  " \n",
111
+ "pinecone.init(api_key=os.environ[apiKeyKey], environment=os.environ[apiEnvKey])\n",
112
  "\n",
113
+ "index_name = pinecone.list_indexes()[0]\n",
114
+ "print(f\"Using index: {index_name}\")\n",
115
+ "index = pinecone.Index(index_name)\n",
116
+ "vector_store = Pinecone(index, embeddings, \"text\") \n",
117
  "\n",
118
+ "query = testQuestion\n",
119
+ "print(f'Test questions: {testQuestion}')\n",
120
+ "result = vector_store.similarity_search(query, k=3)\n",
121
  "print(result[0])"
122
  ]
123
  },
 
138
  }
139
  ],
140
  "source": [
141
+ "llm = OpenAI(temperature=0)"
 
142
  ]
143
  },
144
  {
145
  "cell_type": "markdown",
146
  "metadata": {},
147
+ "source": [
148
+ "# Conversational agent"
149
+ ]
150
  },
151
  {
152
  "cell_type": "code",
153
+ "execution_count": 35,
154
  "metadata": {},
155
  "outputs": [],
156
  "source": [
 
163
  },
164
  {
165
  "cell_type": "code",
166
+ "execution_count": 36,
167
  "metadata": {},
168
  "outputs": [
169
  {
170
  "data": {
171
  "text/plain": [
172
+ "' \\nCrassus was a Roman general who was killed in battle by the Parthians. He was killed while trying to extend the Roman Empire into the Middle East. Ceasar was avenging Crassus by trying to defeat the Parthians and expand the Roman Empire. He was also trying to avenge the death of his friend and mentor.'"
173
  ]
174
  },
175
+ "execution_count": 36,
176
  "metadata": {},
177
  "output_type": "execute_result"
178
  }
 
186
  " The answers should be at least 40 words or longer\n",
187
  " \n",
188
  " QUESTION:\n",
189
+ " Why was he avenging Crassus, what happened to him that Ceasar needed to avenge him? \n",
190
  " \n",
191
  "\"\"\"\n",
192
  "chain.run({'question': query})"
193
  ]
194
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
  {
196
  "cell_type": "code",
197
  "execution_count": 3,