-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_extra_mlflow_langchain.py
More file actions
188 lines (141 loc) · 6.11 KB
/
Copy pathtest_extra_mlflow_langchain.py
File metadata and controls
188 lines (141 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""End-to-end tests for the MLflow → FNNX converter on LangChain/LangGraph
models-from-code (offline).
Each test logs a deterministic models-from-code script via
``mlflow.langchain.save_model``, packages with ``package_mlflow_model``,
loads via ``fnnx.Runtime``, and asserts:
* the embedded ``mlflow_model/`` contains the model-code ``.py`` (the file
models-from-code points ``model_code_path`` at),
* **no** code-availability self-containment warning fires (models-from-code
triggers the self-contained-ish path even for the ``mlflow.langchain``
loader),
* a round-trip ``compute`` on the converted package matches what the
embedded runnable/graph returns when invoked directly via
``mlflow.pyfunc.load_model``.
Both fixtures use an ``Object`` ColSpec so the converter selects
``input_mode="json"`` — the natural shape for GenAI-style I/O.
"""
from __future__ import annotations
import os
import tarfile
import tempfile
import unittest
from unittest import mock
import pytest
pytest.importorskip("mlflow")
pytest.importorskip("langchain_core")
pytest.importorskip("langgraph")
_FIXTURES_DIR = os.path.join(os.path.dirname(__file__), "_mlflow_fixtures")
_LC_FIXTURE = os.path.join(_FIXTURES_DIR, "langchain_model.py")
_LG_FIXTURE = os.path.join(_FIXTURES_DIR, "langgraph_model.py")
def _payload_signature():
"""Object-typed signature shared by both fixtures → json input_mode."""
from mlflow.models import ModelSignature # type: ignore[import-not-found]
from mlflow.types import DataType # type: ignore[import-not-found]
from mlflow.types.schema import ( # type: ignore[import-not-found]
ColSpec,
Object,
Property,
Schema,
)
return ModelSignature(
inputs=Schema(
[
ColSpec(
name="payload",
type=Object(properties=[Property("text", DataType.string)]),
)
]
),
outputs=Schema([ColSpec(type=DataType.string)]),
)
def _save_langchain_model(tmp: str) -> str:
import mlflow # type: ignore[import-not-found]
model_dir = os.path.join(tmp, "lc_model")
mlflow.langchain.save_model( # type: ignore[attr-defined]
lc_model=_LC_FIXTURE,
path=model_dir,
signature=_payload_signature(),
)
return model_dir
def _save_langgraph_model(tmp: str) -> str:
import mlflow # type: ignore[import-not-found]
model_dir = os.path.join(tmp, "lg_model")
mlflow.langchain.save_model( # type: ignore[attr-defined]
lc_model=_LG_FIXTURE,
path=model_dir,
signature=_payload_signature(),
)
return model_dir
def _assert_no_code_availability_warning(warn_mock) -> None:
warned = [c.args[0] for c in warn_mock.call_args_list]
for msg in warned:
assert "serializes Python objects by reference" not in msg, (
f"unexpected by-reference warning: {msg!r}"
)
assert "referenced from the Hugging Face Hub" not in msg, (
f"unexpected Hub-reference warning: {msg!r}"
)
def _assert_model_code_embedded(out_path: str, fixture_basename: str) -> None:
with tarfile.open(out_path, "r") as tar:
names = set(tar.getnames())
expected = (
f"variant_artifacts/extra_files/mlflow_model/{fixture_basename}"
)
assert expected in names, (
f"expected {expected!r} in package, got {sorted(names)!r}"
)
class TestLangChainModelsFromCode(unittest.TestCase):
"""Pure ``RunnableLambda`` round-trips through FNNX."""
def test_round_trip(self):
import mlflow # type: ignore[import-not-found]
from fnnx.extras.mlflow import package_mlflow_model
from fnnx.runtime import Runtime
with tempfile.TemporaryDirectory() as tmp:
model_dir = _save_langchain_model(tmp)
out = os.path.join(tmp, "langchain.fnnx")
with mock.patch("fnnx.extras.mlflow.console.warn") as warn_mock:
package_mlflow_model(model_dir, out)
_assert_model_code_embedded(out, "langchain_model.py")
_assert_no_code_availability_warning(warn_mock)
rt = Runtime(out)
payload = {"text": "hello"}
result = rt.compute({"data": {"payload": payload}}, {})
# Compare to direct invocation through the embedded model.
direct = mlflow.pyfunc.load_model(model_dir).predict( # type: ignore[attr-defined]
{"payload": payload}
)
self.assertIn("predictions", result)
self.assertEqual(result["predictions"], direct)
class TestLangGraphModelsFromCode(unittest.TestCase):
"""Trivial ``StateGraph`` round-trips through FNNX."""
def test_round_trip(self):
import mlflow # type: ignore[import-not-found]
from fnnx.extras.mlflow import package_mlflow_model
from fnnx.runtime import Runtime
with tempfile.TemporaryDirectory() as tmp:
model_dir = _save_langgraph_model(tmp)
out = os.path.join(tmp, "langgraph.fnnx")
with mock.patch("fnnx.extras.mlflow.console.warn") as warn_mock:
package_mlflow_model(model_dir, out)
_assert_model_code_embedded(out, "langgraph_model.py")
_assert_no_code_availability_warning(warn_mock)
# Signature is an Object ColSpec → input_mode="json".
import json
with tarfile.open(out, "r") as tar:
variant_config = json.loads(
tar.extractfile("variant_config.json").read().decode() # type: ignore[union-attr]
)
self.assertEqual(
variant_config["extra_values"]["fnnx_mlflow"]["input_mode"],
"json",
)
rt = Runtime(out)
payload = {"text": "hello"}
result = rt.compute({"data": {"payload": payload}}, {})
direct = mlflow.pyfunc.load_model(model_dir).predict( # type: ignore[attr-defined]
{"payload": payload}
)
self.assertIn("predictions", result)
self.assertEqual(result["predictions"], direct)
if __name__ == "__main__":
unittest.main()