Skip to content

Commit

Permalink
langchain[patch]: Upgrade prompts to optional imports (#21078)
Browse files Browse the repository at this point in the history
Upgrades prompts module to use optional imports.

This code was generated with a migration script, but had to be adjusted
manually a bit.

Testing in preparation for applying this code modification across the
rest of the modules in langchain package to reverse the dependency
between langchain community and langchain.
  • Loading branch information
eyurtsev authored Apr 30, 2024
1 parent 9b6d04a commit 8658d52
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
26 changes: 23 additions & 3 deletions libs/langchain/langchain/prompts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
ChatPromptValue
""" # noqa: E501
from langchain_community.example_selectors.ngram_overlap import (
NGramOverlapExampleSelector,
)
from typing import TYPE_CHECKING, Any

from langchain_core.example_selectors import (
LengthBasedExampleSelector,
MaxMarginalRelevanceExampleSelector,
Expand All @@ -53,8 +52,29 @@
load_prompt,
)

from langchain._api import create_importer
from langchain.prompts.prompt import Prompt

if TYPE_CHECKING:
from langchain_community.example_selectors.ngram_overlap import (
NGramOverlapExampleSelector,
)

# Create a way to dynamically look up deprecated imports.
# Used to consolidate logic for raising deprecation warnings and
# handling optional imports.
MODULE_LOOKUP = {
"NGramOverlapExampleSelector": "langchain_community.example_selectors.ngram_overlap"
}

_import_attribute = create_importer(__file__, module_lookup=MODULE_LOOKUP)


def __getattr__(name: str) -> Any:
"""Look up attributes dynamically."""
return _import_attribute(name)


__all__ = [
"AIMessagePromptTemplate",
"BaseChatPromptTemplate",
Expand Down
27 changes: 24 additions & 3 deletions libs/langchain/langchain/prompts/example_selector/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Logic for selecting examples to include in prompts."""
from langchain_community.example_selectors.ngram_overlap import (
NGramOverlapExampleSelector,
)
from typing import TYPE_CHECKING, Any

from langchain_core.example_selectors.length_based import (
LengthBasedExampleSelector,
)
Expand All @@ -10,6 +9,28 @@
SemanticSimilarityExampleSelector,
)

from langchain._api import create_importer

if TYPE_CHECKING:
from langchain_community.example_selectors.ngram_overlap import (
NGramOverlapExampleSelector,
)

# Create a way to dynamically look up deprecated imports.
# Used to consolidate logic for raising deprecation warnings and
# handling optional imports.
DEPRECATED_LOOKUPS = {
"NGramOverlapExampleSelector": "langchain_community.example_selectors.ngram_overlap"
}

_import_attribute = create_importer(__file__, deprecated_lookups=DEPRECATED_LOOKUPS)


def __getattr__(name: str) -> Any:
"""Look up attributes dynamically."""
return _import_attribute(name)


__all__ = [
"LengthBasedExampleSelector",
"MaxMarginalRelevanceExampleSelector",
Expand Down
31 changes: 27 additions & 4 deletions libs/langchain/langchain/prompts/example_selector/ngram_overlap.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
from langchain_community.example_selectors.ngram_overlap import (
NGramOverlapExampleSelector,
ngram_overlap_score,
)
from typing import TYPE_CHECKING, Any

from langchain._api import create_importer

if TYPE_CHECKING:
from langchain_community.example_selectors.ngram_overlap import (
NGramOverlapExampleSelector,
ngram_overlap_score,
)

# Create a way to dynamically look up deprecated imports.
# Used to consolidate logic for raising deprecation warnings and
# handling optional imports.
MODULE_LOOKUP = {
"NGramOverlapExampleSelector": (
"langchain_community.example_selectors.ngram_overlap"
),
"ngram_overlap_score": "langchain_community.example_selectors.ngram_overlap",
}

_import_attribute = create_importer(__file__, deprecated_lookups=MODULE_LOOKUP)


def __getattr__(name: str) -> Any:
"""Look up attributes dynamically."""
return _import_attribute(name)


__all__ = [
"NGramOverlapExampleSelector",
Expand Down

0 comments on commit 8658d52

Please sign in to comment.