Skip to content

Commit fedb6bb

Browse files
committed
add MLflow operator CLI
1 parent 01060d0 commit fedb6bb

File tree

4 files changed

+87
-3
lines changed

4 files changed

+87
-3
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ create-elasticsearch-index: ## create Elasticsearch index
173173
# ---
174174
# Project / Run agents
175175
# ---
176+
176177
QUESTION ?= "KABUTOの起動時に、画面全体が紫色に点滅し、システムがフリーズします。KABUTO のマニュアルから、関連する情報を取得したり過去のシステムのトラブルシュート事例が蓄積されたデータベースから、関連する情報を取得して質問に答えてください"
178+
PORT ?= 5001
179+
177180
.PHONY: run-chat-with-tools-agent
178181
run-chat-with-tools-agent: ## run chat with tools agent
179182
uv run python scripts/agent_operator.py run \
@@ -227,4 +230,4 @@ mlflow: ## run MLflow
227230
uv run mlflow server \
228231
--backend-store-uri sqlite:///mlflow.db \
229232
--host 0.0.0.0 \
230-
--port 5000
233+
--port $(PORT)

docs/references.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,9 @@
9090
- [DSPy (Declarative Self-improving Python)](https://dspy.ai/)
9191
- [Language Models](https://dspy.ai/learn/programming/language_models/)
9292
- [Language Models / v3.0.3](https://github.com/stanfordnlp/dspy/blob/3.0.3/docs/docs/learn/programming/language_models.md)
93-
- [Software Design誌「実践LLMアプリケーション開発」第25回サンプルコード](https://github.com/mahm/softwaredesign-llm-application/tree/main/25)
93+
- [Software Design 誌「実践 LLM アプリケーション開発」第 25 回サンプルコード](https://github.com/mahm/softwaredesign-llm-application/tree/main/25)
94+
95+
### [MLflow](https://mlflow.org/docs/latest/genai/)
96+
97+
- [LangChain / MLflow](https://docs.langchain.com/oss/python/integrations/providers/mlflow_tracking)
98+
- [MLflow / Tracing LangGraph🦜🕸️](https://mlflow.org/docs/latest/genai/tracing/integrations/listing/langgraph/)

scripts/dspy_operator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def optimize_with_miprov2(trainset, eval_lm, chat_lm):
7676
"""MIPROv2を使用してチャットボットを最適化"""
7777

7878
# MLflowの設定
79-
MLFLOW_PORT = os.getenv("MLFLOW_PORT", "5000")
79+
MLFLOW_PORT = os.getenv("MLFLOW_PORT", "5001")
8080
MLFLOW_TRACKING_URI = f"http://localhost:{MLFLOW_PORT}"
8181
MLFLOW_EXPERIMENT_NAME = "DSPy-EdamameFairy-Optimization"
8282
MLFLOW_RUN_NAME = "miprov2_optimization"

scripts/mlflow_operator.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import logging
2+
from logging import basicConfig
3+
4+
import mlflow
5+
import typer
6+
from dotenv import load_dotenv
7+
from langchain_core.messages import HumanMessage
8+
9+
from template_langgraph.agents.demo_agents.weather_agent import graph
10+
from template_langgraph.loggers import get_logger
11+
12+
app = typer.Typer(
13+
add_completion=False,
14+
help="MLflow operator CLI",
15+
)
16+
logger = get_logger(__name__)
17+
18+
19+
def set_verbose_logging(verbose: bool):
20+
if verbose:
21+
logger.setLevel(logging.DEBUG)
22+
basicConfig(level=logging.DEBUG)
23+
24+
25+
@app.command(
26+
help="Run the LangGraph agent with MLflow tracing ref. https://mlflow.org/docs/2.21.3/tracing/integrations/langgraph"
27+
)
28+
def tracing(
29+
query: str = typer.Option(
30+
"What is the weather like in Japan?",
31+
"--query",
32+
"-q",
33+
help="Query to run with the LangGraph agent",
34+
),
35+
tracking_uri: str = typer.Option(
36+
"http://localhost:5001",
37+
"--tracking-uri",
38+
"-t",
39+
help="MLflow tracking URI",
40+
),
41+
verbose: bool = typer.Option(
42+
True,
43+
"--verbose",
44+
"-v",
45+
help="Enable verbose output",
46+
),
47+
):
48+
set_verbose_logging(verbose)
49+
logger.info("Running...")
50+
51+
mlflow.langchain.autolog()
52+
mlflow.set_tracking_uri(tracking_uri)
53+
mlflow.set_experiment("LangGraph Experiment")
54+
55+
result = graph.invoke(
56+
{
57+
"messages": [
58+
HumanMessage(content=query),
59+
]
60+
},
61+
)
62+
logger.info(f"Result: {result}")
63+
64+
# Get the trace object just created
65+
trace = mlflow.get_trace(
66+
trace_id=mlflow.get_last_active_trace_id(),
67+
)
68+
logger.info(f"Trace info: {trace.info.token_usage}")
69+
70+
71+
if __name__ == "__main__":
72+
load_dotenv(
73+
override=True,
74+
verbose=True,
75+
)
76+
app()

0 commit comments

Comments
 (0)