| 6 | |
| 7 | |
| 8 | class TrinityClient: |
| 9 | def __init__(self, proxy_url: str): |
| 10 | self.proxy_url = proxy_url |
| 11 | self.openai_base_url = f"{self.proxy_url}/v1" |
| 12 | self.feedback_url = f"{self.proxy_url}/feedback" |
| 13 | self.task_id = uuid.uuid4().hex[:6] |
| 14 | |
| 15 | def alive(self) -> bool: |
| 16 | try: |
| 17 | response = requests.get(f"{self.proxy_url}/health", timeout=2) |
| 18 | return response.status_code == 200 |
| 19 | except requests.RequestException: |
| 20 | return False |
| 21 | |
| 22 | def get_openai_client(self) -> openai.OpenAI: |
| 23 | client = openai.OpenAI( |
| 24 | base_url=self.openai_base_url, |
| 25 | api_key="EMPTY", |
| 26 | ) |
| 27 | return client |
| 28 | |
| 29 | def get_openai_async_client(self) -> openai.AsyncOpenAI: |
| 30 | client = openai.AsyncOpenAI( |
| 31 | base_url=self.openai_base_url, |
| 32 | api_key="EMPTY", |
| 33 | ) |
| 34 | return client |
| 35 | |
| 36 | def feedback(self, reward: float, msg_ids: list[str], timeout: float = 10) -> dict: |
| 37 | response = requests.post( |
| 38 | self.feedback_url, |
| 39 | json={"reward": reward, "msg_ids": msg_ids, "task_id": self.task_id}, |
| 40 | timeout=timeout, |
| 41 | ) |
| 42 | return response.json() |
| 43 | |
| 44 | async def feedback_async(self, reward: float, msg_ids: list[str], timeout: float = 10) -> dict: |
| 45 | async with httpx.AsyncClient() as client: |
| 46 | response = await client.post( |
| 47 | self.feedback_url, |
| 48 | json={"reward": reward, "msg_ids": msg_ids, "task_id": self.task_id}, |
| 49 | timeout=timeout, |
| 50 | ) |
| 51 | return response.json() |
| 52 | |
| 53 | def commit(self, timeout: float = 10) -> dict: |
| 54 | response = requests.post(f"{self.proxy_url}/commit", timeout=timeout) |
| 55 | return response.json() |
| 56 | |
| 57 | async def commit_async(self, timeout: float = 10) -> dict: |
| 58 | async with httpx.AsyncClient() as client: |
| 59 | response = await client.post(f"{self.proxy_url}/commit", timeout=timeout) |
| 60 | return response.json() |
| 61 | |
| 62 | def get_metrics(self, timeout: float = 5) -> dict: |
| 63 | response = requests.get(f"{self.proxy_url}/metrics", timeout=timeout) |
| 64 | return response.json() |
| 65 |
no outgoing calls