Spaces:
Runtime error
Runtime error
Download app/server/anthropic_handler.py from 520superxu520/G_AI: direct link, hf CLI and curl.
- Browser
- Download file 15.1 kB
-
https://huggingface.co/spaces/520superxu520/G_AI/resolve/main/app/server/anthropic_handler.py
- Command line
-
hf download hf://spaces/520superxu520/G_AI/app/server/anthropic_handler.py
-
curl -L -o anthropic_handler.py https://huggingface.co/spaces/520superxu520/G_AI/resolve/main/app/server/anthropic_handler.py
15.1 kB
| from __future__ import annotations | |
| from dataclasses import dataclass | |
| from pathlib import Path | |
| from typing import Any, Awaitable, Callable | |
| import orjson | |
| from fastapi import HTTPException, Request | |
| from gemini_webapi import ModelOutput | |
| from loguru import logger | |
| from ..models import ( | |
| AnthropicMessageRequest, | |
| ContentItem, | |
| FunctionCall, | |
| Message, | |
| Tool, | |
| ToolCall, | |
| ToolChoiceFunction, | |
| ToolChoiceFunctionDetail, | |
| ToolFunctionDefinition, | |
| ) | |
| from ..services.client import GeminiClientWrapper | |
| from ..services.lmdb import LMDBConversationStore | |
| StreamFactory = Callable[..., Any] | |
| PersistConversationFn = Callable[..., str | None] | |
| ProcessOutputFn = Callable[..., tuple[str | None, str, str, list[Any]]] | |
| CalculateUsageFn = Callable[..., tuple[int, int, int, int]] | |
| ExecuteCoreSessionFn = Callable[ | |
| ..., Awaitable[tuple[Any, str, int, Any, Any, LMDBConversationStore, Any, list[Any], Any]] | |
| ] | |
| class AnthropicHandlerDependencies: | |
| """Injected runtime hooks extracted from the chat router.""" | |
| execute_core_chat_session: ExecuteCoreSessionFn | |
| create_anthropic_streaming_response: StreamFactory | |
| process_llm_output: ProcessOutputFn | |
| calculate_usage: CalculateUsageFn | |
| persist_conversation: PersistConversationFn | |
| def _convert_system_prompt(system: str | list[dict[str, str]] | None) -> list[Message]: | |
| """Convert Anthropic system payload into internal system messages.""" | |
| if not system: | |
| return [] | |
| system_content = "" | |
| if isinstance(system, str): | |
| system_content = system | |
| elif isinstance(system, list): | |
| for item in system: | |
| if item.get("type") == "text": | |
| system_content += item.get("text", "") + "\n" | |
| if not system_content: | |
| return [] | |
| return [Message(role="system", content=system_content)] | |
| def _convert_messages(messages: list[Any]) -> list[Message]: | |
| """Convert Anthropic message blocks into internal message/tool structures.""" | |
| converted_messages: list[Message] = [] | |
| for raw_message in messages: | |
| if isinstance(raw_message, Message): | |
| message = raw_message.model_copy(deep=True) | |
| role = message.role | |
| original_content = message.content | |
| elif isinstance(raw_message, dict): | |
| role = str(raw_message.get("role") or "user") | |
| original_content = raw_message.get("content") | |
| message = Message(role=role, content=None) | |
| else: | |
| continue | |
| if not isinstance(original_content, list): | |
| if isinstance(original_content, (str, type(None))): | |
| message.content = original_content | |
| else: | |
| try: | |
| message.content = orjson.dumps(original_content).decode("utf-8") | |
| except Exception: | |
| message.content = str(original_content) | |
| converted_messages.append(message) | |
| continue | |
| if role == "assistant": | |
| text_parts: list[ContentItem] = [] | |
| tool_calls: list[ToolCall] = [] | |
| for part in original_content: | |
| if not isinstance(part, dict): | |
| continue | |
| part_type = part.get("type") | |
| if part_type == "text": | |
| text_value = part.get("text") | |
| if text_value: | |
| text_parts.append(ContentItem(type="text", text=text_value)) | |
| elif part_type == "tool_use": | |
| tool_name = part.get("name") | |
| tool_id = part.get("id") | |
| tool_input = part.get("input", {}) | |
| if not tool_name or not tool_id: | |
| continue | |
| try: | |
| tool_arguments = orjson.dumps(tool_input).decode("utf-8") | |
| except Exception: | |
| tool_arguments = orjson.dumps({"raw": tool_input}).decode("utf-8") | |
| tool_calls.append( | |
| ToolCall( | |
| id=tool_id, | |
| type="function", | |
| function=FunctionCall(name=tool_name, arguments=tool_arguments), | |
| ) | |
| ) | |
| elif part_type == "image" and "source" in part: | |
| source = part["source"] | |
| if source.get("type") == "base64": | |
| data = source.get("data") | |
| media_type = source.get("media_type") | |
| text_parts.append( | |
| ContentItem( | |
| type="image_url", | |
| image_url={"url": f"data:{media_type};base64,{data}"}, | |
| ) | |
| ) | |
| message.content = text_parts or None # type: ignore[assignment] | |
| message.tool_calls = tool_calls or None | |
| converted_messages.append(message) | |
| continue | |
| if role == "user": | |
| user_text_parts: list[ContentItem] = [] | |
| for part in original_content: | |
| if not isinstance(part, dict): | |
| continue | |
| part_type = part.get("type") | |
| if part_type == "text": | |
| text_value = part.get("text") | |
| if text_value: | |
| user_text_parts.append(ContentItem(type="text", text=text_value)) | |
| elif part_type == "image" and "source" in part: | |
| source = part["source"] | |
| if source.get("type") == "base64": | |
| data = source.get("data") | |
| media_type = source.get("media_type") | |
| user_text_parts.append( | |
| ContentItem( | |
| type="image_url", | |
| image_url={"url": f"data:{media_type};base64,{data}"}, | |
| ) | |
| ) | |
| elif part_type == "tool_result": | |
| tool_result_id = part.get("tool_use_id") | |
| if not tool_result_id: | |
| continue | |
| result_content = part.get("content") | |
| if isinstance(result_content, str): | |
| tool_content = result_content | |
| elif isinstance(result_content, list): | |
| tool_text_parts: list[str] = [] | |
| for result_part in result_content: | |
| if isinstance(result_part, dict): | |
| if result_part.get("type") == "text": | |
| text_value = result_part.get("text") | |
| if text_value: | |
| tool_text_parts.append(text_value) | |
| else: | |
| tool_text_parts.append( | |
| orjson.dumps(result_part).decode("utf-8") | |
| ) | |
| elif result_part is not None: | |
| tool_text_parts.append(str(result_part)) | |
| tool_content = "\n".join(tool_text_parts).strip() or "{}" | |
| elif result_content is None: | |
| tool_content = "{}" | |
| else: | |
| try: | |
| tool_content = orjson.dumps(result_content).decode("utf-8") | |
| except Exception: | |
| tool_content = str(result_content) | |
| if part.get("is_error"): | |
| tool_content = f'{{"is_error": true, "content": {orjson.dumps(tool_content).decode("utf-8")}}}' | |
| converted_messages.append( | |
| Message( | |
| role="tool", | |
| content=tool_content, | |
| tool_call_id=tool_result_id, | |
| ) | |
| ) | |
| if user_text_parts: | |
| message.content = user_text_parts # type: ignore[assignment] | |
| converted_messages.append(message) | |
| elif not any( | |
| isinstance(part, dict) and part.get("type") == "tool_result" | |
| for part in original_content | |
| ): | |
| message.content = None | |
| converted_messages.append(message) | |
| continue | |
| new_content: list[ContentItem] = [] | |
| for part in original_content: | |
| if not isinstance(part, dict): | |
| continue | |
| part_type = part.get("type") | |
| if part_type == "text": | |
| new_content.append(ContentItem(type="text", text=part.get("text"))) | |
| elif part_type == "image" and "source" in part: | |
| source = part["source"] | |
| if source.get("type") == "base64": | |
| data = source.get("data") | |
| media_type = source.get("media_type") | |
| new_content.append( | |
| ContentItem( | |
| type="image_url", | |
| image_url={"url": f"data:{media_type};base64,{data}"}, | |
| ) | |
| ) | |
| message.content = new_content or None # type: ignore[assignment] | |
| converted_messages.append(message) | |
| return converted_messages | |
| def _convert_tools(tools: list[dict[str, Any]] | None) -> list[Tool] | None: | |
| """Convert Anthropic tool schema into internal OpenAI-style Tool objects.""" | |
| if not tools: | |
| return None | |
| converted_tools: list[Tool] = [] | |
| for tool in tools: | |
| if not isinstance(tool, dict): | |
| continue | |
| tool_name = tool.get("name") | |
| tool_description = tool.get("description", "") | |
| tool_parameters = tool.get("input_schema") or tool.get("parameters", {}) | |
| if not tool_name: | |
| continue | |
| converted_tools.append( | |
| Tool( | |
| type="function", | |
| function=ToolFunctionDefinition( | |
| name=tool_name, | |
| description=tool_description, | |
| parameters=tool_parameters, | |
| ), | |
| ) | |
| ) | |
| return converted_tools or None | |
| def _convert_tool_choice( | |
| tool_choice: dict[str, Any] | str | None, | |
| ) -> str | ToolChoiceFunction | None: | |
| """Convert Anthropic tool choice into internal tool choice format.""" | |
| if not tool_choice: | |
| return None | |
| if isinstance(tool_choice, dict): | |
| choice_type = tool_choice.get("type") | |
| if choice_type == "tool": | |
| tool_name = tool_choice.get("name") | |
| if tool_name: | |
| return ToolChoiceFunction( | |
| type="function", | |
| function=ToolChoiceFunctionDetail(name=tool_name), | |
| ) | |
| if choice_type in ("auto", "any"): | |
| return "auto" | |
| if choice_type == "none": | |
| return "none" | |
| return None | |
| if isinstance(tool_choice, str): | |
| if tool_choice == "none": | |
| return "none" | |
| return "auto" | |
| return None | |
| async def handle_anthropic_messages( | |
| request: AnthropicMessageRequest, | |
| raw_request: Request, | |
| tmp_dir: Path, | |
| deps: AnthropicHandlerDependencies, | |
| ) -> Any: | |
| """ | |
| Handle the Anthropic-compatible messages endpoint. | |
| This module keeps Anthropic protocol adaptation separate from the main router | |
| while reusing the existing Gemini execution and persistence runtime hooks. | |
| """ | |
| _ = raw_request | |
| converted_messages: list[Message] = [] | |
| converted_messages.extend(_convert_system_prompt(request.system)) | |
| converted_messages.extend(_convert_messages(request.messages)) | |
| converted_tools = _convert_tools(request.tools) | |
| converted_tool_choice = _convert_tool_choice(request.tool_choice) | |
| ( | |
| response_or_stream, | |
| completion_id, | |
| created_time, | |
| session, | |
| client, | |
| db, | |
| model, | |
| prepared_messages, | |
| _structured_requirement, | |
| ) = await deps.execute_core_chat_session( | |
| request_model=request.model, | |
| messages=converted_messages, | |
| tmp_dir=tmp_dir, | |
| tools=converted_tools, | |
| tool_choice=converted_tool_choice, | |
| stream=bool(request.stream), | |
| response_format=None, | |
| ) | |
| if request.stream: | |
| return await deps.create_anthropic_streaming_response( | |
| response_or_stream, | |
| completion_id, | |
| created_time, | |
| model.model_name, | |
| prepared_messages, | |
| db, | |
| model, | |
| client, | |
| session, | |
| ) | |
| if not isinstance(response_or_stream, ModelOutput): | |
| raise HTTPException(status_code=502, detail="Malformed non-stream response.") | |
| try: | |
| thoughts = getattr(response_or_stream, "thoughts", None) | |
| raw_clean = GeminiClientWrapper.extract_output(response_or_stream, include_thoughts=False) | |
| except Exception as exc: | |
| logger.exception("Anthropic-compatible response parsing failed.") | |
| raise HTTPException(status_code=502, detail="Malformed response") from exc | |
| thoughts, assistant_visible, assistant_storage, tool_calls = deps.process_llm_output( | |
| thoughts, | |
| raw_clean, | |
| None, | |
| ) | |
| content_blocks: list[dict[str, Any]] = [] | |
| if thoughts: | |
| content_blocks.append({"type": "thinking", "thinking": thoughts}) | |
| if assistant_visible: | |
| content_blocks.append({"type": "text", "text": assistant_visible}) | |
| if tool_calls: | |
| for tool_call in tool_calls: | |
| tool_input: Any = {} | |
| if tool_call.function.arguments: | |
| try: | |
| tool_input = orjson.loads(tool_call.function.arguments) | |
| except orjson.JSONDecodeError: | |
| tool_input = {"raw": tool_call.function.arguments} | |
| content_blocks.append( | |
| { | |
| "type": "tool_use", | |
| "id": tool_call.id, | |
| "name": tool_call.function.name, | |
| "input": tool_input, | |
| } | |
| ) | |
| input_tokens, output_tokens, _total_tokens, _reasoning_tokens = deps.calculate_usage( | |
| converted_messages, | |
| assistant_visible, | |
| tool_calls, | |
| thoughts, | |
| ) | |
| stop_reason = "tool_use" if tool_calls else "end_turn" | |
| deps.persist_conversation( | |
| db, | |
| model.model_name, | |
| client.id, | |
| session.metadata, | |
| prepared_messages, | |
| assistant_storage, | |
| tool_calls, | |
| thoughts, | |
| ) | |
| return { | |
| "id": completion_id, | |
| "type": "message", | |
| "role": "assistant", | |
| "content": content_blocks, | |
| "model": model.model_name, | |
| "stop_reason": stop_reason, | |
| "stop_sequence": None, | |
| "usage": { | |
| "input_tokens": input_tokens, | |
| "output_tokens": output_tokens, | |
| }, | |
| } | |