crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

Native LangChain agents in crewAI?

Open parthbhandari01 opened this issue 2 years ago • 5 comments

I have a query: can we use the native pre-built LangChain agents along with custom agents created in crewAI?

For e.g., instead of creating Agents and providing them roles, can we use LangChain agents like CSV agents or SQL agents to fetch data and do some tasks?

parthbhandari01 avatar Mar 12 '24 18:03 parthbhandari01

Bump Wondering the same. I have tried but they appear incompatible with CrewAI unlike LangChain tools.

EngJean avatar Mar 26 '24 19:03 EngJean

Bump, tried and failed:

from langchain_community.agent_toolkits.sql.base import create_sql_agent
from langchain_community.utilities.sql_database import SQLDatabase
from langchain_openai import ChatOpenAI
from crewai import Agent
from crewai.agent import CrewAgentExecutor


db = SQLDatabase.from_uri("postgresql://localhost:5432/app")
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)

langchain_executor = create_sql_agent(llm, db=db, agent_type="openai-tools", verbose=True)

executor = CrewAgentExecutor.from_agent_and_tools(
  agent=langchain_executor.agent, 
  tools=langchain_executor.tools, 
  verbose=True
)

db_agent = Agent(
  role="Database Admin",
  goal="Interact with SQL database",
  backstory="I am a database admin who can interact with SQL databases to run queries and fetch data",
  tools=executor.tools,
  agent_executor=executor,
  verbose=True,
  allow_delegation=True
)

This snippet however doesn't work and goes into a loop, and the agent tries to fix the Action input

 [DEBUG]: == Working Agent: Database Admin
 [INFO]: == Starting Task: Find total number of active users


> Entering new CrewAgentExecutor chain...
I need to check the available tables in the database to see if there is a table that contains information about the users.

Action:
sql_db_list_tables

Action Input:
""
Thought:
I apologize for the mistake. I should have provided the input as a dictionary. Let's fix that.

Action:
sql_db_list_tables

Action Input:
{}

user_permissions, users

Thought:
There is a table named 'users'. I need to check the schema of this table to see if there is a column that indicates user activity.

Action:
sql_db_schema

Action Input:
{"tables": "users"} <==== this key is wrong, it should be table_names


I encountered an error while trying to use the tool. This was the error: InfoSQLDatabaseTool._run() got an unexpected keyword argument 'tables'.
 Tool sql_db_schema accepts these inputs: Input to this tool is a comma-separated list of tables, output is the schema and sample rows for those tables. Be sure that the tables actually exist by calling sql_db_list_tables first! Example Input: table1, table2, table3

According to the expected types on the input here:

https://github.com/langchain-ai/langchain/blob/22dbcc9441cec11f15e03d963149db491002c1a8/libs/community/langchain_community/tools/sql_database/tool.py#L53-L69

When I run langchain_executor.invoke("Find total number of active users"), it works flawlessly.

What would be the best way to not re-invent the prompts for existing langchain agents, and still use these agents as part of the Crew?

sreecodeslayer avatar Apr 02 '24 17:04 sreecodeslayer

How we can execute the sql query in database which is generated by the llm using agent ????

D1EE7P2U9 avatar May 26 '24 16:05 D1EE7P2U9

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Aug 20 '24 10:08 github-actions[bot]

This issue was closed because it has been stalled for 5 days with no activity.

github-actions[bot] avatar Aug 25 '24 12:08 github-actions[bot]