Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)
Python
upgrade anthropic and support thinking
Abi Raja994e0513a485
diff --git a/backend/models/claude.py b/backend/models/claude.pyindex 5ab74be0..9f178531 100644--- a/backend/models/claude.py+++ b/backend/models/claude.py@@ -1,39 +1,33 @@import copyimport time-from typing import Any, Awaitable, Callable, List, cast+from typing import Any, Awaitable, Callable, Dict, List, Tuple, castfrom anthropic import AsyncAnthropicfrom openai.types.chat import ChatCompletionMessageParamfrom config import IS_DEBUG_ENABLEDfrom debug.DebugFileWriter import DebugFileWriterfrom image_processing.utils import process_imagefrom utils import pprint_prompt-from llm import Completion+from llm import Completion, Llm-async def stream_claude_response(+def convert_openai_messages_to_claude(messages: List[ChatCompletionMessageParam],- api_key: str,- callback: Callable[[str], Awaitable[None]],- model_name: str,-) -> Completion:- start_time = time.time()- client = AsyncAnthropic(api_key=api_key)-- # Base parameters- max_tokens = 8192- temperature = 0.0-- # Claude 3.7 Sonnet can support higher max tokens- if model_name == "claude-3-7-sonnet-20250219":- max_tokens = 20000+) -> Tuple[str, List[Dict[str, Any]]]:+ """+ Convert OpenAI format messages to Claude format, handling image content properly.- # Translate OpenAI messages to Claude messages+ Args:+ messages: List of messages in OpenAI format+ Returns:+ Tuple of (system_prompt, claude_messages)+ """# Deep copy messages to avoid modifying the original listcloned_messages = copy.deepcopy(messages)system_prompt = cast(str, cloned_messages[0].get("content"))claude_messages = [dict(message) for message in cloned_messages[1:]]+for message in claude_messages:if not isinstance(message["content"], list):continue@@ -59,26 +53,69 @@ async def stream_claude_response("data": base64_data,}- # Stream Claude response- async with client.beta.messages.stream(- model=model_name,- max_tokens=max_tokens,- temperature=temperature,- system=system_prompt,- messages=claude_messages, # type: ignore- betas=["output-128k-2025-02-19"],- ) as stream:- async for text in stream.text_stream:- await callback(text)-- # Return final message- response = await stream.get_final_message()+ return system_prompt, claude_messages+++async def stream_claude_response(+ messages: List[ChatCompletionMessageParam],+ api_key: str,+ callback: Callable[[str], Awaitable[None]],+ model_name: str,+) -> Completion:+ start_time = time.time()+ client = AsyncAnthropic(api_key=api_key)++ # Base parameters+ max_tokens = 8192+ temperature = 0.0++ # Claude 3.7 Sonnet can support higher max tokens+ if model_name == "claude-3-7-sonnet-20250219":+ max_tokens = 20000++ # Translate OpenAI messages to Claude messages++ # Convert OpenAI format messages to Claude format+ system_prompt, claude_messages = convert_openai_messages_to_claude(messages)++ response = ""++ if model_name == Llm.CLAUDE_3_7_SONNET_2025_02_19.value:+ # Thinking is not compatible with temperature+ async with client.beta.messages.stream(+ model=model_name,+ thinking={"type": "enabled", "budget_tokens": 5000},+ max_tokens=20000,+ system=system_prompt,+ messages=claude_messages, # type: ignore+ ) as stream:+ async for event in stream:+ if event.type == "content_block_delta":+ if event.delta.type == "thinking_delta":+ print(event.delta.thinking, end="")+ elif event.delta.type == "text_delta":+ response += event.delta.text+ await callback(event.delta.text)++ else:+ # Stream Claude response+ async with client.beta.messages.stream(+ model=model_name,+ max_tokens=max_tokens,+ temperature=temperature,+ system=system_prompt,+ messages=claude_messages, # type: ignore+ betas=["output-128k-2025-02-19"],+ ) as stream:+ async for text in stream.text_stream:+ response += text+ await callback(text)# Close the Anthropic clientawait client.close()completion_time = time.time() - start_time- return {"duration": completion_time, "code": response.content[0].text}+ return {"duration": completion_time, "code": response}async def stream_claude_response_native(diff --git a/backend/poetry.lock b/backend/poetry.lockindex 84036f2a..f6003821 100644--- a/backend/poetry.lock+++ b/backend/poetry.lock@@ -136,19 +136,19 @@ files = [[[package]]name = "anthropic"-version = "0.49.0"+version = "0.51.0"description = "The official Python library for the anthropic API"optional = falsepython-versions = ">=3.8"files = [- {file = "anthropic-0.49.0-py3-none-any.whl", hash = "sha256:bbc17ad4e7094988d2fa86b87753ded8dce12498f4b85fe5810f208f454a8375"},- {file = "anthropic-0.49.0.tar.gz", hash = "sha256:c09e885b0f674b9119b4f296d8508907f6cff0009bc20d5cf6b35936c40b4398"},+ {file = "anthropic-0.51.0-py3-none-any.whl", hash = "sha256:b8b47d482c9aa1f81b923555cebb687c2730309a20d01be554730c8302e0f62a"},+ {file = "anthropic-0.51.0.tar.gz", hash = "sha256:6f824451277992af079554430d5b2c8ff5bc059cc2c968cdc3f06824437da201"},][package.dependencies]anyio = ">=3.5.0,<5"distro = ">=1.7.0,<2"-httpx = ">=0.23.0,<1"+httpx = ">=0.25.0,<1"jiter = ">=0.4.0,<1"pydantic = ">=1.9.0,<3"sniffio = "*"@@ -156,7 +156,7 @@ typing-extensions = ">=4.10,<5"[package.extras]bedrock = ["boto3 (>=1.28.57)", "botocore (>=1.31.57)"]-vertex = ["google-auth (>=2,<3)"]+vertex = ["google-auth[requests] (>=2,<3)"][[package]]name = "anyio"@@ -2038,4 +2038,4 @@ propcache = ">=0.2.0"[metadata]lock-version = "2.0"python-versions = "^3.10"-content-hash = "2056289f68d094e01e5e7b5ca7d4c2e03318565caadb89a76c09f4e4a510b734"+content-hash = "f61ed2762b3174d4558b7bdc83c29fdf6c06a64ff6fe365674a1adae0b2a369d"diff --git a/backend/pyproject.toml b/backend/pyproject.tomlindex 632e1302..d1414017 100644--- a/backend/pyproject.toml+++ b/backend/pyproject.toml@@ -15,7 +15,7 @@ python-dotenv = "^1.0.0"beautifulsoup4 = "^4.12.2"httpx = "^0.28.1"pre-commit = "^3.6.2"-anthropic = "^0.49.0"+anthropic = "^0.51.0"moviepy = "^1.0.3"pillow = "^10.3.0"types-pillow = "^10.2.0.20240520"