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

[Bug] Double quotation around SQL query issue in 06-langchain-agents.ipynb #180

Open
2 tasks done
janzheng opened this issue Apr 19, 2023 · 6 comments
Open
2 tasks done
Labels
bug Something isn't working

Comments

@janzheng
Copy link

Is this a new bug?

  • I believe this is a new bug
  • I have searched the existing issues, and I could not find an existing issue for this bug

Current Behavior

When replicating in a Python notebook, the SQL Database tool produces this error, because of the double quotation marks ("")
OperationalError: near ""SELECT stock_ticker, price, date FROM stocks WHERE (stock_ticker = 'ABC' OR stock_ticker = 'XYZ') AND (date = '2023-01-03' OR date = '2023-01-04') LIMIT 5"": syntax error

Expected Behavior

Single quotation marks: "SELECT stock_ticker, price, date FROM stocks WHERE (stock_ticker = 'ABC' OR stock_ticker = 'XYZ') AND (date = '2023-01-03' OR date = '2023-01-04') LIMIT 5"

This can be done by changing the description to:

sql_tool = Tool( ... ,description="Useful for when you need to answer questions about stocks and their prices. The SQL query should be outputted plainly, do not surround it in quotes or anything else.")

Steps To Reproduce

Run with colab

Relevant log output

No response

Environment

Colab and Replit

Additional Context

Adding the extra description seems to prevent double quotes

@janzheng janzheng added the bug Something isn't working label Apr 19, 2023
@JacobGoldenArt
Copy link

Yep. I'm running into this too. But even after updating the description suggested above, I'm getting the following error:

OperationalError                          Traceback (most recent call last)
[/usr/local/lib/python3.9/dist-packages/sqlalchemy/engine/base.py](https://localhost:8080/#) in _execute_context(self, dialect, constructor, statement, parameters, execution_options, *args, **kw)
   1899                 if not evt_handled:
-> 1900                     self.dialect.do_execute(
   1901                         cursor, statement, parameters, context

22 frames
OperationalError: near ""SELECT (price/ (SELECT price FROM stocks WHERE stock_ticker = 'XYZ' AND date = '2023-01-03')) AS ratio_Jan3, (price/ (SELECT price FROM stocks WHERE stock_ticker = 'XYZ' AND date = '2023-01-04')) AS ratio_Jan4 FROM stocks WHERE stock_ticker = 'ABC' AND (date = '2023-01-03' OR date = '2023-01-04') LIMIT 5"": syntax error

@janzheng
Copy link
Author

James left a comment in the video:

try preappending "Use sqlite syntax to answer this query:" to the prompt (thanks to @mrburns4031 and @memesofproduction27 for pointing this out)

https://www.youtube.com/watch?v=jSP-gSEyVeI&t=172s

@davidsilvasmith
Copy link

Thanks for the fix @janzheng. Had this issue on my mac terminal. Doing this as you said in your initial issue fixed it for me.

sql_tool = Tool( ... ,description="Useful for when you need to answer questions about stocks and their prices. The SQL query should be outputted plainly, do not surround it in quotes or anything else.")

Seems strange the description has anything to do with the output. Seems more like a command to an LLM.

@davidsilvasmith
Copy link

OK I see how the description works is explained in the next step.

print(zero_shot_agent.agent.llm_chain.prompt.template)

@davidsilvasmith
Copy link

The quotes command helped for the first example, but then on the next example failed with an issue. Using the prepending like this worked for both.

sql_tool = Tool(
    name='Stock DB',
    func=sql_chain.run,
    description="Use sqlite syntax to answer this query: Useful for when you need to answer questions about stocks " \
                "and their prices."
    
)

@jellederijke
Copy link

I still get similar syntax errors despite using different variations like:

from langchain.agents import Tool

sql_tool = Tool(
    name='Stock DB',
    func=sql_chain.run,
    description="Use sqlite syntax to answer this query: Useful for when you need to answer questions about stocks and their prices. The SQL query should be outputted plainly, do not surround it in quotes or anything else."
)

For instance this:

result = count_tokens( zero_shot_agent, "What is in the first row of my stocks database?" )

At first seems to create a nice query:

Entering new SQLDatabaseChain chain...
SELECT * FROM stocks LIMIT 1;

But one line later messes it up again:
SQLQuery: "SELECT * FROM stocks LIMIT 1;"

Which gives the OperationalError.

Any thought are very much appreciated :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants