wip
Browse files- .gitignore +3 -1
- app.py +8 -2
- climateqa/engine/chains/drias_retriever.py +1 -1
- climateqa/{talk_to_data → engine/talk_to_data}/deprecated_vanna_remote.py +0 -0
- climateqa/engine/talk_to_data/drias_metadata.json +58 -0
- climateqa/{talk_to_data → engine/talk_to_data}/how_to_use_main.ipynb +45 -32
- climateqa/engine/talk_to_data/main.py +91 -0
- climateqa/{talk_to_data → engine/talk_to_data}/myVanna.py +1 -1
- climateqa/engine/talk_to_data/pinecone_vanna_training.ipynb +592 -0
- climateqa/engine/talk_to_data/step_by_step_vanna copy.ipynb +0 -0
- climateqa/engine/talk_to_data/step_by_step_vanna.ipynb +0 -0
- climateqa/{talk_to_data → engine/talk_to_data}/utils.py +0 -0
- climateqa/{talk_to_data → engine/talk_to_data}/vanna_class.py +137 -61
- climateqa/talk_to_data/main.py +0 -43
- sandbox/20241104 - CQA - StepByStep CQA.ipynb +104 -39
.gitignore
CHANGED
@@ -13,4 +13,6 @@ data/
|
|
13 |
sandbox/
|
14 |
|
15 |
climateqa/talk_to_data/database/
|
16 |
-
*.db
|
|
|
|
|
|
13 |
sandbox/
|
14 |
|
15 |
climateqa/talk_to_data/database/
|
16 |
+
*.db
|
17 |
+
|
18 |
+
data_ingestion/
|
app.py
CHANGED
@@ -12,6 +12,7 @@ from climateqa.engine.reranker import get_reranker
|
|
12 |
from climateqa.engine.graph import make_graph_agent
|
13 |
from climateqa.engine.chains.retrieve_papers import find_papers
|
14 |
from climateqa.chat import start_chat, chat_stream, finish_chat
|
|
|
15 |
|
16 |
from front.tabs import (create_config_modal, create_examples_tab, create_papers_tab, create_figures_tab, create_chat_interface, create_about_tab)
|
17 |
from front.utils import process_figures
|
@@ -141,7 +142,8 @@ def cqa_tab(tab_name):
|
|
141 |
elem_id="graphs-container"
|
142 |
)
|
143 |
with gr.Tab("Vanna", elem_id="tab-vanna", id=6) as tab_vanna:
|
144 |
-
|
|
|
145 |
|
146 |
return {
|
147 |
"chatbot": chatbot,
|
@@ -164,6 +166,7 @@ def cqa_tab(tab_name):
|
|
164 |
"tab_graphs": tab_graphs,
|
165 |
"tab_papers": tab_papers,
|
166 |
"graph_container": graphs_container,
|
|
|
167 |
"vanna_display": vanna_display
|
168 |
}
|
169 |
|
@@ -194,8 +197,10 @@ def event_handling(
|
|
194 |
tab_graphs = main_tab_components["tab_graphs"]
|
195 |
tab_papers = main_tab_components["tab_papers"]
|
196 |
graphs_container = main_tab_components["graph_container"]
|
|
|
197 |
vanna_display = main_tab_components["vanna_display"]
|
198 |
|
|
|
199 |
config_open = config_components["config_open"]
|
200 |
config_modal = config_components["config_modal"]
|
201 |
dropdown_sources = config_components["dropdown_sources"]
|
@@ -244,7 +249,8 @@ def event_handling(
|
|
244 |
for component in [textbox, examples_hidden]:
|
245 |
component.submit(find_papers, [component, after, dropdown_external_sources], [papers_html, citations_network, papers_summary])
|
246 |
|
247 |
-
ttd_data.change(lambda x: x["df_output"], inputs=[ttd_data], outputs=[vanna_display])
|
|
|
248 |
|
249 |
def main_ui():
|
250 |
# config_open = gr.State(True)
|
|
|
12 |
from climateqa.engine.graph import make_graph_agent
|
13 |
from climateqa.engine.chains.retrieve_papers import find_papers
|
14 |
from climateqa.chat import start_chat, chat_stream, finish_chat
|
15 |
+
from climateqa.engine.talk_to_data.main import ask_vanna
|
16 |
|
17 |
from front.tabs import (create_config_modal, create_examples_tab, create_papers_tab, create_figures_tab, create_chat_interface, create_about_tab)
|
18 |
from front.utils import process_figures
|
|
|
142 |
elem_id="graphs-container"
|
143 |
)
|
144 |
with gr.Tab("Vanna", elem_id="tab-vanna", id=6) as tab_vanna:
|
145 |
+
vanna_table = gr.DataFrame([], elem_id="vanna-display")
|
146 |
+
vanna_display = gr.Plot()
|
147 |
|
148 |
return {
|
149 |
"chatbot": chatbot,
|
|
|
166 |
"tab_graphs": tab_graphs,
|
167 |
"tab_papers": tab_papers,
|
168 |
"graph_container": graphs_container,
|
169 |
+
"vanna_table" : vanna_table,
|
170 |
"vanna_display": vanna_display
|
171 |
}
|
172 |
|
|
|
197 |
tab_graphs = main_tab_components["tab_graphs"]
|
198 |
tab_papers = main_tab_components["tab_papers"]
|
199 |
graphs_container = main_tab_components["graph_container"]
|
200 |
+
vanna_table = main_tab_components["vanna_table"]
|
201 |
vanna_display = main_tab_components["vanna_display"]
|
202 |
|
203 |
+
|
204 |
config_open = config_components["config_open"]
|
205 |
config_modal = config_components["config_modal"]
|
206 |
dropdown_sources = config_components["dropdown_sources"]
|
|
|
249 |
for component in [textbox, examples_hidden]:
|
250 |
component.submit(find_papers, [component, after, dropdown_external_sources], [papers_html, citations_network, papers_summary])
|
251 |
|
252 |
+
# ttd_data.change(lambda x: x["df_output"], inputs=[ttd_data], outputs=[vanna_display])
|
253 |
+
textbox.submit(ask_vanna, [textbox], [vanna_table, vanna_display])
|
254 |
|
255 |
def main_ui():
|
256 |
# config_open = gr.State(True)
|
climateqa/engine/chains/drias_retriever.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import sys
|
2 |
import os
|
3 |
-
from climateqa.talk_to_data.main import ask_vanna
|
4 |
|
5 |
|
6 |
def make_drias_retriever_node(llm):
|
|
|
1 |
import sys
|
2 |
import os
|
3 |
+
from climateqa.engine.talk_to_data.main import ask_vanna
|
4 |
|
5 |
|
6 |
def make_drias_retriever_node(llm):
|
climateqa/{talk_to_data → engine/talk_to_data}/deprecated_vanna_remote.py
RENAMED
File without changes
|
climateqa/engine/talk_to_data/drias_metadata.json
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"Frequency of rainy days index": {
|
3 |
+
"description": "Frequency_of_rainy_days_index table contains the frequency index of rainy days for each latitude longitude couple for each date in the past and the future.\nThe columns include:\n- `time`: Timestamp representing the date of the data.\n- `x`: Coordinate in the Lambert II projection for the location.\n- `y`: Coordinate in the Lambert II projection for the location.\n- `IFM40D`: Frequency index of rainy days.\n- `Lon`: Geographic longitude of the location.\n- `lat`: Geographic latitude of the location.",
|
4 |
+
"sql_query": "CREATE TABLE Frequency_of_rainy_days_index (\n time TIMESTAMP,\n x FLOAT,\n y FLOAT,\n IFM40D FLOAT,\n Lon FLOAT,\n lat FLOAT\n);"
|
5 |
+
},
|
6 |
+
"Remarkable daily precipitation total (Q99)": {
|
7 |
+
"description": "Remarkable_daily_precipitation_total_ total table contains the daily cumulative exceptional rainfall (Q99) for each latitude longitude couple for each date in the past and the future.\nThe columns include:\n- `time`: Timestamp representing the date of the data.\n- `x`: Coordinate in the Lambert II projection for the location.\n- `y`: Coordinate in the Lambert II projection for the location.\n- `LambertParisII`: Indicates that the x, y coordinates are in the Lambert Paris II projection.\n- `RRq99`: Cumulative exceptional rainfall.\n- `lat`: Geographic latitude of the location.\n- `lon`: Geographic longitude of the location.",
|
8 |
+
"sql_query": "CREATE TABLE Remarkable_daily_precipitation_total_(Q99) (\n time TIMESTAMP,\n x FLOAT,\n y FLOAT,\n LambertParisII VARCHAR(255),\n RRq99 FLOAT,\n lat FLOAT,\n lon FLOAT\n);"
|
9 |
+
},
|
10 |
+
"Frequency of remarkable daily precipitation": {
|
11 |
+
"description": "The Frequency of remarkable daily precipitation table contains the frequency of daily exceptional rainfall in the past and the future.\nThe columns include:\n- `time`: Timestamp representing the date of the data.\n- `x`: Coordinate in the Lambert II projection for the location.\n- `y`: Coordinate in the Lambert II projection for the location.\n- `LambertParisII`: Indicates that the x, y coordinates are in the Lambert Paris II projection.\n- `RRq99refD`: Frequency of exceptional rainfall.\n- `lat`: Geographic latitude of the location.\n- `lon`: Geographic longitude of the location.",
|
12 |
+
"sql_query": "CREATE TABLE Frequency_of_remarkable_daily_precipitation (\n time TIMESTAMP,\n x FLOAT,\n y FLOAT,\n LambertParisII VARCHAR(255),\n RRq99refD FLOAT,\n lat FLOAT,\n lon FLOAT\n);"
|
13 |
+
},
|
14 |
+
"Winter precipitation total": {
|
15 |
+
"description": "The Winter precipitation total table contains the cumulative winter precipitation in the past and the future.\nThe columns include:\n- `time`: Timestamp representing the date of the data.\n- `x`: Coordinate in the Lambert II projection for the location.\n- `y`: Coordinate in the Lambert II projection for the location.\n- `LambertParisII`: Indicates that the x, y coordinates are in the Lambert Paris II projection.\n- `RR`: Cumulative winter precipitation.\n- `lat`: Geographic latitude of the location.\n- `lon`: Geographic longitude of the location.",
|
16 |
+
"sql_query": "CREATE TABLE Winter_precipitation_total (\n time TIMESTAMP,\n x FLOAT,\n y FLOAT,\n LambertParisII VARCHAR(255),\n RR FLOAT,\n lat FLOAT,\n lon FLOAT\n);"
|
17 |
+
},
|
18 |
+
"Summer precipitation total": {
|
19 |
+
"description": "The Summer precipitation total table contains the cumulative summer precipitation in the past and the future.\nThe columns include:\n- `time`: Timestamp representing the date of the data.\n- `x`: Coordinate in the Lambert II projection for the location.\n- `y`: Coordinate in the Lambert II projection for the location.\n- `LambertParisII`: Indicates that the x, y coordinates are in the Lambert Paris II projection.\n- `RR`: Cumulative summer precipitation.\n- `lat`: Geographic latitude of the location.\n- `lon`: Geographic longitude of the location.",
|
20 |
+
"sql_query": "CREATE TABLE Summer_precipitation_total (\n time TIMESTAMP,\n x FLOAT,\n y FLOAT,\n LambertParisII VARCHAR(255),\n RR FLOAT,\n lat FLOAT,\n lon FLOAT\n);"
|
21 |
+
},
|
22 |
+
"Annual precipitation total": {
|
23 |
+
"description": "The Annual precipitation total table contains information on the cumulative annual precipitation in the past and the future.\nbased on Lambert Paris II projections.\nThe variables are as follows:\n- 'y' and 'x': Lambert Paris II coordinates for the location.\n- 'time': Timestamp indicating the observation period.\n- 'LambertParisII': Indicates that the x, y coordinates are in the Lambert Paris II projection.\n- 'lat' and 'lon': Latitude and longitude of the location.\n- 'RR': Cumulative annual precipitation.",
|
24 |
+
"sql_query": "CREATE TABLE Annual_precipitation_total (\n y FLOAT,\n x FLOAT,\n time TIMESTAMP,\n LambertParisII VARCHAR(255),\n lat FLOAT,\n lon FLOAT,\n RR FLOAT\n);"
|
25 |
+
},
|
26 |
+
"Extreme precipitation intensity": {
|
27 |
+
"description": "The Extreme precipitation intensity table contains information on the intensity of extreme precipitation in the past and the future,\nwhich represents the maximum value of total annual precipitation.\nThe variables are as follows:\n- 'y' and 'x': Lambert Paris II coordinates for the location.\n- 'time': Timestamp indicating the observation period.\n- 'LambertParisII': Indicates that the x, y coordinates are in the Lambert Paris II projection.\n- 'lat' and 'lon': Latitude and longitude of the location.\n- 'RX1d': Intensity of extreme precipitation (maximum annual total precipitation).",
|
28 |
+
"sql_query": "CREATE TABLE Extreme_precipitation_intensity (\n y FLOAT,\n x FLOAT,\n time TIMESTAMP,\n LambertParisII VARCHAR(255),\n lat FLOAT,\n lon FLOAT,\n RX1d FLOAT\n);"
|
29 |
+
},
|
30 |
+
"Drought index": {
|
31 |
+
"description": "The Drought index table contains information on the drought index based on observations over the past and the future.\nThe variables are as follows:\n- 'time': Timestamp indicating the observation period.\n- 'y' and 'x': Lambert Paris II coordinates for the location.\n- 'LambertParisII': Indicates that the x, y coordinates are in the Lambert Paris II projection.\n- 'lat' and 'lon': Latitude and longitude of the location.\n- 'SWI04D': Drought index based on the analysis of precipitation and temperatures.",
|
32 |
+
"sql_query": "CREATE TABLE Drought_index (\n time TIMESTAMP,\n y FLOAT,\n x FLOAT,\n lat FLOAT,\n lon FLOAT,\n LambertParisII VARCHAR(255),\n SWI04D FLOAT\n);"
|
33 |
+
},
|
34 |
+
"Mean winter temperature": {
|
35 |
+
"description": "The Mean winter temperature table contains information on the average (mean) winter temperature in the past and the future.\nThe variables are as follows:\n- 'y' and 'x': Lambert Paris II coordinates for the location.\n- 'time': Timestamp indicating the observation period.\n- 'LambertParisII': Indicates that the x, y coordinates are in the Lambert Paris II projection.\n- 'lat' and 'lon': Latitude and longitude of the location.\n- 'TMm': Average winter temperature.",
|
36 |
+
"sql_query": "CREATE TABLE Mean_winter_temperature (\n y FLOAT,\n x FLOAT,\n time TIMESTAMP,\n LambertParisII VARCHAR(255),\n lat FLOAT,\n lon FLOAT,\n TMm FLOAT\n);"
|
37 |
+
},
|
38 |
+
"Mean summer temperature": {
|
39 |
+
"description": "The Mean summer temperature table contains information on the average summer temperature in the past and the future.\nThe variables are as follows:\n- 'y' and 'x': Lambert Paris II coordinates for the location.\n- 'time': Timestamp indicating the observation period.\n- 'LambertParisII': Indicates that the x and y coordinates are in Lambert Paris II projection.\n- 'lat' and 'lon': Latitude and longitude of the location.\n- 'TMm': Average summer temperature.",
|
40 |
+
"sql_query": "CREATE TABLE Mean_summer_temperature (\n y FLOAT,\n x FLOAT,\n time TIMESTAMP,\n LambertParisII VARCHAR(255),\n lat FLOAT,\n lon FLOAT,\n TMm FLOAT\n);"
|
41 |
+
},
|
42 |
+
"Number of tropical nights": {
|
43 |
+
"description": "The Number of tropical nights table contains information on the average summer temperature in the past and the future.\nThe variables are as follows:\n- 'y' and 'x': Lambert Paris II coordinates for the location.\n- 'time': Timestamp indicating the observation period.\n- 'LambertParisII': Indicates that the x, y coordinates are in the Lambert Paris II projection.\n- 'lat' and 'lon': Latitude and longitude of the location.\n- 'TMm': Average summer temperature.",
|
44 |
+
"sql_query": "CREATE TABLE Number_of_tropical_nights (\n y FLOAT,\n x FLOAT,\n time TIMESTAMP,\n LambertParisII VARCHAR(255),\n lat FLOAT,\n lon FLOAT,\n TR FLOAT\n);"
|
45 |
+
},
|
46 |
+
"Number of days with Tx above 30C": {
|
47 |
+
"description": "The Number of days with Tx above 30C table contains information on the number of days when the maximum temperature in the past and the future\nis greater than or equal to 30°C.\nThe variables are as follows:\n- 'y' and 'x': Lambert Paris II coordinates for the location.\n- 'time': Timestamp indicating the observation period.\n- 'LambertParisII': Indicates that the x, y coordinates are in the Lambert Paris II projection.\n- 'lat' and 'lon': Latitude and longitude of the location.\n- 'TX30D': Number of days with Tx ≥ 30°C.",
|
48 |
+
"sql_query": "CREATE TABLE Number_of_days_with_Tx_above_30C (\n y FLOAT,\n x FLOAT,\n time TIMESTAMP,\n LambertParisII VARCHAR(255),\n lat FLOAT,\n lon FLOAT,\n TX30D FLOAT\n);"
|
49 |
+
},
|
50 |
+
"Number of days with Tx above 35C": {
|
51 |
+
"description": "The Number of days with Tx above 35C table contains information on the number of days when the maximum temperature in the past and the future\nis greater than or equal to 35°C.\nThe variables are as follows:\n- 'y' and 'x': Lambert Paris II coordinates for the location.\n- 'time': Timestamp indicating the observation period.\n- 'LambertParisII': Indicates that the x, y coordinates are in the Lambert Paris II projection.\n- 'lat' and 'lon': Latitude and longitude of the location.\n- 'TX35D': Number of days with Tx ≥ 35°C.",
|
52 |
+
"sql_query": "CREATE TABLE Number_of_days_with_Tx_above_35C (\n y FLOAT,\n x FLOAT,\n time TIMESTAMP,\n LambertParisII VARCHAR(255),\n lat FLOAT,\n lon FLOAT,\n TX35D FLOAT\n);"
|
53 |
+
},
|
54 |
+
"Maximum summer temperature": {
|
55 |
+
"description": "The Maximum summer temperature table contains information on the maximum temperature in summer in the past and the future,\nwhich is the highest temperature recorded during the summer period.\nThe variables are as follows:\n- 'y' and 'x': Lambert Paris II coordinates for the location.\n- 'time': Timestamp indicating the observation period.\n- 'LambertParisII': Indicates that the x, y coordinates are in the Lambert Paris II projection.\n- 'lat' and 'lon': Latitude and longitude of the location.\n- 'TXm': Maximum temperature recorded in summer.",
|
56 |
+
"sql_query": "CREATE TABLE Maximum_summer_temperature (\n y FLOAT,\n x FLOAT,\n time TIMESTAMP,\n LambertParisII VARCHAR(255),\n lat FLOAT,\n lon FLOAT,\n TXm FLOAT\n);"
|
57 |
+
}
|
58 |
+
}
|
climateqa/{talk_to_data → engine/talk_to_data}/how_to_use_main.ipynb
RENAMED
@@ -9,7 +9,7 @@
|
|
9 |
},
|
10 |
{
|
11 |
"cell_type": "code",
|
12 |
-
"execution_count":
|
13 |
"metadata": {},
|
14 |
"outputs": [
|
15 |
{
|
@@ -17,8 +17,7 @@
|
|
17 |
"output_type": "stream",
|
18 |
"text": [
|
19 |
"The autoreload extension is already loaded. To reload it, use:\n",
|
20 |
-
" %reload_ext autoreload\n"
|
21 |
-
"Loading embeddings model: BAAI/bge-base-en-v1.5\n"
|
22 |
]
|
23 |
}
|
24 |
],
|
@@ -42,7 +41,7 @@
|
|
42 |
},
|
43 |
{
|
44 |
"cell_type": "code",
|
45 |
-
"execution_count":
|
46 |
"metadata": {},
|
47 |
"outputs": [],
|
48 |
"source": [
|
@@ -61,44 +60,58 @@
|
|
61 |
},
|
62 |
{
|
63 |
"cell_type": "code",
|
64 |
-
"execution_count":
|
65 |
"metadata": {},
|
66 |
"outputs": [
|
67 |
{
|
68 |
"name": "stdout",
|
69 |
"output_type": "stream",
|
70 |
"text": [
|
71 |
-
"SQL Prompt: [{'role': 'system', 'content': \"You are a SQLite expert. Please help to generate a SQL query to answer the question. Your response should ONLY be based on the given context and follow the response guidelines and format instructions. \\n===Tables \\n\\n CREATE TABLE Mean_winter_temperature (\\n y FLOAT,\\n x FLOAT,\\n year INT, \\n month INT, \\n day INT \\n,\\n LambertParisII VARCHAR(255),\\n lat FLOAT,\\n lon FLOAT,\\n TMm FLOAT, -- Température moyenne en hiver\\n );\\n \\n\\n\\n CREATE TABLE Mean_summer_temperature (\\n y FLOAT,\\n x FLOAT,\\n year INT, \\n month INT, \\n day INT \\n,\\n LambertParisII VARCHAR(255),\\n lat FLOAT,\\n lon FLOAT,\\n TMm FLOAT, -- Température moyenne en été\\n );\\n \\n\\n\\n===Additional Context \\n\\n\\n
|
72 |
-
"Using model gpt-4o-mini for
|
73 |
-
"LLM Response:
|
74 |
-
"
|
75 |
-
"
|
76 |
-
"
|
77 |
-
"
|
|
|
|
|
|
|
78 |
"```\n",
|
79 |
-
"Extracted SQL: SELECT
|
80 |
-
"
|
81 |
-
"
|
82 |
-
"
|
83 |
-
"
|
84 |
-
"
|
85 |
-
"
|
86 |
-
"Using model gpt-4o-mini for
|
87 |
-
"
|
88 |
-
"Couldn't run sql: Execution failed on sql 'La context fourni ne contient pas d'informations sur les prévisions de température pour les prochaines années. Par conséquent, je ne peux pas générer de requête SQL pour répondre à cette question.': near \"La\": syntax error\n"
|
89 |
]
|
90 |
},
|
91 |
{
|
92 |
-
"
|
93 |
-
|
94 |
-
|
95 |
-
"
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
}
|
103 |
],
|
104 |
"source": [
|
|
|
9 |
},
|
10 |
{
|
11 |
"cell_type": "code",
|
12 |
+
"execution_count": 3,
|
13 |
"metadata": {},
|
14 |
"outputs": [
|
15 |
{
|
|
|
17 |
"output_type": "stream",
|
18 |
"text": [
|
19 |
"The autoreload extension is already loaded. To reload it, use:\n",
|
20 |
+
" %reload_ext autoreload\n"
|
|
|
21 |
]
|
22 |
}
|
23 |
],
|
|
|
41 |
},
|
42 |
{
|
43 |
"cell_type": "code",
|
44 |
+
"execution_count": 4,
|
45 |
"metadata": {},
|
46 |
"outputs": [],
|
47 |
"source": [
|
|
|
60 |
},
|
61 |
{
|
62 |
"cell_type": "code",
|
63 |
+
"execution_count": 5,
|
64 |
"metadata": {},
|
65 |
"outputs": [
|
66 |
{
|
67 |
"name": "stdout",
|
68 |
"output_type": "stream",
|
69 |
"text": [
|
70 |
+
"SQL Prompt: [{'role': 'system', 'content': \"You are a SQLite expert. Please help to generate a SQL query to answer the question. Your response should ONLY be based on the given context and follow the response guidelines and format instructions. \\n===Tables \\n\\n CREATE TABLE Mean_winter_temperature (\\n y FLOAT,\\n x FLOAT,\\n year INT, \\n month INT, \\n day INT \\n,\\n LambertParisII VARCHAR(255),\\n lat FLOAT,\\n lon FLOAT,\\n TMm FLOAT, -- Température moyenne en hiver\\n );\\n \\n\\n\\n CREATE TABLE Mean_summer_temperature (\\n y FLOAT,\\n x FLOAT,\\n year INT, \\n month INT, \\n day INT \\n,\\n LambertParisII VARCHAR(255),\\n lat FLOAT,\\n lon FLOAT,\\n TMm FLOAT, -- Température moyenne en été\\n );\\n \\n\\n\\n===Additional Context \\n\\n\\n The Number of days with Tx above 35C table contains information on the number of days when the maximum temperature in the past and the future\\n is greater than or equal to 35°C.\\n The variables are as follows:\\n - 'y' and 'x': Lambert Paris II coordinates for the location.\\n - year: Year of the observation.\\n\\n - month : Month of the observation.\\n\\n - day: Day of the observation.\\n\\n - 'LambertParisII': Indicates that the x, y coordinates are in the Lambert Paris II projection.\\n - 'lat' and 'lon': Latitude and longitude of the location.\\n - 'TX35D': Number of days with Tx ≥ 35°C.\\n \\n\\n\\n The Number of days with Tx above 30C table contains information on the number of days when the maximum temperature in the past and the future\\n is greater than or equal to 30°C.\\n The variables are as follows:\\n - 'y' and 'x': Lambert Paris II coordinates for the location.\\n - year: Year of the observation.\\n\\n - month : Month of the observation.\\n\\n - day: Day of the observation.\\n\\n - 'LambertParisII': Indicates that the x, y coordinates are in the Lambert Paris II projection.\\n - 'lat' and 'lon': Latitude and longitude of the location.\\n - 'TX30D': Number of days with Tx ≥ 30°C.\\n \\n\\n===Response Guidelines \\n1. If the provided context is sufficient, please generate a valid SQL query without any explanations for the question. \\n2. If the provided context is almost sufficient but requires knowledge of a specific string in a particular column, please generate an intermediate SQL query to find the distinct strings in that column. Prepend the query with a comment saying intermediate_sql \\n3. If the provided context is insufficient, please explain why it can't be generated. \\n4. Please use the most relevant table(s). \\n5. If the question has been asked and answered before, please repeat the answer exactly as it was given before. \\n6. Ensure that the output SQL is SQLite-compliant and executable, and free of syntax errors. \\n\"}, {'role': 'user', 'content': 'Quelle sera la température à lat, long : (43.2961743, 5.3699525) sur les prochaines années ?'}]\n",
|
71 |
+
"Using model gpt-4o-mini for 828.0 tokens (approx)\n",
|
72 |
+
"LLM Response: ```sql\n",
|
73 |
+
"SELECT year, TMm \n",
|
74 |
+
"FROM Mean_winter_temperature \n",
|
75 |
+
"WHERE lat = 43.2961743 AND lon = 5.3699525\n",
|
76 |
+
"UNION ALL\n",
|
77 |
+
"SELECT year, TMm \n",
|
78 |
+
"FROM Mean_summer_temperature \n",
|
79 |
+
"WHERE lat = 43.2961743 AND lon = 5.3699525;\n",
|
80 |
"```\n",
|
81 |
+
"Extracted SQL: SELECT year, TMm \n",
|
82 |
+
"FROM Mean_winter_temperature \n",
|
83 |
+
"WHERE lat = 43.2961743 AND lon = 5.3699525\n",
|
84 |
+
"UNION ALL\n",
|
85 |
+
"SELECT year, TMm \n",
|
86 |
+
"FROM Mean_summer_temperature \n",
|
87 |
+
"WHERE lat = 43.2961743 AND lon = 5.3699525;\n",
|
88 |
+
"Using model gpt-4o-mini for 218.5 tokens (approx)\n",
|
89 |
+
"[(2031, 9.952474117647114), (2031, 9.952474117647114), (2032, 10.142322941176474), (2032, 10.142322941176474), (2033, 9.907942941176486), (2033, 9.907942941176486), (2034, 9.548873529411765), (2034, 9.548873529411765), (2035, 10.284758235294191), (2035, 10.284758235294191), (2036, 10.372100000000046), (2036, 10.372100000000046), (2037, 9.98571000000004), (2037, 9.98571000000004), (2038, 10.221372352941216), (2038, 10.221372352941216), (2039, 10.222609411764722), (2039, 10.222609411764722), (2040, 10.473662941176485), (2040, 10.473662941176485), (2041, 10.427640588235306), (2041, 10.427640588235306), (2042, 10.364736470588241), (2042, 10.364736470588241), (2043, 10.112910588235309), (2043, 10.112910588235309), (2044, 10.250792352941176), (2044, 10.250792352941176), (2045, 10.166119411764669), (2045, 10.166119411764669), (2046, 10.728997647058861), (2046, 10.728997647058861), (2047, 10.347248823529412), (2047, 10.347248823529412), (2048, 10.706604117647089), (2048, 10.706604117647089), (2049, 10.59243764705883), (2049, 10.59243764705883), (2050, 10.63225529411767), (2050, 10.63225529411767), (2031, 24.061035294117687), (2031, 24.061035294117687), (2032, 24.530692941176483), (2032, 24.530692941176483), (2033, 24.722234705882386), (2033, 24.722234705882386), (2034, 23.84629176470588), (2034, 23.84629176470588), (2035, 24.231422352941195), (2035, 24.231422352941195), (2036, 24.488941764705885), (2036, 24.488941764705885), (2037, 24.79424117647062), (2037, 24.79424117647062), (2038, 24.730553529411793), (2038, 24.730553529411793), (2039, 24.44979882352942), (2039, 24.44979882352942), (2040, 24.40726882352942), (2040, 24.40726882352942), (2041, 24.768547647058824), (2041, 24.768547647058824), (2042, 24.53479647058822), (2042, 24.53479647058822), (2043, 24.769181176470624), (2043, 24.769181176470624), (2044, 24.489877058823538), (2044, 24.489877058823538), (2045, 24.448076470588262), (2045, 24.448076470588262), (2046, 25.111282352941203), (2046, 25.111282352941203), (2047, 24.72313823529413), (2047, 24.72313823529413), (2048, 25.187577058823535), (2048, 25.187577058823535), (2049, 24.829653529411814), (2049, 24.829653529411814), (2050, 25.053394117647144), (2050, 25.053394117647144)]\n"
|
|
|
90 |
]
|
91 |
},
|
92 |
{
|
93 |
+
"data": {
|
94 |
+
"text/plain": [
|
95 |
+
"('SELECT year, TMm \\nFROM Mean_winter_temperature \\nWHERE lat = 43.166954040527344 AND lon = 5.430534839630127\\nUNION ALL\\nSELECT year, TMm \\nFROM Mean_summer_temperature \\nWHERE lat = 43.166954040527344 AND lon = 5.430534839630127;',\n",
|
96 |
+
" year TMm\n",
|
97 |
+
" 0 2031 9.952474\n",
|
98 |
+
" 1 2031 9.952474\n",
|
99 |
+
" 2 2032 10.142323\n",
|
100 |
+
" 3 2032 10.142323\n",
|
101 |
+
" 4 2033 9.907943\n",
|
102 |
+
" .. ... ...\n",
|
103 |
+
" 75 2048 25.187577\n",
|
104 |
+
" 76 2049 24.829654\n",
|
105 |
+
" 77 2049 24.829654\n",
|
106 |
+
" 78 2050 25.053394\n",
|
107 |
+
" 79 2050 25.053394\n",
|
108 |
+
" \n",
|
109 |
+
" [80 rows x 2 columns])"
|
110 |
+
]
|
111 |
+
},
|
112 |
+
"execution_count": 5,
|
113 |
+
"metadata": {},
|
114 |
+
"output_type": "execute_result"
|
115 |
}
|
116 |
],
|
117 |
"source": [
|
climateqa/engine/talk_to_data/main.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from climateqa.engine.talk_to_data.myVanna import MyVanna
|
2 |
+
from climateqa.engine.talk_to_data.utils import loc2coords, detect_location_with_openai, detectTable, nearestNeighbourSQL
|
3 |
+
import sqlite3
|
4 |
+
import os
|
5 |
+
import pandas as pd
|
6 |
+
from climateqa.engine.llm import get_llm
|
7 |
+
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
|
10 |
+
load_dotenv()
|
11 |
+
|
12 |
+
|
13 |
+
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
14 |
+
PC_API_KEY = os.getenv('VANNA_PINECONE_API_KEY')
|
15 |
+
INDEX_NAME = os.getenv('VANNA_INDEX_NAME')
|
16 |
+
VANNA_MODEL = os.getenv('VANNA_MODEL')
|
17 |
+
|
18 |
+
|
19 |
+
#Vanna object
|
20 |
+
vn = MyVanna(config = {"temperature": 0, "api_key": OPENAI_API_KEY, 'model': VANNA_MODEL, 'pc_api_key': PC_API_KEY, 'index_name': INDEX_NAME})
|
21 |
+
db_vanna_path = os.path.join(os.path.dirname(__file__), "database/drias.db")
|
22 |
+
vn.connect_to_sqlite(db_vanna_path)
|
23 |
+
|
24 |
+
llm = get_llm(provider="openai")
|
25 |
+
|
26 |
+
# def ask_vanna(query):
|
27 |
+
# location = detect_location_with_openai(OPENAI_API_KEY, query)
|
28 |
+
# if location:
|
29 |
+
# coords = loc2coords(location)
|
30 |
+
# user_input = query.replace(location, f"lat, long : {coords}")
|
31 |
+
# answer = vn.ask(user_input, print_results=False, allow_llm_to_see_data=True)
|
32 |
+
# table = detectTable(answer[0])
|
33 |
+
# coords2 = nearestNeighbourSQL(db_vanna_path, coords, table[0])
|
34 |
+
|
35 |
+
# query = answer[0].replace(f"{coords[0]}", f"{coords2[0]}")
|
36 |
+
# sql_query = query.replace(f"{coords[1]}", f"{coords2[1]}")
|
37 |
+
|
38 |
+
# db = sqlite3.connect(db_vanna_path)
|
39 |
+
# result = db.cursor().execute(sql_query).fetchall()
|
40 |
+
# print(result)
|
41 |
+
# df = pd.DataFrame(result, columns=answer[1].columns)
|
42 |
+
|
43 |
+
# else:
|
44 |
+
# answer = vn.ask(query, visualize=True, print_results=False, allow_llm_to_see_data=True)
|
45 |
+
# sql_query = answer[0]
|
46 |
+
# df = answer[1]
|
47 |
+
|
48 |
+
# return (sql_query, df)
|
49 |
+
def replace_coordonates(coords, sql_query, coords_tables):
|
50 |
+
n = sql_query.count(str(coords[0]))
|
51 |
+
sql_query_new_coords = sql_query
|
52 |
+
|
53 |
+
for i in range(n):
|
54 |
+
sql_query_new_coords = sql_query_new_coords.replace(str(coords[0]), str(coords_tables[i][0]),1)
|
55 |
+
sql_query_new_coords = sql_query_new_coords.replace(str(coords[1]), str(coords_tables[i][1]),1)
|
56 |
+
return sql_query_new_coords
|
57 |
+
|
58 |
+
def ask_vanna(query):
|
59 |
+
location = detect_location_with_openai(OPENAI_API_KEY, query)
|
60 |
+
if location:
|
61 |
+
|
62 |
+
coords = loc2coords(location)
|
63 |
+
user_input = query.replace(location, f"lat, long : {coords}")
|
64 |
+
sql_query, result_dataframe, figure = vn.ask(user_input, print_results=False, allow_llm_to_see_data=True)
|
65 |
+
table = detectTable(sql_query)
|
66 |
+
coords_tables = [nearestNeighbourSQL(db_vanna_path, coords, table[i]) for i in range(len(table))]
|
67 |
+
sql_query_new_coords = replace_coordonates(coords, sql_query, coords_tables)
|
68 |
+
sql_with_table_names = llm.invoke(f"Make the following sql query display the source table in the rows {sql_query_new_coords}. Just answer the query. The answer should not include ```sql\n").content
|
69 |
+
print("execute sql query : ", sql_with_table_names)
|
70 |
+
db = sqlite3.connect(db_vanna_path)
|
71 |
+
# if "lat" not in sql_with_table_names:
|
72 |
+
# sql_with_table_names = sql_with_table_names.replace("SELECT", "SELECT lat, lon,")
|
73 |
+
# result = db.cursor().execute(sql_with_table_names).fetchall()
|
74 |
+
result = db.cursor().execute(sql_query_new_coords).fetchall()
|
75 |
+
# df = pd.DataFrame(result, columns = list(result_dataframe.columns))
|
76 |
+
# df = pd.DataFrame(result, columns=["data_name"] + list(result_dataframe.columns))
|
77 |
+
df = pd.DataFrame(result)
|
78 |
+
|
79 |
+
plotly_code = vn.generate_plotly_code(
|
80 |
+
question="query",
|
81 |
+
sql="sql_with_table_names",
|
82 |
+
df_metadata=f"Running df.dtypes gives:\n {df.dtypes}",
|
83 |
+
)
|
84 |
+
|
85 |
+
fig = vn.get_plotly_figure(plotly_code=plotly_code, df=df)
|
86 |
+
|
87 |
+
return df, fig
|
88 |
+
else :
|
89 |
+
empty_df = pd.DataFrame()
|
90 |
+
empty_fig = {}
|
91 |
+
return empty_df, empty_fig
|
climateqa/{talk_to_data → engine/talk_to_data}/myVanna.py
RENAMED
@@ -1,5 +1,5 @@
|
|
1 |
from dotenv import load_dotenv
|
2 |
-
from climateqa.talk_to_data.vanna_class import MyCustomVectorDB
|
3 |
from vanna.openai import OpenAI_Chat
|
4 |
import os
|
5 |
|
|
|
1 |
from dotenv import load_dotenv
|
2 |
+
from climateqa.engine.talk_to_data.vanna_class import MyCustomVectorDB
|
3 |
from vanna.openai import OpenAI_Chat
|
4 |
import os
|
5 |
|
climateqa/engine/talk_to_data/pinecone_vanna_training.ipynb
ADDED
@@ -0,0 +1,592 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 37,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [
|
8 |
+
{
|
9 |
+
"name": "stdout",
|
10 |
+
"output_type": "stream",
|
11 |
+
"text": [
|
12 |
+
"The autoreload extension is already loaded. To reload it, use:\n",
|
13 |
+
" %reload_ext autoreload\n",
|
14 |
+
"{'temperature': 0.2, 'api_key': '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', 'model': 'gpt-4o-mini', 'pc_api_key': 'pcsk_5pEfJ8_GqSCikBaVhK3V6wehh4YHCegspQeshyWVesKeqmqzcmfLgkQRpWaUVJvSyTcdG', 'index_name': 'cqa-vanna'}\n",
|
15 |
+
"Loading embeddings model: BAAI/bge-base-en-v1.5\n"
|
16 |
+
]
|
17 |
+
}
|
18 |
+
],
|
19 |
+
"source": [
|
20 |
+
"from vanna_class import MyCustomVectorDB\n",
|
21 |
+
"from vanna.openai import OpenAI_Chat\n",
|
22 |
+
"import os\n",
|
23 |
+
"from dotenv import load_dotenv\n",
|
24 |
+
"\n",
|
25 |
+
"%load_ext autoreload\n",
|
26 |
+
"%autoreload 2\n",
|
27 |
+
"\n",
|
28 |
+
"load_dotenv()\n",
|
29 |
+
"\n",
|
30 |
+
"OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')\n",
|
31 |
+
"OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')\n",
|
32 |
+
"PC_API_KEY = os.getenv('VANNA_PINECONE_API_KEY')\n",
|
33 |
+
"INDEX_NAME = os.getenv('VANNA_INDEX_NAME')\n",
|
34 |
+
"VANNA_MODEL = os.getenv('VANNA_MODEL')\n",
|
35 |
+
"\n",
|
36 |
+
"INDEX_NAME = \"cqa-vanna\"\n",
|
37 |
+
"PC_API_KEY = \"pcsk_5pEfJ8_GqSCikBaVhK3V6wehh4YHCegspQeshyWVesKeqmqzcmfLgkQRpWaUVJvSyTcdG\"\n",
|
38 |
+
"\n",
|
39 |
+
"class MyVanna(MyCustomVectorDB, OpenAI_Chat):\n",
|
40 |
+
" def __init__(self, config=None):\n",
|
41 |
+
" print(config)\n",
|
42 |
+
" MyCustomVectorDB.__init__(self, config=config)\n",
|
43 |
+
" OpenAI_Chat.__init__(self, config=config)\n",
|
44 |
+
"\n",
|
45 |
+
"vn = MyVanna(\n",
|
46 |
+
" config={\n",
|
47 |
+
" 'temperature': 0.2,\n",
|
48 |
+
" 'api_key': OPENAI_API_KEY,\n",
|
49 |
+
" 'model': 'gpt-4o-mini',\n",
|
50 |
+
" 'pc_api_key': PC_API_KEY,\n",
|
51 |
+
" 'index_name': INDEX_NAME\n",
|
52 |
+
" }\n",
|
53 |
+
")"
|
54 |
+
]
|
55 |
+
},
|
56 |
+
{
|
57 |
+
"cell_type": "code",
|
58 |
+
"execution_count": 49,
|
59 |
+
"metadata": {},
|
60 |
+
"outputs": [],
|
61 |
+
"source": [
|
62 |
+
"import json\n",
|
63 |
+
"\n",
|
64 |
+
"with open('drias_metadata.json', 'r') as file:\n",
|
65 |
+
" tables_info = json.load(file)\n"
|
66 |
+
]
|
67 |
+
},
|
68 |
+
{
|
69 |
+
"cell_type": "code",
|
70 |
+
"execution_count": 50,
|
71 |
+
"metadata": {},
|
72 |
+
"outputs": [],
|
73 |
+
"source": [
|
74 |
+
"def convert(tables_info):\n",
|
75 |
+
" text = \"\"\"- year: Year of the observation.\\n\n",
|
76 |
+
" - month : Month of the observation.\\n\n",
|
77 |
+
" - day: Day of the observation.\\n\"\"\"\n",
|
78 |
+
" for table_name in tables_info:\n",
|
79 |
+
" tables_info[table_name]['description'] = tables_info[table_name]['description'].replace(\"- 'time': Timestamp indicating the observation period.\", text)\n",
|
80 |
+
" tables_info[table_name]['description'] = tables_info[table_name]['description'].replace(\"- `time`: Timestamp representing the date of the data.\", text)\n",
|
81 |
+
" tables_info[table_name]['sql_query'] = tables_info[table_name]['sql_query'].replace(\"time TIMESTAMP\", \"year INT, \\n month INT, \\n day INT \\n\")\n",
|
82 |
+
" \n",
|
83 |
+
" return tables_info"
|
84 |
+
]
|
85 |
+
},
|
86 |
+
{
|
87 |
+
"cell_type": "code",
|
88 |
+
"execution_count": 51,
|
89 |
+
"metadata": {},
|
90 |
+
"outputs": [
|
91 |
+
{
|
92 |
+
"data": {
|
93 |
+
"text/plain": [
|
94 |
+
"{'Frequency of rainy days index': {'description': 'The Frequency of rainy days index table contains the frequency index of rainy days for each latitude longitude couple for each date in the past and the future.\\nThe columns include:\\n- year: Year of the observation.\\n\\n - month : Month of the observation.\\n\\n - day: Day of the observation.\\n\\n- `x`: Coordinate in the Lambert II projection for the location.\\n- `y`: Coordinate in the Lambert II projection for the location.\\n- `IFM40D`: Frequency index of rainy days.\\n- `Lon`: Geographic longitude of the location.\\n- `lat`: Geographic latitude of the location.',\n",
|
95 |
+
" 'sql_query': 'CREATE TABLE Frequency_of_rainy_days_index (\\n year INT, \\n month INT, \\n day INT \\n,\\n x FLOAT,\\n y FLOAT,\\n IFM40D FLOAT,\\n Lon FLOAT,\\n lat FLOAT\\n);'},\n",
|
96 |
+
" 'Remarkable daily precipitation total (Q99)': {'description': 'The Remarkable daily precipitation total table contains the daily cumulative exceptional rainfall (Q99) for each latitude longitude couple for each date in the past and the future.\\nThe columns include:\\n- year: Year of the observation.\\n\\n - month : Month of the observation.\\n\\n - day: Day of the observation.\\n\\n- `x`: Coordinate in the Lambert II projection for the location.\\n- `y`: Coordinate in the Lambert II projection for the location.\\n- `LambertParisII`: Indicates that the x, y coordinates are in the Lambert Paris II projection.\\n- `RRq99`: Cumulative exceptional rainfall.\\n- `lat`: Geographic latitude of the location.\\n- `lon`: Geographic longitude of the location.',\n",
|
97 |
+
" 'sql_query': 'CREATE TABLE Remarkable_daily_precipitation_total_(Q99) (\\n year INT, \\n month INT, \\n day INT \\n,\\n x FLOAT,\\n y FLOAT,\\n LambertParisII VARCHAR(255),\\n RRq99 FLOAT,\\n lat FLOAT,\\n lon FLOAT\\n);'},\n",
|
98 |
+
" 'Frequency of remarkable daily precipitation': {'description': 'The Frequency of remarkable daily precipitation table contains the frequency of daily exceptional rainfall in the past and the future.\\nThe columns include:\\n- year: Year of the observation.\\n\\n - month : Month of the observation.\\n\\n - day: Day of the observation.\\n\\n- `x`: Coordinate in the Lambert II projection for the location.\\n- `y`: Coordinate in the Lambert II projection for the location.\\n- `LambertParisII`: Indicates that the x, y coordinates are in the Lambert Paris II projection.\\n- `RRq99refD`: Frequency of exceptional rainfall.\\n- `lat`: Geographic latitude of the location.\\n- `lon`: Geographic longitude of the location.',\n",
|
99 |
+
" 'sql_query': 'CREATE TABLE Frequency_of_remarkable_daily_precipitation (\\n year INT, \\n month INT, \\n day INT \\n,\\n x FLOAT,\\n y FLOAT,\\n LambertParisII VARCHAR(255),\\n RRq99refD FLOAT,\\n lat FLOAT,\\n lon FLOAT\\n);'},\n",
|
100 |
+
" 'Winter precipitation total': {'description': 'The Winter precipitation total table contains the cumulative winter precipitation in the past and the future.\\nThe columns include:\\n- year: Year of the observation.\\n\\n - month : Month of the observation.\\n\\n - day: Day of the observation.\\n\\n- `x`: Coordinate in the Lambert II projection for the location.\\n- `y`: Coordinate in the Lambert II projection for the location.\\n- `LambertParisII`: Indicates that the x, y coordinates are in the Lambert Paris II projection.\\n- `RR`: Cumulative winter precipitation.\\n- `lat`: Geographic latitude of the location.\\n- `lon`: Geographic longitude of the location.',\n",
|
101 |
+
" 'sql_query': 'CREATE TABLE Winter_precipitation_total (\\n year INT, \\n month INT, \\n day INT \\n,\\n x FLOAT,\\n y FLOAT,\\n LambertParisII VARCHAR(255),\\n RR FLOAT,\\n lat FLOAT,\\n lon FLOAT\\n);'},\n",
|
102 |
+
" 'Summer precipitation total': {'description': 'The Summer precipitation total table contains the cumulative summer precipitation in the past and the future.\\nThe columns include:\\n- year: Year of the observation.\\n\\n - month : Month of the observation.\\n\\n - day: Day of the observation.\\n\\n- `x`: Coordinate in the Lambert II projection for the location.\\n- `y`: Coordinate in the Lambert II projection for the location.\\n- `LambertParisII`: Indicates that the x, y coordinates are in the Lambert Paris II projection.\\n- `RR`: Cumulative summer precipitation.\\n- `lat`: Geographic latitude of the location.\\n- `lon`: Geographic longitude of the location.',\n",
|
103 |
+
" 'sql_query': 'CREATE TABLE Summer_precipitation_total (\\n year INT, \\n month INT, \\n day INT \\n,\\n x FLOAT,\\n y FLOAT,\\n LambertParisII VARCHAR(255),\\n RR FLOAT,\\n lat FLOAT,\\n lon FLOAT\\n);'},\n",
|
104 |
+
" 'Annual precipitation total': {'description': \"The Annual precipitation total table contains information on the cumulative annual precipitation in the past and the future.\\nbased on Lambert Paris II projections.\\nThe variables are as follows:\\n- 'y' and 'x': Lambert Paris II coordinates for the location.\\n- year: Year of the observation.\\n\\n - month : Month of the observation.\\n\\n - day: Day of the observation.\\n\\n- 'LambertParisII': Indicates that the x, y coordinates are in the Lambert Paris II projection.\\n- 'lat' and 'lon': Latitude and longitude of the location.\\n- 'RR': Cumulative annual precipitation.\",\n",
|
105 |
+
" 'sql_query': 'CREATE TABLE Annual_precipitation_total (\\n y FLOAT,\\n x FLOAT,\\n year INT, \\n month INT, \\n day INT \\n,\\n LambertParisII VARCHAR(255),\\n lat FLOAT,\\n lon FLOAT,\\n RR FLOAT\\n);'},\n",
|
106 |
+
" 'Extreme precipitation intensity': {'description': \"The Extreme precipitation intensity table contains information on the intensity of extreme precipitation in the past and the future,\\nwhich represents the maximum value of total annual precipitation.\\nThe variables are as follows:\\n- 'y' and 'x': Lambert Paris II coordinates for the location.\\n- year: Year of the observation.\\n\\n - month : Month of the observation.\\n\\n - day: Day of the observation.\\n\\n- 'LambertParisII': Indicates that the x, y coordinates are in the Lambert Paris II projection.\\n- 'lat' and 'lon': Latitude and longitude of the location.\\n- 'RX1d': Intensity of extreme precipitation (maximum annual total precipitation).\",\n",
|
107 |
+
" 'sql_query': 'CREATE TABLE Extreme_precipitation_intensity (\\n y FLOAT,\\n x FLOAT,\\n year INT, \\n month INT, \\n day INT \\n,\\n LambertParisII VARCHAR(255),\\n lat FLOAT,\\n lon FLOAT,\\n RX1d FLOAT\\n);'},\n",
|
108 |
+
" 'Drought index': {'description': \"The Drought index table contains information on the drought index based on observations over the past and the future.\\nThe variables are as follows:\\n- year: Year of the observation.\\n\\n - month : Month of the observation.\\n\\n - day: Day of the observation.\\n\\n- 'y' and 'x': Lambert Paris II coordinates for the location.\\n- 'LambertParisII': Indicates that the x, y coordinates are in the Lambert Paris II projection.\\n- 'lat' and 'lon': Latitude and longitude of the location.\\n- 'SWI04D': Drought index based on the analysis of precipitation and temperatures.\",\n",
|
109 |
+
" 'sql_query': 'CREATE TABLE Drought_index (\\n year INT, \\n month INT, \\n day INT \\n,\\n y FLOAT,\\n x FLOAT,\\n lat FLOAT,\\n lon FLOAT,\\n LambertParisII VARCHAR(255),\\n SWI04D FLOAT\\n);'},\n",
|
110 |
+
" 'Mean winter temperature': {'description': \"The Mean winter temperature table contains information on the average (mean) winter temperature in the past and the future.\\nThe variables are as follows:\\n- 'y' and 'x': Lambert Paris II coordinates for the location.\\n- year: Year of the observation.\\n\\n - month : Month of the observation.\\n\\n - day: Day of the observation.\\n\\n- 'LambertParisII': Indicates that the x, y coordinates are in the Lambert Paris II projection.\\n- 'lat' and 'lon': Latitude and longitude of the location.\\n- 'TMm': Average winter temperature.\",\n",
|
111 |
+
" 'sql_query': 'CREATE TABLE Mean_winter_temperature (\\n y FLOAT,\\n x FLOAT,\\n year INT, \\n month INT, \\n day INT \\n,\\n LambertParisII VARCHAR(255),\\n lat FLOAT,\\n lon FLOAT,\\n TMm FLOAT\\n);'},\n",
|
112 |
+
" 'Mean summer temperature': {'description': \"The Mean summer temperature table contains information on the average summer temperature in the past and the future.\\nThe variables are as follows:\\n- 'y' and 'x': Lambert Paris II coordinates for the location.\\n- year: Year of the observation.\\n\\n - month : Month of the observation.\\n\\n - day: Day of the observation.\\n\\n- 'LambertParisII': Indicates that the x and y coordinates are in Lambert Paris II projection.\\n- 'lat' and 'lon': Latitude and longitude of the location.\\n- 'TMm': Average summer temperature.\",\n",
|
113 |
+
" 'sql_query': 'CREATE TABLE Mean_summer_temperature (\\n y FLOAT,\\n x FLOAT,\\n year INT, \\n month INT, \\n day INT \\n,\\n LambertParisII VARCHAR(255),\\n lat FLOAT,\\n lon FLOAT,\\n TMm FLOAT\\n);'},\n",
|
114 |
+
" 'Number of tropical nights': {'description': \"The Number of tropical nights table contains information on the average summer temperature in the past and the future.\\nThe variables are as follows:\\n- 'y' and 'x': Lambert Paris II coordinates for the location.\\n- year: Year of the observation.\\n\\n - month : Month of the observation.\\n\\n - day: Day of the observation.\\n\\n- 'LambertParisII': Indicates that the x, y coordinates are in the Lambert Paris II projection.\\n- 'lat' and 'lon': Latitude and longitude of the location.\\n- 'TMm': Average summer temperature.\",\n",
|
115 |
+
" 'sql_query': 'CREATE TABLE Number_of_tropical_nights (\\n y FLOAT,\\n x FLOAT,\\n year INT, \\n month INT, \\n day INT \\n,\\n LambertParisII VARCHAR(255),\\n lat FLOAT,\\n lon FLOAT,\\n TR FLOAT\\n);'},\n",
|
116 |
+
" 'Number of days with Tx above 30C': {'description': \"The Number of days with Tx above 30C table contains information on the number of days when the maximum temperature in the past and the future\\nis greater than or equal to 30°C.\\nThe variables are as follows:\\n- 'y' and 'x': Lambert Paris II coordinates for the location.\\n- year: Year of the observation.\\n\\n - month : Month of the observation.\\n\\n - day: Day of the observation.\\n\\n- 'LambertParisII': Indicates that the x, y coordinates are in the Lambert Paris II projection.\\n- 'lat' and 'lon': Latitude and longitude of the location.\\n- 'TX30D': Number of days with Tx ≥ 30°C.\",\n",
|
117 |
+
" 'sql_query': 'CREATE TABLE Number_of_days_with_Tx_above_30C (\\n y FLOAT,\\n x FLOAT,\\n year INT, \\n month INT, \\n day INT \\n,\\n LambertParisII VARCHAR(255),\\n lat FLOAT,\\n lon FLOAT,\\n TX30D FLOAT\\n);'},\n",
|
118 |
+
" 'Number of days with Tx above 35C': {'description': \"The Number of days with Tx above 35C table contains information on the number of days when the maximum temperature in the past and the future\\nis greater than or equal to 35°C.\\nThe variables are as follows:\\n- 'y' and 'x': Lambert Paris II coordinates for the location.\\n- year: Year of the observation.\\n\\n - month : Month of the observation.\\n\\n - day: Day of the observation.\\n\\n- 'LambertParisII': Indicates that the x, y coordinates are in the Lambert Paris II projection.\\n- 'lat' and 'lon': Latitude and longitude of the location.\\n- 'TX35D': Number of days with Tx ≥ 35°C.\",\n",
|
119 |
+
" 'sql_query': 'CREATE TABLE Number_of_days_with_Tx_above_35C (\\n y FLOAT,\\n x FLOAT,\\n year INT, \\n month INT, \\n day INT \\n,\\n LambertParisII VARCHAR(255),\\n lat FLOAT,\\n lon FLOAT,\\n TX35D FLOAT\\n);'},\n",
|
120 |
+
" 'Maximum summer temperature': {'description': \"The Maximum summer temperature table contains information on the maximum temperature in summer in the past and the future,\\nwhich is the highest temperature recorded during the summer period.\\nThe variables are as follows:\\n- 'y' and 'x': Lambert Paris II coordinates for the location.\\n- year: Year of the observation.\\n\\n - month : Month of the observation.\\n\\n - day: Day of the observation.\\n\\n- 'LambertParisII': Indicates that the x, y coordinates are in the Lambert Paris II projection.\\n- 'lat' and 'lon': Latitude and longitude of the location.\\n- 'TXm': Maximum temperature recorded in summer.\",\n",
|
121 |
+
" 'sql_query': 'CREATE TABLE Maximum_summer_temperature (\\n y FLOAT,\\n x FLOAT,\\n year INT, \\n month INT, \\n day INT \\n,\\n LambertParisII VARCHAR(255),\\n lat FLOAT,\\n lon FLOAT,\\n TXm FLOAT\\n);'}}"
|
122 |
+
]
|
123 |
+
},
|
124 |
+
"execution_count": 51,
|
125 |
+
"metadata": {},
|
126 |
+
"output_type": "execute_result"
|
127 |
+
}
|
128 |
+
],
|
129 |
+
"source": [
|
130 |
+
"new_tables_info = convert(tables_info)\n",
|
131 |
+
"new_tables_info\n"
|
132 |
+
]
|
133 |
+
},
|
134 |
+
{
|
135 |
+
"cell_type": "code",
|
136 |
+
"execution_count": 52,
|
137 |
+
"metadata": {},
|
138 |
+
"outputs": [
|
139 |
+
{
|
140 |
+
"name": "stdout",
|
141 |
+
"output_type": "stream",
|
142 |
+
"text": [
|
143 |
+
"Adding ddl: CREATE TABLE Frequency_of_rainy_days_index (\n",
|
144 |
+
" year INT, \n",
|
145 |
+
" month INT, \n",
|
146 |
+
" day INT \n",
|
147 |
+
",\n",
|
148 |
+
" x FLOAT,\n",
|
149 |
+
" y FLOAT,\n",
|
150 |
+
" IFM40D FLOAT,\n",
|
151 |
+
" Lon FLOAT,\n",
|
152 |
+
" lat FLOAT\n",
|
153 |
+
");\n"
|
154 |
+
]
|
155 |
+
},
|
156 |
+
{
|
157 |
+
"name": "stdout",
|
158 |
+
"output_type": "stream",
|
159 |
+
"text": [
|
160 |
+
"Adding documentation....\n",
|
161 |
+
"Adding ddl: CREATE TABLE Remarkable_daily_precipitation_total_(Q99) (\n",
|
162 |
+
" year INT, \n",
|
163 |
+
" month INT, \n",
|
164 |
+
" day INT \n",
|
165 |
+
",\n",
|
166 |
+
" x FLOAT,\n",
|
167 |
+
" y FLOAT,\n",
|
168 |
+
" LambertParisII VARCHAR(255),\n",
|
169 |
+
" RRq99 FLOAT,\n",
|
170 |
+
" lat FLOAT,\n",
|
171 |
+
" lon FLOAT\n",
|
172 |
+
");\n",
|
173 |
+
"Adding documentation....\n",
|
174 |
+
"Adding ddl: CREATE TABLE Frequency_of_remarkable_daily_precipitation (\n",
|
175 |
+
" year INT, \n",
|
176 |
+
" month INT, \n",
|
177 |
+
" day INT \n",
|
178 |
+
",\n",
|
179 |
+
" x FLOAT,\n",
|
180 |
+
" y FLOAT,\n",
|
181 |
+
" LambertParisII VARCHAR(255),\n",
|
182 |
+
" RRq99refD FLOAT,\n",
|
183 |
+
" lat FLOAT,\n",
|
184 |
+
" lon FLOAT\n",
|
185 |
+
");\n",
|
186 |
+
"Adding documentation....\n",
|
187 |
+
"Adding ddl: CREATE TABLE Winter_precipitation_total (\n",
|
188 |
+
" year INT, \n",
|
189 |
+
" month INT, \n",
|
190 |
+
" day INT \n",
|
191 |
+
",\n",
|
192 |
+
" x FLOAT,\n",
|
193 |
+
" y FLOAT,\n",
|
194 |
+
" LambertParisII VARCHAR(255),\n",
|
195 |
+
" RR FLOAT,\n",
|
196 |
+
" lat FLOAT,\n",
|
197 |
+
" lon FLOAT\n",
|
198 |
+
");\n",
|
199 |
+
"Adding documentation....\n",
|
200 |
+
"Adding ddl: CREATE TABLE Summer_precipitation_total (\n",
|
201 |
+
" year INT, \n",
|
202 |
+
" month INT, \n",
|
203 |
+
" day INT \n",
|
204 |
+
",\n",
|
205 |
+
" x FLOAT,\n",
|
206 |
+
" y FLOAT,\n",
|
207 |
+
" LambertParisII VARCHAR(255),\n",
|
208 |
+
" RR FLOAT,\n",
|
209 |
+
" lat FLOAT,\n",
|
210 |
+
" lon FLOAT\n",
|
211 |
+
");\n",
|
212 |
+
"Adding documentation....\n",
|
213 |
+
"Adding ddl: CREATE TABLE Annual_precipitation_total (\n",
|
214 |
+
" y FLOAT,\n",
|
215 |
+
" x FLOAT,\n",
|
216 |
+
" year INT, \n",
|
217 |
+
" month INT, \n",
|
218 |
+
" day INT \n",
|
219 |
+
",\n",
|
220 |
+
" LambertParisII VARCHAR(255),\n",
|
221 |
+
" lat FLOAT,\n",
|
222 |
+
" lon FLOAT,\n",
|
223 |
+
" RR FLOAT\n",
|
224 |
+
");\n",
|
225 |
+
"Adding documentation....\n",
|
226 |
+
"Adding ddl: CREATE TABLE Extreme_precipitation_intensity (\n",
|
227 |
+
" y FLOAT,\n",
|
228 |
+
" x FLOAT,\n",
|
229 |
+
" year INT, \n",
|
230 |
+
" month INT, \n",
|
231 |
+
" day INT \n",
|
232 |
+
",\n",
|
233 |
+
" LambertParisII VARCHAR(255),\n",
|
234 |
+
" lat FLOAT,\n",
|
235 |
+
" lon FLOAT,\n",
|
236 |
+
" RX1d FLOAT\n",
|
237 |
+
");\n",
|
238 |
+
"Adding documentation....\n",
|
239 |
+
"Adding ddl: CREATE TABLE Drought_index (\n",
|
240 |
+
" year INT, \n",
|
241 |
+
" month INT, \n",
|
242 |
+
" day INT \n",
|
243 |
+
",\n",
|
244 |
+
" y FLOAT,\n",
|
245 |
+
" x FLOAT,\n",
|
246 |
+
" lat FLOAT,\n",
|
247 |
+
" lon FLOAT,\n",
|
248 |
+
" LambertParisII VARCHAR(255),\n",
|
249 |
+
" SWI04D FLOAT\n",
|
250 |
+
");\n",
|
251 |
+
"Adding documentation....\n",
|
252 |
+
"Adding ddl: CREATE TABLE Mean_winter_temperature (\n",
|
253 |
+
" y FLOAT,\n",
|
254 |
+
" x FLOAT,\n",
|
255 |
+
" year INT, \n",
|
256 |
+
" month INT, \n",
|
257 |
+
" day INT \n",
|
258 |
+
",\n",
|
259 |
+
" LambertParisII VARCHAR(255),\n",
|
260 |
+
" lat FLOAT,\n",
|
261 |
+
" lon FLOAT,\n",
|
262 |
+
" TMm FLOAT\n",
|
263 |
+
");\n",
|
264 |
+
"Adding documentation....\n",
|
265 |
+
"Adding ddl: CREATE TABLE Mean_summer_temperature (\n",
|
266 |
+
" y FLOAT,\n",
|
267 |
+
" x FLOAT,\n",
|
268 |
+
" year INT, \n",
|
269 |
+
" month INT, \n",
|
270 |
+
" day INT \n",
|
271 |
+
",\n",
|
272 |
+
" LambertParisII VARCHAR(255),\n",
|
273 |
+
" lat FLOAT,\n",
|
274 |
+
" lon FLOAT,\n",
|
275 |
+
" TMm FLOAT\n",
|
276 |
+
");\n",
|
277 |
+
"Adding documentation....\n",
|
278 |
+
"Adding ddl: CREATE TABLE Number_of_tropical_nights (\n",
|
279 |
+
" y FLOAT,\n",
|
280 |
+
" x FLOAT,\n",
|
281 |
+
" year INT, \n",
|
282 |
+
" month INT, \n",
|
283 |
+
" day INT \n",
|
284 |
+
",\n",
|
285 |
+
" LambertParisII VARCHAR(255),\n",
|
286 |
+
" lat FLOAT,\n",
|
287 |
+
" lon FLOAT,\n",
|
288 |
+
" TR FLOAT\n",
|
289 |
+
");\n",
|
290 |
+
"Adding documentation....\n",
|
291 |
+
"Adding ddl: CREATE TABLE Number_of_days_with_Tx_above_30C (\n",
|
292 |
+
" y FLOAT,\n",
|
293 |
+
" x FLOAT,\n",
|
294 |
+
" year INT, \n",
|
295 |
+
" month INT, \n",
|
296 |
+
" day INT \n",
|
297 |
+
",\n",
|
298 |
+
" LambertParisII VARCHAR(255),\n",
|
299 |
+
" lat FLOAT,\n",
|
300 |
+
" lon FLOAT,\n",
|
301 |
+
" TX30D FLOAT\n",
|
302 |
+
");\n",
|
303 |
+
"Adding documentation....\n",
|
304 |
+
"Adding ddl: CREATE TABLE Number_of_days_with_Tx_above_35C (\n",
|
305 |
+
" y FLOAT,\n",
|
306 |
+
" x FLOAT,\n",
|
307 |
+
" year INT, \n",
|
308 |
+
" month INT, \n",
|
309 |
+
" day INT \n",
|
310 |
+
",\n",
|
311 |
+
" LambertParisII VARCHAR(255),\n",
|
312 |
+
" lat FLOAT,\n",
|
313 |
+
" lon FLOAT,\n",
|
314 |
+
" TX35D FLOAT\n",
|
315 |
+
");\n",
|
316 |
+
"Adding documentation....\n",
|
317 |
+
"Adding ddl: CREATE TABLE Maximum_summer_temperature (\n",
|
318 |
+
" y FLOAT,\n",
|
319 |
+
" x FLOAT,\n",
|
320 |
+
" year INT, \n",
|
321 |
+
" month INT, \n",
|
322 |
+
" day INT \n",
|
323 |
+
",\n",
|
324 |
+
" LambertParisII VARCHAR(255),\n",
|
325 |
+
" lat FLOAT,\n",
|
326 |
+
" lon FLOAT,\n",
|
327 |
+
" TXm FLOAT\n",
|
328 |
+
");\n",
|
329 |
+
"Adding documentation....\n"
|
330 |
+
]
|
331 |
+
}
|
332 |
+
],
|
333 |
+
"source": [
|
334 |
+
"for table in new_tables_info:\n",
|
335 |
+
" vn.train(ddl = new_tables_info[table]['sql_query'])\n",
|
336 |
+
" vn.train(documentation = new_tables_info[table]['description'])"
|
337 |
+
]
|
338 |
+
},
|
339 |
+
{
|
340 |
+
"cell_type": "markdown",
|
341 |
+
"metadata": {},
|
342 |
+
"source": [
|
343 |
+
"# Requests"
|
344 |
+
]
|
345 |
+
},
|
346 |
+
{
|
347 |
+
"cell_type": "code",
|
348 |
+
"execution_count": null,
|
349 |
+
"metadata": {},
|
350 |
+
"outputs": [],
|
351 |
+
"source": []
|
352 |
+
},
|
353 |
+
{
|
354 |
+
"cell_type": "markdown",
|
355 |
+
"metadata": {},
|
356 |
+
"source": [
|
357 |
+
"# examples"
|
358 |
+
]
|
359 |
+
},
|
360 |
+
{
|
361 |
+
"cell_type": "code",
|
362 |
+
"execution_count": 56,
|
363 |
+
"metadata": {},
|
364 |
+
"outputs": [
|
365 |
+
{
|
366 |
+
"name": "stdout",
|
367 |
+
"output_type": "stream",
|
368 |
+
"text": [
|
369 |
+
"Loading embeddings model: BAAI/bge-base-en-v1.5\n"
|
370 |
+
]
|
371 |
+
}
|
372 |
+
],
|
373 |
+
"source": [
|
374 |
+
"question_sql = [\n",
|
375 |
+
" # ['How will the precipitations change in the coming years', \n",
|
376 |
+
" # 'SELECT * FROM Annual_precipitation_total WHERE year > 2024;'],\n",
|
377 |
+
" # ['What is the number of days where the temperature above 35 in year 2050 in (lat, lon) = (48.82337188720703, 2.390951633453369)', \n",
|
378 |
+
" # 'SELECT TX35D FROM Number_of_days_with_Tx_above_35C WHERE year = 2050 AND lat = 48.82337188720703 AND lon = 2.390951633453369;'],\n",
|
379 |
+
" # ['How will change the mean winter temperature in the coming years in lat = 43.2961743 lon = 5.3699525', \n",
|
380 |
+
" # 'SELECT * FROM Mean_winter_temperature WHERE year > 2023 AND lat = 43.2961743 AND lon = 5.3699525'],\n",
|
381 |
+
" # ['How will change the mean summer temperature in the coming years in lat = 43.2961743, lon = 5.3699525', \n",
|
382 |
+
" # 'SELECT * FROM Mean_summer_temperature WHERE year > 2023 AND lat = 43.2961743 AND lon = 5.3699525'],\n",
|
383 |
+
" # ['How will change the temperature in the coming years', \n",
|
384 |
+
" # 'SELECT * FROM Mean_summer_temperature JOIN Mean_winter_temperature WHERE year > 2024']\n",
|
385 |
+
" [\"Quelle sera la température à lat, long : (43.2961743, 5.3699525) sur les prochaines années ?\",\n",
|
386 |
+
" 'SELECT Mean_winter_temperature AS table_name, lat, lon, year, TMm \\nFROM Mean_winter_temperature \\nWHERE lat = 43.2961743 AND lon = 5.3699525\\nUNION ALL\\nSELECT \"Mean_summer_temperature\" AS table_name, lat, lon, year, TMm \\nFROM Mean_summer_temperature \\nWHERE lat = 43.2961743 AND lon = 5.3699525;']\n",
|
387 |
+
"]"
|
388 |
+
]
|
389 |
+
},
|
390 |
+
{
|
391 |
+
"cell_type": "code",
|
392 |
+
"execution_count": 58,
|
393 |
+
"metadata": {},
|
394 |
+
"outputs": [
|
395 |
+
{
|
396 |
+
"name": "stdout",
|
397 |
+
"output_type": "stream",
|
398 |
+
"text": [
|
399 |
+
"de6738cb3a67eaec29a04119ad160b27acd64f2d87cf83782c5b2bdc84be445b_sql\n"
|
400 |
+
]
|
401 |
+
}
|
402 |
+
],
|
403 |
+
"source": [
|
404 |
+
"for question in question_sql:\n",
|
405 |
+
" print(vn.train(question = question[0], sql = question[1]))"
|
406 |
+
]
|
407 |
+
},
|
408 |
+
{
|
409 |
+
"cell_type": "markdown",
|
410 |
+
"metadata": {},
|
411 |
+
"source": [
|
412 |
+
"# Delete"
|
413 |
+
]
|
414 |
+
},
|
415 |
+
{
|
416 |
+
"cell_type": "code",
|
417 |
+
"execution_count": 31,
|
418 |
+
"metadata": {},
|
419 |
+
"outputs": [
|
420 |
+
{
|
421 |
+
"data": {
|
422 |
+
"text/plain": [
|
423 |
+
"'cqa-vanna'"
|
424 |
+
]
|
425 |
+
},
|
426 |
+
"execution_count": 31,
|
427 |
+
"metadata": {},
|
428 |
+
"output_type": "execute_result"
|
429 |
+
}
|
430 |
+
],
|
431 |
+
"source": [
|
432 |
+
"INDEX_NAME"
|
433 |
+
]
|
434 |
+
},
|
435 |
+
{
|
436 |
+
"cell_type": "code",
|
437 |
+
"execution_count": 36,
|
438 |
+
"metadata": {},
|
439 |
+
"outputs": [
|
440 |
+
{
|
441 |
+
"data": {
|
442 |
+
"text/plain": []
|
443 |
+
},
|
444 |
+
"execution_count": 36,
|
445 |
+
"metadata": {},
|
446 |
+
"output_type": "execute_result"
|
447 |
+
}
|
448 |
+
],
|
449 |
+
"source": [
|
450 |
+
"from pinecone.grpc import PineconeGRPC as Pinecone\n",
|
451 |
+
"\n",
|
452 |
+
"pc = Pinecone(api_key=PC_API_KEY)\n",
|
453 |
+
"\n",
|
454 |
+
"# To get the unique host for an index, \n",
|
455 |
+
"# see https://docs.pinecone.io/guides/data/target-an-index\n",
|
456 |
+
"index = pc.Index(INDEX_NAME)\n",
|
457 |
+
"# index\n",
|
458 |
+
"index.delete(delete_all=True, namespace='ddl')\n",
|
459 |
+
"index.delete(delete_all=True, namespace='documentation')\n"
|
460 |
+
]
|
461 |
+
},
|
462 |
+
{
|
463 |
+
"cell_type": "markdown",
|
464 |
+
"metadata": {},
|
465 |
+
"source": [
|
466 |
+
"# OLD"
|
467 |
+
]
|
468 |
+
},
|
469 |
+
{
|
470 |
+
"cell_type": "code",
|
471 |
+
"execution_count": 32,
|
472 |
+
"metadata": {},
|
473 |
+
"outputs": [],
|
474 |
+
"source": [
|
475 |
+
"import vanna\n",
|
476 |
+
"from vanna.remote import VannaDefault"
|
477 |
+
]
|
478 |
+
},
|
479 |
+
{
|
480 |
+
"cell_type": "code",
|
481 |
+
"execution_count": 33,
|
482 |
+
"metadata": {},
|
483 |
+
"outputs": [],
|
484 |
+
"source": [
|
485 |
+
"# PC_API_KEY = os.getenv('VANNA_PINECONE_API_KEY')\n"
|
486 |
+
]
|
487 |
+
},
|
488 |
+
{
|
489 |
+
"cell_type": "code",
|
490 |
+
"execution_count": 34,
|
491 |
+
"metadata": {},
|
492 |
+
"outputs": [
|
493 |
+
{
|
494 |
+
"data": {
|
495 |
+
"text/plain": [
|
496 |
+
"[\n",
|
497 |
+
" {\n",
|
498 |
+
" \"name\": \"cqa-vanna\",\n",
|
499 |
+
" \"dimension\": 768,\n",
|
500 |
+
" \"metric\": \"cosine\",\n",
|
501 |
+
" \"host\": \"cqa-vanna-9xlylwt.svc.aped-4627-b74a.pinecone.io\",\n",
|
502 |
+
" \"spec\": {\n",
|
503 |
+
" \"serverless\": {\n",
|
504 |
+
" \"cloud\": \"aws\",\n",
|
505 |
+
" \"region\": \"us-east-1\"\n",
|
506 |
+
" }\n",
|
507 |
+
" },\n",
|
508 |
+
" \"status\": {\n",
|
509 |
+
" \"ready\": true,\n",
|
510 |
+
" \"state\": \"Ready\"\n",
|
511 |
+
" },\n",
|
512 |
+
" \"deletion_protection\": \"disabled\"\n",
|
513 |
+
" },\n",
|
514 |
+
" {\n",
|
515 |
+
" \"name\": \"unepqa\",\n",
|
516 |
+
" \"dimension\": 768,\n",
|
517 |
+
" \"metric\": \"cosine\",\n",
|
518 |
+
" \"host\": \"unepqa-9xlylwt.svc.aped-4627-b74a.pinecone.io\",\n",
|
519 |
+
" \"spec\": {\n",
|
520 |
+
" \"serverless\": {\n",
|
521 |
+
" \"cloud\": \"aws\",\n",
|
522 |
+
" \"region\": \"us-east-1\"\n",
|
523 |
+
" }\n",
|
524 |
+
" },\n",
|
525 |
+
" \"status\": {\n",
|
526 |
+
" \"ready\": true,\n",
|
527 |
+
" \"state\": \"Ready\"\n",
|
528 |
+
" },\n",
|
529 |
+
" \"deletion_protection\": \"disabled\"\n",
|
530 |
+
" }\n",
|
531 |
+
"]"
|
532 |
+
]
|
533 |
+
},
|
534 |
+
"execution_count": 34,
|
535 |
+
"metadata": {},
|
536 |
+
"output_type": "execute_result"
|
537 |
+
}
|
538 |
+
],
|
539 |
+
"source": [
|
540 |
+
"from pinecone.grpc import PineconeGRPC as Pinecone\n",
|
541 |
+
"\n",
|
542 |
+
"pc = Pinecone(api_key=PC_API_KEY)\n",
|
543 |
+
"pc.list_indexes()"
|
544 |
+
]
|
545 |
+
},
|
546 |
+
{
|
547 |
+
"cell_type": "code",
|
548 |
+
"execution_count": 35,
|
549 |
+
"metadata": {},
|
550 |
+
"outputs": [
|
551 |
+
{
|
552 |
+
"data": {
|
553 |
+
"text/plain": [
|
554 |
+
"{'dimension': 768,\n",
|
555 |
+
" 'index_fullness': 0.0,\n",
|
556 |
+
" 'namespaces': {'ddl': {'vector_count': 14},\n",
|
557 |
+
" 'documentation': {'vector_count': 14}},\n",
|
558 |
+
" 'total_vector_count': 28}"
|
559 |
+
]
|
560 |
+
},
|
561 |
+
"execution_count": 35,
|
562 |
+
"metadata": {},
|
563 |
+
"output_type": "execute_result"
|
564 |
+
}
|
565 |
+
],
|
566 |
+
"source": [
|
567 |
+
"pc.Index(\"cqa-vanna\").describe_index_stats()"
|
568 |
+
]
|
569 |
+
}
|
570 |
+
],
|
571 |
+
"metadata": {
|
572 |
+
"kernelspec": {
|
573 |
+
"display_name": "climateqa",
|
574 |
+
"language": "python",
|
575 |
+
"name": "python3"
|
576 |
+
},
|
577 |
+
"language_info": {
|
578 |
+
"codemirror_mode": {
|
579 |
+
"name": "ipython",
|
580 |
+
"version": 3
|
581 |
+
},
|
582 |
+
"file_extension": ".py",
|
583 |
+
"mimetype": "text/x-python",
|
584 |
+
"name": "python",
|
585 |
+
"nbconvert_exporter": "python",
|
586 |
+
"pygments_lexer": "ipython3",
|
587 |
+
"version": "3.11.9"
|
588 |
+
}
|
589 |
+
},
|
590 |
+
"nbformat": 4,
|
591 |
+
"nbformat_minor": 2
|
592 |
+
}
|
climateqa/engine/talk_to_data/step_by_step_vanna copy.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
climateqa/engine/talk_to_data/step_by_step_vanna.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
climateqa/{talk_to_data → engine/talk_to_data}/utils.py
RENAMED
File without changes
|
climateqa/{talk_to_data → engine/talk_to_data}/vanna_class.py
RENAMED
@@ -167,80 +167,156 @@ class MyCustomVectorDB(VannaBase):
|
|
167 |
pass
|
168 |
|
169 |
|
170 |
-
def get_sql_prompt(
|
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 |
-
initial_prompt, question_sql_list, max_tokens=self.max_tokens
|
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 |
-
|
|
|
167 |
pass
|
168 |
|
169 |
|
170 |
+
def get_sql_prompt(
|
171 |
+
self,
|
172 |
+
initial_prompt : str,
|
173 |
+
question: str,
|
174 |
+
question_sql_list: list,
|
175 |
+
ddl_list: list,
|
176 |
+
doc_list: list,
|
177 |
+
**kwargs,
|
178 |
+
):
|
179 |
+
"""
|
180 |
+
Example:
|
181 |
+
```python
|
182 |
+
vn.get_sql_prompt(
|
183 |
+
question="What are the top 10 customers by sales?",
|
184 |
+
question_sql_list=[{"question": "What are the top 10 customers by sales?", "sql": "SELECT * FROM customers ORDER BY sales DESC LIMIT 10"}],
|
185 |
+
ddl_list=["CREATE TABLE customers (id INT, name TEXT, sales DECIMAL)"],
|
186 |
+
doc_list=["The customers table contains information about customers and their sales."],
|
187 |
+
)
|
188 |
|
189 |
+
```
|
190 |
|
191 |
+
This method is used to generate a prompt for the LLM to generate SQL.
|
192 |
|
193 |
+
Args:
|
194 |
+
question (str): The question to generate SQL for.
|
195 |
+
question_sql_list (list): A list of questions and their corresponding SQL statements.
|
196 |
+
ddl_list (list): A list of DDL statements.
|
197 |
+
doc_list (list): A list of documentation.
|
198 |
|
199 |
+
Returns:
|
200 |
+
any: The prompt for the LLM to generate SQL.
|
201 |
+
"""
|
202 |
|
203 |
+
if initial_prompt is None:
|
204 |
+
initial_prompt = f"You are a {self.dialect} expert. " + \
|
205 |
+
"Please help to generate a SQL query to answer the question. Your response should ONLY be based on the given context and follow the response guidelines and format instructions. "
|
206 |
|
207 |
+
initial_prompt = self.add_ddl_to_prompt(
|
208 |
+
initial_prompt, ddl_list, max_tokens=self.max_tokens
|
209 |
+
)
|
210 |
|
211 |
+
if self.static_documentation != "":
|
212 |
+
doc_list.append(self.static_documentation)
|
213 |
|
214 |
+
initial_prompt = self.add_documentation_to_prompt(
|
215 |
+
initial_prompt, doc_list, max_tokens=self.max_tokens
|
216 |
+
)
|
217 |
|
218 |
+
# initial_prompt = self.add_sql_to_prompt(
|
219 |
+
# initial_prompt, question_sql_list, max_tokens=self.max_tokens
|
220 |
+
# )
|
221 |
|
222 |
|
223 |
+
initial_prompt += (
|
224 |
+
"===Response Guidelines \n"
|
225 |
+
"1. If the provided context is sufficient, please generate a valid SQL query without any explanations for the question. \n"
|
226 |
+
"2. If the provided context is almost sufficient but requires knowledge of a specific string in a particular column, please generate an intermediate SQL query to find the distinct strings in that column. Prepend the query with a comment saying intermediate_sql \n"
|
227 |
+
"3. If the provided context is insufficient, please give a sql query based on your knowledge and the context provided. \n"
|
228 |
+
"4. Please use the most relevant table(s). \n"
|
229 |
+
"5. If the question has been asked and answered before, please repeat the answer exactly as it was given before. \n"
|
230 |
+
f"6. Ensure that the output SQL is {self.dialect}-compliant and executable, and free of syntax errors. \n"
|
231 |
+
# "7. Add a description of the table in the result of the sql query."
|
232 |
+
# "7. If the question is about a specific latitude, longitude, query an interval of 0.3 and keep only the first set of coordinate. \n"
|
233 |
+
# "7. Table names should be included in the result of the sql query. Use for example Mean_winter_temperature AS table_name in the query \n"
|
234 |
+
)
|
235 |
+
|
236 |
+
|
237 |
+
message_log = [self.system_message(initial_prompt)]
|
238 |
+
|
239 |
+
for example in question_sql_list:
|
240 |
+
if example is None:
|
241 |
+
print("example is None")
|
242 |
+
else:
|
243 |
+
if example is not None and "question" in example and "sql" in example:
|
244 |
+
message_log.append(self.user_message(example["question"]))
|
245 |
+
message_log.append(self.assistant_message(example["sql"]))
|
246 |
+
|
247 |
+
message_log.append(self.user_message(question))
|
248 |
+
|
249 |
+
return message_log
|
250 |
+
|
251 |
+
|
252 |
+
# def get_sql_prompt(
|
253 |
+
# self,
|
254 |
+
# initial_prompt : str,
|
255 |
+
# question: str,
|
256 |
+
# question_sql_list: list,
|
257 |
+
# ddl_list: list,
|
258 |
+
# doc_list: list,
|
259 |
+
# **kwargs,
|
260 |
+
# ):
|
261 |
+
# """
|
262 |
+
# Example:
|
263 |
+
# ```python
|
264 |
+
# vn.get_sql_prompt(
|
265 |
+
# question="What are the top 10 customers by sales?",
|
266 |
+
# question_sql_list=[{"question": "What are the top 10 customers by sales?", "sql": "SELECT * FROM customers ORDER BY sales DESC LIMIT 10"}],
|
267 |
+
# ddl_list=["CREATE TABLE customers (id INT, name TEXT, sales DECIMAL)"],
|
268 |
+
# doc_list=["The customers table contains information about customers and their sales."],
|
269 |
+
# )
|
270 |
+
|
271 |
+
# ```
|
272 |
+
|
273 |
+
# This method is used to generate a prompt for the LLM to generate SQL.
|
274 |
+
|
275 |
+
# Args:
|
276 |
+
# question (str): The question to generate SQL for.
|
277 |
+
# question_sql_list (list): A list of questions and their corresponding SQL statements.
|
278 |
+
# ddl_list (list): A list of DDL statements.
|
279 |
+
# doc_list (list): A list of documentation.
|
280 |
+
|
281 |
+
# Returns:
|
282 |
+
# any: The prompt for the LLM to generate SQL.
|
283 |
+
# """
|
284 |
+
|
285 |
+
# if initial_prompt is None:
|
286 |
+
# initial_prompt = f"You are a {self.dialect} expert. " + \
|
287 |
+
# "Please help to generate a SQL query to answer the question. Your response should ONLY be based on the given context and follow the response guidelines and format instructions. "
|
288 |
+
|
289 |
+
# initial_prompt = self.add_ddl_to_prompt(
|
290 |
+
# initial_prompt, ddl_list, max_tokens=self.max_tokens
|
291 |
+
# )
|
292 |
+
|
293 |
+
# if self.static_documentation != "":
|
294 |
+
# doc_list.append(self.static_documentation)
|
295 |
+
|
296 |
+
# initial_prompt = self.add_documentation_to_prompt(
|
297 |
+
# initial_prompt, doc_list, max_tokens=self.max_tokens
|
298 |
+
# )
|
299 |
|
300 |
+
# initial_prompt += (
|
301 |
+
# "===Response Guidelines \n"
|
302 |
+
# "1. If the provided context is sufficient, please generate a valid SQL query without any explanations for the question. \n"
|
303 |
+
# "2. If the provided context is almost sufficient but requires knowledge of a specific string in a particular column, please generate an intermediate SQL query to find the distinct strings in that column. Prepend the query with a comment saying intermediate_sql \n"
|
304 |
+
# "3. If the provided context is insufficient, please explain why it can't be generated. \n"
|
305 |
+
# "4. Please use the most relevant table(s). \n"
|
306 |
+
# "5. If the question has been asked and answered before, please repeat the answer exactly as it was given before. \n"
|
307 |
+
# f"6. Ensure that the output SQL is {self.dialect}-compliant and executable, and free of syntax errors. \n"
|
308 |
+
# )
|
309 |
|
310 |
+
# message_log = [self.system_message(initial_prompt)]
|
311 |
|
312 |
+
# for example in question_sql_list:
|
313 |
+
# if example is None:
|
314 |
+
# print("example is None")
|
315 |
+
# else:
|
316 |
+
# if example is not None and "question" in example and "sql" in example:
|
317 |
+
# message_log.append(self.user_message(example["question"]))
|
318 |
+
# message_log.append(self.assistant_message(example["sql"]))
|
319 |
|
320 |
+
# message_log.append(self.user_message(question))
|
321 |
|
322 |
+
# return message_log
|
climateqa/talk_to_data/main.py
DELETED
@@ -1,43 +0,0 @@
|
|
1 |
-
from climateqa.talk_to_data.myVanna import MyVanna
|
2 |
-
from climateqa.talk_to_data.utils import loc2coords, detect_location_with_openai, detectTable, nearestNeighbourSQL
|
3 |
-
import sqlite3
|
4 |
-
import os
|
5 |
-
import pandas as pd
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
10 |
-
PC_API_KEY = os.getenv('VANNA_PINECONE_API_KEY')
|
11 |
-
INDEX_NAME = os.getenv('VANNA_INDEX_NAME')
|
12 |
-
VANNA_MODEL = os.getenv('VANNA_MODEL')
|
13 |
-
|
14 |
-
|
15 |
-
#Vanna object
|
16 |
-
vn = MyVanna(config = {"temperature": 0, "api_key": OPENAI_API_KEY, 'model': VANNA_MODEL, 'pc_api_key': PC_API_KEY, 'index_name': INDEX_NAME})
|
17 |
-
db_vanna_path = os.path.join(os.path.dirname(__file__), "database/drias.db")
|
18 |
-
vn.connect_to_sqlite(db_vanna_path)
|
19 |
-
|
20 |
-
|
21 |
-
def ask_vanna(query):
|
22 |
-
location = detect_location_with_openai(OPENAI_API_KEY, query)
|
23 |
-
if location:
|
24 |
-
coords = loc2coords(location)
|
25 |
-
user_input = query.replace(location, f"lat, long : {coords}")
|
26 |
-
answer = vn.ask(user_input, print_results=False, allow_llm_to_see_data=True)
|
27 |
-
table = detectTable(answer[0])
|
28 |
-
coords2 = nearestNeighbourSQL(db_vanna_path, coords, table[0])
|
29 |
-
|
30 |
-
query = answer[0].replace(f"{coords[0]}", f"{coords2[0]}")
|
31 |
-
sql_query = query.replace(f"{coords[1]}", f"{coords2[1]}")
|
32 |
-
|
33 |
-
db = sqlite3.connect(db_vanna_path)
|
34 |
-
result = db.cursor().execute(sql_query).fetchall()
|
35 |
-
print(result)
|
36 |
-
df = pd.DataFrame(result, columns=answer[1].columns)
|
37 |
-
|
38 |
-
else:
|
39 |
-
answer = vn.ask(query, visualize=True, print_results=False, allow_llm_to_see_data=True)
|
40 |
-
sql_query = answer[0]
|
41 |
-
df = answer[1]
|
42 |
-
|
43 |
-
return (sql_query, df)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sandbox/20241104 - CQA - StepByStep CQA.ipynb
CHANGED
@@ -163,43 +163,15 @@
|
|
163 |
},
|
164 |
{
|
165 |
"cell_type": "code",
|
166 |
-
"execution_count":
|
167 |
"metadata": {},
|
168 |
"outputs": [
|
169 |
-
{
|
170 |
-
"name": "stderr",
|
171 |
-
"output_type": "stream",
|
172 |
-
"text": [
|
173 |
-
"INFO:sentence_transformers.SentenceTransformer:Load pretrained SentenceTransformer: BAAI/bge-base-en-v1.5\n"
|
174 |
-
]
|
175 |
-
},
|
176 |
-
{
|
177 |
-
"name": "stdout",
|
178 |
-
"output_type": "stream",
|
179 |
-
"text": [
|
180 |
-
"Loading embeddings model: BAAI/bge-base-en-v1.5\n"
|
181 |
-
]
|
182 |
-
},
|
183 |
-
{
|
184 |
-
"name": "stderr",
|
185 |
-
"output_type": "stream",
|
186 |
-
"text": [
|
187 |
-
"INFO:sentence_transformers.SentenceTransformer:Use pytorch device_name: cpu\n",
|
188 |
-
"INFO:pinecone_plugin_interface.logging:Discovering subpackages in _NamespacePath(['/home/tim/anaconda3/envs/climateqa/lib/python3.11/site-packages/pinecone_plugins'])\n",
|
189 |
-
"INFO:pinecone_plugin_interface.logging:Looking for plugins in pinecone_plugins.inference\n",
|
190 |
-
"INFO:pinecone_plugin_interface.logging:Installing plugin inference into Pinecone\n",
|
191 |
-
"INFO:pinecone_plugin_interface.logging:Discovering subpackages in _NamespacePath(['/home/tim/anaconda3/envs/climateqa/lib/python3.11/site-packages/pinecone_plugins'])\n",
|
192 |
-
"INFO:pinecone_plugin_interface.logging:Looking for plugins in pinecone_plugins.inference\n",
|
193 |
-
"INFO:pinecone_plugin_interface.logging:Installing plugin inference into Pinecone\n",
|
194 |
-
"INFO:pinecone_plugin_interface.logging:Discovering subpackages in _NamespacePath(['/home/tim/anaconda3/envs/climateqa/lib/python3.11/site-packages/pinecone_plugins'])\n",
|
195 |
-
"INFO:pinecone_plugin_interface.logging:Looking for plugins in pinecone_plugins.inference\n",
|
196 |
-
"INFO:pinecone_plugin_interface.logging:Installing plugin inference into Pinecone\n"
|
197 |
-
]
|
198 |
-
},
|
199 |
{
|
200 |
"name": "stdout",
|
201 |
"output_type": "stream",
|
202 |
"text": [
|
|
|
|
|
203 |
"Loading FlashRankRanker model ms-marco-TinyBERT-L-2-v2\n",
|
204 |
"Loading model FlashRank model ms-marco-TinyBERT-L-2-v2...\n"
|
205 |
]
|
@@ -238,7 +210,7 @@
|
|
238 |
},
|
239 |
{
|
240 |
"cell_type": "code",
|
241 |
-
"execution_count":
|
242 |
"metadata": {},
|
243 |
"outputs": [],
|
244 |
"source": [
|
@@ -261,7 +233,7 @@
|
|
261 |
},
|
262 |
{
|
263 |
"cell_type": "code",
|
264 |
-
"execution_count":
|
265 |
"metadata": {},
|
266 |
"outputs": [],
|
267 |
"source": [
|
@@ -279,9 +251,51 @@
|
|
279 |
},
|
280 |
{
|
281 |
"cell_type": "code",
|
282 |
-
"execution_count":
|
283 |
"metadata": {},
|
284 |
-
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
285 |
"source": [
|
286 |
"cat_node = make_intent_categorization_node(llm)\n",
|
287 |
"state.update(cat_node(inial_state))\n",
|
@@ -290,7 +304,7 @@
|
|
290 |
},
|
291 |
{
|
292 |
"cell_type": "code",
|
293 |
-
"execution_count":
|
294 |
"metadata": {},
|
295 |
"outputs": [],
|
296 |
"source": [
|
@@ -300,7 +314,7 @@
|
|
300 |
},
|
301 |
{
|
302 |
"cell_type": "code",
|
303 |
-
"execution_count":
|
304 |
"metadata": {},
|
305 |
"outputs": [],
|
306 |
"source": [
|
@@ -312,9 +326,60 @@
|
|
312 |
},
|
313 |
{
|
314 |
"cell_type": "code",
|
315 |
-
"execution_count":
|
316 |
"metadata": {},
|
317 |
-
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
318 |
"source": [
|
319 |
"state.update(make_query_transform_node(llm)(state))\n",
|
320 |
"state"
|
|
|
163 |
},
|
164 |
{
|
165 |
"cell_type": "code",
|
166 |
+
"execution_count": 8,
|
167 |
"metadata": {},
|
168 |
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
{
|
170 |
"name": "stdout",
|
171 |
"output_type": "stream",
|
172 |
"text": [
|
173 |
+
"Loading embeddings model: BAAI/bge-base-en-v1.5\n",
|
174 |
+
"Loading embeddings model: BAAI/bge-base-en-v1.5\n",
|
175 |
"Loading FlashRankRanker model ms-marco-TinyBERT-L-2-v2\n",
|
176 |
"Loading model FlashRank model ms-marco-TinyBERT-L-2-v2...\n"
|
177 |
]
|
|
|
210 |
},
|
211 |
{
|
212 |
"cell_type": "code",
|
213 |
+
"execution_count": 9,
|
214 |
"metadata": {},
|
215 |
"outputs": [],
|
216 |
"source": [
|
|
|
233 |
},
|
234 |
{
|
235 |
"cell_type": "code",
|
236 |
+
"execution_count": 10,
|
237 |
"metadata": {},
|
238 |
"outputs": [],
|
239 |
"source": [
|
|
|
251 |
},
|
252 |
{
|
253 |
"cell_type": "code",
|
254 |
+
"execution_count": 11,
|
255 |
"metadata": {},
|
256 |
+
"outputs": [
|
257 |
+
{
|
258 |
+
"name": "stdout",
|
259 |
+
"output_type": "stream",
|
260 |
+
"text": [
|
261 |
+
"---- Categorize_message ----\n"
|
262 |
+
]
|
263 |
+
},
|
264 |
+
{
|
265 |
+
"name": "stderr",
|
266 |
+
"output_type": "stream",
|
267 |
+
"text": [
|
268 |
+
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n"
|
269 |
+
]
|
270 |
+
},
|
271 |
+
{
|
272 |
+
"name": "stdout",
|
273 |
+
"output_type": "stream",
|
274 |
+
"text": [
|
275 |
+
"\n",
|
276 |
+
"\n",
|
277 |
+
"Output intent categorization: {'language': 'French', 'intent': 'search'}\n",
|
278 |
+
"\n"
|
279 |
+
]
|
280 |
+
},
|
281 |
+
{
|
282 |
+
"data": {
|
283 |
+
"text/plain": [
|
284 |
+
"{'user_input': \"Quel est l'impact du changement climatique sur Bordeaux ?\",\n",
|
285 |
+
" '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",
|
286 |
+
" 'relevant_content_sources_selection': ['Figures (IPCC/IPBES)', 'POC region'],\n",
|
287 |
+
" 'search_only': False,\n",
|
288 |
+
" 'reports': [],\n",
|
289 |
+
" 'language': 'French',\n",
|
290 |
+
" 'intent': 'search',\n",
|
291 |
+
" 'query': \"Quel est l'impact du changement climatique sur Bordeaux ?\"}"
|
292 |
+
]
|
293 |
+
},
|
294 |
+
"execution_count": 11,
|
295 |
+
"metadata": {},
|
296 |
+
"output_type": "execute_result"
|
297 |
+
}
|
298 |
+
],
|
299 |
"source": [
|
300 |
"cat_node = make_intent_categorization_node(llm)\n",
|
301 |
"state.update(cat_node(inial_state))\n",
|
|
|
304 |
},
|
305 |
{
|
306 |
"cell_type": "code",
|
307 |
+
"execution_count": 12,
|
308 |
"metadata": {},
|
309 |
"outputs": [],
|
310 |
"source": [
|
|
|
314 |
},
|
315 |
{
|
316 |
"cell_type": "code",
|
317 |
+
"execution_count": 13,
|
318 |
"metadata": {},
|
319 |
"outputs": [],
|
320 |
"source": [
|
|
|
326 |
},
|
327 |
{
|
328 |
"cell_type": "code",
|
329 |
+
"execution_count": 14,
|
330 |
"metadata": {},
|
331 |
+
"outputs": [
|
332 |
+
{
|
333 |
+
"name": "stdout",
|
334 |
+
"output_type": "stream",
|
335 |
+
"text": [
|
336 |
+
"---- Transform query ----\n"
|
337 |
+
]
|
338 |
+
},
|
339 |
+
{
|
340 |
+
"name": "stderr",
|
341 |
+
"output_type": "stream",
|
342 |
+
"text": [
|
343 |
+
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
|
344 |
+
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
|
345 |
+
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
|
346 |
+
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
|
347 |
+
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
|
348 |
+
"INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n"
|
349 |
+
]
|
350 |
+
},
|
351 |
+
{
|
352 |
+
"data": {
|
353 |
+
"text/plain": [
|
354 |
+
"{'user_input': \"Quel est l'impact du changement climatique sur Bordeaux ?\",\n",
|
355 |
+
" '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",
|
356 |
+
" 'relevant_content_sources_selection': ['Figures (IPCC/IPBES)', 'POC region'],\n",
|
357 |
+
" 'search_only': False,\n",
|
358 |
+
" 'reports': [],\n",
|
359 |
+
" 'language': 'French',\n",
|
360 |
+
" 'intent': 'search',\n",
|
361 |
+
" 'query': \"Quel est l'impact du changement climatique sur Bordeaux ?\",\n",
|
362 |
+
" 'questions_list': [{'question': \"Quel est l'impact du changement climatique sur la région de Bordeaux ?\",\n",
|
363 |
+
" 'sources': ['AcclimaTerra'],\n",
|
364 |
+
" 'index': 'POC',\n",
|
365 |
+
" 'source_type': 'POC'},\n",
|
366 |
+
" {'question': 'How does climate change affect the economy and environment of Bordeaux?',\n",
|
367 |
+
" 'sources': ['IPCC'],\n",
|
368 |
+
" 'index': 'IPx',\n",
|
369 |
+
" 'source_type': 'IPx'},\n",
|
370 |
+
" {'question': \"Comment le changement climatique affecte-t-il l'économie et l'environnement de Bordeaux ?\",\n",
|
371 |
+
" 'sources': ['AcclimaTerra'],\n",
|
372 |
+
" 'index': 'POC',\n",
|
373 |
+
" 'source_type': 'POC'}],\n",
|
374 |
+
" 'n_questions': {'total': 3, 'IPx': 1, 'POC': 2},\n",
|
375 |
+
" 'handled_questions_index': []}"
|
376 |
+
]
|
377 |
+
},
|
378 |
+
"execution_count": 14,
|
379 |
+
"metadata": {},
|
380 |
+
"output_type": "execute_result"
|
381 |
+
}
|
382 |
+
],
|
383 |
"source": [
|
384 |
"state.update(make_query_transform_node(llm)(state))\n",
|
385 |
"state"
|