Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support log_model with code model in langchain #11817

Merged
merged 26 commits into from
Apr 29, 2024

Conversation

annzhang-db
Copy link
Collaborator

@annzhang-db annzhang-db commented Apr 24, 2024

🛠 DevTools 🛠

Open in GitHub Codespaces

Install mlflow from this PR

pip install git+https://github.com/mlflow/mlflow.git@refs/pull/11817/merge

Checkout with GitHub CLI

gh pr checkout 11817

Related Issues/PRs

Work to follow:

What changes are proposed in this pull request?

  • Remove restriction for file to be called "chain.py"
  • Add logic to read from file or databricks notebook
  • Introduce model_code as separate from code

How is this PR tested?

  • Existing unit/integration tests
  • New unit/integration tests
  • Manual tests

Does this PR require documentation update?

  • No. You can skip the rest of this section.
  • Yes. I've updated:
    • Examples
    • API references
    • Instructions

Release Notes

Is this a user-facing change?

  • No. You can skip the rest of this section.
  • Yes. Give a description of this change to be included in the release notes for MLflow users.

What component(s), interfaces, languages, and integrations does this PR affect?

Components

  • area/artifacts: Artifact stores and artifact logging
  • area/build: Build and test infrastructure for MLflow
  • area/deployments: MLflow Deployments client APIs, server, and third-party Deployments integrations
  • area/docs: MLflow documentation pages
  • area/examples: Example code
  • area/model-registry: Model Registry service, APIs, and the fluent client calls for Model Registry
  • area/models: MLmodel format, model serialization/deserialization, flavors
  • area/recipes: Recipes, Recipe APIs, Recipe configs, Recipe Templates
  • area/projects: MLproject format, project running backends
  • area/scoring: MLflow Model server, model deployment tools, Spark UDFs
  • area/server-infra: MLflow Tracking server backend
  • area/tracking: Tracking Service, tracking client APIs, autologging

Interface

  • area/uiux: Front-end, user experience, plotting, JavaScript, JavaScript dev server
  • area/docker: Docker use across MLflow's components, such as MLflow Projects and MLflow Models
  • area/sqlalchemy: Use of SQLAlchemy in the Tracking Service or Model Registry
  • area/windows: Windows support

Language

  • language/r: R APIs and clients
  • language/java: Java APIs and clients
  • language/new: Proposals for new client languages

Integrations

  • integrations/azure: Azure and Azure ML integrations
  • integrations/sagemaker: SageMaker integrations
  • integrations/databricks: Databricks integrations

How should the PR be classified in the release notes? Choose one:

  • rn/none - No description will be included. The PR will be mentioned only by the PR number in the "Small Bugfixes and Documentation Updates" section
  • rn/breaking-change - The PR will be mentioned in the "Breaking Changes" section
  • rn/feature - A new user-facing feature worth mentioning in the release notes
  • rn/bug-fix - A user-facing bug fix worth mentioning in the release notes
  • rn/documentation - A user-facing documentation change worth mentioning in the release notes

Should this PR be included in the next patch release?

Yes should be selected for bug fixes, documentation updates, and other small changes. No should be selected for new features and larger changes. If you're unsure about the release classification of this PR, leave this unchecked to let the maintainers decide.

What is a minor/patch release?
  • Minor release: a release that increments the second part of the version number (e.g., 1.2.0 -> 1.3.0).
    Bug fixes, doc updates and new features usually go into minor releases.
  • Patch release: a release that increments the third part of the version number (e.g., 1.2.0 -> 1.2.1).
    Bug fixes and doc updates usually go into patch releases.
  • Yes (this PR will be cherry-picked and included in the next patch release)
  • No (this PR will be included in the next minor release)

Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Copy link

github-actions bot commented Apr 24, 2024

Documentation preview for 22af799 will be available when this CircleCI job
completes successfully.

More info

@github-actions github-actions bot added rn/none List under Small Changes in Changelogs. and removed rn/none List under Small Changes in Changelogs. labels Apr 24, 2024
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
@annzhang-db annzhang-db changed the title log_model Support log_model with file path in langchain Apr 25, 2024
@github-actions github-actions bot added rn/none List under Small Changes in Changelogs. and removed rn/none List under Small Changes in Changelogs. labels Apr 25, 2024
@@ -2615,7 +2601,6 @@ def test_save_load_chain_as_code_optional_code_path():
artifact_path="model_path",
signature=signature,
input_example=input_example,
code_paths=[],
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test code_paths=None

@github-actions github-actions bot added the rn/none List under Small Changes in Changelogs. label Apr 25, 2024
Comment on lines 254 to 255
f"If the provided model '{lc_model}' is a string, it must be a valid python "
"file path containing the code for defining the chain instance."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the case, can we log it as a separate model metadata like model_code_path instead of reusing "code_paths" ?
Because it has differences with existing code_paths files (its code is not a common module, but it should contain set_chain method, and it might come from databricks notebook),
and recently we are expanding MLflow code_paths functionality (e.g. auto inferring code_paths), and the newly added model_code_path can't support the expanded code_paths functionality and make code messy.

I have some related discussion in this doc:
https://docs.google.com/document/d/144wAwgXsQ40C3dDsoObX0LfXp33aRbvEVCCq-VYgJqw/edit#bookmark=id.d1xwn4gq68ce

https://docs.google.com/document/d/144wAwgXsQ40C3dDsoObX0LfXp33aRbvEVCCq-VYgJqw/edit#bookmark=id.w62724445pwk

CC @BenWilson2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we should keep this logic separate. This main entry point to a chain definition should be handled distinctly to avoid having to add error handling logic that would need to be applied to branching decision logic if this were overloaded into code_paths for directory traversal for dependent relative and absolute import statements with dependency inference.

Regardless of this fact, what is the mechanism for handling dependent imports within this implementation? If a user has external imports to custom code that rely on absolute imports, will this notebook path preserve its directory structure from the workspace root?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the PR to separate this logic. There isn't any mechanism for handling dependent imports right now, and the notebook path does not preserve its directory structure from the workspace root. Should this be a requirement here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't any mechanism for handling dependent imports right now, and the notebook path does not preserve its directory structure from the workspace root. Should this be a requirement here?

No need to handle this.

Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
@annzhang-db annzhang-db changed the title Support log_model with file path in langchain Support log_model with code model in langchain Apr 27, 2024
@annzhang-db annzhang-db mentioned this pull request Apr 27, 2024
39 tasks
)

if len(code_paths) > 1:
if code_paths and len(code_paths) > 1:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#11844 (comment)

Can we delete this check since now we will use model_config instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will get removed in the next PR: #11843, when we start to actually use model_config

@@ -256,7 +258,8 @@ def load_retriever(persist_directory):
f"Current code paths: {code_paths}"
)

code_dir_subpath = _validate_and_copy_code_paths(formatted_code_path, path)
code_dir_subpath = _validate_and_copy_code_paths(code_paths, path)
model_code_dir_subpath = _validate_and_copy_model_code_path(model_code_path, path)

if signature is None:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#11844 (comment)

When the signature is None, I am not sure if this code would work: _LangChainModelWrapper(lc_model)
So we need to figure out a way to load the model here and use that as a wrapped model so we can infer_signature

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolve signature issues with https://github.com/mlflow/mlflow/pull/11817/files#r1583519300 this potentially

Maybe we can move this code on top when the lc_model is a str, we load the model and make that the lc_model. Lot of things would be solved because of that. What do you think?

**model_data_kwargs,
)

if Version(langchain.__version__) >= Version("0.0.311"):
checker_model = lc_model
if isinstance(lc_model, str):
# TODO: use model_config instead of code_paths[0]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#11844 (comment)

Maybe we can move this code on top when the lc_model is a str, we load the model and make that the lc_model. Lot of things would be solved because of that. What do you think?

from databricks.sdk import WorkspaceClient
from databricks.sdk.service.workspace import ExportFormat

w = WorkspaceClient()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#11844 (comment)

Nit: This function is already huge :D
Can we extract this out in a function?

@@ -162,6 +163,20 @@ def _validate_and_copy_code_paths(code_paths, path, default_subpath="code"):
return code_dir_subpath


def _validate_and_copy_model_code_path(code_path, path, default_subpath="model_code"):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#11844 (comment)

default_subpath= FLAVOR_CONFIG_MODEL_CODE
Can we update the above so we don't define this var in 2 places?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is reworked in the next PR #11843, let's address it there

Copy link
Collaborator

@sunishsheth2009 sunishsheth2009 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving this so we can merge this and we can address the TODOs so we can do it in small batch

@annzhang-db annzhang-db merged commit a990a30 into mlflow:master Apr 29, 2024
56 checks passed
BenWilson2 pushed a commit that referenced this pull request May 6, 2024
Signed-off-by: Ann Zhang <ann.zhang@databricks.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patch-2.12.2 rn/none List under Small Changes in Changelogs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants