Home > Workload Solutions > Data Analytics > White Papers > Multimodal RAG Chatbot Powered by Dell Data Lakehouse > 8. Text-to-SQL Conversion
The LLM translates the SQL components into executable SQL queries. These queries are designed to retrieve specific information from the structured data stored in the Data Analytics Engine.
# Function to refine SQL queries using the LLM
def refine_sql_queries(sql_queries):
refined_queries = []
for query in sql_queries:
# Construct input prompt for LLM
input_prompt = f"Refine the following SQL query for execution:\n{query}\n\nRefined SQL query:"
# Tokenize and generate a refined SQL query
inputs = tokenizer(input_prompt, return_tensors="pt")
outputs = model.generate(**inputs)
refined_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Extract the refined SQL query (usually found after the prompt)
refined_query = refined_query.split("Refined SQL query:")[1].strip()
refined_queries.append(refined_query)
return refined_queries
# Refine the SQL queries using the LLM
refined_sql_queries = refine_sql_queries(sql_queries)
# Output the refined SQL queries
for query in refined_sql_queries:
print("Refined SQL Query:", query)
The constructed SQL queries are submitted to the DDAE for execution.
import trino
import pandas as pd
# Function to execute SQL query on Dell Data Analytics Engine
def execute_query_on_ddae(query, host, port, user, password):
# Establish connection to the DDAE server
conn = trino.dbapi.connect(
host=host,
port=port,
http_scheme='https',
verify=False,
auth=trino.auth.BasicAuthentication(user, password)
)
# Execute the query and fetch results
df = pd.read_sql_query(query, conn)
# Close the connection
conn.close()
return df
# ddae server details
ddae_host = 'Dell Data Analytics Engine host'
ddae_port = 443
ddae_user = 'user'
ddae_password = 'password'
# Submit each refined SQL query to DDAE and get the results
for query in refined_sql_queries:
try:
result_df = execute_query_on_ddae(query, ddae_host, ddae_port, ddae_user, ddae_password)
print("Query Result:\n", result_df)
except Exception as e:
print(f"Error executing query: {e}")