Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -49,79 +49,78 @@ os.environ["MISTRAL_API_KEY"] = MISTRAL_API_KEY
|
|
| 49 |
from langchain_groq import ChatGroq
|
| 50 |
from langchain_mistralai.chat_models import ChatMistralAI
|
| 51 |
|
| 52 |
-
#llm = ChatMistralAI(model="mistral-large-latest")
|
| 53 |
-
llm = ChatGroq(model="llama3-70b-8192")
|
| 54 |
-
|
| 55 |
-
@tool
|
| 56 |
-
def db_query_tool(query: str) -> str:
|
| 57 |
-
"""
|
| 58 |
-
Execute a SQL query against the database and return the result.
|
| 59 |
-
If the query is invalid or returns no result, an error message will be returned.
|
| 60 |
-
In case of an error, the user is advised to rewrite the query and try again.
|
| 61 |
-
"""
|
| 62 |
-
result = db_instance.run_no_throw(query)
|
| 63 |
-
if not result:
|
| 64 |
-
return "Error: Query failed. Please rewrite your query and try again."
|
| 65 |
-
return result
|
| 66 |
-
|
| 67 |
-
# Define a Pydantic model for submitting the final answer
|
| 68 |
-
class SubmitFinalAnswer(BaseModel):
|
| 69 |
-
"""Submit the final answer to the user based on the query results."""
|
| 70 |
-
final_answer: str = Field(..., description="The final answer to the user")
|
| 71 |
-
|
| 72 |
-
# Define the state type
|
| 73 |
-
class State(TypedDict):
|
| 74 |
-
messages: Annotated[list[AnyMessage], add_messages]
|
| 75 |
-
|
| 76 |
-
# Define prompt templates for query checking and query generation
|
| 77 |
-
from langchain_core.prompts import ChatPromptTemplate
|
| 78 |
-
|
| 79 |
-
query_check_system = """You are a SQL expert with a strong attention to detail.
|
| 80 |
-
Double check the SQLite query for common mistakes, including:
|
| 81 |
-
- Using NOT IN with NULL values
|
| 82 |
-
- Using UNION when UNION ALL should have been used
|
| 83 |
-
- Using BETWEEN for exclusive ranges
|
| 84 |
-
- Data type mismatch in predicates
|
| 85 |
-
- Properly quoting identifiers
|
| 86 |
-
- Using the correct number of arguments for functions
|
| 87 |
-
- Casting to the correct data type
|
| 88 |
-
- Using the proper columns for joins
|
| 89 |
-
|
| 90 |
-
If there are any of the above mistakes, rewrite the query. If there are no mistakes, just reproduce the original query.
|
| 91 |
-
|
| 92 |
-
You will call the appropriate tool to execute the query after running this check."""
|
| 93 |
-
query_check_prompt = ChatPromptTemplate.from_messages([("system", query_check_system), ("placeholder", "{messages}")])
|
| 94 |
-
query_check = query_check_prompt | llm.bind_tools([db_query_tool])
|
| 95 |
-
|
| 96 |
-
query_gen_system = """You are a SQL expert with a strong attention to detail.
|
| 97 |
-
|
| 98 |
-
Given an input question, output a syntactically correct SQLite query to run, then look at the results of the query and return the answer.
|
| 99 |
-
|
| 100 |
-
DO NOT call any tool besides SubmitFinalAnswer to submit the final answer.
|
| 101 |
-
|
| 102 |
-
When generating the query:
|
| 103 |
-
|
| 104 |
-
Output the SQL query that answers the input question without a tool call.
|
| 105 |
-
|
| 106 |
-
Unless the user specifies a specific number of examples they wish to obtain, always limit your query to at most 5 results.
|
| 107 |
-
You can order the results by a relevant column to return the most interesting examples in the database.
|
| 108 |
-
Never query for all the columns from a specific table, only ask for the relevant columns given the question.
|
| 109 |
-
|
| 110 |
-
If you get an error while executing a query, rewrite the query and try again.
|
| 111 |
-
|
| 112 |
-
If you get an empty result set, you should try to rewrite the query to get a non-empty result set.
|
| 113 |
-
NEVER make stuff up if you don't have enough information to answer the query... just say you don't have enough information.
|
| 114 |
-
|
| 115 |
-
If you have enough information to answer the input question, simply invoke the appropriate tool to submit the final answer to the user.
|
| 116 |
-
|
| 117 |
-
DO NOT make any DML statements (INSERT, UPDATE, DELETE, DROP etc.) to the database. Do not return any sql query except answer."""
|
| 118 |
-
query_gen_prompt = ChatPromptTemplate.from_messages([("system", query_gen_system), ("placeholder", "{messages}")])
|
| 119 |
-
query_gen = query_gen_prompt | llm.bind_tools([SubmitFinalAnswer])
|
| 120 |
-
|
| 121 |
-
|
| 122 |
def create_agent_app(db_path: str):
|
| 123 |
# Construct the SQLite URI from the given file path.
|
| 124 |
# Ensure the db_path is absolute so that SQLAlchemy can locate the file.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
abs_db_path = os.path.abspath(db_path)
|
| 126 |
global DATABASE_URI
|
| 127 |
DATABASE_URI = abs_db_path
|
|
|
|
| 49 |
from langchain_groq import ChatGroq
|
| 50 |
from langchain_mistralai.chat_models import ChatMistralAI
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
def create_agent_app(db_path: str):
|
| 53 |
# Construct the SQLite URI from the given file path.
|
| 54 |
# Ensure the db_path is absolute so that SQLAlchemy can locate the file.
|
| 55 |
+
#llm = ChatMistralAI(model="mistral-large-latest")
|
| 56 |
+
llm = ChatGroq(model="llama3-70b-8192")
|
| 57 |
+
|
| 58 |
+
@tool
|
| 59 |
+
def db_query_tool(query: str) -> str:
|
| 60 |
+
"""
|
| 61 |
+
Execute a SQL query against the database and return the result.
|
| 62 |
+
If the query is invalid or returns no result, an error message will be returned.
|
| 63 |
+
In case of an error, the user is advised to rewrite the query and try again.
|
| 64 |
+
"""
|
| 65 |
+
result = db_instance.run_no_throw(query)
|
| 66 |
+
if not result:
|
| 67 |
+
return "Error: Query failed. Please rewrite your query and try again."
|
| 68 |
+
return result
|
| 69 |
+
|
| 70 |
+
# Define a Pydantic model for submitting the final answer
|
| 71 |
+
class SubmitFinalAnswer(BaseModel):
|
| 72 |
+
"""Submit the final answer to the user based on the query results."""
|
| 73 |
+
final_answer: str = Field(..., description="The final answer to the user")
|
| 74 |
+
|
| 75 |
+
# Define the state type
|
| 76 |
+
class State(TypedDict):
|
| 77 |
+
messages: Annotated[list[AnyMessage], add_messages]
|
| 78 |
+
|
| 79 |
+
# Define prompt templates for query checking and query generation
|
| 80 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 81 |
+
|
| 82 |
+
query_check_system = """You are a SQL expert with a strong attention to detail.
|
| 83 |
+
Double check the SQLite query for common mistakes, including:
|
| 84 |
+
- Using NOT IN with NULL values
|
| 85 |
+
- Using UNION when UNION ALL should have been used
|
| 86 |
+
- Using BETWEEN for exclusive ranges
|
| 87 |
+
- Data type mismatch in predicates
|
| 88 |
+
- Properly quoting identifiers
|
| 89 |
+
- Using the correct number of arguments for functions
|
| 90 |
+
- Casting to the correct data type
|
| 91 |
+
- Using the proper columns for joins
|
| 92 |
+
|
| 93 |
+
If there are any of the above mistakes, rewrite the query. If there are no mistakes, just reproduce the original query.
|
| 94 |
+
|
| 95 |
+
You will call the appropriate tool to execute the query after running this check."""
|
| 96 |
+
query_check_prompt = ChatPromptTemplate.from_messages([("system", query_check_system), ("placeholder", "{messages}")])
|
| 97 |
+
query_check = query_check_prompt | llm.bind_tools([db_query_tool])
|
| 98 |
+
|
| 99 |
+
query_gen_system = """You are a SQL expert with a strong attention to detail.
|
| 100 |
+
|
| 101 |
+
Given an input question, output a syntactically correct SQLite query to run, then look at the results of the query and return the answer.
|
| 102 |
+
|
| 103 |
+
DO NOT call any tool besides SubmitFinalAnswer to submit the final answer.
|
| 104 |
+
|
| 105 |
+
When generating the query:
|
| 106 |
+
|
| 107 |
+
Output the SQL query that answers the input question without a tool call.
|
| 108 |
+
|
| 109 |
+
Unless the user specifies a specific number of examples they wish to obtain, always limit your query to at most 5 results.
|
| 110 |
+
You can order the results by a relevant column to return the most interesting examples in the database.
|
| 111 |
+
Never query for all the columns from a specific table, only ask for the relevant columns given the question.
|
| 112 |
+
|
| 113 |
+
If you get an error while executing a query, rewrite the query and try again.
|
| 114 |
+
|
| 115 |
+
If you get an empty result set, you should try to rewrite the query to get a non-empty result set.
|
| 116 |
+
NEVER make stuff up if you don't have enough information to answer the query... just say you don't have enough information.
|
| 117 |
+
|
| 118 |
+
If you have enough information to answer the input question, simply invoke the appropriate tool to submit the final answer to the user.
|
| 119 |
+
|
| 120 |
+
DO NOT make any DML statements (INSERT, UPDATE, DELETE, DROP etc.) to the database. Do not return any sql query except answer."""
|
| 121 |
+
query_gen_prompt = ChatPromptTemplate.from_messages([("system", query_gen_system), ("placeholder", "{messages}")])
|
| 122 |
+
query_gen = query_gen_prompt | llm.bind_tools([SubmitFinalAnswer])
|
| 123 |
+
|
| 124 |
abs_db_path = os.path.abspath(db_path)
|
| 125 |
global DATABASE_URI
|
| 126 |
DATABASE_URI = abs_db_path
|