Spaces:
Paused
Paused
File size: 1,524 Bytes
0b84707 91d2a4b 0b84707 91d2a4b d2ef8b3 91d2a4b 0b84707 d2ef8b3 91d2a4b d2ef8b3 0b84707 91d2a4b 0b84707 d2ef8b3 0b84707 91d2a4b 0b84707 91d2a4b 0b84707 91d2a4b 0b84707 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import time
from openai import OpenAI
from src.models import TargetResponse
from src.config import OPENCODE_ZEN_API_KEY, LLM_BASE_URL, LLM_MODEL, MAX_TOKENS_PER_CALL
from src.retry import with_retry
_client: OpenAI | None = None
def _get_client() -> OpenAI:
global _client
if _client is None:
_client = OpenAI(api_key=OPENCODE_ZEN_API_KEY, base_url=LLM_BASE_URL)
return _client
@with_retry(max_retries=5, base_delay=3.0)
def _do_call(prompt: str):
client = _get_client()
return client.chat.completions.create(
model=LLM_MODEL,
messages=[
{"role": "system", "content": "You are a helpful assistant. Answer concisely and accurately."},
{"role": "user", "content": prompt},
],
max_tokens=MAX_TOKENS_PER_CALL,
temperature=0.1,
)
def call_target(prompt: str) -> TargetResponse:
start = time.perf_counter()
try:
response = _do_call(prompt)
except Exception as e:
elapsed = (time.perf_counter() - start) * 1000
return TargetResponse(
output="",
error=str(e),
success=False,
latency_ms=round(elapsed, 1),
)
elapsed = (time.perf_counter() - start) * 1000
output = response.choices[0].message.content or ""
tokens_used = response.usage.total_tokens if response.usage else 0
return TargetResponse(
output=output,
success=True,
latency_ms=round(elapsed, 1),
tokens_used=tokens_used,
)
|