Skip to content
This repository was archived by the owner on May 19, 2025. It is now read-only.

Commit 8b5f4cc

Browse files
committed
feat: migration via gritql
1 parent 53293da commit 8b5f4cc

39 files changed

+4289
-13210
lines changed

libs/cli/langchain_cli/cli.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import importlib
21
from typing import Optional
32

43
import typer
4+
from gritql import run
55
from typing_extensions import Annotated
66

77
from langchain_cli.namespaces import app as app_namespace
88
from langchain_cli.namespaces import integration as integration_namespace
99
from langchain_cli.namespaces import template as template_namespace
10+
from langchain_cli.namespaces.migrate import main as migrate_namespace
1011
from langchain_cli.utils.packages import get_langserve_export, get_package_root
1112

1213
__version__ = "0.0.22rc0"
@@ -23,11 +24,23 @@
2324
)
2425

2526

26-
# If libcst is installed, add the migrate namespace
27-
if importlib.util.find_spec("libcst"):
28-
from langchain_cli.namespaces.migrate import main as migrate_namespace
29-
30-
app.add_typer(migrate_namespace.app, name="migrate", help=migrate_namespace.__doc__)
27+
@app.command(
28+
context_settings={
29+
# Let Grit handle the arguments
30+
"allow_extra_args": True,
31+
"ignore_unknown_options": True,
32+
},
33+
# Grit embeds its own help
34+
add_help_option=False,
35+
)
36+
def migrate(ctx: typer.Context):
37+
final_code = run.apply_pattern(
38+
"langchain_all_migrations()",
39+
ctx.args,
40+
grit_dir=migrate_namespace.get_gritdir_path(),
41+
)
42+
43+
raise typer.Exit(code=final_code)
3144

3245

3346
def version_callback(show_version: bool) -> None:

libs/cli/langchain_cli/namespaces/app.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ def new(
123123
typer.echo(f" cd ./{app_name}\n")
124124
typer.echo("Then add templates with commands like:\n")
125125
typer.echo(" langchain app add extraction-openai-functions")
126-
typer.echo(" langchain app add git+ssh://[email protected]/efriis/simple-pirate.git\n\n")
126+
typer.echo(
127+
" langchain app add git+ssh://[email protected]/efriis/simple-pirate.git\n\n"
128+
)
127129

128130

129131
@app_cli.command()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.gritmodules*
2+
*.log
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version: 0.0.1
2+
patterns:
3+
- name: github.com/getgrit/stdlib#*
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Testing the replace_imports migration
2+
3+
This runs the v0.2 migration with a desired set of rules.
4+
5+
```grit
6+
language python
7+
8+
langchain_all_migrations()
9+
```
10+
11+
## Single import
12+
13+
Before:
14+
15+
```python
16+
from langchain.chat_models import ChatOpenAI
17+
```
18+
19+
After:
20+
21+
```python
22+
from langchain_community.chat_models import ChatOpenAI
23+
```
24+
25+
## Community to partner
26+
27+
```python
28+
from langchain_community.chat_models import ChatOpenAI
29+
```
30+
31+
```python
32+
from langchain_openai import ChatOpenAI
33+
```
34+
35+
## Noop
36+
37+
This file should not match at all.
38+
39+
```python
40+
from foo import ChatOpenAI
41+
```
42+
43+
## Mixed imports
44+
45+
```python
46+
from langchain_community.chat_models import ChatOpenAI, ChatAnthropic, foo
47+
```
48+
49+
```python
50+
from langchain_community.chat_models import foo
51+
52+
from langchain_openai import ChatOpenAI
53+
54+
from langchain_anthropic import ChatAnthropic
55+
56+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
language python
3+
4+
// This migration is generated automatically - do not manually edit this file
5+
pattern langchain_migrate_anthropic() {
6+
find_replace_imports(list=[
7+
[`langchain_community.chat_models.anthropic`, `ChatAnthropic`, `langchain_anthropic`, `ChatAnthropic`],
8+
[`langchain_community.llms.anthropic`, `Anthropic`, `langchain_anthropic`, `Anthropic`],
9+
[`langchain_community.chat_models`, `ChatAnthropic`, `langchain_anthropic`, `ChatAnthropic`],
10+
[`langchain_community.llms`, `Anthropic`, `langchain_anthropic`, `Anthropic`]
11+
])
12+
}
13+
14+
// Add this for invoking directly
15+
langchain_migrate_anthropic()
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
language python
3+
4+
// This migration is generated automatically - do not manually edit this file
5+
pattern langchain_migrate_astradb() {
6+
find_replace_imports(list=[
7+
8+
[
9+
`langchain_community.vectorstores.astradb`,
10+
`AstraDB`,
11+
`langchain_astradb`,
12+
`AstraDBVectorStore`
13+
]
14+
,
15+
16+
[
17+
`langchain_community.storage.astradb`,
18+
`AstraDBByteStore`,
19+
`langchain_astradb`,
20+
`AstraDBByteStore`
21+
]
22+
,
23+
24+
[
25+
`langchain_community.storage.astradb`,
26+
`AstraDBStore`,
27+
`langchain_astradb`,
28+
`AstraDBStore`
29+
]
30+
,
31+
32+
[
33+
`langchain_community.cache`,
34+
`AstraDBCache`,
35+
`langchain_astradb`,
36+
`AstraDBCache`
37+
]
38+
,
39+
40+
[
41+
`langchain_community.cache`,
42+
`AstraDBSemanticCache`,
43+
`langchain_astradb`,
44+
`AstraDBSemanticCache`
45+
]
46+
,
47+
48+
[
49+
`langchain_community.chat_message_histories.astradb`,
50+
`AstraDBChatMessageHistory`,
51+
`langchain_astradb`,
52+
`AstraDBChatMessageHistory`
53+
]
54+
,
55+
56+
[
57+
`langchain_community.document_loaders.astradb`,
58+
`AstraDBLoader`,
59+
`langchain_astradb`,
60+
`AstraDBLoader`
61+
]
62+
63+
])
64+
}
65+
66+
// Add this for invoking directly
67+
langchain_migrate_astradb()
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
language python
3+
4+
// This migration is generated automatically - do not manually edit this file
5+
pattern langchain_migrate_community_to_core() {
6+
find_replace_imports(list=[
7+
[`langchain_community.callbacks.tracers`, `ConsoleCallbackHandler`, `langchain_core.tracers`, `ConsoleCallbackHandler`],
8+
[`langchain_community.callbacks.tracers`, `FunctionCallbackHandler`, `langchain_core.tracers.stdout`, `FunctionCallbackHandler`],
9+
[`langchain_community.callbacks.tracers`, `LangChainTracer`, `langchain_core.tracers`, `LangChainTracer`],
10+
[`langchain_community.callbacks.tracers`, `LangChainTracerV1`, `langchain_core.tracers.langchain_v1`, `LangChainTracerV1`],
11+
[`langchain_community.docstore.document`, `Document`, `langchain_core.documents`, `Document`],
12+
[`langchain_community.document_loaders`, `Blob`, `langchain_core.document_loaders`, `Blob`],
13+
[`langchain_community.document_loaders`, `BlobLoader`, `langchain_core.document_loaders`, `BlobLoader`],
14+
[`langchain_community.document_loaders.base`, `BaseBlobParser`, `langchain_core.document_loaders`, `BaseBlobParser`],
15+
[`langchain_community.document_loaders.base`, `BaseLoader`, `langchain_core.document_loaders`, `BaseLoader`],
16+
[`langchain_community.document_loaders.blob_loaders`, `Blob`, `langchain_core.document_loaders`, `Blob`],
17+
[`langchain_community.document_loaders.blob_loaders`, `BlobLoader`, `langchain_core.document_loaders`, `BlobLoader`],
18+
[`langchain_community.document_loaders.blob_loaders.schema`, `Blob`, `langchain_core.document_loaders`, `Blob`],
19+
[`langchain_community.document_loaders.blob_loaders.schema`, `BlobLoader`, `langchain_core.document_loaders`, `BlobLoader`],
20+
[`langchain_community.tools`, `BaseTool`, `langchain_core.tools`, `BaseTool`],
21+
[`langchain_community.tools`, `StructuredTool`, `langchain_core.tools`, `StructuredTool`],
22+
[`langchain_community.tools`, `Tool`, `langchain_core.tools`, `Tool`],
23+
[`langchain_community.tools`, `format_tool_to_openai_function`, `langchain_core.utils.function_calling`, `format_tool_to_openai_function`],
24+
[`langchain_community.tools`, `tool`, `langchain_core.tools`, `tool`],
25+
[`langchain_community.tools.convert_to_openai`, `format_tool_to_openai_function`, `langchain_core.utils.function_calling`, `format_tool_to_openai_function`],
26+
[`langchain_community.tools.convert_to_openai`, `format_tool_to_openai_tool`, `langchain_core.utils.function_calling`, `format_tool_to_openai_tool`],
27+
[`langchain_community.tools.render`, `format_tool_to_openai_function`, `langchain_core.utils.function_calling`, `format_tool_to_openai_function`],
28+
[`langchain_community.tools.render`, `format_tool_to_openai_tool`, `langchain_core.utils.function_calling`, `format_tool_to_openai_tool`],
29+
[`langchain_community.utils.openai_functions`, `FunctionDescription`, `langchain_core.utils.function_calling`, `FunctionDescription`],
30+
[`langchain_community.utils.openai_functions`, `ToolDescription`, `langchain_core.utils.function_calling`, `ToolDescription`],
31+
[`langchain_community.utils.openai_functions`, `convert_pydantic_to_openai_function`, `langchain_core.utils.function_calling`, `convert_pydantic_to_openai_function`],
32+
[`langchain_community.utils.openai_functions`, `convert_pydantic_to_openai_tool`, `langchain_core.utils.function_calling`, `convert_pydantic_to_openai_tool`],
33+
[`langchain_community.vectorstores`, `VectorStore`, `langchain_core.vectorstores`, `VectorStore`]
34+
])
35+
}
36+
37+
// Add this for invoking directly
38+
langchain_migrate_community_to_core()

libs/cli/langchain_cli/namespaces/migrate/codemods/migrations/community_to_core.json renamed to libs/cli/langchain_cli/namespaces/migrate/.grit/patterns/community_to_core.json

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,17 @@
5151
"langchain_community.document_loaders.blob_loaders.schema.BlobLoader",
5252
"langchain_core.document_loaders.BlobLoader"
5353
],
54-
[
55-
"langchain_community.tools.BaseTool",
56-
"langchain_core.tools.BaseTool"
57-
],
54+
["langchain_community.tools.BaseTool", "langchain_core.tools.BaseTool"],
5855
[
5956
"langchain_community.tools.StructuredTool",
6057
"langchain_core.tools.StructuredTool"
6158
],
62-
[
63-
"langchain_community.tools.Tool",
64-
"langchain_core.tools.Tool"
65-
],
59+
["langchain_community.tools.Tool", "langchain_core.tools.Tool"],
6660
[
6761
"langchain_community.tools.format_tool_to_openai_function",
6862
"langchain_core.utils.function_calling.format_tool_to_openai_function"
6963
],
70-
[
71-
"langchain_community.tools.tool",
72-
"langchain_core.tools.tool"
73-
],
64+
["langchain_community.tools.tool", "langchain_core.tools.tool"],
7465
[
7566
"langchain_community.tools.convert_to_openai.format_tool_to_openai_function",
7667
"langchain_core.utils.function_calling.format_tool_to_openai_function"
@@ -107,4 +98,4 @@
10798
"langchain_community.vectorstores.VectorStore",
10899
"langchain_core.vectorstores.VectorStore"
109100
]
110-
]
101+
]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
language python
2+
3+
pattern langchain_all_migrations() {
4+
any {
5+
langchain_migrate_community_to_core(),
6+
langchain_migrate_fireworks(),
7+
langchain_migrate_ibm(),
8+
langchain_migrate_langchain_to_core(),
9+
langchain_migrate_langchain_to_langchain_community(),
10+
langchain_migrate_langchain_to_textsplitters(),
11+
langchain_migrate_openai(),
12+
langchain_migrate_pinecone(),
13+
langchain_migrate_anthropic()
14+
}
15+
}
16+
17+
langchain_all_migrations()

0 commit comments

Comments
 (0)