MCPcopy Create free account
hub / github.com/THESIS-AGENT/AIRouter / Harness_localAPI

Class Harness_localAPI

LoadBalancing.py:27–122  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

25
26
27class Harness_localAPI:
28 @staticmethod
29 def check_healthy():
30 """从本地健康检查服务获取API健康状态数据
31
32 Returns:
33 dict: 包含时间戳、检查间隔和健康数据的字典
34 """
35 logger = logging.getLogger(__name__)
36 # 防止日志向上传播,避免重复打印
37 logger.propagate = False
38 try:
39 # 调用本地健康检查服务API
40 logger.info("正在调用健康检查服务API获取健康状态数据")
41 response = requests.get(API_HEALTH_CHECK_URL, timeout=INNER_TIMEOUT)
42 if response.status_code == 200:
43 result = response.json()
44 # 将字符串键转换为元组键
45 if "data" in result:
46 result["data"] = {tuple(key.split("|")): [np.nan if v is None else v for v in value] for key, value in result["data"].items()}
47 logger.info("成功获取健康状态数据")
48 return result
49 else:
50 # 如果API调用失败,返回空数据结构和当前时间
51 logger.error(f"健康检查API返回错误状态码: {response.status_code}")
52 return {"timestamp": datetime.now().isoformat(), "check_timer_span": 15, "data": {}}
53 except Exception as e:
54 # 出现异常时记录错误并返回空数据结构
55 logger.error(f"获取健康状态数据时出错: {str(e)}")
56 return {"timestamp": datetime.now().isoformat(), "check_timer_span": 15, "data": {}}
57
58 @staticmethod
59 def get_api_key(source_name):
60 """从API密钥管理服务获取API密钥,失败时使用备用方案
61
62 Args:
63 source_name (str): 源名称
64
65 Returns:
66 str: API密钥
67
68 Raises:
69 Exception: 如果主备用方案都无法获取API密钥
70 """
71 logger = logging.getLogger(__name__)
72 # 防止日志向上传播,避免重复打印
73 logger.propagate = False
74
75 try:
76 # 优先尝试调用API密钥管理服务API
77 response = requests.post(
78 f"{API_KEY_MANAGER_URL}{API_KEY_MANAGER_GET_ENDPOINT}",
79 json={"source_name": source_name},
80 timeout=INNER_TIMEOUT
81 )
82
83 if response.status_code == 200:
84 result = response.json()

Callers 3

generate_docMethod · 0.90
generate_embeddingMethod · 0.90
generate_rerankerMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected