Spaces:
Sleeping
Sleeping
Update llm_utils.py
Browse files- llm_utils.py +43 -43
llm_utils.py
CHANGED
|
@@ -1,43 +1,43 @@
|
|
| 1 |
-
from langchain_groq import ChatGroq
|
| 2 |
-
from langchain.prompts import PromptTemplate
|
| 3 |
-
from langchain.chains import LLMChain
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
-
def get_llm():
|
| 7 |
-
return ChatGroq(
|
| 8 |
-
model="llama3-8b-8192",
|
| 9 |
-
temperature=0.0,
|
| 10 |
-
max_retries=2,
|
| 11 |
-
api_key=os.environ.get(
|
| 12 |
-
)
|
| 13 |
-
|
| 14 |
-
def get_sql_prompt():
|
| 15 |
-
return PromptTemplate(
|
| 16 |
-
input_variables=["question", "schema"],
|
| 17 |
-
template="""
|
| 18 |
-
You are an expert SQL query generator and database assistant for a SQLite database. Given the following database schema:
|
| 19 |
-
{schema}
|
| 20 |
-
|
| 21 |
-
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.
|
| 22 |
-
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.
|
| 23 |
-
If the user asks for a SQL query, generate an optimized SQL query for this user request:
|
| 24 |
-
"{question}"
|
| 25 |
-
|
| 26 |
-
Guidelines:
|
| 27 |
-
- Only use functions and syntax supported by SQLite. Do NOT use EXTRACT, INTERVAL, DATEADD, or other non-SQLite features.
|
| 28 |
-
- For date filtering, use strftime (e.g., strftime('%Y', "Table"."Date") = strftime('%Y', 'now', '-1 year')).
|
| 29 |
-
- Use proper JOINs if necessary.
|
| 30 |
-
- Always prefix column names with their table names to avoid ambiguity.
|
| 31 |
-
- Ensure column and table names are used exactly as in the schema, including case, spaces, and special characters.
|
| 32 |
-
- 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.
|
| 33 |
-
- Order results logically and apply LIMIT where needed (e.g., for 'top N' queries).
|
| 34 |
-
- Only use columns and tables present in the schema above. Do not invent or guess names.
|
| 35 |
-
- If unsure, ask the user to clarify or show the schema.
|
| 36 |
-
|
| 37 |
-
Example for date filtering in SQLite:
|
| 38 |
-
To get records from the previous year:
|
| 39 |
-
WHERE strftime('%Y', "Table"."Date") = strftime('%Y', 'now', '-1 year')
|
| 40 |
-
|
| 41 |
-
Return only the SQL query—no explanations.
|
| 42 |
-
"""
|
| 43 |
-
)
|
|
|
|
| 1 |
+
from langchain_groq import ChatGroq
|
| 2 |
+
from langchain.prompts import PromptTemplate
|
| 3 |
+
from langchain.chains import LLMChain
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def get_llm():
|
| 7 |
+
return ChatGroq(
|
| 8 |
+
model="llama3-8b-8192",
|
| 9 |
+
temperature=0.0,
|
| 10 |
+
max_retries=2,
|
| 11 |
+
api_key=os.environ.get(groq_key)
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
def get_sql_prompt():
|
| 15 |
+
return PromptTemplate(
|
| 16 |
+
input_variables=["question", "schema"],
|
| 17 |
+
template="""
|
| 18 |
+
You are an expert SQL query generator and database assistant for a SQLite database. Given the following database schema:
|
| 19 |
+
{schema}
|
| 20 |
+
|
| 21 |
+
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.
|
| 22 |
+
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.
|
| 23 |
+
If the user asks for a SQL query, generate an optimized SQL query for this user request:
|
| 24 |
+
"{question}"
|
| 25 |
+
|
| 26 |
+
Guidelines:
|
| 27 |
+
- Only use functions and syntax supported by SQLite. Do NOT use EXTRACT, INTERVAL, DATEADD, or other non-SQLite features.
|
| 28 |
+
- For date filtering, use strftime (e.g., strftime('%Y', "Table"."Date") = strftime('%Y', 'now', '-1 year')).
|
| 29 |
+
- Use proper JOINs if necessary.
|
| 30 |
+
- Always prefix column names with their table names to avoid ambiguity.
|
| 31 |
+
- Ensure column and table names are used exactly as in the schema, including case, spaces, and special characters.
|
| 32 |
+
- 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.
|
| 33 |
+
- Order results logically and apply LIMIT where needed (e.g., for 'top N' queries).
|
| 34 |
+
- Only use columns and tables present in the schema above. Do not invent or guess names.
|
| 35 |
+
- If unsure, ask the user to clarify or show the schema.
|
| 36 |
+
|
| 37 |
+
Example for date filtering in SQLite:
|
| 38 |
+
To get records from the previous year:
|
| 39 |
+
WHERE strftime('%Y', "Table"."Date") = strftime('%Y', 'now', '-1 year')
|
| 40 |
+
|
| 41 |
+
Return only the SQL query—no explanations.
|
| 42 |
+
"""
|
| 43 |
+
)
|