(
self,
request: Request,
task: SideChannelTaskConfig,
system_prompt: str,
user_prompt: str,
*,
source_text: str,
query_text: str,
)
| 849 | ) |
| 850 | |
| 851 | async def _run_task( |
| 852 | self, |
| 853 | request: Request, |
| 854 | task: SideChannelTaskConfig, |
| 855 | system_prompt: str, |
| 856 | user_prompt: str, |
| 857 | *, |
| 858 | source_text: str, |
| 859 | query_text: str, |
| 860 | ) -> SemanticCallResult | None: |
| 861 | input_tokens = estimate_tokens(system_prompt) + estimate_tokens(user_prompt) |
| 862 | client = _get_client() |
| 863 | quality_fallbacks = 0 |
| 864 | attempts = 0 |
| 865 | for model_id in task.candidates(): |
| 866 | attempts += 1 |
| 867 | resolved = self._resolve_request(model_id, request) |
| 868 | if resolved is None: |
| 869 | continue |
| 870 | target_chat_url, headers, upstream_model = resolved |
| 871 | payload = { |
| 872 | "model": upstream_model, |
| 873 | "messages": [ |
| 874 | {"role": "system", "content": system_prompt}, |
| 875 | {"role": "user", "content": user_prompt}, |
| 876 | ], |
| 877 | "max_tokens": task.max_tokens, |
| 878 | "stream": False, |
| 879 | } |
| 880 | try: |
| 881 | resp = await client.post(target_chat_url, json=payload, headers=headers) |
| 882 | except (httpx.ConnectError, httpx.TimeoutException): |
| 883 | continue |
| 884 | if resp.status_code >= 400: |
| 885 | if resp.status_code in (400, 404, 422) and _is_model_error(resp.content): |
| 886 | continue |
| 887 | continue |
| 888 | text = _extract_assistant_text(resp.content).strip() |
| 889 | if not text: |
| 890 | continue |
| 891 | ok, quality_score, _reason = score_semantic_quality( |
| 892 | text, |
| 893 | source_text=source_text, |
| 894 | query_text=query_text, |
| 895 | policy=task.quality, |
| 896 | ) |
| 897 | if not ok: |
| 898 | quality_fallbacks += 1 |
| 899 | continue |
| 900 | actual_cost = _parse_usage_cost(resp.content, model_id) |
| 901 | estimated_cost = _estimate_cost(model_id, input_tokens, task.max_tokens) |
| 902 | return SemanticCallResult( |
| 903 | text=text, |
| 904 | model=model_id, |
| 905 | estimated_cost=estimated_cost, |
| 906 | actual_cost=actual_cost, |
| 907 | quality_score=quality_score, |
| 908 | attempts=attempts, |
no test coverage detected