Draichi commited on
Commit
9a1be75
·
unverified ·
1 Parent(s): 646f473

feat: add `AI_session_overview_generator`notebook

Browse files
notebooks/AI_session_overview_generator.ipynb ADDED
@@ -0,0 +1,232 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# Generating Session Summary with LLMs\n",
8
+ "\n",
9
+ "This project utilizes LlamaIndex and AI technology to analyze Formula 1 car data and generate session summaries. It is particularly beneficial for race engineers seeking detailed insights and performance analysis during races.\n"
10
+ ]
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
+ },
22
+ {
23
+ "cell_type": "markdown",
24
+ "metadata": {},
25
+ "source": [
26
+ "## Optional: Setup Observability\n",
27
+ "\n",
28
+ "Here we will use our Arize Phoenix integration to view traces through the query engine. It will be available at http://localhost:6006\n"
29
+ ]
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
+ ],
44
+ "source": [
45
+ "from openinference.instrumentation.llama_index import LlamaIndexInstrumentor\n",
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",
52
+ "tracer_provider = trace_sdk.TracerProvider()\n",
53
+ "tracer_provider.add_span_processor(\n",
54
+ " SimpleSpanProcessor(OTLPSpanExporter(endpoint)))\n",
55
+ "\n",
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": {},
83
+ "source": [
84
+ "### Define the LLM and the Embedding\n"
85
+ ]
86
+ },
87
+ {
88
+ "cell_type": "code",
89
+ "execution_count": 19,
90
+ "metadata": {},
91
+ "outputs": [],
92
+ "source": [
93
+ "from llama_index.embeddings.openai import OpenAIEmbedding\n",
94
+ "from llama_index.llms.openai import OpenAI\n",
95
+ "from llama_index.core import Settings\n",
96
+ "\n",
97
+ "Settings.llm = OpenAI(model=\"gpt-3.5-turbo\", temperature=0)\n",
98
+ "Settings.embed_model = OpenAIEmbedding(model=\"text-embedding-ada-002\")"
99
+ ]
100
+ },
101
+ {
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
+ ],
211
+ "metadata": {
212
+ "kernelspec": {
213
+ "display_name": "llama",
214
+ "language": "python",
215
+ "name": "python3"
216
+ },
217
+ "language_info": {
218
+ "codemirror_mode": {
219
+ "name": "ipython",
220
+ "version": 3
221
+ },
222
+ "file_extension": ".py",
223
+ "mimetype": "text/x-python",
224
+ "name": "python",
225
+ "nbconvert_exporter": "python",
226
+ "pygments_lexer": "ipython3",
227
+ "version": "3.11.9"
228
+ }
229
+ },
230
+ "nbformat": 4,
231
+ "nbformat_minor": 2
232
+ }