Spaces:
Running on Zero
Running on Zero
File size: 1,808 Bytes
7f9dfed | 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 57 58 59 60 61 62 63 64 65 66 67 | from __future__ import annotations
from collections.abc import Callable
from typing import Any
import requests
from models.http_chat import post_chat_completion
from models.response_parsing import extract_chat_response
def host_port_args(host: str, port: int) -> list[str]:
return ["--host", host, "--port", str(port)]
def request_openai_chat_completion(
post_func: Callable[..., requests.Response],
base_url: str,
model: str,
system_prompt: str,
user_prompt: str,
temperature: float,
max_tokens: int,
timeout_seconds: float,
headers: dict[str, str] | None = None,
) -> dict[str, Any]:
return post_chat_completion(
post_func=post_func,
url=f"{base_url.rstrip('/')}/v1/chat/completions",
model=model,
system_prompt=system_prompt,
user_prompt=user_prompt,
temperature=temperature,
max_tokens=max_tokens,
timeout=timeout_seconds,
headers=headers,
)
def request_openai_chat_text(
post_func: Callable[..., requests.Response],
base_url: str,
model: str,
system_prompt: str,
user_prompt: str,
temperature: float,
max_tokens: int,
timeout_seconds: float,
failure_label: str,
headers: dict[str, str] | None = None,
) -> str:
try:
data = request_openai_chat_completion(
post_func=post_func,
base_url=base_url,
model=model,
system_prompt=system_prompt,
user_prompt=user_prompt,
temperature=temperature,
max_tokens=max_tokens,
timeout_seconds=timeout_seconds,
headers=headers,
)
except requests.RequestException as exc:
return f"[{failure_label} request failed]\n\n{exc}"
return extract_chat_response(data)
|