# sql_tool.py
from langchain.agents import Tool
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain_groq import ChatGroq

def create_run_sql_query_tool(db, api_key):
    llm = ChatGroq(groq_api_key=api_key, model_name="Llama3-8b-8192", streaming=True)
    
    # Toolkit
    toolkit = SQLDatabaseToolkit(db=db, llm=llm)

    # Create SQL database tool
    sql_funcs = toolkit.get_tools()[0]

    return Tool(
        name="Run SQL Query",
        func=sql_funcs.run,
        description="Useful for querying SQL databases with query already prepared"
    )