langchain[patch]: Upgrade prompts to optional imports (#21078)

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.
This commit is contained in:
Eugene Yurtsev 2024-04-30 16:23:39 -04:00 committed by GitHub
parent 9b6d04a187
commit 8658d52587
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 74 additions and 10 deletions

View File

@ -27,9 +27,8 @@ from multiple components. Prompt classes and functions make constructing
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,
@ -53,8 +52,29 @@ from langchain_core.prompts import (
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",

View File

@ -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,
)
@ -10,6 +9,28 @@ from langchain_core.example_selectors.semantic_similarity import (
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",

View File

@ -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",