Draichi commited on
Commit
de53b1e
·
unverified ·
1 Parent(s): fd05923

wip(AI_session_overview_generator): add Part 1

Browse files
notebooks/AI_session_overview_generator.ipynb CHANGED
@@ -11,11 +11,24 @@
11
  },
12
  {
13
  "cell_type": "code",
14
- "execution_count": null,
15
  "metadata": {},
16
- "outputs": [],
 
 
 
 
 
 
 
 
 
 
 
17
  "source": [
18
  "from dotenv import load_dotenv\n",
 
 
19
  "load_dotenv()"
20
  ]
21
  },
@@ -30,14 +43,22 @@
30
  },
31
  {
32
  "cell_type": "code",
33
- "execution_count": 18,
34
  "metadata": {},
35
  "outputs": [
36
  {
37
  "name": "stderr",
38
  "output_type": "stream",
39
  "text": [
40
- "WARNI [opentelemetry.instrumentation.instrumentor] Attempting to instrument while already instrumented\n"
 
 
 
 
 
 
 
 
41
  ]
42
  }
43
  ],
@@ -46,6 +67,10 @@
46
  "from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter\n",
47
  "from opentelemetry.sdk import trace as trace_sdk\n",
48
  "from opentelemetry.sdk.trace.export import SimpleSpanProcessor\n",
 
 
 
 
49
  "\n",
50
  "endpoint = \"http://127.0.0.1:6006/v1/traces\" # Phoenix receiver address\n",
51
  "\n",
@@ -56,27 +81,6 @@
56
  "LlamaIndexInstrumentor().instrument(tracer_provider=tracer_provider)"
57
  ]
58
  },
59
- {
60
- "cell_type": "markdown",
61
- "metadata": {},
62
- "source": [
63
- "### Start SQL Database\n"
64
- ]
65
- },
66
- {
67
- "cell_type": "code",
68
- "execution_count": 4,
69
- "metadata": {},
70
- "outputs": [],
71
- "source": [
72
- "from sqlalchemy import create_engine\n",
73
- "from llama_index.core import SQLDatabase\n",
74
- "\n",
75
- "engine = create_engine('sqlite:///spain_practice_1.db')\n",
76
- "\n",
77
- "sql_database = SQLDatabase(engine, include_tables=[\"mercedes\"])"
78
- ]
79
- },
80
  {
81
  "cell_type": "markdown",
82
  "metadata": {},
@@ -86,7 +90,7 @@
86
  },
87
  {
88
  "cell_type": "code",
89
- "execution_count": 19,
90
  "metadata": {},
91
  "outputs": [],
92
  "source": [
@@ -102,109 +106,125 @@
102
  "cell_type": "markdown",
103
  "metadata": {},
104
  "source": [
105
- "### Define the Retriever and the Query Engine\n"
106
  ]
107
  },
108
  {
109
  "cell_type": "code",
110
- "execution_count": 7,
111
  "metadata": {},
112
  "outputs": [],
113
  "source": [
114
- "from llama_index.core.retrievers import NLSQLRetriever\n",
115
- "from llama_index.core.query_engine import RetrieverQueryEngine\n",
116
  "\n",
117
- "nl_sql_retriever = NLSQLRetriever(\n",
118
- " sql_database, tables=[\"mercedes\"], return_raw=True\n",
119
- ")\n",
120
  "\n",
121
- "query_engine = RetrieverQueryEngine.from_args(nl_sql_retriever)"
122
  ]
123
  },
124
  {
125
  "cell_type": "markdown",
126
  "metadata": {},
127
  "source": [
128
- "### Start retrieving information to construct the report\n"
 
 
 
 
129
  ]
130
  },
131
  {
132
  "cell_type": "code",
133
- "execution_count": 8,
134
  "metadata": {},
135
  "outputs": [
136
  {
137
- "name": "stderr",
138
- "output_type": "stream",
139
- "text": [
140
- "WARNING: All log messages before absl::InitializeLog() is called are written to STDERR\n",
141
- "I0000 00:00:1723244252.910416 2380678 fork_posix.cc:77] Other threads are currently calling into gRPC, skipping fork() handlers\n"
142
- ]
 
 
 
 
143
  }
144
  ],
145
  "source": [
146
- "response = query_engine.query(\n",
147
- " \"Which driver was faster on average on sector 1?\"\n",
148
- ")"
 
 
 
 
 
149
  ]
150
  },
151
  {
152
- "cell_type": "code",
153
- "execution_count": 9,
154
  "metadata": {},
155
- "outputs": [
156
- {
157
- "name": "stdout",
158
- "output_type": "stream",
159
- "text": [
160
- ">>> RUS was faster on average on sector 1.\n"
161
- ]
162
- }
163
- ],
164
  "source": [
165
- "print(\">>> \", str(response))"
 
 
 
 
 
 
166
  ]
167
  },
168
  {
169
  "cell_type": "code",
170
- "execution_count": 15,
171
  "metadata": {},
172
- "outputs": [
173
- {
174
- "name": "stdout",
175
- "output_type": "stream",
176
- "text": [
177
- ">>> HAM\n"
178
- ]
179
- }
180
- ],
181
  "source": [
182
- "response2 = query_engine.query(\n",
183
- " \"Which driver was faster on average on sector 2?\"\n",
 
 
 
184
  ")\n",
 
185
  "\n",
186
- "print(\">>> \", str(response2))"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
  ]
188
  },
189
  {
190
  "cell_type": "code",
191
- "execution_count": 20,
192
  "metadata": {},
193
- "outputs": [
194
- {
195
- "name": "stdout",
196
- "output_type": "stream",
197
- "text": [
198
- ">>> The driver HAM is 28 years old and has a lap time of 98.0705652173913 seconds with a top speed of 289.0 km/h.\n"
199
- ]
200
- }
201
- ],
202
  "source": [
203
- "response3 = query_engine.query(\n",
204
- " \"Write a summary about the driver HAM\"\n",
 
 
 
205
  ")\n",
206
  "\n",
207
- "print(\">>> \", str(response3))"
208
  ]
209
  }
210
  ],
 
11
  },
12
  {
13
  "cell_type": "code",
14
+ "execution_count": 4,
15
  "metadata": {},
16
+ "outputs": [
17
+ {
18
+ "data": {
19
+ "text/plain": [
20
+ "True"
21
+ ]
22
+ },
23
+ "execution_count": 4,
24
+ "metadata": {},
25
+ "output_type": "execute_result"
26
+ }
27
+ ],
28
  "source": [
29
  "from dotenv import load_dotenv\n",
30
+ "from IPython.display import Markdown, display\n",
31
+ "\n",
32
  "load_dotenv()"
33
  ]
34
  },
 
43
  },
44
  {
45
  "cell_type": "code",
46
+ "execution_count": 8,
47
  "metadata": {},
48
  "outputs": [
49
  {
50
  "name": "stderr",
51
  "output_type": "stream",
52
  "text": [
53
+ "WARNI [phoenix.session.session] Existing running Phoenix instance detected! Shutting it down and starting a new instance...\n"
54
+ ]
55
+ },
56
+ {
57
+ "name": "stdout",
58
+ "output_type": "stream",
59
+ "text": [
60
+ "🌍 To view the Phoenix app in your browser, visit http://localhost:6006/\n",
61
+ "📖 For more information on how to use Phoenix, check out https://docs.arize.com/phoenix\n"
62
  ]
63
  }
64
  ],
 
67
  "from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter\n",
68
  "from opentelemetry.sdk import trace as trace_sdk\n",
69
  "from opentelemetry.sdk.trace.export import SimpleSpanProcessor\n",
70
+ "import phoenix as px\n",
71
+ "\n",
72
+ "px.launch_app()\n",
73
+ "\n",
74
  "\n",
75
  "endpoint = \"http://127.0.0.1:6006/v1/traces\" # Phoenix receiver address\n",
76
  "\n",
 
81
  "LlamaIndexInstrumentor().instrument(tracer_provider=tracer_provider)"
82
  ]
83
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  {
85
  "cell_type": "markdown",
86
  "metadata": {},
 
90
  },
91
  {
92
  "cell_type": "code",
93
+ "execution_count": 5,
94
  "metadata": {},
95
  "outputs": [],
96
  "source": [
 
106
  "cell_type": "markdown",
107
  "metadata": {},
108
  "source": [
109
+ "### Start SQL Database\n"
110
  ]
111
  },
112
  {
113
  "cell_type": "code",
114
+ "execution_count": 6,
115
  "metadata": {},
116
  "outputs": [],
117
  "source": [
118
+ "from sqlalchemy import create_engine\n",
119
+ "from llama_index.core import SQLDatabase\n",
120
  "\n",
121
+ "engine = create_engine('sqlite:///Spain_2024_FP1.db')\n",
 
 
122
  "\n",
123
+ "sql_database = SQLDatabase(engine)"
124
  ]
125
  },
126
  {
127
  "cell_type": "markdown",
128
  "metadata": {},
129
  "source": [
130
+ "# Part 1: Text-to-SQL Query Engine\n",
131
+ "\n",
132
+ "Once we have constructed our SQL database, we can use the `NLSQLTableQueryEngine` to construct natural language queries that are synthesized into SQL queries.\n",
133
+ "\n",
134
+ "Note that we need to specify the tables we want to use with this query engine. If we don't the query engine will pull all the schema context, which could overflow the context window of the LLM.\n"
135
  ]
136
  },
137
  {
138
  "cell_type": "code",
139
+ "execution_count": 9,
140
  "metadata": {},
141
  "outputs": [
142
  {
143
+ "data": {
144
+ "text/markdown": [
145
+ "In the first free practice session, driver HAM started on the HARD compound. His speed varied throughout the session, with his fastest lap being 77.632 seconds on lap 2. He showed consistent speed on the straights, reaching up to 316 km/h, but struggled with lower speeds in some corners. Overall, his performance on the HARD compound seemed solid, with room for improvement in certain areas."
146
+ ],
147
+ "text/plain": [
148
+ "<IPython.core.display.Markdown object>"
149
+ ]
150
+ },
151
+ "metadata": {},
152
+ "output_type": "display_data"
153
  }
154
  ],
155
  "source": [
156
+ "from llama_index.core.query_engine import NLSQLTableQueryEngine\n",
157
+ "\n",
158
+ "hamilton_query_engine = NLSQLTableQueryEngine(\n",
159
+ " sql_database=sql_database, tables=[\"hamilton\"])\n",
160
+ "query_str = \"This database contains the data for the driver HAM in the first free practice, based on this data provide a short overview on how was his speed and time taking into account his compound\"\n",
161
+ "response = hamilton_query_engine.query(query_str)\n",
162
+ "\n",
163
+ "display(Markdown(f\"{response}\"))"
164
  ]
165
  },
166
  {
167
+ "cell_type": "markdown",
 
168
  "metadata": {},
 
 
 
 
 
 
 
 
 
169
  "source": [
170
+ "This query engine should be used in any case where you can specify the tables you want to query over beforehand, or the total size of all the table schema plus the rest of the prompt fits your context window.\n",
171
+ "\n",
172
+ "# Part 2: Query-Time Retrieval of Tables for Text-to-SQL\n",
173
+ "\n",
174
+ "If we don't know ahead of time which table we would like to use, and the total size of the table schema overflows your context window size, we should store the table schema in an index so that during query time we can retrieve the right schema.\n",
175
+ "\n",
176
+ "The way we can do this is using the `SQLTableNodeMapping` object, which takes in a `SQLDatabase` and produces a Node object for each `SQLTableSchema` object passed into the `ObjectIndex` constructor.\n"
177
  ]
178
  },
179
  {
180
  "cell_type": "code",
181
+ "execution_count": null,
182
  "metadata": {},
183
+ "outputs": [],
 
 
 
 
 
 
 
 
184
  "source": [
185
+ "from llama_index.core.indices.struct_store.sql_query import SQLTableRetrieverQueryEngine\n",
186
+ "from llama_index.core.objects import (\n",
187
+ " SQLTableNodeMapping,\n",
188
+ " ObjectIndex,\n",
189
+ " SQLTableSchema,\n",
190
  ")\n",
191
+ "from llama_index.core import VectorStoreIndex\n",
192
  "\n",
193
+ "table_node_mapping = SQLTableNodeMapping(sql_database)\n",
194
+ "table_schema_objs = [\n",
195
+ " (SQLTableSchema(table_name=\"hamilton\", context_str=\"This table contains the lap time for the first Hamilton's free practice at Spain.\"))]\n",
196
+ "\n",
197
+ "obj_index = ObjectIndex.from_objects(objects=table_schema_objs,\n",
198
+ " object_mapping=table_node_mapping,\n",
199
+ " index_cls=VectorStoreIndex)\n",
200
+ "\n",
201
+ "table_retriever = obj_index.as_retriever(similarity_top_k=1)\n",
202
+ "\n",
203
+ "query_engine = SQLTableRetrieverQueryEngine(\n",
204
+ " sql_database=sql_database, table_retriever=table_retriever)"
205
+ ]
206
+ },
207
+ {
208
+ "cell_type": "markdown",
209
+ "metadata": {},
210
+ "source": [
211
+ "### Define the Retriever and the Query Engine\n"
212
  ]
213
  },
214
  {
215
  "cell_type": "code",
216
+ "execution_count": 7,
217
  "metadata": {},
218
+ "outputs": [],
 
 
 
 
 
 
 
 
219
  "source": [
220
+ "from llama_index.core.retrievers import NLSQLRetriever\n",
221
+ "from llama_index.core.query_engine import RetrieverQueryEngine\n",
222
+ "\n",
223
+ "nl_sql_retriever = NLSQLRetriever(\n",
224
+ " sql_database, tables=[\"mercedes\"], return_raw=True\n",
225
  ")\n",
226
  "\n",
227
+ "query_engine = RetrieverQueryEngine.from_args(nl_sql_retriever)"
228
  ]
229
  }
230
  ],