cli[minor]: Fix bug to account for name changes (#20948)

* Fix bug to account for name changes / aliases
* Generate migration list from langchain to langchain_core
pull/20949/head
Eugene Yurtsev 2 weeks ago committed by GitHub
parent 989e4a92c2
commit 8ed150b2fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -961,7 +961,7 @@
],
[
"langchain.callbacks.streamlit._InternalStreamlitCallbackHandler",
"langchain_community.callbacks.streamlit.streamlit_callback_handler._InternalStreamlitCallbackHandler"
"langchain_community.callbacks.streamlit.streamlit_callback_handler.StreamlitCallbackHandler"
],
[
"langchain.callbacks.streamlit.mutable_expander.ChildType",
@ -1953,7 +1953,7 @@
],
[
"langchain.document_loaders.PagedPDFSplitter",
"langchain_community.document_loaders.pdf.PagedPDFSplitter"
"langchain_community.document_loaders.PyPDFLoader"
],
[
"langchain.document_loaders.PlaywrightURLLoader",
@ -2069,7 +2069,7 @@
],
[
"langchain.document_loaders.TelegramChatLoader",
"langchain_community.document_loaders.telegram.TelegramChatLoader"
"langchain_community.document_loaders.TelegramChatLoader"
],
[
"langchain.document_loaders.TensorflowDatasetLoader",
@ -3377,7 +3377,7 @@
],
[
"langchain.embeddings.SentenceTransformerEmbeddings",
"langchain_community.embeddings.huggingface.SentenceTransformerEmbeddings"
"langchain_community.embeddings.SentenceTransformerEmbeddings"
],
[
"langchain.embeddings.GooglePalmEmbeddings",
@ -3673,7 +3673,7 @@
],
[
"langchain.embeddings.sentence_transformer.SentenceTransformerEmbeddings",
"langchain_community.embeddings.huggingface.SentenceTransformerEmbeddings"
"langchain_community.embeddings.SentenceTransformerEmbeddings"
],
[
"langchain.embeddings.spacy_embeddings.SpacyEmbeddings",
@ -4905,7 +4905,7 @@
],
[
"langchain.requests.RequestsWrapper",
"langchain_community.utilities.requests.RequestsWrapper"
"langchain_community.utilities.TextRequestsWrapper"
],
[
"langchain.requests.TextRequestsWrapper",
@ -7113,7 +7113,7 @@
],
[
"langchain.utilities.RequestsWrapper",
"langchain_community.utilities.requests.RequestsWrapper"
"langchain_community.utilities.TextRequestsWrapper"
],
[
"langchain.utilities.SteamWebAPIWrapper",
@ -7409,7 +7409,7 @@
],
[
"langchain.utilities.requests.RequestsWrapper",
"langchain_community.utilities.requests.RequestsWrapper"
"langchain_community.utilities.TextRequestsWrapper"
],
[
"langchain.utilities.scenexplain.SceneXplainAPIWrapper",

@ -54,7 +54,7 @@ PARTNERS = [
def _load_migrations_from_fixtures() -> List[Tuple[str, str]]:
"""Load migrations from fixtures."""
paths: List[str] = PARTNERS + ["langchain.json"]
paths: List[str] = PARTNERS + ["langchain_to_langchain_community.json"]
data = []
for path in paths:
data.extend(_load_migrations_by_file(path))

@ -5,11 +5,11 @@ import pkgutil
from typing import List, Tuple
def generate_raw_migrations_to_community() -> List[Tuple[str, str]]:
def generate_raw_migrations(
from_package: str, to_package: str
) -> List[Tuple[str, str]]:
"""Scan the `langchain` package and generate migrations for all modules."""
import langchain as package
to_package = "langchain_community"
package = importlib.import_module(from_package)
items = []
for importer, modname, ispkg in pkgutil.walk_packages(
@ -36,7 +36,9 @@ def generate_raw_migrations_to_community() -> List[Tuple[str, str]]:
continue
if obj and (inspect.isclass(obj) or inspect.isfunction(obj)):
if obj.__module__.startswith(to_package):
items.append((f"{modname}.{name}", f"{obj.__module__}.{name}"))
items.append(
(f"{modname}.{name}", f"{obj.__module__}.{obj.__name__}")
)
# Iterate over all members of the module
for name, obj in inspect.getmembers(module):
@ -44,12 +46,14 @@ def generate_raw_migrations_to_community() -> List[Tuple[str, str]]:
if inspect.isclass(obj) or inspect.isfunction(obj):
# Check if the module name of the obj starts with 'langchain_community'
if obj.__module__.startswith(to_package):
items.append((f"{modname}.{name}", f"{obj.__module__}.{name}"))
items.append(
(f"{modname}.{name}", f"{obj.__module__}.{obj.__name__}")
)
return items
def generate_top_level_imports_community() -> List[Tuple[str, str]]:
def generate_top_level_imports(pkg: str) -> List[Tuple[str, str]]:
"""This code will look at all the top level modules in langchain_community.
It'll attempt to import everything from each __init__ file
@ -73,7 +77,9 @@ def generate_top_level_imports_community() -> List[Tuple[str, str]]:
to importing it from the top level namespaces
(e.g., langchain_community.chat_models.XYZ)
"""
import langchain_community as package
import importlib
package = importlib.import_module(pkg)
items = []
# Only iterate through top-level modules/packages
@ -105,10 +111,12 @@ def generate_top_level_imports_community() -> List[Tuple[str, str]]:
return items
def generate_simplified_migrations() -> List[Tuple[str, str]]:
def generate_simplified_migrations(
from_package: str, to_package: str
) -> List[Tuple[str, str]]:
"""Get all the raw migrations, then simplify them if possible."""
raw_migrations = generate_raw_migrations_to_community()
top_level_simplifications = generate_top_level_imports_community()
raw_migrations = generate_raw_migrations(from_package, to_package)
top_level_simplifications = generate_top_level_imports(to_package)
top_level_dict = {full: top_level for full, top_level in top_level_simplifications}
simple_migrations = []
for migration in raw_migrations:

@ -4,7 +4,7 @@ import pkgutil
import click
from langchain_cli.namespaces.migrate.generate.langchain import (
from langchain_cli.namespaces.migrate.generate.generic import (
generate_simplified_migrations,
)
from langchain_cli.namespaces.migrate.generate.partner import (
@ -19,15 +19,27 @@ def cli():
@cli.command()
@click.option(
"--pkg1",
default="langchain",
)
@click.option(
"--pkg2",
default="langchain_community",
)
@click.option(
"--output",
default="langchain_migrations.json",
default=None,
help="Output file for the migration script.",
)
def langchain(output: str) -> None:
def generic(pkg1: str, pkg2: str, output: str) -> None:
"""Generate a migration script."""
click.echo("Migration script generated.")
migrations = generate_simplified_migrations()
migrations = generate_simplified_migrations(pkg1, pkg2)
if output is None:
output = f"{pkg1}_to_{pkg2}.json"
with open(output, "w") as f:
f.write(json.dumps(migrations, indent=2, sort_keys=True))

@ -1,11 +1,14 @@
from langchain_cli.namespaces.migrate.generate.langchain import (
from langchain_cli.namespaces.migrate.generate.generic import (
generate_simplified_migrations,
generate_raw_migrations,
)
def test_create_json_agent_migration() -> None:
"""Test the migration of create_json_agent from langchain to langchain_community."""
raw_migrations = generate_simplified_migrations()
raw_migrations = generate_simplified_migrations(
from_package="langchain", to_package="langchain_community"
)
json_agent_migrations = [
migration for migration in raw_migrations if "create_json_agent" in migration[0]
]
@ -23,3 +26,20 @@ def test_create_json_agent_migration() -> None:
"langchain_community.agent_toolkits.create_json_agent",
),
]
def test_create_single_store_retriever_db() -> None:
"""Test migration from langchain to langchain_core"""
raw_migrations = generate_simplified_migrations(
from_package="langchain", to_package="langchain_core"
)
# SingleStore was an old name for VectorStoreRetriever
single_store_migration = [
migration for migration in raw_migrations if "SingleStore" in migration[0]
]
assert single_store_migration == [
(
"langchain.vectorstores.singlestoredb.SingleStoreDBRetriever",
"langchain_core.vectorstores.VectorStoreRetriever",
),
]

Loading…
Cancel
Save