"""Throttled OpenAI chat completions for auxiliary LLM call sites.""" from __future__ import annotations from typing import Any from app.async_executor import run_sync_in_executor from app.config import settings from app.llm.llm_throttle import throttled_llm_call async def chat_completions_create_raw( *, messages: list[Any], model: str | None = None, max_tokens: int = 800, temperature: float = 0.0, response_format: dict[str, str] | None = None, phase: str = "chat", section_id: str | None = None, api_key: str | None = None, tenant_id: str | None = None, ) -> str: """Chat completion with arbitrary message content (text or multimodal).""" model_name = model or settings.chat_model key = (api_key or settings.openai_api_key or "").strip() if not key: return "" kwargs: dict[str, Any] = { "model": model_name, "messages": messages, "max_tokens": max_tokens, "temperature": temperature, } if response_format is not None: kwargs["response_format"] = response_format if settings.enable_async_pipeline: from openai import AsyncOpenAI from app.llm.llm_throttle import make_cache_hit_slot from app.llm.prompt_cache import ( log_openai_cache_usage, normalize_messages_for_caching, openai_extra_kwargs, prompt_caching_active, ) client = AsyncOpenAI(api_key=key) if prompt_caching_active() and isinstance(messages, list): kwargs["messages"] = normalize_messages_for_caching(messages) kwargs.update( openai_extra_kwargs( phase=phase, model=model_name, tenant_id=tenant_id ) ) cache_slot = make_cache_hit_slot() async def _call() -> str: resp = await client.chat.completions.create(**kwargs) if prompt_caching_active(): cache_slot[0] = log_openai_cache_usage( resp, phase=phase, section_id=section_id ) return (resp.choices[0].message.content or "").strip() return await throttled_llm_call( phase=phase, section_id=section_id, cache_hit_out=cache_slot, call=_call, ) def _sync() -> str: from openai import OpenAI from app.llm.llm_throttle import throttled_sync_llm_call client = OpenAI(api_key=key) def _call() -> str: resp = client.chat.completions.create(**kwargs) return (resp.choices[0].message.content or "").strip() return throttled_sync_llm_call(phase=phase, section_id=section_id, call=_call) return await run_sync_in_executor(_sync) async def chat_completions_create( *, messages: list[dict[str, str]], model: str | None = None, max_tokens: int = 800, temperature: float = 0.0, response_format: dict[str, str] | None = None, phase: str = "chat", section_id: str | None = None, api_key: str | None = None, tenant_id: str | None = None, ) -> str: """Return assistant message content from a text-only chat completion.""" return await chat_completions_create_raw( messages=messages, model=model, max_tokens=max_tokens, temperature=temperature, response_format=response_format, api_key=api_key, phase=phase, section_id=section_id, tenant_id=tenant_id, )