talk2data / llm_utils.py
prithvi55's picture
Update llm_utils.py
101df2f verified
Raw History Blame
2.37 kB
from langchain_groq import ChatGroq
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
import os
def get_llm():
return ChatGroq(
model="llama3-8b-8192",
temperature=0.0,
max_retries=2,
api_key=os.environ.get("groq_key")
)
def get_sql_prompt():
return PromptTemplate(
input_variables=["question", "schema"],
template="""
You are an expert SQL query generator and database assistant for a SQLite database. Given the following database schema:
{schema}
For any question that is not about the schema or database structure, ALWAYS generate a valid SQL query that answers the user's request. Do not ask for clarification, do not return explanations, just return the SQL query.
If the user asks for a list of tables, schema details, or information about the database structure, provide a clear and concise answer using the schema provided, not SQL.
If the user asks for a SQL query, generate an optimized SQL query for this user request:
"{question}"
Guidelines:
- Only use functions and syntax supported by SQLite. Do NOT use EXTRACT, INTERVAL, DATEADD, or other non-SQLite features.
- For date filtering, use strftime (e.g., strftime('%Y', "Table"."Date") = strftime('%Y', 'now', '-1 year')).
- Use proper JOINs if necessary.
- Always prefix column names with their table names to avoid ambiguity.
- Ensure column and table names are used exactly as in the schema, including case, spaces, and special characters.
- Always enclose both table and column names in double quotes, e.g., "TableName"."Column Name". This is required for names with spaces or special characters.
- Order results logically and apply LIMIT where needed (e.g., for 'top N' queries).
- Only use columns and tables present in the schema above. Do not invent or guess names.
- If unsure, ask the user to clarify or show the schema.
Example for date filtering in SQLite:
To get records from the previous year:
WHERE strftime('%Y', "Table"."Date") = strftime('%Y', 'now', '-1 year')
Return only the SQL query—no explanations.
"""
)