MCPcopy Create free account
hub / github.com/FSoft-AI4Code/CodeWiki / _call_llm_via_azure

Function _call_llm_via_azure

codewiki/src/be/llm_services.py:261–290  ·  view source on GitHub ↗

Call LLM via Azure OpenAI. Uses the AzureOpenAI client from the openai package with azure_endpoint, api_version, and deployment name.

(
    prompt: str,
    config: Config,
    model: str,
    temperature: float = 0.0
)

Source from the content-addressed store, hash-verified

259 """Create fallback models chain from configuration."""
260 main = create_main_model(config)
261 fallback = create_fallback_model(config)
262 # The default fallback_on (ModelAPIError, or ModelHTTPError on older pydantic-ai)
263 # misses UnexpectedModelBehavior, which pydantic-ai raises for a 200 response whose
264 # body does not parse (e.g. choices=None from an OpenAI-compatible gateway); without
265 # it such a response skips the fallback model.
266 return FallbackModel(main, fallback, fallback_on=(ModelAPIError, UnexpectedModelBehavior))
267
268
269def create_openai_client(config: Config) -> OpenAI:
270 """Create OpenAI client from configuration."""
271 return OpenAI(base_url=config.llm_base_url, api_key=config.llm_api_key)
272
273
274# Usage of the most recent completion, read by LLMBackend.complete implementations.
275_LAST_USAGE: dict = {"usage": None}
276
277
278def pop_last_usage() -> Optional[dict]:
279 usage = _LAST_USAGE["usage"]
280 _LAST_USAGE["usage"] = None
281 return usage
282
283
284def _remember_usage(response) -> None:
285 from codewiki.src.be.backend import usage_to_dict
286
287 try:
288 _LAST_USAGE["usage"] = usage_to_dict(getattr(response, "usage", None))
289 except Exception: # noqa: BLE001 — usage is optional telemetry
290 _LAST_USAGE["usage"] = None
291
292
293def _extract_content(response, model: str) -> Optional[str]:

Callers 1

call_llmFunction · 0.85

Calls 2

debugMethod · 0.80
createMethod · 0.80

Tested by

no test coverage detected