Skip to content

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

Python

read anthropic api key from front-end

Abi Rajac64968b8f09e
diff --git a/backend/routes/generate_code.py b/backend/routes/generate_code.py
index e7186fcd..61ce9c9a 100644
--- a/backend/routes/generate_code.py
+++ b/backend/routes/generate_code.py
@@ -120,6 +120,17 @@ async def stream_code(websocket: WebSocket):
)
return
+ # Get the Anthropic API key from the request. Fall back to environment variable if not provided.
+ # If neither is provided, we throw an error later only if Claude is used.
+ anthropic_api_key = None
+ if params["anthropicApiKey"]:
+ anthropic_api_key = params["anthropicApiKey"]
+ print("Using Anthropic API key from client-side settings dialog")
+ else:
+ anthropic_api_key = ANTHROPIC_API_KEY
+ if anthropic_api_key:
+ print("Using Anthropic API key from environment variable")
+
# Get the OpenAI Base URL from the request. Fall back to environment variable if not provided.
openai_base_url = None
# Disable user-specified OpenAI Base URL in prod
@@ -219,31 +230,31 @@ async def stream_code(websocket: WebSocket):
else:
try:
if validated_input_mode == "video":
- if not ANTHROPIC_API_KEY:
+ if not anthropic_api_key:
await throw_error(
- "Video only works with Anthropic models. No Anthropic API key found. Please add the environment variable ANTHROPIC_API_KEY to backend/.env"
+ "Video only works with Anthropic models. No Anthropic API key found. Please add the environment variable ANTHROPIC_API_KEY to backend/.env or in the settings dialog"
)
raise Exception("No Anthropic key")
completion = await stream_claude_response_native(
system_prompt=VIDEO_PROMPT,
messages=prompt_messages, # type: ignore
- api_key=ANTHROPIC_API_KEY,
+ api_key=anthropic_api_key,
callback=lambda x: process_chunk(x),
model=Llm.CLAUDE_3_OPUS,
include_thinking=True,
)
exact_llm_version = Llm.CLAUDE_3_OPUS
elif code_generation_model == Llm.CLAUDE_3_SONNET:
- if not ANTHROPIC_API_KEY:
+ if not anthropic_api_key:
await throw_error(
- "No Anthropic API key found. Please add the environment variable ANTHROPIC_API_KEY to backend/.env"
+ "No Anthropic API key found. Please add the environment variable ANTHROPIC_API_KEY to backend/.env or in the settings dialog"
)
raise Exception("No Anthropic key")
completion = await stream_claude_response(
prompt_messages, # type: ignore
- api_key=ANTHROPIC_API_KEY,
+ api_key=anthropic_api_key,
callback=lambda x: process_chunk(x),
)
exact_llm_version = code_generation_model