Can anyone help out with this error,when executing the code, it has to pass the question to the tool, but it is not passing the user_query to the tool,the tool will take the question and returns the results.
My code:
from crewai import Agent, Task, Crew
from langchain_community.tools.sql_database.tool import (
InfoSQLDatabaseTool,
ListSQLDatabaseTool,
QuerySQLCheckerTool,
QuerySQLDataBaseTool,
)
from langchain_community.utilities.sql_database import SQLDatabase
from langchain_core.tools import tool
from textwrap import dedent
from langchain_openai import ChatOpenAI
import os
import re
import sqlite3
Initialize the LLM
llm = ChatOpenAI(
model=“gpt-4o-mini”,
api_key=os.getenv(“OPENAI_API_KEY”),
temperature=0.1,
max_tokens=200
)
@tool(‘facility_search_tool’)
def facility_search_tool_using_user_query(user_query: str) → dict:
“”“Uses GPT-4o-mini to generate and execute an SQL query based on a user query for facility data.”“”
# Create prompt to guide the model in generating an SQL query
prompt = (
f"You're a data collector responsible for gathering facility data from the MAINTENANCE_DATA table "
f"in the SQLite database. Your task is to ensure accurate and timely data collection for analysis. "
f"Important columns of MAINTENANCE_DATA table: CLIENT_NAME, FACILITY_NAME, TOTAL_DOWNTIME, AVAILABLE_TIME, "
f"TIME_BASED_AVAILABILITY, PRODUCTION_BASED_AVAILABILITY. Given the user's question: '{user_query}', create an SQL query for "
f"the MAINTENANCE_DATA table to retrieve relevant data. Provide a syntactically correct SQL query only."
)
# Generate SQL query using the LLM model instance
response = llm.predict(prompt)
# Define regex pattern to match SQL queries (SELECT statements)
sql_pattern = r"(?i)(SELECT\s+.*?FROM\s+\w+.*?;)"
# Extract SQL query from response
match = re.search(sql_pattern, response, re.DOTALL)
sql_query = match.group(1).strip() if match else None
if sql_query:
# Connect to the SQLite database
conn = sqlite3.connect("finalversion1wind.db")
cursor = conn.cursor()
# Execute the generated SQL query
cursor.execute(sql_query)
# Fetch results and get column names
facility_info = cursor.fetchall() # Fetch all results
column_names = [description[0] for description in cursor.description]
# Close the database connection
conn.close()
# Create a list of dictionaries from the results
results = [dict(zip(column_names, row)) for row in facility_info]
return results # Return all results as a list of dictionaries
else:
print("No valid SQL query generated.")
return None
Define Facility Data Collector Agent
facility_data_collector_agent = Agent(
role=“Facility Data Collector Agent”,
goal=“Construct and execute SQL queries based on user questions”,
backstory=dedent(
“”“You are a Wind subject matter expert and an experienced database engineer who excels at creating efficient
and complex SQL queries. You have a deep understanding of how different databases work and how to optimize queries.
Use the ‘facility_search_tool’ for writing SQL queries and executing them against the database.
Note: Always use LIMIT 5 in your SQL queries according to user questions.”“”
),
llm=llm,
tools=[facility_search_tool_using_user_query],
allow_delegation=False,
)
Task for Facility Data Collection
facility_data_collector_task = Task(
description=(
"Your job is to take the information from the user question {user_query}
and collect client data from "
“the SQLite database using the MAINTENANCE_DATA table. Pass the exact user_query to the tool,the tool will gave the question and give us the results of database”
),
expected_output=(
“Corresponding SQL query with results and a concise answer relating to the user question.”
),
agent=facility_data_collector_agent,
)
Define Crew with agents and tasks
financial_trading_crew = Crew(
agents=[facility_data_collector_agent],
tasks=[facility_data_collector_task],
manager_llm=ChatOpenAI(model=“gpt-3.5-turbo”, temperature=0.7),
process=Process.hierarchical,
verbose=True
)
Input for execution
inputs = {
‘user_query’: ‘Give me top 5 facilities list’
}
Execute Crew tasks
result = financial_trading_crew.kickoff(inputs=inputs)
Print result output if available
if result:
print(“Result:”, result)
else:
print(“No result returned.”)
When executing the code it is throwing this error:
2024-11-01 17:54:08,850 - 8238723648 - init.py-init:538 - WARNING: Overriding of current TracerProvider is not allowed
Agent: Crew Manager
Task: Your job is to take the information from the user question Give me top 5 facilities list
and collect client data from the SQLite database using the MAINTENANCE_DATA table. Pass the exact user_query to the tool,the tool will gave the question and give us the results of database
Agent: Crew Manager
Thought: I need to delegate the task of collecting client data from the SQLite database to the Facility Data Collector Agent using the tool available.
Using tool: Delegate work to coworker
Tool Input:
“{"task": "Collect client data from the SQLite database using the MAINTENANCE_DATA table", "context": "The user query is ‘Give me top 5 facilities list’. Please provide the corresponding SQL query with results and a concise answer relating to the user question."}”
Tool Output:
Error executing tool. coworker mentioned not found, it must be one of the following options:
- facility data collector agent
Agent: Facility Data Collector Agent
Task: Collect client data from the SQLite database using the MAINTENANCE_DATA table
Agent: Facility Data Collector Agent
Thought: I need to construct an SQL query to retrieve the top 5 facilities from the MAINTENANCE_DATA table.
Using tool: facility_search_tool
Tool Input:
“{"name": "GetTopFacilities", "description": "Retrieve top 5 facilities from MAINTENANCE_DATA", "args_schema": {}, "return_direct": false, "verbose": false}”
Tool Output:
Error: the Action Input is not a valid key, value dictionary.
Agent: Facility Data Collector Agent
Thought: Thought: I need to construct a valid SQL query to retrieve the top 5 facilities from the MAINTENANCE_DATA table.
Using tool: facility_search_tool
Tool Input:
“{"name": "GetTopFacilities", "description": "Retrieve top 5 facilities from MAINTENANCE_DATA", "args_schema": {}, "return_direct": false, "verbose": false}”
Tool Output:
Error: the Action Input is not a valid key, value dictionary.
Agent: Facility Data Collector Agent
Thought: Thought: I need to create a proper SQL query to fetch the top 5 facilities from the MAINTENANCE_DATA table.
Using tool: facility_search_tool
Tool Input:
“{"name": "GetTopFacilities", "description": "Retrieve top 5 facilities from MAINTENANCE_DATA", "args_schema": {}, "return_direct": false, "verbose": false}”
Tool Output:
Error: the Action Input is not a valid key, value dictionary.
Agent: Facility Data Collector Agent
Thought: Thought: I need to construct a SQL query to retrieve the top 5 facilities from the MAINTENANCE_DATA table. I will formulate the query correctly.
Using tool: facility_search_tool
Tool Input:
“{"name": "GetTopFacilities", "description": "Retrieve top 5 facilities from MAINTENANCE_DATA", "args_schema": {}, "return_direct": false, "verbose": false}”
Tool Output:
Error: the Action Input is not a valid key, value dictionary.