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

Deprecate direct access to globals like debug and verbose. #11311

Merged
merged 29 commits into from
Oct 12, 2023

Conversation

obi1kenobi
Copy link
Collaborator

@obi1kenobi obi1kenobi commented Oct 2, 2023

Instead of accessing langchain.debug, langchain.verbose, or langchain.llm_cache, please use the new getter/setter functions in langchain.globals:

  • langchain.globals.set_debug() and langchain.globals.get_debug()
  • langchain.globals.set_verbose() and langchain.globals.get_verbose()
  • langchain.globals.set_llm_cache() and langchain.globals.get_llm_cache()

Using the old globals directly will now raise a warning.

@vercel
Copy link

vercel bot commented Oct 2, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchain ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 12, 2023 10:47pm

@dosubot dosubot bot added the 🤖:refactor A large refactor of a feature(s) or restructuring of many files label Oct 2, 2023
@obi1kenobi
Copy link
Collaborator Author

obi1kenobi commented Oct 2, 2023

Still needs:

  • better deprecation warning message that points users to the new code to use
  • conversion of internal write uses to the new functions
  • conversion of internal read uses to make them use the new code path instead of using top-level langchain import
    • There's an unfortunate Python quirk: the __getattr__() in langchain/__init__.py creates a separate copy of the verbose and debug variables, so setting langchain.verbose with this PR does not actually set langchain.utils.globals._verbose.
    • I implemented a workaround: the new explicit setter functions set both copies, and the explicit getters will read both copies and return whichever one was set. That way using the new getters and setters is backward-compatible, and uses of the old top-level langchain values will also be able to set the values and see any changes done to them via the new APIs as well.
    • Using the old langchain top-level values directly now triggers a warning with a concrete suggestion to use the corresponding explicit get/set functions. At some point in the future when users have migrated to using the new API, we can remove the top-level values entirely.

Copy link
Contributor

@hwchase17 hwchase17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems reasonable, lets update all the docs (and internal usage) that do langchain.verbose=True to use these

@obi1kenobi obi1kenobi marked this pull request as ready for review October 9, 2023 14:39
@obi1kenobi obi1kenobi changed the title Move globals like debug and verbose to their own module. Deprecate direct access to globals like debug and verbose. Oct 9, 2023
Comment on lines 47 to 51
def __setattr__(name, value):
print("foo")
if name == 'debug' and value is True:
warnings.warn("Debug mode is enabled in LangChain", UserWarning)
globals()[name] = value
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also tried this sort of thing and hoped it would work, but no luck.

PEP 562 which defines the module-level __getattr__() doesn't allow for a __setattr__() so I don't think this ever gets called: https://peps.python.org/pep-0562/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah whoops i tried this out, it didnt work, and i forgot to remove 🤦

Copy link
Collaborator Author

@obi1kenobi obi1kenobi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating the docs, I completely forgot they are in the same repo. Will keep in mind for the future.

@hwchase17 hwchase17 merged commit 9e32120 into master Oct 12, 2023
32 checks passed
@hwchase17 hwchase17 deleted the pg/move_globals_to_own_module branch October 12, 2023 22:48
@xb3sox
Copy link

xb3sox commented Oct 16, 2023

Any idea how to remove this warning, it appear after each message:

D:\Agents\langchain\.venv\Lib\site-packages\langchain\__init__.py:34: UserWarning: Importing llm_cache from langchain root module is no longer supported. Please use langchain.globals.set_llm_cache() / langchain.globals.get_llm_cache() instead.
  warnings.warn(

@obi1kenobi
Copy link
Collaborator Author

Any idea how to remove this warning, it appear after each message:

D:\Agents\langchain\.venv\Lib\site-packages\langchain\__init__.py:34: UserWarning: Importing llm_cache from langchain root module is no longer supported. Please use langchain.globals.set_llm_cache() / langchain.globals.get_llm_cache() instead.
  warnings.warn(

I believe this might have been fixed in a subsequent PR, though I'm not exactly sure if it was already released or not yet. Please try upgrading langchain, and if the problem persists consider opening a new issue for it.

hoanq1811 pushed a commit to hoanq1811/langchain that referenced this pull request Feb 2, 2024
…hain-ai#11311)

Instead of accessing `langchain.debug`, `langchain.verbose`, or
`langchain.llm_cache`, please use the new getter/setter functions in
`langchain.globals`:
- `langchain.globals.set_debug()` and `langchain.globals.get_debug()`
- `langchain.globals.set_verbose()` and
`langchain.globals.get_verbose()`
- `langchain.globals.set_llm_cache()` and
`langchain.globals.get_llm_cache()`

Using the old globals directly will now raise a warning.

---------

Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:refactor A large refactor of a feature(s) or restructuring of many files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants