model_setup_flows_azure.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. """Azure Foundry wizard (OpenAI-style or Anthropic-style transport, API-key or Entra ID auth).
  2. Imports of hermes_cli.config / azure_detect stay lazy (tests patch them at call time).
  3. Prompt strings and config write order are behavior.
  4. """
  5. from __future__ import annotations
  6. from pm import install_hint
  7. from dataclasses import dataclass, field
  8. from hermes_cli.config import clear_model_endpoint_credentials
  9. from hermes_cli.model_setup_flows_common import _HTTP, _ask, _commit_model_config, _load_config_model_section, _say
  10. def _azure_mode_label(mode: str) -> str:
  11. return "OpenAI-style" if mode == "chat_completions" else "Anthropic-style"
  12. @dataclass
  13. class _AzureCurrent:
  14. """The active Azure Foundry settings shown as prompt defaults."""
  15. base_url: str = ""
  16. api_mode: str = ""
  17. auth_mode: str = "api_key"
  18. entra: dict = field(default_factory=dict)
  19. api_key: str = ""
  20. def _azure_current(config) -> _AzureCurrent:
  21. from hermes_cli.config import get_env_value
  22. cur = _AzureCurrent(api_key=get_env_value("AZURE_FOUNDRY_API_KEY") or "")
  23. model_cfg = config.get("model", {})
  24. if isinstance(model_cfg, dict) and model_cfg.get("provider") == "azure-foundry":
  25. cur.base_url = str(model_cfg.get("base_url", "") or "")
  26. cur.api_mode = str(model_cfg.get("api_mode", "") or "")
  27. cur.auth_mode = str(model_cfg.get("auth_mode") or "api_key").strip().lower() or "api_key"
  28. _cur_entra = model_cfg.get("entra") or {}
  29. cur.entra = _cur_entra if isinstance(_cur_entra, dict) else {}
  30. return cur
  31. def _azure_entra_preflight(current_entra: dict):
  32. """Entra ID credential preflight. Returns ``(token_provider, entra_overrides)``; ``None``
  33. when the user cancelled; ``False`` when the adapter is missing (fall back to API key)."""
  34. try:
  35. from agent.azure_identity_adapter import (
  36. EntraIdentityConfig, SCOPE_AI_AZURE_DEFAULT, build_token_provider, describe_active_credential,
  37. has_azure_identity_installed)
  38. except ImportError as exc:
  39. _say("", f"⚠ Could not import azure-identity adapter: {exc}", " Falling back to API key auth.")
  40. return False
  41. print()
  42. if not has_azure_identity_installed():
  43. _say("◐ The 'azure-identity' package is not installed yet.",
  44. " The preflight requests it through PM if lazy installs are enabled.",
  45. " To install explicitly, run from the Hermes environment:",
  46. f" {install_hint('azure-identity')}",
  47. " Then restart Hermes.")
  48. # Only the optional scope override is persisted; identity selection (tenant,
  49. # user-assigned MI, workload identity, SP) stays in AZURE_* SDK env vars.
  50. entra_overrides: dict = {}
  51. _persisted_scope_override = str(current_entra.get("scope") or "").strip()
  52. entra_scope = _persisted_scope_override or SCOPE_AI_AZURE_DEFAULT
  53. if _persisted_scope_override:
  54. entra_overrides["scope"] = _persisted_scope_override
  55. _say("", "◐ Probing Microsoft Entra ID credential chain (up to 10s)...")
  56. _config = EntraIdentityConfig(scope=entra_scope)
  57. info = describe_active_credential(config=_config, timeout_seconds=10.0)
  58. if info.get("ok"):
  59. env_sources = info.get("env_sources") or []
  60. tag = ", ".join(env_sources) if env_sources else "default chain"
  61. print(f"✓ Entra ID token acquired ({tag}, scope={entra_scope})")
  62. else:
  63. err = info.get("error") or "credential chain exhausted"
  64. hint = info.get("hint") or (
  65. "Run `az login`, attach a managed identity to this VM, or set AZURE_TENANT_ID/AZURE_CLIENT_ID/AZURE_CLIENT_SECRET."
  66. )
  67. _say(f"⚠ {err}", f" Hint: {hint}")
  68. ans = _ask("Save Entra config anyway and validate later? [Y/n]: ", raw=True)
  69. if ans is None:
  70. return None
  71. if ans.lower() not in ("", "y", "yes"):
  72. print("Cancelled.")
  73. return None
  74. # Best-effort token provider for the detection probe; on failure the probe falls back
  75. # to manual entry.
  76. try:
  77. token_provider = build_token_provider(config=_config)
  78. except Exception as exc:
  79. print(f"⚠ Could not build token provider for probing: {exc}")
  80. token_provider = None
  81. return token_provider, entra_overrides
  82. def _azure_pick_model(discovered_models: list, current_model: str):
  83. """Model/deployment step of the Azure flow; None when cancelled."""
  84. if not discovered_models:
  85. model_name = _ask(f"Model / deployment name [{current_model or 'e.g. gpt-5.4, claude-sonnet-4-6'}]: ")
  86. return None if model_name is None else (model_name or current_model)
  87. print("Available models on this endpoint:")
  88. for i, mid in enumerate(discovered_models[:30], start=1):
  89. print(f" {i:>2}. {mid}")
  90. if len(discovered_models) > 30:
  91. print(f" ... and {len(discovered_models) - 30} more (type name manually if not shown)")
  92. print()
  93. pick = _ask(f"Pick by number, or type a deployment name [{current_model or discovered_models[0]}]: ", raw=True)
  94. if pick is None:
  95. return None
  96. if not pick:
  97. return current_model or discovered_models[0]
  98. if pick.isdigit() and 1 <= int(pick) <= min(len(discovered_models), 30):
  99. return discovered_models[int(pick) - 1]
  100. return pick
  101. def _azure_detect_transport(effective_url: str, effective_key: str, token_provider, current_api_mode: str):
  102. """Probe the endpoint; returns ``(api_mode, discovered_models)`` (manual pick of the API
  103. format when detection is incomplete) or None when cancelled."""
  104. from hermes_cli import azure_detect
  105. _say("", "◐ Probing endpoint to auto-detect transport and models...")
  106. detection = azure_detect.detect(effective_url, api_key=effective_key, token_provider=token_provider)
  107. discovered_models: list[str] = list(detection.models)
  108. api_mode: str = detection.api_mode or ""
  109. if api_mode:
  110. print(f"✓ Detected API transport: {_azure_mode_label(api_mode)}")
  111. if detection.reason:
  112. print(f" ({detection.reason})")
  113. if discovered_models:
  114. print(f"✓ Found {len(discovered_models)} deployed model(s) on this endpoint")
  115. return api_mode, discovered_models
  116. _say(f"⚠ Auto-detection incomplete: {detection.reason}", "",
  117. "Select the API format your Azure Foundry endpoint uses:",
  118. " 1. OpenAI-style (POST /v1/chat/completions)",
  119. " For: GPT models, Llama, Mistral, and most open models",
  120. " 2. Anthropic-style (POST /v1/messages)",
  121. " For: Claude models deployed via Anthropic API format")
  122. default_choice = "2" if current_api_mode == "anthropic_messages" else "1"
  123. mode_choice = _ask(f"API format [1/2] ({default_choice}): ", raw=True)
  124. if mode_choice is None:
  125. return None
  126. return ("anthropic_messages" if (mode_choice or default_choice) == "2" else "chat_completions"), discovered_models
  127. def _model_flow_azure_foundry(config, current_model=""):
  128. """Azure Foundry: endpoint, auth mode, API mode, model. Two transports (OpenAI-style
  129. ``/v1/chat/completions``, Anthropic-style ``/v1/messages``) and two auth modes: API key
  130. (``AZURE_FOUNDRY_API_KEY``) or Microsoft Entra ID (keyless RBAC via ``azure-identity``; the
  131. ``Azure AI User`` role covers both transports). Detection: ``/anthropic`` URL suffix → Anthropic;
  132. ``GET <base>/models`` → OpenAI-style + picker; Anthropic Messages probe; manual entry."""
  133. from hermes_cli.config import get_env_value, save_env_value
  134. from hermes_cli import azure_detect
  135. cur = _azure_current(config)
  136. _say("", "Azure Foundry Configuration", "=" * 50, "",
  137. "Azure Foundry can host models with either OpenAI-style or",
  138. "Anthropic-style API endpoints. Hermes will probe your",
  139. "endpoint to auto-detect the transport and the deployed",
  140. "models when possible.", "")
  141. if cur.base_url:
  142. print(f" Current endpoint: {cur.base_url}")
  143. if cur.api_mode:
  144. print(f" Current API mode: {_azure_mode_label(cur.api_mode)}")
  145. if cur.auth_mode == "entra_id":
  146. print(" Current auth mode: Microsoft Entra ID (keyless)")
  147. elif cur.api_key:
  148. print(f" Current auth mode: API key ({cur.api_key[:8]}...)")
  149. print()
  150. # Step 1: endpoint URL
  151. _placeholder = cur.base_url or (
  152. "e.g. https://<resource>.openai.azure.com/openai/v1 or https://<resource>.services.ai.azure.com/anthropic")
  153. base_url = _ask(f"API endpoint URL [{_placeholder}]: ")
  154. if base_url is None:
  155. return
  156. effective_url = (base_url or cur.base_url).rstrip("/")
  157. if not effective_url:
  158. print("No endpoint URL provided. Cancelled.")
  159. return
  160. if not effective_url.startswith(_HTTP):
  161. print(f"Invalid URL: {effective_url} (must start with http:// or https://)")
  162. return
  163. # Step 2: authentication mode
  164. _say("", "Authentication:", " 1. API key (AZURE_FOUNDRY_API_KEY in .env)",
  165. " 2. Microsoft Entra ID (managed identity / workload identity / az login)",
  166. " Recommended by Microsoft. Works for both OpenAI-style and Anthropic-style endpoints.",
  167. " Requires the 'Azure AI User' role on the Foundry resource.")
  168. _auth_default = "2" if cur.auth_mode == "entra_id" else "1"
  169. auth_choice = _ask(f"Authentication mode [1/2] ({_auth_default}): ", raw=True)
  170. if auth_choice is None:
  171. return
  172. use_entra = (auth_choice or _auth_default) == "2"
  173. # Step 3: credentials (key OR Entra preflight)
  174. effective_key: str = ""
  175. entra_overrides: dict = {}
  176. token_provider = None # callable when entra
  177. if use_entra:
  178. preflight = _azure_entra_preflight(cur.entra)
  179. if preflight is None:
  180. return
  181. if preflight is False:
  182. use_entra = False
  183. else:
  184. token_provider, entra_overrides = preflight
  185. if not use_entra:
  186. print()
  187. api_key = _ask(f"API key [{cur.api_key[:8] + '...' if cur.api_key else 'required'}]: ", secret=True)
  188. if api_key is None:
  189. return
  190. effective_key = api_key or cur.api_key
  191. if not effective_key:
  192. print("No API key provided. Cancelled.")
  193. return
  194. # Step 4: auto-detect transport + models
  195. detected = _azure_detect_transport(effective_url, effective_key, token_provider, cur.api_mode)
  196. if detected is None:
  197. return
  198. api_mode, discovered_models = detected
  199. # Step 5: model name
  200. print()
  201. effective_model = _azure_pick_model(discovered_models, current_model)
  202. if effective_model is None:
  203. return
  204. if not effective_model:
  205. print("No model name provided. Cancelled.")
  206. return
  207. # Step 6: context-length lookup
  208. ctx_len = azure_detect.lookup_context_length(effective_model, effective_url, api_key=effective_key, token_provider=token_provider)
  209. # Step 7: persist
  210. if not use_entra:
  211. save_env_value("AZURE_FOUNDRY_API_KEY", effective_key)
  212. cfg, model = _load_config_model_section()
  213. model["provider"] = "azure-foundry"
  214. model["base_url"] = effective_url
  215. model["api_mode"] = api_mode
  216. model["default"] = effective_model
  217. model["auth_mode"] = "entra_id" if use_entra else "api_key"
  218. clear_model_endpoint_credentials(model, clear_api_mode=False)
  219. # Persist only a non-default Entra scope so config.yaml stays tidy.
  220. clean_entra = {k: v for k in ("scope",) if (v := entra_overrides.get(k))}
  221. if use_entra and clean_entra:
  222. model["entra"] = clean_entra
  223. else:
  224. model.pop("entra", None)
  225. if ctx_len:
  226. model["context_length"] = ctx_len
  227. _commit_model_config(cfg)
  228. config["model"] = dict(model)
  229. # Clear conflicting env vars so auxiliary clients don't pick up a stale OpenAI base URL / key.
  230. for var in ("OPENAI_BASE_URL", "OPENAI_API_KEY"):
  231. if get_env_value(var):
  232. save_env_value(var, "")
  233. _say("", "✓ Azure Foundry configured:", f" Endpoint: {effective_url}",
  234. f" API mode: {_azure_mode_label(api_mode)}",
  235. f" Auth: {'Microsoft Entra ID (keyless)' if use_entra else 'API key'}",
  236. f" Model: {effective_model}",
  237. f" Context length: {ctx_len:,} tokens" if ctx_len else " Context length: not auto-detected (will fall back at runtime)",
  238. "")