Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to execute related query functions using CassandraDatabaseToolkit. #28878

Open
5 tasks done
BACMiao opened this issue Dec 22, 2024 · 2 comments
Open
5 tasks done
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@BACMiao
Copy link

BACMiao commented Dec 22, 2024

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

# Import necessary libraries
import os

import cassio
from langchain import hub
from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain_community.agent_toolkits.cassandra_database.toolkit import (
    CassandraDatabaseToolkit,
)
from langchain_community.tools.cassandra_database.prompt import QUERY_PATH_PROMPT
from langchain_community.utilities.cassandra_database import CassandraDatabase
from langchain_openai import ChatOpenAI
from dotenv import load_dotenv


def init_cassandra():
    load_dotenv(override=True)
    cassio.init(auto=True)
    session = cassio.config.resolve_session()
    if not session:
        raise Exception(
            "Check environment configuration or manually configure cassio connection parameters"
        )

    session.execute("""DROP KEYSPACE IF EXISTS langchain_agent_test; """)
    session.execute(
        """
    CREATE KEYSPACE if not exists langchain_agent_test 
    WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
    """
    )
    session.execute(
        """
        CREATE TABLE IF NOT EXISTS langchain_agent_test.user_credentials (
        user_email text PRIMARY KEY,
        user_id UUID,
        password TEXT
    );
    """
    )
    session.execute(
        """
        CREATE TABLE IF NOT EXISTS langchain_agent_test.users (
        id UUID PRIMARY KEY,
        name TEXT,
        email TEXT
    );"""
    )
    session.execute(
        """
        CREATE TABLE IF NOT EXISTS langchain_agent_test.user_videos ( 
        user_id UUID,
        video_id UUID,
        title TEXT,
        description TEXT,
        PRIMARY KEY (user_id, video_id)
    );
    """
    )

    user_id = "522b1fe2-2e36-4cef-a667-cd4237d08b89"
    video_id = "27066014-bad7-9f58-5a30-f63fe03718f6"

    session.execute(
        f"""
        INSERT INTO langchain_agent_test.user_credentials (user_id, user_email) 
        VALUES ({user_id}, '[email protected]');
    """
    )

    session.execute(
        f"""
        INSERT INTO langchain_agent_test.users (id, name, email) 
        VALUES ({user_id}, 'Patrick McFadin', '[email protected]');
    """
    )

    session.execute(
        f"""
        INSERT INTO langchain_agent_test.user_videos (user_id, video_id, title)
        VALUES ({user_id}, {video_id}, 'Use Langflow to Build a LangChain LLM Application in 5 Minutes');
    """
    )

    session.set_keyspace("langchain_agent_test")


def test_sql_agent(prompt):
    load_dotenv(override=True)
    cassio.init(auto=True)
    session = cassio.config.resolve_session()
    if not session:
        raise Exception(
            "Check environment configuration or manually configure cassio connection parameters"
        )
    db = CassandraDatabase(session=session)
    llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo")
    toolkit = CassandraDatabaseToolkit(db=db)
    tools = toolkit.get_tools()
    print("Available tools:")
    for tool in tools:
        print(tool.name + "\t- " + tool.description)
    agent = create_openai_tools_agent(llm, tools, hub.pull("hwchase17/openai-tools-agent"))
    input = (
            QUERY_PATH_PROMPT
            + "\n\n" + prompt
    )

    agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
    response = agent_executor.invoke({"input": input})
    print(response["output"])


if __name__ == "__main__":
    # init_cassandra()
    prompt = f"Here is your task: In the langchain_agent_test keyspace Find all the videos that the user with the email address '[email protected]' has uploaded to the langchain_agent_test keyspace and tell me the result"
    test_sql_agent(prompt)

Error Message and Stack Trace (if applicable)

No response

Description

I want to use CassandraDatabaseToolkit to process data. I referred to the tutorial at https://python.langchain.com/v0.2/docs/integrations/tools/cassandra_database/, but unfortunately, my output is completely different from what is shown in the documentation. Through debugging, we found that it directly returned ActionFinish without executing the tools. Further analysis revealed that tool_calls is empty. Could you please explain why this is happening?

System Info

openai==1.14.0
langchain-community==0.3.18

@dosubot dosubot bot added the 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature label Dec 22, 2024
@keenborder786
Copy link
Contributor

@BACMiao It is very difficult to help you without you providing the output. Can you please provide the output? But please note one thing, the model used in tutorial is gpt-4-1106-preview while model you are using is gpt-3.5-turbo which might be causing the different output.

@BACMiao
Copy link
Author

BACMiao commented Dec 23, 2024

@keenborder786 Thank you very much. Based on your suggestion, I updated the model to gpt-4-1106-preview and tested again using the latest downloaded commit. Unfortunately, I still got the same results as before.

The codes:

# Import necessary libraries
import os

import cassio
from langchain import hub
from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain_community.agent_toolkits.cassandra_database.toolkit import (
    CassandraDatabaseToolkit,
)
from langchain_community.tools.cassandra_database.prompt import QUERY_PATH_PROMPT
from langchain_community.utilities.cassandra_database import CassandraDatabase
from langchain_community.chat_models.openai import ChatOpenAI
from dotenv import load_dotenv


def init_cassandra():
    load_dotenv(override=True)
    cassio.init(auto=True)
    session = cassio.config.resolve_session()
    if not session:
        raise Exception(
            "Check environment configuration or manually configure cassio connection parameters"
        )

    session.execute("""DROP KEYSPACE IF EXISTS langchain_agent_test; """)
    session.execute(
        """
    CREATE KEYSPACE if not exists langchain_agent_test 
    WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
    """
    )
    session.execute(
        """
        CREATE TABLE IF NOT EXISTS langchain_agent_test.user_credentials (
        user_email text PRIMARY KEY,
        user_id UUID,
        password TEXT
    );
    """
    )
    session.execute(
        """
        CREATE TABLE IF NOT EXISTS langchain_agent_test.users (
        id UUID PRIMARY KEY,
        name TEXT,
        email TEXT
    );"""
    )
    session.execute(
        """
        CREATE TABLE IF NOT EXISTS langchain_agent_test.user_videos ( 
        user_id UUID,
        video_id UUID,
        title TEXT,
        description TEXT,
        PRIMARY KEY (user_id, video_id)
    );
    """
    )

    user_id = "522b1fe2-2e36-4cef-a667-cd4237d08b89"
    video_id = "27066014-bad7-9f58-5a30-f63fe03718f6"

    session.execute(
        f"""
        INSERT INTO langchain_agent_test.user_credentials (user_id, user_email) 
        VALUES ({user_id}, '[email protected]');
    """
    )

    session.execute(
        f"""
        INSERT INTO langchain_agent_test.users (id, name, email) 
        VALUES ({user_id}, 'Patrick McFadin', '[email protected]');
    """
    )

    session.execute(
        f"""
        INSERT INTO langchain_agent_test.user_videos (user_id, video_id, title)
        VALUES ({user_id}, {video_id}, 'Use Langflow to Build a LangChain LLM Application in 5 Minutes');
    """
    )

    session.set_keyspace("langchain_agent_test")


def test_sql_agent(prompt):
    load_dotenv(override=True)
    cassio.init(auto=True)
    session = cassio.config.resolve_session()
    if not session:
        raise Exception(
            "Check environment configuration or manually configure cassio connection parameters"
        )
    db = CassandraDatabase(session=session)
    llm = ChatOpenAI(temperature=0, model="gpt-4-1106-preview")
    toolkit = CassandraDatabaseToolkit(db=db)
    tools = toolkit.get_tools()
    print("Available tools:")
    for tool in tools:
        print(tool.name + "\t- " + tool.description)
    agent = create_openai_tools_agent(llm, tools, hub.pull("hwchase17/openai-tools-agent"))
    input = (
            QUERY_PATH_PROMPT
            + "\n\n" + prompt
    )

    agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
    response = agent_executor.invoke({"input": input})
    print(response["output"])


if __name__ == "__main__":
    # init_cassandra()
    prompt = f"Forget all our previous conversations. Here is your task: In the langchain_agent_test keyspace Find all the videos that the user with the email address '[email protected]' has uploaded to the langchain_agent_test keyspace and tell me the result"
    test_sql_agent(prompt)

The output:
image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

No branches or pull requests

2 participants