|
| 1 | +import aiohttp |
| 2 | +from fastapi import HTTPException |
| 3 | +from typing import Any, AsyncGenerator, Dict, List, Optional, Union |
| 4 | + |
| 5 | + |
| 6 | +def extract_text_from_anthropic_messages( |
| 7 | + messages: List[Dict[str, Any]], |
| 8 | + system: Optional[Union[str, List[Dict[str, Any]]]] = None, |
| 9 | +) -> str: |
| 10 | + """Extract plain text from Anthropic-format messages for token estimation.""" |
| 11 | + parts = [] |
| 12 | + |
| 13 | + if isinstance(system, str): |
| 14 | + parts.append(system) |
| 15 | + elif isinstance(system, list): |
| 16 | + for block in system: |
| 17 | + if block.get("type") == "text": |
| 18 | + parts.append(block.get("text", "")) |
| 19 | + |
| 20 | + for msg in messages: |
| 21 | + content = msg.get("content", "") |
| 22 | + if isinstance(content, str): |
| 23 | + parts.append(content) |
| 24 | + elif isinstance(content, list): |
| 25 | + for block in content: |
| 26 | + if block.get("type") == "text": |
| 27 | + parts.append(block.get("text", "")) |
| 28 | + |
| 29 | + return " ".join(parts) |
| 30 | + |
| 31 | + |
| 32 | +async def fetch_anthropic_messages( |
| 33 | + model_config: Dict[str, Any], |
| 34 | + request_data: Dict[str, Any], |
| 35 | + anthropic_headers: Dict[str, str], |
| 36 | +) -> Dict[str, Any]: |
| 37 | + """Proxy a non-streaming request to a native Anthropic Messages API endpoint.""" |
| 38 | + api_base = model_config["params"]["api_base"] |
| 39 | + # Strip trailing /v1 if present so we can append /v1/messages |
| 40 | + url = api_base.rstrip("/").removesuffix("/v1") + "/v1/messages" |
| 41 | + |
| 42 | + headers: Dict[str, str] = {"Content-Type": "application/json"} |
| 43 | + |
| 44 | + api_key = model_config["params"].get("api_key") |
| 45 | + if api_key and api_key != "no_token": |
| 46 | + headers["x-api-key"] = api_key |
| 47 | + |
| 48 | + # Forward Anthropic-specific headers from the client |
| 49 | + for h in ("anthropic-beta", "anthropic-version"): |
| 50 | + if anthropic_headers.get(h): |
| 51 | + headers[h] = anthropic_headers[h] |
| 52 | + |
| 53 | + # Ensure a default anthropic-version if the client didn't send one |
| 54 | + if "anthropic-version" not in headers: |
| 55 | + headers["anthropic-version"] = "2023-06-01" |
| 56 | + |
| 57 | + print(f"[anthropic-native] POST {url}") |
| 58 | + |
| 59 | + async with aiohttp.ClientSession() as session: |
| 60 | + async with session.post(url, headers=headers, json=request_data) as resp: |
| 61 | + if resp.status != 200: |
| 62 | + text = await resp.text() |
| 63 | + print(f"[anthropic-native] Error {resp.status}: {text}") |
| 64 | + raise HTTPException(status_code=resp.status, detail=f"Model API error: {text}") |
| 65 | + return await resp.json() |
| 66 | + |
| 67 | + |
| 68 | +async def fetch_anthropic_messages_stream( |
| 69 | + model_config: Dict[str, Any], |
| 70 | + request_data: Dict[str, Any], |
| 71 | + anthropic_headers: Dict[str, str], |
| 72 | +) -> AsyncGenerator[str, None]: |
| 73 | + """Proxy a streaming request to a native Anthropic Messages API endpoint. |
| 74 | +
|
| 75 | + Yields raw SSE chunks exactly as received from the backend. |
| 76 | + """ |
| 77 | + api_base = model_config["params"]["api_base"] |
| 78 | + url = api_base.rstrip("/").removesuffix("/v1") + "/v1/messages" |
| 79 | + |
| 80 | + headers: Dict[str, str] = {"Content-Type": "application/json"} |
| 81 | + |
| 82 | + api_key = model_config["params"].get("api_key") |
| 83 | + if api_key and api_key != "no_token": |
| 84 | + headers["x-api-key"] = api_key |
| 85 | + |
| 86 | + for h in ("anthropic-beta", "anthropic-version"): |
| 87 | + if anthropic_headers.get(h): |
| 88 | + headers[h] = anthropic_headers[h] |
| 89 | + |
| 90 | + if "anthropic-version" not in headers: |
| 91 | + headers["anthropic-version"] = "2023-06-01" |
| 92 | + |
| 93 | + print(f"[anthropic-native-stream] POST {url}") |
| 94 | + |
| 95 | + async with aiohttp.ClientSession() as session: |
| 96 | + async with session.post(url, headers=headers, json=request_data) as resp: |
| 97 | + if resp.status != 200: |
| 98 | + text = await resp.text() |
| 99 | + print(f"[anthropic-native-stream] Error {resp.status}: {text}") |
| 100 | + raise HTTPException(status_code=resp.status, detail=f"Model API error: {text}") |
| 101 | + |
| 102 | + async for line in resp.content: |
| 103 | + line_str = line.decode("utf-8") |
| 104 | + if line_str.strip(): |
| 105 | + yield line_str |
0 commit comments