Skip to content

Commit

Permalink
feat: migration via gritql
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante committed May 22, 2024
1 parent 53293da commit 8b5f4cc
Show file tree
Hide file tree
Showing 39 changed files with 4,289 additions and 13,210 deletions.
25 changes: 19 additions & 6 deletions libs/cli/langchain_cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import importlib
from typing import Optional

import typer
from gritql import run
from typing_extensions import Annotated

from langchain_cli.namespaces import app as app_namespace
from langchain_cli.namespaces import integration as integration_namespace
from langchain_cli.namespaces import template as template_namespace
from langchain_cli.namespaces.migrate import main as migrate_namespace
from langchain_cli.utils.packages import get_langserve_export, get_package_root

__version__ = "0.0.22rc0"
Expand All @@ -23,11 +24,23 @@
)


# If libcst is installed, add the migrate namespace
if importlib.util.find_spec("libcst"):
from langchain_cli.namespaces.migrate import main as migrate_namespace

app.add_typer(migrate_namespace.app, name="migrate", help=migrate_namespace.__doc__)
@app.command(
context_settings={
# Let Grit handle the arguments
"allow_extra_args": True,
"ignore_unknown_options": True,
},
# Grit embeds its own help
add_help_option=False,
)
def migrate(ctx: typer.Context):
final_code = run.apply_pattern(
"langchain_all_migrations()",
ctx.args,
grit_dir=migrate_namespace.get_gritdir_path(),
)

raise typer.Exit(code=final_code)


def version_callback(show_version: bool) -> None:
Expand Down
4 changes: 3 additions & 1 deletion libs/cli/langchain_cli/namespaces/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ def new(
typer.echo(f" cd ./{app_name}\n")
typer.echo("Then add templates with commands like:\n")
typer.echo(" langchain app add extraction-openai-functions")
typer.echo(" langchain app add git+ssh://[email protected]/efriis/simple-pirate.git\n\n")
typer.echo(
" langchain app add git+ssh://[email protected]/efriis/simple-pirate.git\n\n"
)


@app_cli.command()
Expand Down
2 changes: 2 additions & 0 deletions libs/cli/langchain_cli/namespaces/migrate/.grit/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.gritmodules*
*.log
3 changes: 3 additions & 0 deletions libs/cli/langchain_cli/namespaces/migrate/.grit/grit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version: 0.0.1
patterns:
- name: github.com/getgrit/stdlib#*
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Testing the replace_imports migration

This runs the v0.2 migration with a desired set of rules.

```grit
language python
langchain_all_migrations()
```

## Single import

Before:

```python
from langchain.chat_models import ChatOpenAI
```

After:

```python
from langchain_community.chat_models import ChatOpenAI
```

## Community to partner

```python
from langchain_community.chat_models import ChatOpenAI
```

```python
from langchain_openai import ChatOpenAI
```

## Noop

This file should not match at all.

```python
from foo import ChatOpenAI
```

## Mixed imports

```python
from langchain_community.chat_models import ChatOpenAI, ChatAnthropic, foo
```

```python
from langchain_community.chat_models import foo

from langchain_openai import ChatOpenAI

from langchain_anthropic import ChatAnthropic

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

language python

// This migration is generated automatically - do not manually edit this file
pattern langchain_migrate_anthropic() {
find_replace_imports(list=[
[`langchain_community.chat_models.anthropic`, `ChatAnthropic`, `langchain_anthropic`, `ChatAnthropic`],
[`langchain_community.llms.anthropic`, `Anthropic`, `langchain_anthropic`, `Anthropic`],
[`langchain_community.chat_models`, `ChatAnthropic`, `langchain_anthropic`, `ChatAnthropic`],
[`langchain_community.llms`, `Anthropic`, `langchain_anthropic`, `Anthropic`]
])
}

// Add this for invoking directly
langchain_migrate_anthropic()
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

language python

// This migration is generated automatically - do not manually edit this file
pattern langchain_migrate_astradb() {
find_replace_imports(list=[

[
`langchain_community.vectorstores.astradb`,
`AstraDB`,
`langchain_astradb`,
`AstraDBVectorStore`
]
,

[
`langchain_community.storage.astradb`,
`AstraDBByteStore`,
`langchain_astradb`,
`AstraDBByteStore`
]
,

[
`langchain_community.storage.astradb`,
`AstraDBStore`,
`langchain_astradb`,
`AstraDBStore`
]
,

[
`langchain_community.cache`,
`AstraDBCache`,
`langchain_astradb`,
`AstraDBCache`
]
,

[
`langchain_community.cache`,
`AstraDBSemanticCache`,
`langchain_astradb`,
`AstraDBSemanticCache`
]
,

[
`langchain_community.chat_message_histories.astradb`,
`AstraDBChatMessageHistory`,
`langchain_astradb`,
`AstraDBChatMessageHistory`
]
,

[
`langchain_community.document_loaders.astradb`,
`AstraDBLoader`,
`langchain_astradb`,
`AstraDBLoader`
]

])
}

// Add this for invoking directly
langchain_migrate_astradb()
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

language python

// This migration is generated automatically - do not manually edit this file
pattern langchain_migrate_community_to_core() {
find_replace_imports(list=[
[`langchain_community.callbacks.tracers`, `ConsoleCallbackHandler`, `langchain_core.tracers`, `ConsoleCallbackHandler`],
[`langchain_community.callbacks.tracers`, `FunctionCallbackHandler`, `langchain_core.tracers.stdout`, `FunctionCallbackHandler`],
[`langchain_community.callbacks.tracers`, `LangChainTracer`, `langchain_core.tracers`, `LangChainTracer`],
[`langchain_community.callbacks.tracers`, `LangChainTracerV1`, `langchain_core.tracers.langchain_v1`, `LangChainTracerV1`],
[`langchain_community.docstore.document`, `Document`, `langchain_core.documents`, `Document`],
[`langchain_community.document_loaders`, `Blob`, `langchain_core.document_loaders`, `Blob`],
[`langchain_community.document_loaders`, `BlobLoader`, `langchain_core.document_loaders`, `BlobLoader`],
[`langchain_community.document_loaders.base`, `BaseBlobParser`, `langchain_core.document_loaders`, `BaseBlobParser`],
[`langchain_community.document_loaders.base`, `BaseLoader`, `langchain_core.document_loaders`, `BaseLoader`],
[`langchain_community.document_loaders.blob_loaders`, `Blob`, `langchain_core.document_loaders`, `Blob`],
[`langchain_community.document_loaders.blob_loaders`, `BlobLoader`, `langchain_core.document_loaders`, `BlobLoader`],
[`langchain_community.document_loaders.blob_loaders.schema`, `Blob`, `langchain_core.document_loaders`, `Blob`],
[`langchain_community.document_loaders.blob_loaders.schema`, `BlobLoader`, `langchain_core.document_loaders`, `BlobLoader`],
[`langchain_community.tools`, `BaseTool`, `langchain_core.tools`, `BaseTool`],
[`langchain_community.tools`, `StructuredTool`, `langchain_core.tools`, `StructuredTool`],
[`langchain_community.tools`, `Tool`, `langchain_core.tools`, `Tool`],
[`langchain_community.tools`, `format_tool_to_openai_function`, `langchain_core.utils.function_calling`, `format_tool_to_openai_function`],
[`langchain_community.tools`, `tool`, `langchain_core.tools`, `tool`],
[`langchain_community.tools.convert_to_openai`, `format_tool_to_openai_function`, `langchain_core.utils.function_calling`, `format_tool_to_openai_function`],
[`langchain_community.tools.convert_to_openai`, `format_tool_to_openai_tool`, `langchain_core.utils.function_calling`, `format_tool_to_openai_tool`],
[`langchain_community.tools.render`, `format_tool_to_openai_function`, `langchain_core.utils.function_calling`, `format_tool_to_openai_function`],
[`langchain_community.tools.render`, `format_tool_to_openai_tool`, `langchain_core.utils.function_calling`, `format_tool_to_openai_tool`],
[`langchain_community.utils.openai_functions`, `FunctionDescription`, `langchain_core.utils.function_calling`, `FunctionDescription`],
[`langchain_community.utils.openai_functions`, `ToolDescription`, `langchain_core.utils.function_calling`, `ToolDescription`],
[`langchain_community.utils.openai_functions`, `convert_pydantic_to_openai_function`, `langchain_core.utils.function_calling`, `convert_pydantic_to_openai_function`],
[`langchain_community.utils.openai_functions`, `convert_pydantic_to_openai_tool`, `langchain_core.utils.function_calling`, `convert_pydantic_to_openai_tool`],
[`langchain_community.vectorstores`, `VectorStore`, `langchain_core.vectorstores`, `VectorStore`]
])
}

// Add this for invoking directly
langchain_migrate_community_to_core()
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,17 @@
"langchain_community.document_loaders.blob_loaders.schema.BlobLoader",
"langchain_core.document_loaders.BlobLoader"
],
[
"langchain_community.tools.BaseTool",
"langchain_core.tools.BaseTool"
],
["langchain_community.tools.BaseTool", "langchain_core.tools.BaseTool"],
[
"langchain_community.tools.StructuredTool",
"langchain_core.tools.StructuredTool"
],
[
"langchain_community.tools.Tool",
"langchain_core.tools.Tool"
],
["langchain_community.tools.Tool", "langchain_core.tools.Tool"],
[
"langchain_community.tools.format_tool_to_openai_function",
"langchain_core.utils.function_calling.format_tool_to_openai_function"
],
[
"langchain_community.tools.tool",
"langchain_core.tools.tool"
],
["langchain_community.tools.tool", "langchain_core.tools.tool"],
[
"langchain_community.tools.convert_to_openai.format_tool_to_openai_function",
"langchain_core.utils.function_calling.format_tool_to_openai_function"
Expand Down Expand Up @@ -107,4 +98,4 @@
"langchain_community.vectorstores.VectorStore",
"langchain_core.vectorstores.VectorStore"
]
]
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language python

pattern langchain_all_migrations() {
any {
langchain_migrate_community_to_core(),
langchain_migrate_fireworks(),
langchain_migrate_ibm(),
langchain_migrate_langchain_to_core(),
langchain_migrate_langchain_to_langchain_community(),
langchain_migrate_langchain_to_textsplitters(),
langchain_migrate_openai(),
langchain_migrate_pinecone(),
langchain_migrate_anthropic()
}
}

langchain_all_migrations()
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

language python

// This migration is generated automatically - do not manually edit this file
pattern langchain_migrate_fireworks() {
find_replace_imports(list=[
[`langchain_community.chat_models.fireworks`, `ChatFireworks`, `langchain_fireworks`, `ChatFireworks`],
[`langchain_community.llms.fireworks`, `Fireworks`, `langchain_fireworks`, `Fireworks`],
[`langchain_community.chat_models`, `ChatFireworks`, `langchain_fireworks`, `ChatFireworks`],
[`langchain_community.llms`, `Fireworks`, `langchain_fireworks`, `Fireworks`]
])
}

// Add this for invoking directly
langchain_migrate_fireworks()
13 changes: 13 additions & 0 deletions libs/cli/langchain_cli/namespaces/migrate/.grit/patterns/ibm.grit
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

language python

// This migration is generated automatically - do not manually edit this file
pattern langchain_migrate_ibm() {
find_replace_imports(list=[
[`langchain_community.llms.watsonxllm`, `WatsonxLLM`, `langchain_ibm`, `WatsonxLLM`],
[`langchain_community.llms`, `WatsonxLLM`, `langchain_ibm`, `WatsonxLLM`]
])
}

// Add this for invoking directly
langchain_migrate_ibm()
Loading

0 comments on commit 8b5f4cc

Please sign in to comment.