mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-05 21:12:48 +00:00
multiple: pydantic 2 compatibility, v0.3 (#26443)
Signed-off-by: ChengZi <chen.zhang@zilliz.com> Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com> Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: Dan O'Donovan <dan.odonovan@gmail.com> Co-authored-by: Tom Daniel Grande <tomdgrande@gmail.com> Co-authored-by: Grande <Tom.Daniel.Grande@statsbygg.no> Co-authored-by: Bagatur <baskaryan@gmail.com> Co-authored-by: ccurme <chester.curme@gmail.com> Co-authored-by: Harrison Chase <hw.chase.17@gmail.com> Co-authored-by: Tomaz Bratanic <bratanic.tomaz@gmail.com> Co-authored-by: ZhangShenao <15201440436@163.com> Co-authored-by: Friso H. Kingma <fhkingma@gmail.com> Co-authored-by: ChengZi <chen.zhang@zilliz.com> Co-authored-by: Nuno Campos <nuno@langchain.dev> Co-authored-by: Morgante Pell <morgantep@google.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""Script to generate migrations for the migration script."""
|
||||
|
||||
import json
|
||||
import os
|
||||
import pkgutil
|
||||
|
||||
import click
|
||||
@@ -8,6 +9,9 @@ import click
|
||||
from langchain_cli.namespaces.migrate.generate.generic import (
|
||||
generate_simplified_migrations,
|
||||
)
|
||||
from langchain_cli.namespaces.migrate.generate.grit import (
|
||||
dump_migrations_as_grit,
|
||||
)
|
||||
from langchain_cli.namespaces.migrate.generate.partner import (
|
||||
get_migrations_for_partner_package,
|
||||
)
|
||||
@@ -38,16 +42,48 @@ def cli():
|
||||
default=True,
|
||||
help="Output file for the migration script.",
|
||||
)
|
||||
def generic(pkg1: str, pkg2: str, output: str, filter_by_all: bool) -> None:
|
||||
@click.option(
|
||||
"--format",
|
||||
type=click.Choice(["json", "grit"], case_sensitive=False),
|
||||
default="json",
|
||||
help="The output format for the migration script (json or grit).",
|
||||
)
|
||||
def generic(
|
||||
pkg1: str, pkg2: str, output: str, filter_by_all: bool, format: str
|
||||
) -> None:
|
||||
"""Generate a migration script."""
|
||||
click.echo("Migration script generated.")
|
||||
migrations = generate_simplified_migrations(pkg1, pkg2, filter_by_all=filter_by_all)
|
||||
|
||||
if output is not None:
|
||||
name = output.removesuffix(".json").removesuffix(".grit")
|
||||
else:
|
||||
name = f"{pkg1}_to_{pkg2}"
|
||||
|
||||
if output is None:
|
||||
output = f"{pkg1}_to_{pkg2}.json"
|
||||
output = f"{name}.json" if format == "json" else f"{name}.grit"
|
||||
|
||||
if format == "json":
|
||||
dumped = json.dumps(migrations, indent=2, sort_keys=True)
|
||||
else:
|
||||
dumped = dump_migrations_as_grit(name, migrations)
|
||||
|
||||
with open(output, "w") as f:
|
||||
f.write(json.dumps(migrations, indent=2, sort_keys=True))
|
||||
f.write(dumped)
|
||||
|
||||
|
||||
def handle_partner(pkg: str, output: str = None):
|
||||
migrations = get_migrations_for_partner_package(pkg)
|
||||
# Run with python 3.9+
|
||||
name = pkg.removeprefix("langchain_")
|
||||
data = dump_migrations_as_grit(name, migrations)
|
||||
output_name = f"{name}.grit" if output is None else output
|
||||
if migrations:
|
||||
with open(output_name, "w") as f:
|
||||
f.write(data)
|
||||
click.secho(f"LangChain migration script saved to {output_name}")
|
||||
else:
|
||||
click.secho(f"No migrations found for {pkg}", fg="yellow")
|
||||
|
||||
|
||||
@cli.command()
|
||||
@@ -56,21 +92,27 @@ def generic(pkg1: str, pkg2: str, output: str, filter_by_all: bool) -> None:
|
||||
def partner(pkg: str, output: str) -> None:
|
||||
"""Generate migration scripts specifically for LangChain modules."""
|
||||
click.echo("Migration script for LangChain generated.")
|
||||
migrations = get_migrations_for_partner_package(pkg)
|
||||
# Run with python 3.9+
|
||||
output_name = f"{pkg.removeprefix('langchain_')}.json" if output is None else output
|
||||
if migrations:
|
||||
with open(output_name, "w") as f:
|
||||
f.write(json.dumps(migrations, indent=2, sort_keys=True))
|
||||
click.secho(f"LangChain migration script saved to {output_name}")
|
||||
else:
|
||||
click.secho(f"No migrations found for {pkg}", fg="yellow")
|
||||
handle_partner(pkg, output)
|
||||
|
||||
|
||||
@cli.command()
|
||||
@click.argument("json_file")
|
||||
def json_to_grit(json_file: str) -> None:
|
||||
"""Generate a Grit migration from an old JSON migration file."""
|
||||
with open(json_file, "r") as f:
|
||||
migrations = json.load(f)
|
||||
name = os.path.basename(json_file).removesuffix(".json").removesuffix(".grit")
|
||||
data = dump_migrations_as_grit(name, migrations)
|
||||
output_name = f"{name}.grit"
|
||||
with open(output_name, "w") as f:
|
||||
f.write(data)
|
||||
click.secho(f"GritQL migration script saved to {output_name}")
|
||||
|
||||
|
||||
@cli.command()
|
||||
def all_installed_partner_pkgs() -> None:
|
||||
"""Generate migration scripts for all LangChain modules."""
|
||||
# Will generate migrations for all pather packages.
|
||||
# Will generate migrations for all partner packages.
|
||||
# Define as "langchain_<partner_name>".
|
||||
# First let's determine which packages are installed in the environment
|
||||
# and then generate migrations for them.
|
||||
@@ -81,15 +123,7 @@ def all_installed_partner_pkgs() -> None:
|
||||
and name not in {"langchain_core", "langchain_cli", "langchain_community"}
|
||||
]
|
||||
for pkg in langchain_pkgs:
|
||||
migrations = get_migrations_for_partner_package(pkg)
|
||||
# Run with python 3.9+
|
||||
output_name = f"{pkg.removeprefix('langchain_')}.json"
|
||||
if migrations:
|
||||
with open(output_name, "w") as f:
|
||||
f.write(json.dumps(migrations, indent=2, sort_keys=True))
|
||||
click.secho(f"LangChain migration script saved to {output_name}")
|
||||
else:
|
||||
click.secho(f"No migrations found for {pkg}", fg="yellow")
|
||||
handle_partner(pkg)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Reference in New Issue
Block a user