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

imports of OpenAI and SQLDatabase don't work in .py file #13906

Closed
2 of 14 tasks
nyck33 opened this issue Nov 27, 2023 · 3 comments
Closed
2 of 14 tasks

imports of OpenAI and SQLDatabase don't work in .py file #13906

nyck33 opened this issue Nov 27, 2023 · 3 comments
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature Ɑ: models Related to LLMs or chat model modules

Comments

@nyck33
Copy link

nyck33 commented Nov 27, 2023

System Info

Ubuntu 23.10

Who can help?

@hwchase17 @agola11

Information

  • The official example notebooks/scripts
  • My own modified scripts

Related Components

  • LLMs/Chat Models
  • Embedding Models
  • Prompts / Prompt Templates / Prompt Selectors
  • Output Parsers
  • Document Loaders
  • Vector Stores / Retrievers
  • Memory
  • Agents / Agent Executors
  • Tools / Toolkits
  • Chains
  • Callbacks/Tracing
  • Async

Reproduction

from langchain import OpenAI, SQLDatabase
from snowflake.snowpark import Session 
from langchain.chains import create_sql_query_chain

from dotenv import load_dotenv
import os
from urllib.parse import quote

load_dotenv()

# use the env vars in comments above to set the vars below
OpenAI_API_KEY = os.getenv("OPENAI_API_KEY")
snowflake_account = os.getenv("ACCOUNT")
username = os.getenv("USER")
password = os.getenv("SNOWSQL_PWD")
warehouse = os.getenv("WAREHOUSE")
database = 'LANGCHAIN_DEMO_DB' #os.getenv("DATABASE")
schema = 'PUBLIC' #os.getenv("SCHEMA")
role = os.getenv("ROLE")


# print out all env vars using f-strings each on a separate line but x out password
print(f"OpenAI_API_KEY: {'x' * len(OpenAI_API_KEY)}")
print(f"snowflake_account: {snowflake_account}")
#print(f"username: {username}")
#print(f"password: {password}")
print(f"warehouse: {warehouse}")
print(f"database: {database}")
print(f"schema: {schema}")
print(f"role: {role}")

encoded_password = quote(password, safe='')

but it works in my Jupyter notebook

image

https://medium.com/@muriithicliffernest/snowflake-langchain-generating-sql-queries-from-natural-language-queries-12c4e2918631 is the tutorial I followed for the .ipynb.

pip install --upgrade pip
pip install "snowflake-snowpark-python[pandas]" snowflake-sqlalchemy
pip install langchain openai langchain-experimental jupyter

are the instructions to install packages in that Medium article so I matched versions for both the conda env I'm using for the .py and .ipynb. Even if I use the same langchain-snowlfake env for both the error is still there. See the red line under from langchain import OpenAI, SQLDatabase in the right half of the image which is showing lanchain-sql.py

Expected behavior

The import should work, no red line.

@nyck33
Copy link
Author

nyck33 commented Nov 27, 2023

The errors I see when I hover over the red line are

"SQLDatabase" is not accessedPylance
No name 'OpenAI' in module 'langchain'Pylint[E0611:no-name-in-module](https://pylint.readthedocs.io/en/latest/user_guide/messages/error/no-name-in-module.html)
No name 'SQLDatabase' in module 'langchain'Pylint[E0611:no-name-in-module](https://pylint.readthedocs.io/en/latest/user_guide/messages/error/no-name-in-module.html)
Unused OpenAI imported from langchainPylint[W0611:unused-import](https://pylint.readthedocs.io/en/latest/user_guide/messages/warning/unused-import.html)
Unused SQLDatabase imported from langchainPylint[W0611:unused-import](https://pylint.readthedocs.io/en/latest/user_guide/messages/warning/unused-import.html)
`langchain.SQLDatabase` imported but unusedRuff[F401](https://docs.astral.sh/ruff/rules/unused-import)

@nyck33
Copy link
Author

nyck33 commented Nov 27, 2023

er, I uninstalled PyLint from VS Code and now the red underline is gone.

Copy link

dosubot bot commented Nov 27, 2023

🤖

Hello @nyck33! I'm here to help you with your LangChain issues while we wait for a human maintainer. Just so you know, I'm an AI, and I'm designed to assist with bug fixing, answering questions, and helping users like you become contributors. If you have any questions or need further clarification, feel free to ask. Let's get started!

Based on the information you've provided and the context from the LangChain repository, it seems like the issue might be due to how Python resolves imports in a script (.py file) versus a Jupyter notebook.

In a Jupyter notebook, the working directory is the directory where the notebook is located, so relative imports work as expected. However, in a Python script, the working directory is the directory from where the script is run, which can cause issues with relative imports.

To resolve this issue, you can try one of the following solutions:

  1. Modify your PYTHONPATH environment variable to include the directory containing the LangChain library. This will allow Python to find the LangChain library no matter where your script is located.

  2. Use absolute imports in your script. Instead of importing like this:

from langchain.utilities.sql_database import SQLDatabase

You would import like this:

from libs.langchain.langchain.utilities.sql_database import SQLDatabase

Remember to replace "libs" with the actual path to the directory containing the LangChain library.

Please note that these solutions assume that the LangChain library is correctly installed in your environment and that the SQLDatabase class is correctly defined in the langchain.utilities.sql_database module. If this is not the case, you might need to check the installation of the LangChain library or the definition of the SQLDatabase class.

Additionally, the OpenAI class needs to be imported from a specific module within the langchain package. It is not available for direct import from the langchain package. The correct import statement would be from langchain.llms.openai import OpenAI.

I hope this helps! If you have any other questions or need further clarification, feel free to ask.

Sources

@dosubot dosubot bot added Ɑ: models Related to LLMs or chat model modules 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Nov 27, 2023
@nyck33 nyck33 closed this as completed Feb 9, 2024
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 Ɑ: models Related to LLMs or chat model modules
Projects
None yet
Development

No branches or pull requests

1 participant