Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

refactor llm functions

Abi Rajab9ff2bbb8319
diff --git a/backend/evals/core.py b/backend/evals/core.py
index aea91332..dcd27335 100644
--- a/backend/evals/core.py
+++ b/backend/evals/core.py
@@ -1,10 +1,6 @@
from config import ANTHROPIC_API_KEY, GEMINI_API_KEY, OPENAI_API_KEY
-from llm import (
- Llm,
- stream_claude_response,
- stream_openai_response,
-)
-from gemini import stream_gemini_response
+from llm import Llm
+from models import stream_claude_response, stream_openai_response, stream_gemini_response
from prompts import assemble_prompt
from prompts.types import Stack
from openai.types.chat import ChatCompletionMessageParam
@@ -35,7 +31,7 @@ async def generate_code_core(
prompt_messages,
api_key=ANTHROPIC_API_KEY,
callback=lambda x: process_chunk(x),
- model=model,
+ model_name=model.value,
)
elif (
model == Llm.GEMINI_2_0_FLASH_EXP
@@ -51,7 +47,7 @@ async def generate_code_core(
prompt_messages,
api_key=GEMINI_API_KEY,
callback=lambda x: process_chunk(x),
- model=model,
+ model_name=model.value,
)
else:
if not OPENAI_API_KEY:
@@ -62,7 +58,7 @@ async def generate_code_core(
api_key=OPENAI_API_KEY,
base_url=None,
callback=lambda x: process_chunk(x),
- model=model,
+ model_name=model.value,
)
return completion["code"]
diff --git a/backend/llm.py b/backend/llm.py
index c382050b..aab9279f 100644
--- a/backend/llm.py
+++ b/backend/llm.py
@@ -1,15 +1,5 @@
-import copy
from enum import Enum
-import time
-from typing import Any, Awaitable, Callable, List, cast, TypedDict
-from anthropic import AsyncAnthropic
-from openai import AsyncOpenAI
-from openai.types.chat import ChatCompletionMessageParam, ChatCompletionChunk
-from config import IS_DEBUG_ENABLED
-from debug.DebugFileWriter import DebugFileWriter
-from image_processing.utils import process_image
-
-from utils import pprint_prompt
+from typing import TypedDict
# Actual model versions that are passed to the LLMs and stored in our logs
@@ -41,244 +31,3 @@ class Llm(Enum):
class Completion(TypedDict):
duration: float
code: str
-
-
-async def stream_openai_response(
- messages: List[ChatCompletionMessageParam],
- api_key: str,
- base_url: str | None,
- callback: Callable[[str], Awaitable[None]],
- model: Llm,
-) -> Completion:
- start_time = time.time()
- client = AsyncOpenAI(api_key=api_key, base_url=base_url)
-
- # Base parameters
- params = {
- "model": model.value,
- "messages": messages,
- "timeout": 600,
- }
-
- # O1 doesn't support streaming or temperature
- if (
- model != Llm.O1_2024_12_17
- and model != Llm.O4_MINI_2025_04_16
- and model != Llm.O3_2025_04_16
- ):
- params["temperature"] = 0
- params["stream"] = True
-
- # 4.1 series
- if (
- model == Llm.GPT_4_1_2025_04_14
- or model == Llm.GPT_4_1_MINI_2025_04_14
- or model == Llm.GPT_4_1_NANO_2025_04_14
- ):
- params["temperature"] = 0
- params["stream"] = True
- params["max_tokens"] = 10000
-
- if model == Llm.GPT_4O_2024_05_13:
- params["max_tokens"] = 4096
-
- if model == Llm.GPT_4O_2024_11_20:
- params["max_tokens"] = 16384
-
- if model == Llm.O1_2024_12_17:
- params["max_completion_tokens"] = 20000
-
- if model == Llm.O4_MINI_2025_04_16 or model == Llm.O3_2025_04_16:
- params["max_completion_tokens"] = 20000
- params["stream"] = True
- params["reasoning_effort"] = "high"
-
- # O1 doesn't support streaming
- if model == Llm.O1_2024_12_17:
- response = await client.chat.completions.create(**params) # type: ignore
- full_response = response.choices[0].message.content # type: ignore
- else:
- stream = await client.chat.completions.create(**params) # type: ignore
- full_response = ""
- async for chunk in stream: # type: ignore
- assert isinstance(chunk, ChatCompletionChunk)
- if (
- chunk.choices
- and len(chunk.choices) > 0
- and chunk.choices[0].delta
- and chunk.choices[0].delta.content
- ):
- content = chunk.choices[0].delta.content or ""
- full_response += content
- await callback(content)
-
- await client.close()
-
- completion_time = time.time() - start_time
- return {"duration": completion_time, "code": full_response}
-
-
-# TODO: Have a seperate function that translates OpenAI messages to Claude messages
-async def stream_claude_response(
- messages: List[ChatCompletionMessageParam],
- api_key: str,
- callback: Callable[[str], Awaitable[None]],
- model: Llm,
-) -> 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 == Llm.CLAUDE_3_7_SONNET_2025_02_19:
- max_tokens = 20000
-
- # Translate OpenAI messages to Claude messages
-
- # Deep copy messages to avoid modifying the original list
- cloned_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
-
- for content in message["content"]: # type: ignore
- if content["type"] == "image_url":
- content["type"] = "image"
-
- # Extract base64 data and media type from data URL
- # Example base64 data URL: data:image/png;base64,iVBOR...
- image_data_url = cast(str, content["image_url"]["url"])
-
- # Process image and split media type and data
- # so it works with Claude (under 5mb in base64 encoding)
- (media_type, base64_data) = process_image(image_data_url)
-
- # Remove OpenAI parameter
- del content["image_url"]
-
- content["source"] = {
- "type": "base64",
- "media_type": media_type,
- "data": base64_data,
- }
-
- # Stream Claude response
- async with client.beta.messages.stream(
- model=model.value,
- 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()
-
- # Close the Anthropic client
- await client.close()
-
- completion_time = time.time() - start_time
- return {"duration": completion_time, "code": response.content[0].text}
-
-
-async def stream_claude_response_native(
- system_prompt: str,
- messages: list[Any],
- api_key: str,
- callback: Callable[[str], Awaitable[None]],
- include_thinking: bool = False,
- model: Llm = Llm.CLAUDE_3_7_SONNET_2025_02_19,
-) -> Completion:
- start_time = time.time()
- client = AsyncAnthropic(api_key=api_key)
-
- # Base model parameters
- max_tokens = 4096
- temperature = 0.0
-
- # Multi-pass flow
- current_pass_num = 1
- max_passes = 2
-
- prefix = "<thinking>"
- response = None
-
- # For debugging
- full_stream = ""
- debug_file_writer = DebugFileWriter()
-
- while current_pass_num <= max_passes:
- current_pass_num += 1
-
- # Set up message depending on whether we have a <thinking> prefix
- messages_to_send = (
- messages + [{"role": "assistant", "content": prefix}]
- if include_thinking
- else messages
- )
-
- pprint_prompt(messages_to_send)
-
- async with client.messages.stream(
- model=model.value,
- max_tokens=max_tokens,
- temperature=temperature,
- system=system_prompt,
- messages=messages_to_send, # type: ignore
- ) as stream:
- async for text in stream.text_stream:
- print(text, end="", flush=True)
- full_stream += text
- await callback(text)
-
- response = await stream.get_final_message()
- response_text = response.content[0].text
-
- # Write each pass's code to .html file and thinking to .txt file
- if IS_DEBUG_ENABLED:
- debug_file_writer.write_to_file(
- f"pass_{current_pass_num - 1}.html",
- debug_file_writer.extract_html_content(response_text),
- )
- debug_file_writer.write_to_file(
- f"thinking_pass_{current_pass_num - 1}.txt",
- response_text.split("</thinking>")[0],
- )
-
- # Set up messages array for next pass
- messages += [
- {"role": "assistant", "content": str(prefix) + response.content[0].text},
- {
- "role": "user",
- "content": "You've done a good job with a first draft. Improve this further based on the original instructions so that the app is fully functional and looks like the original video of the app we're trying to replicate.",
- },
- ]
-
- print(
- f"Token usage: Input Tokens: {response.usage.input_tokens}, Output Tokens: {response.usage.output_tokens}"
- )
-
- # Close the Anthropic client
- await client.close()
-
- completion_time = time.time() - start_time
-
- if IS_DEBUG_ENABLED:
- debug_file_writer.write_to_file("full_stream.txt", full_stream)
-
- if not response:
- raise Exception("No HTML response found in AI response")
- else:
- return {
- "duration": completion_time,
- "code": response.content[0].text, # type: ignore
- }
diff --git a/backend/models/__init__.py b/backend/models/__init__.py
new file mode 100644
index 00000000..3dc0d429
--- /dev/null
+++ b/backend/models/__init__.py
@@ -0,0 +1,13 @@
+from .claude import stream_claude_response, stream_claude_response_native
+from .openai_client import stream_openai_response
+from .gemini import stream_gemini_response
+from llm import Completion
+
+# Re-export the functions for backward compatibility
+__all__ = [
+ "stream_claude_response",
+ "stream_claude_response_native",
+ "stream_openai_response",
+ "stream_gemini_response",
+ "Completion"
+]
diff --git a/backend/models/claude.py b/backend/models/claude.py
new file mode 100644
index 00000000..5ab74be0
--- /dev/null
+++ b/backend/models/claude.py
@@ -0,0 +1,175 @@
+import copy
+import time
+from typing import Any, Awaitable, Callable, List, cast
+from anthropic import AsyncAnthropic
+from openai.types.chat import ChatCompletionMessageParam
+from config import IS_DEBUG_ENABLED
+from debug.DebugFileWriter import DebugFileWriter
+from image_processing.utils import process_image
+from utils import pprint_prompt
+from llm import Completion
+
+
+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
+
+ # Deep copy messages to avoid modifying the original list
+ cloned_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
+
+ for content in message["content"]: # type: ignore
+ if content["type"] == "image_url":
+ content["type"] = "image"
+
+ # Extract base64 data and media type from data URL
+ # Example base64 data URL: data:image/png;base64,iVBOR...
+ image_data_url = cast(str, content["image_url"]["url"])
+
+ # Process image and split media type and data
+ # so it works with Claude (under 5mb in base64 encoding)
+ (media_type, base64_data) = process_image(image_data_url)
+
+ # Remove OpenAI parameter
+ del content["image_url"]
+
+ content["source"] = {
+ "type": "base64",
+ "media_type": media_type,
+ "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()
+
+ # Close the Anthropic client
+ await client.close()
+
+ completion_time = time.time() - start_time
+ return {"duration": completion_time, "code": response.content[0].text}
+
+
+async def stream_claude_response_native(
+ system_prompt: str,
+ messages: list[Any],
+ api_key: str,
+ callback: Callable[[str], Awaitable[None]],
+ include_thinking: bool = False,
+ model_name: str = "claude-3-7-sonnet-20250219",
+) -> Completion:
+ start_time = time.time()
+ client = AsyncAnthropic(api_key=api_key)
+
+ # Base model parameters
+ max_tokens = 4096
+ temperature = 0.0
+
+ # Multi-pass flow
+ current_pass_num = 1
+ max_passes = 2
+
+ prefix = "<thinking>"
+ response = None
+
+ # For debugging
+ full_stream = ""
+ debug_file_writer = DebugFileWriter()
+
+ while current_pass_num <= max_passes:
+ current_pass_num += 1
+
+ # Set up message depending on whether we have a <thinking> prefix
+ messages_to_send = (
+ messages + [{"role": "assistant", "content": prefix}]
+ if include_thinking
+ else messages
+ )
+
+ pprint_prompt(messages_to_send)
+
+ async with client.messages.stream(
+ model=model_name,
+ max_tokens=max_tokens,
+ temperature=temperature,
+ system=system_prompt,
+ messages=messages_to_send, # type: ignore
+ ) as stream:
+ async for text in stream.text_stream:
+ print(text, end="", flush=True)
+ full_stream += text
+ await callback(text)
+
+ response = await stream.get_final_message()
+ response_text = response.content[0].text
+
+ # Write each pass's code to .html file and thinking to .txt file
+ if IS_DEBUG_ENABLED:
+ debug_file_writer.write_to_file(
+ f"pass_{current_pass_num - 1}.html",
+ debug_file_writer.extract_html_content(response_text),
+ )
+ debug_file_writer.write_to_file(
+ f"thinking_pass_{current_pass_num - 1}.txt",
+ response_text.split("</thinking>")[0],
+ )
+
+ # Set up messages array for next pass
+ messages += [
+ {"role": "assistant", "content": str(prefix) + response.content[0].text},
+ {
+ "role": "user",
+ "content": "You've done a good job with a first draft. Improve this further based on the original instructions so that the app is fully functional and looks like the original video of the app we're trying to replicate.",
+ },
+ ]
+
+ print(
+ f"Token usage: Input Tokens: {response.usage.input_tokens}, Output Tokens: {response.usage.output_tokens}"
+ )
+
+ # Close the Anthropic client
+ await client.close()
+
+ completion_time = time.time() - start_time
+
+ if IS_DEBUG_ENABLED:
+ debug_file_writer.write_to_file("full_stream.txt", full_stream)
+
+ if not response:
+ raise Exception("No HTML response found in AI response")
+ else:
+ return {
+ "duration": completion_time,
+ "code": response.content[0].text, # type: ignore
+ }
diff --git a/backend/gemini.py b/backend/models/gemini.py
similarity index 94%
rename from backend/gemini.py
rename to backend/models/gemini.py
index beecc273..1cc0a3dd 100644
--- a/backend/gemini.py
+++ b/backend/models/gemini.py
@@ -1,14 +1,10 @@
import base64
import time
-from typing import Awaitable, Callable, List, TypedDict
+from typing import Awaitable, Callable, List
from openai.types.chat import ChatCompletionMessageParam
from google import genai
from google.genai import types
-
-
-class Completion(TypedDict):
- duration: float
- code: str
+from llm import Completion
async def stream_gemini_response(
diff --git a/backend/models/openai_client.py b/backend/models/openai_client.py
new file mode 100644
index 00000000..0834aa00
--- /dev/null
+++ b/backend/models/openai_client.py
@@ -0,0 +1,76 @@
+import time
+from typing import Awaitable, Callable, List
+from openai import AsyncOpenAI
+from openai.types.chat import ChatCompletionMessageParam, ChatCompletionChunk
+from llm import Completion
+
+
+async def stream_openai_response(
+ messages: List[ChatCompletionMessageParam],
+ api_key: str,
+ base_url: str | None,
+ callback: Callable[[str], Awaitable[None]],
+ model_name: str,
+) -> Completion:
+ start_time = time.time()
+ client = AsyncOpenAI(api_key=api_key, base_url=base_url)
+
+ # Base parameters
+ params = {
+ "model": model_name,
+ "messages": messages,
+ "timeout": 600,
+ }
+
+ # O1 doesn't support streaming or temperature
+ if model_name not in ["o1-2024-12-17", "o4-mini-2025-04-16", "o3-2025-04-16"]:
+ params["temperature"] = 0
+ params["stream"] = True
+
+ # 4.1 series
+ if model_name in [
+ "gpt-4.1-2025-04-14",
+ "gpt-4.1-mini-2025-04-14",
+ "gpt-4.1-nano-2025-04-14"
+ ]:
+ params["temperature"] = 0
+ params["stream"] = True
+ params["max_tokens"] = 10000
+
+ if model_name == "gpt-4o-2024-05-13":
+ params["max_tokens"] = 4096
+
+ if model_name == "gpt-4o-2024-11-20":
+ params["max_tokens"] = 16384
+
+ if model_name == "o1-2024-12-17":
+ params["max_completion_tokens"] = 20000
+
+ if model_name in ["o4-mini-2025-04-16", "o3-2025-04-16"]:
+ params["max_completion_tokens"] = 20000
+ params["stream"] = True
+ params["reasoning_effort"] = "high"
+
+ # O1 doesn't support streaming
+ if model_name == "o1-2024-12-17":
+ response = await client.chat.completions.create(**params) # type: ignore
+ full_response = response.choices[0].message.content # type: ignore
+ else:
+ stream = await client.chat.completions.create(**params) # type: ignore
+ full_response = ""
+ async for chunk in stream: # type: ignore
+ assert isinstance(chunk, ChatCompletionChunk)
+ if (
+ chunk.choices
+ and len(chunk.choices) > 0
+ and chunk.choices[0].delta
+ and chunk.choices[0].delta.content
+ ):
+ content = chunk.choices[0].delta.content or ""
+ full_response += content
+ await callback(content)
+
+ await client.close()
+
+ completion_time = time.time() - start_time
+ return {"duration": completion_time, "code": full_response}
diff --git a/backend/routes/generate_code.py b/backend/routes/generate_code.py
index 8f627528..d6a92d2c 100644
--- a/backend/routes/generate_code.py
+++ b/backend/routes/generate_code.py
@@ -15,14 +15,8 @@ from config import (
SHOULD_MOCK_AI_RESPONSE,
)
from custom_types import InputMode
-from llm import (
- Completion,
- Llm,
- stream_claude_response,
- stream_claude_response_native,
- stream_openai_response,
-)
-from gemini import stream_gemini_response
+from llm import Completion, Llm
+from models import stream_claude_response, stream_claude_response_native, stream_openai_response, stream_gemini_response
from fs_logging.core import write_logs
from mock_llm import mock_completion
from typing import Any, Callable, Coroutine, Dict, List, Literal, cast, get_args
@@ -244,7 +238,7 @@ async def stream_code(websocket: WebSocket):
messages=prompt_messages, # type: ignore
api_key=anthropic_api_key,
callback=lambda x: process_chunk(x, 0),
- model=Llm.CLAUDE_3_OPUS,
+ model_name=Llm.CLAUDE_3_OPUS.value,
include_thinking=True,
)
]
@@ -309,7 +303,7 @@ async def stream_code(websocket: WebSocket):
api_key=openai_api_key,
base_url=openai_base_url,
callback=lambda x, i=index: process_chunk(x, i),
- model=model,
+ model_name=model.value,
)
)
elif GEMINI_API_KEY and (
@@ -348,7 +342,7 @@ async def stream_code(websocket: WebSocket):
prompt_messages,
api_key=anthropic_api_key,
callback=lambda x, i=index: process_chunk(x, i),
- model=claude_model,
+ model_name=claude_model.value,
)
)
diff --git a/backend/video_to_app.py b/backend/video_to_app.py
index c876804c..b8836f2f 100644
--- a/backend/video_to_app.py
+++ b/backend/video_to_app.py
@@ -16,10 +16,8 @@ from prompts.claude_prompts import VIDEO_PROMPT
from utils import pprint_prompt
from config import ANTHROPIC_API_KEY
from video.utils import extract_tag_content, assemble_claude_prompt_video
-from llm import (
- Llm,
- stream_claude_response_native,
-)
+from llm import Llm
+from models import stream_claude_response_native
STACK = "html_tailwind"
@@ -86,7 +84,7 @@ async def main():
messages=prompt_messages,
api_key=ANTHROPIC_API_KEY,
callback=lambda x: process_chunk(x),
- model=Llm.CLAUDE_3_OPUS,
+ model_name=Llm.CLAUDE_3_OPUS.value,
include_thinking=True,
)