langchain sqlagent reply with wrong result

575 Views Asked by At

I have a simple SQLagent which queries a database with id and email address on a table. I want to check if there is a matching email and reply with id. if no match reply with none.

If there is a match, it works fine, but if there is no match, but there is a closer matching email, it returns the random value from the ID column. For example if there is a match for [email protected] it works, but if I pass in myFirstName.LastnamA@mydomain.com it will return a random ID. Any idea on how to fix this?

``*dburi = "sqlite:///data/users.db"
db = SQLDatabase.from_uri(dburi)
toolkit = SQLDatabaseToolkit(db=db, llm=OpenAI(temperature=0))

agent_executor = create_sql_agent(
    llm=OpenAI(temperature=0),
    toolkit=toolkit,
    verbose=True,
    agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
)

agent_executor.run("What is the id for email is exactly [email protected]. I want 100% match ignoring the case. If you can not find it reply with NONE")*`
`

If there is a match, it works fine, but if there is no match, but there is a closer matching email, it returns the random value from the ID column. For example if there is a match for [email protected] it works, but if I pass in myFirstName.LastnamA@mydomain.com it will return a random ID.

2

There are 2 best solutions below

0
ML Ops On

I managed to change the query and sort this out. My new query is:

agent_executor.run("Do you have a record with email is [email protected]. I want 100% match ignoring the case. If you have a match reply with the ID. If you can not find it reply with NONE")
0
ZKS On

You can use below code with minor tweak based on your requirements

        dburi = "sqlite:///data/users.db"
        db = SQLDatabase.from_uri(dburi)
        toolkit = SQLDatabaseToolkit(db=db, llm=OpenAI(temperature=0))

        custom_suffix = """
            Filter the records based on exact match, ignore similar search or close matched records
        """

        agent_executor = create_sql_agent(
            llm=OpenAI(temperature=0),
            toolkit=toolkit,
            verbose=True,
            agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
            suffix=custom_suffix
        )

        agent_executor.run("What is the id for email [email protected]")