mirror of
https://github.com/langgenius/dify.git
synced 2026-10-04 13:47:55 +08:00
feat(dify-builder): agent ping endpoint for live LLM connectivity
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
4c0efd947d
commit
affe84777a
3 files changed
+83
No files matched your search
@@ -140,6 +140,16 @@ def _message(session_id: str, body, actor: Actor) -> tuple[dict, int]:
|
||||
)
|
||||
|
||||
|
||||
def _ping(body, actor: Actor) -> tuple[dict, int]:
|
||||
model_config = body.get("model_config") if isinstance(body, dict) else None
|
||||
try:
|
||||
from services.dify_builder.agent.ping import ping_model
|
||||
|
||||
return ping_model(actor.tenant_id, model_config), 200
|
||||
except Exception as exc: # a health check returns ok:false rather than 500
|
||||
return {"ok": False, "error": str(exc)}, 200
|
||||
|
||||
|
||||
@console_ns.route("/dify-builder/sessions")
|
||||
class DifyBuilderSessionsApi(Resource):
|
||||
@setup_required
|
||||
@@ -212,3 +222,15 @@ class DifyBuilderStreamApi(Resource):
|
||||
mimetype="text/event-stream",
|
||||
headers={"Cache-Control": "no-cache", "Connection": "keep-alive", "X-Accel-Buffering": "no"},
|
||||
)
|
||||
|
||||
|
||||
@console_ns.route("/dify-builder/agent/ping")
|
||||
class DifyBuilderAgentPingApi(Resource):
|
||||
@setup_required
|
||||
@login_required
|
||||
@account_initialization_required
|
||||
@with_current_user
|
||||
@with_current_tenant_id
|
||||
@dify_builder_required
|
||||
def post(self, current_tenant_id, current_user):
|
||||
return _ping(request.get_json(silent=True), _actor(current_user, current_tenant_id))
|
||||
@@ -0,0 +1,28 @@
|
||||
"""Live LLM connectivity check for the Dify Builder agent.
|
||||
|
||||
Resolves the chosen model (or the tenant default) and does one real, cheap
|
||||
completion — so operators can confirm the AI wiring works on a deployed stack
|
||||
before any cognition exists.
|
||||
"""
|
||||
|
||||
from typing import Any
|
||||
|
||||
from services.dify_builder.agent.llm import invoke_text
|
||||
from services.dify_builder.agent.model_resolver import (
|
||||
normalize_completion_params,
|
||||
resolve_model_instance,
|
||||
resolved_model_names,
|
||||
)
|
||||
|
||||
|
||||
def ping_model(tenant_id: str, model_config: dict[str, Any] | None) -> dict[str, Any]:
|
||||
instance = resolve_model_instance(tenant_id, model_config)
|
||||
params, stop = normalize_completion_params((model_config or {}).get("completion_params", {}))
|
||||
reply = invoke_text(
|
||||
instance,
|
||||
system="You are a health check.",
|
||||
user="Reply with the single word OK.",
|
||||
model_parameters=params,
|
||||
stop=stop,
|
||||
)
|
||||
return {"ok": True, "model": resolved_model_names(tenant_id, model_config), "reply": reply}
|
||||
@@ -0,0 +1,33 @@
|
||||
from services.dify_builder.agent import ping
|
||||
|
||||
MC = {"provider": "openai", "name": "gpt-4o", "mode": "chat", "completion_params": {"temperature": 0.0, "stop": ["x"]}}
|
||||
|
||||
|
||||
def test_ping_success(monkeypatch):
|
||||
captured = {}
|
||||
|
||||
def fake_resolve(tenant_id, model_config): return "INSTANCE"
|
||||
def fake_names(tenant_id, model_config): return {"provider": "openai", "name": "gpt-4o"}
|
||||
def fake_invoke_text(instance, *, system, user, model_parameters=None, stop=None):
|
||||
captured["instance"] = instance
|
||||
captured["params"] = model_parameters
|
||||
captured["stop"] = stop
|
||||
return "OK"
|
||||
|
||||
monkeypatch.setattr(ping, "resolve_model_instance", fake_resolve)
|
||||
monkeypatch.setattr(ping, "resolved_model_names", fake_names)
|
||||
monkeypatch.setattr(ping, "invoke_text", fake_invoke_text)
|
||||
|
||||
result = ping.ping_model("t1", MC)
|
||||
assert result == {"ok": True, "model": {"provider": "openai", "name": "gpt-4o"}, "reply": "OK"}
|
||||
assert captured["instance"] == "INSTANCE"
|
||||
assert captured["params"] == {"temperature": 0.0} # stop split out
|
||||
assert captured["stop"] == ["x"]
|
||||
|
||||
|
||||
def test_ping_default_model_no_config(monkeypatch):
|
||||
monkeypatch.setattr(ping, "resolve_model_instance", lambda t, m: "INSTANCE")
|
||||
monkeypatch.setattr(ping, "resolved_model_names", lambda t, m: {"provider": "anthropic", "name": "claude-sonnet-5"})
|
||||
monkeypatch.setattr(ping, "invoke_text", lambda inst, *, system, user, model_parameters=None, stop=None: "OK")
|
||||
result = ping.ping_model("t1", None)
|
||||
assert result["ok"] is True and result["model"]["provider"] == "anthropic"
|
||||
Reference in new issue
Block a user