94057c3d3e
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
139 lines
4.8 KiB
Python
139 lines
4.8 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
import asyncio
|
|
from typing import Any, Optional
|
|
|
|
import aiohttp
|
|
import msgspec
|
|
|
|
from sglang.srt.environ import envs
|
|
from sglang.srt.utils import print_warning_once
|
|
|
|
EXA_API_BASE_URL = "https://api.exa.ai"
|
|
EXA_INTEGRATION_HEADER = "x-exa-integration"
|
|
EXA_INTEGRATION_NAME = "sglang"
|
|
EXA_DEFAULT_NUM_RESULTS = 10
|
|
EXA_DEFAULT_SEARCH_TYPE = "auto"
|
|
EXA_DEFAULT_HIGHLIGHTS = True
|
|
|
|
_SEARCH_TYPES = {"instant", "fast", "auto", "deep-lite", "deep", "deep-reasoning"}
|
|
|
|
|
|
class ExaClientError(RuntimeError):
|
|
"""Raised when an Exa API request fails."""
|
|
|
|
|
|
class ExaSearchConfig(msgspec.Struct, frozen=True, kw_only=True):
|
|
num_results: int = EXA_DEFAULT_NUM_RESULTS
|
|
search_type: str = EXA_DEFAULT_SEARCH_TYPE
|
|
include_highlights: bool = EXA_DEFAULT_HIGHLIGHTS
|
|
|
|
@classmethod
|
|
def from_env(cls) -> "ExaSearchConfig":
|
|
# EnvField handles type parsing (int/bool) and falls back to its own
|
|
# default on a parse error; here we only enforce the semantic bounds
|
|
# that the generic descriptors cannot express.
|
|
num_results = envs.SGLANG_EXA_NUM_RESULTS.get()
|
|
if not 1 <= num_results <= 100:
|
|
print_warning_once(
|
|
f"Ignoring invalid SGLANG_EXA_NUM_RESULTS={num_results!r}; "
|
|
f"expected a value from 1 to 100."
|
|
)
|
|
num_results = EXA_DEFAULT_NUM_RESULTS
|
|
|
|
search_type = envs.SGLANG_EXA_SEARCH_TYPE.get()
|
|
if search_type not in _SEARCH_TYPES:
|
|
print_warning_once(
|
|
f"Ignoring invalid SGLANG_EXA_SEARCH_TYPE={search_type!r}; "
|
|
f"expected one of {', '.join(sorted(_SEARCH_TYPES))}."
|
|
)
|
|
search_type = EXA_DEFAULT_SEARCH_TYPE
|
|
|
|
return cls(
|
|
num_results=num_results,
|
|
search_type=search_type,
|
|
include_highlights=envs.SGLANG_EXA_INCLUDE_HIGHLIGHTS.get(),
|
|
)
|
|
|
|
|
|
class ExaClient:
|
|
def __init__(
|
|
self,
|
|
api_key: str,
|
|
*,
|
|
config: Optional[ExaSearchConfig] = None,
|
|
base_url: str = EXA_API_BASE_URL,
|
|
timeout: float = 30.0,
|
|
):
|
|
self.api_key = api_key
|
|
self.config = config or ExaSearchConfig()
|
|
self.base_url = base_url.rstrip("/")
|
|
self.timeout = timeout
|
|
self._session: Optional[aiohttp.ClientSession] = None
|
|
self._session_lock = asyncio.Lock()
|
|
|
|
def _headers(self) -> dict[str, str]:
|
|
return {
|
|
"x-api-key": self.api_key,
|
|
"Content-Type": "application/json",
|
|
EXA_INTEGRATION_HEADER: EXA_INTEGRATION_NAME,
|
|
}
|
|
|
|
def _search_payload(self, query: str) -> dict[str, Any]:
|
|
payload: dict[str, Any] = {
|
|
"query": query,
|
|
"numResults": self.config.num_results,
|
|
"type": self.config.search_type,
|
|
}
|
|
if self.config.include_highlights:
|
|
payload["contents"] = {"highlights": True}
|
|
return payload
|
|
|
|
def _contents_payload(self, urls: list[str]) -> dict[str, Any]:
|
|
payload: dict[str, Any] = {"urls": urls, "text": True}
|
|
if self.config.include_highlights:
|
|
payload["highlights"] = True
|
|
return payload
|
|
|
|
async def search(self, query: str) -> dict[str, Any]:
|
|
return await self._post("/search", self._search_payload(query))
|
|
|
|
async def contents(self, urls: list[str]) -> dict[str, Any]:
|
|
return await self._post("/contents", self._contents_payload(urls))
|
|
|
|
async def _get_session(self) -> aiohttp.ClientSession:
|
|
# Reuse a single session across requests so the connection pool and
|
|
# DNS cache survive; aiohttp.ClientSession is intended to be long-lived.
|
|
if self._session is None:
|
|
async with self._session_lock:
|
|
if self._session is None:
|
|
self._session = aiohttp.ClientSession(
|
|
timeout=aiohttp.ClientTimeout(total=self.timeout)
|
|
)
|
|
return self._session
|
|
|
|
async def close(self):
|
|
if self._session is not None:
|
|
await self._session.close()
|
|
self._session = None
|
|
|
|
async def _post(self, path: str, payload: dict[str, Any]) -> dict[str, Any]:
|
|
session = await self._get_session()
|
|
url = f"{self.base_url}{path}"
|
|
async with session.post(
|
|
url,
|
|
json=payload,
|
|
headers=self._headers(),
|
|
) as response:
|
|
response_text = await response.text()
|
|
if response.status >= 400:
|
|
raise ExaClientError(
|
|
f"Exa API request failed with status {response.status}: "
|
|
f"{response_text}"
|
|
)
|
|
try:
|
|
return await response.json()
|
|
except Exception as exc:
|
|
raise ExaClientError(
|
|
f"Failed to decode Exa API response: {response_text}"
|
|
) from exc
|