052591281d
- Complete agent system with planning and execution - Bridge API with FastAPI server - WhatsApp-style mobile dashboard - Enhanced web search with query improvement - Security guards and policy checks - Comprehensive documentation
46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
"""OpenAI adapter — real if OPENAI_API_KEY is set, explicit stub otherwise."""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
|
|
from adapters.base import AdapterResult
|
|
|
|
_API_KEY: str = os.environ.get("OPENAI_API_KEY", "")
|
|
_MODEL: str = os.environ.get("OPENAI_MODEL", "gpt-4o-mini")
|
|
_REAL: bool = bool(_API_KEY)
|
|
|
|
|
|
def execute(
|
|
prompt: str,
|
|
model: str = _MODEL,
|
|
temperature: float = 0.0,
|
|
max_tokens: int = 300,
|
|
timeout: float = 30.0,
|
|
) -> AdapterResult:
|
|
print("=== THIS ADAPTER IS USED: openai_adapter ===")
|
|
if not _REAL:
|
|
return AdapterResult(
|
|
response=f"[openai-stub] OPENAI_API_KEY not set. Would send {len(prompt)} chars to {model}.",
|
|
error=None,
|
|
exit_code=0,
|
|
mode="stub",
|
|
)
|
|
try:
|
|
from openai import OpenAI # type: ignore
|
|
client = OpenAI(api_key=_API_KEY, timeout=timeout)
|
|
completion = client.chat.completions.create(
|
|
model=model,
|
|
messages=[{"role": "user", "content": prompt}],
|
|
temperature=temperature,
|
|
max_tokens=max_tokens,
|
|
)
|
|
text = completion.choices[0].message.content or ""
|
|
return AdapterResult(response=text, error=None, exit_code=0, mode="real")
|
|
except Exception as exc: # noqa: BLE001
|
|
return AdapterResult(response=None, error=f"{type(exc).__name__}: {exc}",
|
|
exit_code=1, mode="real")
|
|
|
|
|
|
def mode() -> str:
|
|
return "real" if _REAL else "stub"
|