File size: 19,950 Bytes
16522e2
 
 
 
ecfc436
16522e2
ecfc436
16522e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4945d8
 
ecfc436
b4945d8
 
 
 
 
 
 
 
 
 
16522e2
 
 
 
 
 
 
 
 
ecfc436
16522e2
ecfc436
16522e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52bc1cc
16522e2
52bc1cc
16522e2
 
 
 
 
 
 
 
 
 
 
 
 
 
52bc1cc
16522e2
52bc1cc
16522e2
 
 
 
 
 
 
 
 
 
 
 
 
b4945d8
 
 
 
 
 
 
 
 
16522e2
 
 
 
 
 
 
 
 
b4945d8
16522e2
b4945d8
16522e2
 
 
 
 
b4945d8
16522e2
 
 
 
 
 
 
 
 
 
 
 
089629c
16522e2
ecfc436
16522e2
089629c
16522e2
b4945d8
 
 
 
 
 
 
 
089629c
 
 
16522e2
 
 
 
b4945d8
 
 
16522e2
 
 
 
 
 
 
 
 
 
b4945d8
16522e2
 
 
 
 
 
 
 
 
089629c
16522e2
 
 
b4945d8
089629c
 
b4945d8
 
 
 
 
 
16522e2
 
 
 
 
52bc1cc
16522e2
52bc1cc
16522e2
 
 
 
 
 
 
 
ecfc436
16522e2
b4945d8
16522e2
b4945d8
 
16522e2
 
 
 
ecfc436
16522e2
 
 
 
 
 
 
 
 
 
 
52bc1cc
16522e2
52bc1cc
16522e2
 
 
 
 
 
 
52bc1cc
16522e2
52bc1cc
089629c
 
 
 
 
 
 
 
52bc1cc
089629c
52bc1cc
089629c
 
ecfc436
 
089629c
ecfc436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16522e2
 
 
 
 
b4945d8
16522e2
b4945d8
16522e2
ecfc436
 
b4945d8
 
 
 
 
 
 
16522e2
ecfc436
 
16522e2
 
 
 
 
 
 
 
 
 
 
089629c
16522e2
ecfc436
16522e2
b4945d8
 
 
 
 
ecfc436
089629c
b4945d8
 
089629c
b4945d8
 
 
089629c
b4945d8
16522e2
 
 
 
 
ecfc436
16522e2
ecfc436
b4945d8
 
 
 
 
 
 
089629c
 
ecfc436
089629c
ecfc436
089629c
 
 
 
 
 
 
 
 
 
 
 
ecfc436
089629c
ecfc436
089629c
 
 
 
 
 
 
 
 
ecfc436
089629c
ecfc436
089629c
 
 
 
 
 
ecfc436
089629c
ecfc436
089629c
 
 
 
 
 
ecfc436
089629c
ecfc436
089629c
 
 
 
b4945d8
 
ecfc436
b4945d8
ecfc436
16522e2
b4945d8
 
 
 
 
 
 
 
 
 
16522e2
b4945d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecfc436
b4945d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16522e2
b4945d8
 
 
 
 
ecfc436
b4945d8
 
 
 
 
 
 
 
52bc1cc
b4945d8
52bc1cc
b4945d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16522e2
b4945d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecfc436
b4945d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16522e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import numpy as np\n",
    "import os\n",
    "\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import sys\n",
    "sys.path.append(os.path.dirname(os.getcwd()))\n",
    "\n",
    "from dotenv import load_dotenv\n",
    "load_dotenv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from climateqa.engine.llm import get_llm\n",
    "from climateqa.engine.vectorstore import get_pinecone_vectorstore\n",
    "from climateqa.engine.embeddings import get_embeddings_function\n",
    "from climateqa.engine.reranker import get_reranker\n",
    "from climateqa.engine.graph import make_graph_agent, display_graph\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## LLM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from climateqa.engine.llm import get_llm\n",
    "\n",
    "llm = get_llm(provider=\"openai\")\n",
    "llm.invoke(\"Say Hello !\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Retriever "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from climateqa.engine.vectorstore import get_pinecone_vectorstore\n",
    "from climateqa.engine.embeddings import get_embeddings_function\n",
    "\n",
    "question = \"What is the impact of climate change on the environment?\"\n",
    "\n",
    "embeddings_function = get_embeddings_function()\n",
    "vectorstore_ipcc = get_pinecone_vectorstore(embeddings_function)\n",
    "docs_question = vectorstore_ipcc.search(query = question, search_type=\"similarity\")\n",
    "docs_question"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# optional filters\n",
    "sources_owid = [\"OWID\"]\n",
    "filters = {}\n",
    "filters[\"source\"] = {\"$in\": sources_owid}\n",
    "\n",
    "# vectorestore_graphs\n",
    "vectorstore_graphs = get_pinecone_vectorstore(embeddings_function, index_name = os.getenv(\"PINECONE_API_INDEX_OWID\"), text_key=\"title\")\n",
    "owid_graphs = vectorstore_graphs.search(query = question, search_type=\"similarity\")\n",
    "owid_graphs = vectorstore_graphs.similarity_search_with_score(query = question, filter=filters, k=5)\n",
    "owid_graphs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "vectorstore_region = get_pinecone_vectorstore(embeddings_function, index_name=os.getenv(\"PINECONE_API_INDEX_REGION\"))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Reranker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from climateqa.engine.reranker import get_reranker\n",
    "from climateqa.engine.reranker import rerank_docs\n",
    "\n",
    "reranker = get_reranker(\"nano\")\n",
    "reranked_docs_question = rerank_docs(reranker,docs_question,question)\n",
    "reranked_docs_question"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from climateqa.engine.graph import make_graph_agent, display_graph, make_graph_agent_poc\n",
    "\n",
    "llm = get_llm(provider=\"openai\")\n",
    "embeddings_function = get_embeddings_function()\n",
    "vectorstore_ipcc = get_pinecone_vectorstore(embeddings_function)\n",
    "vectorstore_graphs = get_pinecone_vectorstore(embeddings_function, index_name = os.getenv(\"PINECONE_API_INDEX_OWID\"), text_key=\"title\")\n",
    "vectorstore_region = get_pinecone_vectorstore(embeddings_function, index_name=os.getenv(\"PINECONE_API_INDEX_REGION\"))\n",
    "reranker = get_reranker(\"nano\")\n",
    "\n",
    "app = make_graph_agent(llm=llm, vectorstore_ipcc=vectorstore_ipcc, vectorstore_graphs=vectorstore_graphs, vectorstore_region=vectorstore_region, reranker=reranker)\n",
    "app2 = make_graph_agent_poc(llm=llm, vectorstore_ipcc=vectorstore_ipcc, vectorstore_graphs=vectorstore_graphs, vectorstore_region=vectorstore_region, reranker=reranker)\n",
    "display_graph(app)\n",
    "display_graph(app2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from climateqa.engine.graph import search \n",
    "\n",
    "from climateqa.engine.chains.intent_categorization import make_intent_categorization_node\n",
    "\n",
    "\n",
    "from climateqa.engine.chains.answer_chitchat import make_chitchat_node\n",
    "from climateqa.engine.chains.answer_ai_impact import make_ai_impact_node\n",
    "from climateqa.engine.chains.query_transformation import make_query_transform_node\n",
    "from climateqa.engine.chains.translation import make_translation_node\n",
    "from climateqa.engine.chains.retrieve_documents import make_IPx_retriever_node, make_POC_retriever_node\n",
    "from climateqa.engine.chains.answer_rag import make_rag_node\n",
    "from climateqa.engine.chains.graph_retriever import make_graph_retriever_node\n",
    "from climateqa.engine.chains.chitchat_categorization import make_chitchat_intent_categorization_node\n",
    "from climateqa.engine.chains.prompts import audience_prompts\n",
    "from climateqa.engine.graph import route_intent\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "inial_state = {\n",
    "    \"user_input\": \"What is the impact of climate change on the environment?\", \n",
    "    # \"user_input\": \"Quel est l'impact du changement climatique sur Bordeaux ?\",\n",
    "    \"audience\" : audience_prompts[\"general\"],\n",
    "    # \"sources_input\":[\"IPCC\"],\n",
    "    \"relevant_content_sources_selection\": [\"Figures (IPCC/IPBES)\",\"POC region\"],\n",
    "    \"search_only\" : False,\n",
    "    \"reports\": [],\n",
    "}\n",
    "state=inial_state.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cat_node = make_intent_categorization_node(llm)\n",
    "state.update(cat_node(inial_state))\n",
    "state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# state.update(search(state))\n",
    "# state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "intent = route_intent(state)\n",
    "\n",
    "if route_intent(state) == \"translate_query\":\n",
    "    make_translation_node(llm)(state)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "state.update(make_query_transform_node(llm)(state))\n",
    "state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from climateqa.engine.chains.retrieve_documents import retrieve_documents\n",
    "res = await retrieve_documents(state[\"questions_list\"][0],{},\"IPx\", vectorstore_ipcc,reranker)\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from climateqa.engine.chains.retrieve_documents import retrieve_documents_for_all_questions\n",
    "\n",
    "source_type = \"IPx\"\n",
    "to_handle_questions_index = [i for i, x in enumerate(state[\"questions_list\"]) if x[\"source_type\"] == \"IPx\"]\n",
    "\n",
    "search_figures = \"Figures (IPCC/IPBES)\" in state[\"relevant_content_sources_selection\"]\n",
    "search_only = state[\"search_only\"]\n",
    "reports = state[\"reports\"]\n",
    "questions_list = state[\"questions_list\"]\n",
    "n_questions=state[\"n_questions\"][\"total\"]\n",
    "k_final=15\n",
    "k_before_reranking=100\n",
    "\n",
    "res = await retrieve_documents_for_all_questions(\n",
    "            search_figures=search_figures,\n",
    "            search_only=search_only,\n",
    "            reports=reports,\n",
    "            questions_list=questions_list,\n",
    "            n_questions=n_questions,\n",
    "            config={},\n",
    "            source_type=source_type,\n",
    "            to_handle_questions_index=to_handle_questions_index,\n",
    "            vectorstore=vectorstore_ipcc,\n",
    "            reranker=reranker,\n",
    "            rerank_by_question=True,\n",
    "            k_final=k_final,\n",
    "            k_before_reranking=k_before_reranking,\n",
    "        )\n",
    "state.update(res)\n",
    "state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "state.update(await make_graph_retriever_node(vectorstore_graphs, reranker)(state))\n",
    "state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "answer_rag = await make_rag_node(llm)(state,{})\n",
    "state.update(answer_rag)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# stream event of the whole chain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from climateqa.engine.graph import make_graph_agent, display_graph\n",
    "from climateqa.engine.chains.prompts import audience_prompts\n",
    "\n",
    "\n",
    "inial_state = { \n",
    "    \"user_input\": \"Comment le changement climatique m'affectera à Paris?\",\n",
    "    \"audience\" : audience_prompts[\"general\"],\n",
    "    \"sources_input\":[\"IPCC\"],\n",
    "    \"relevant_content_sources_selection\": [\"Figures (IPCC/IPBES)\",\"POC region\"],\n",
    "    \"search_only\" : False,\n",
    "    \"reports\": [],\n",
    "}\n",
    "app = make_graph_agent_poc(llm=llm, vectorstore_ipcc=vectorstore_ipcc, vectorstore_graphs=vectorstore_graphs, vectorstore_region=vectorstore_region, reranker=reranker)\n",
    "\n",
    "inial_state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "event_list = app.astream_events(inial_state, version = \"v1\")\n",
    "static_event_list = []\n",
    "async for event in event_list:\n",
    "    static_event_list.append(event)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_events = pd.DataFrame(static_event_list)\n",
    "df_events[\"node\"] = df_events[\"metadata\"].apply(lambda x: x[\"langgraph_node\"] if  \"langgraph_node\" in x  else \"None\")\n",
    "# df_events_chat = df_events[(df_events[\"event\"] ==\"on_chat_model_stream\")]\n",
    "df_events_chat = df_events[df_events[\"node\"].apply(lambda x : x in [\"answer_rag\",\"answer_rag_no_docs\", \"answer_search\", \"answer_chitchat\"])]\n",
    "node_end_answer = df_events_chat[(df_events_chat[\"event\"] ==\"on_chain_end\") & (df_events_chat[\"name\"] ==\"answer_rag\")]\n",
    "node_end_answer[\"data\"].values[0][\"output\"]\n",
    "# df_events_chat[\"data\"].apply(lambda x: x[\"content\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ev_rel_doc = df_events.iloc[df_events[(df_events[\"event\"] ==\"on_chain_end\")][\"data\"].apply(lambda x: x[\"output\"]).dropna().apply(lambda x: x[\"related_contents\"] if \"related_contents\" in x else None).dropna().index].iloc[-1]\n",
    "related_content = ev_rel_doc[\"data\"][\"output\"][\"related_contents\"]\n",
    "# [f\"{d.metadata['short_name']} - {d.metadata['name']}\" for d in related_content]\n",
    "related_content[0].metadata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_events[(df_events[\"event\"] ==\"on_chain_end\") & (df_events[\"name\"]==\"transform_query\")][\"data\"].values[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_events[(df_events[\"event\"] ==\"on_chain_end\") & (df_events[\"name\"]==\"answer_search\")][\"data\"].values[0][\"input\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "node_end_answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get the answer at the end\n",
    "from climateqa.handle_stream_events import stream_answer\n",
    "event_list = app.astream_events(inial_state, version = \"v1\")\n",
    "history = []\n",
    "start_streaming = False\n",
    "answer_message_content = \"\"\n",
    "async for event in event_list:\n",
    "\n",
    "    if \"langgraph_node\" in event[\"metadata\"]:\n",
    "        node = event[\"metadata\"][\"langgraph_node\"]\n",
    "\n",
    "        if (event[\"name\"] != \"transform_query\" and \n",
    "                      event[\"event\"] == \"on_chat_model_stream\" and\n",
    "                      node in [\"answer_rag\",\"answer_rag_no_docs\", \"answer_search\", \"answer_chitchat\"]):\n",
    "                    history, start_streaming, answer_message_content = stream_answer(\n",
    "                        history, event, start_streaming, answer_message_content\n",
    "                    )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test events logs\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "inial_state = {'user_input': 'What is the impact of climate  in Bordeaux',\n",
    " 'audience': 'the general public who know the basics in science and climate change and want to learn more about it without technical terms. Still use references to passages.',\n",
    " 'sources_input': ['IPCC'],\n",
    " 'relevant_content_sources_selection': ['Figures (IPCC/IPBES)', 'POC region'],\n",
    " 'search_only': False,\n",
    " 'reports': []\n",
    " }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get the answer at the end\n",
    "from climateqa.handle_stream_events import stream_answer\n",
    "app = make_graph_agent(llm=llm, vectorstore_ipcc=vectorstore_ipcc, vectorstore_graphs=vectorstore_graphs, vectorstore_region=vectorstore_region, reranker=reranker)\n",
    "\n",
    "event_list = app.astream_events(inial_state, version = \"v1\")\n",
    "history = []\n",
    "start_streaming = False\n",
    "answer_message_content = \"\"\n",
    "static_event_list = []\n",
    "async for event in event_list:\n",
    "    static_event_list.append(event)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_static_events = pd.DataFrame(static_event_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_static_events.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_static_events[\"name\"].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "selected_events = df_static_events[\n",
    "    (df_static_events[\"event\"] == \"on_chain_end\") &\n",
    "    (df_static_events[\"name\"].isin([\"retrieve_documents\", \"retrieve_local_data\", \"retrieve_POC_docs_node\",\"retrieve_IPx_docs\"]))\n",
    "    # (df_static_events[\"data\"].apply(lambda x: x[\"output\"] is not None))\n",
    "]\n",
    "selected_events"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# selected_events[selected_events[\"data\"].apply(lambda x : \"output\" in x and x[\"output\"] is not None)]\n",
    "selected_events[\"data\"].apply(lambda x : x[\"output\"][\"documents\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "selected_events = df_static_events[\n",
    "    (df_static_events[\"event\"] == \"on_chain_end\") &\n",
    "    (df_static_events[\"name\"].isin([\"answer_search\"]))\n",
    "    # (df_static_events[\"data\"].apply(lambda x: x[\"output\"] is not None))\n",
    "]\n",
    "selected_events[\"metadata\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "selected_events[\"data\"].iloc[0][\"input\"][\"related_contents\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "selected_events[\"data\"].apply(lambda x : x[\"output\"]).iloc[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "selected_events.iloc[0][\"data\"].values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "selected_events.iloc[1][\"data\"].values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list(selected_events.iloc[0][\"data\"].values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list(selected_events.iloc[1][\"data\"].values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list(selected_events.iloc[2][\"data\"].values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list(selected_events.iloc[3][\"data\"].values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import json\n",
    "\n",
    "# print(json.dumps(list(selected_events.iloc[1][\"data\"].values()), indent=4))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "data_values = selected_events.iloc[1][\"data\"].values()\n",
    "formatted_data = json.dumps(list(data_values)[0], indent=4)\n",
    "print(formatted_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pprint import pprint\n",
    "import json\n",
    "selected_events.iloc[2][\"data\"].values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "selected_events.iloc[3][\"data\"].values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_static_events[df_static_events[\"name\"] == \"retrieve_POC_docs_node\"].iloc[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "climateqa",
   "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.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}