switch to openrouter
Browse filesFiles changed (2)
- backend_api.py+7-55
- backend_models.py+17-3
98 unmodified lines99100101102102103104105739 unmodified lines84584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090184884985085185285385485585698 unmodified lines
# Define models and languages here to avoid importing Gradio UIAVAILABLE_MODELS = [ {"name": "Devstral Medium 2512", "id": "devstral-medium-2512", "description": "Mistral Devstral Medium 2512 - Expert code generation model via Mistral Conversations API (Default)", "supports_images": False}, {"name": "Devstral Medium 2512", "id": "devstral-medium-2512", "description": "Mistral Devstral 2512 - Expert code generation model via OpenRouter (Default)", "supports_images": False}, {"name": "GLM-4.6V 👁️", "id": "zai-org/GLM-4.6V:zai-org", "description": "GLM-4.6V vision model - supports image uploads for visual understanding", "supports_images": True}, {"name": "DeepSeek V3.2", "id": "deepseek-ai/DeepSeek-V3.2-Exp", "description": "DeepSeek V3.2 Experimental - Fast model for code generation via HuggingFace Router with Novita provider", "supports_images": False}, {"name": "DeepSeek R1", "id": "deepseek-ai/DeepSeek-R1-0528", "description": "DeepSeek R1 model for code generation", "supports_images": False},739 unmodified lines if is_mistral_model(selected_model_id): print(f"[Generate] Using Mistral SDK for {selected_model_id}") # devstral-medium-2512 uses the beta Conversations API if selected_model_id == "devstral-medium-2512": # Convert messages to inputs format for Conversations API # Extract system instruction from messages instructions = "" inputs = [] for msg in messages: if msg["role"] == "system": instructions = msg["content"] else: inputs.append({ "role": msg["role"], "content": msg["content"] }) # Use beta Conversations API response = client.beta.conversations.start( inputs=inputs, model=actual_model_id, instructions=instructions, completion_args={ "temperature": 0.7, "max_tokens": 10000, "top_p": 1 }, tools=[], ) # For non-streaming response, yield the complete content # Note: Conversations API might not support streaming in the same way # We'll yield the complete response as chunks for consistency full_response = str(response) generated_code = full_response # Yield in chunks to maintain consistency with streaming API chunk_size = 100 for i in range(0, len(full_response), chunk_size): chunk_content = full_response[i:i+chunk_size] event_data = json.dumps({ "type": "chunk", "content": chunk_content }) yield f"data: {event_data}\\n\\n" await asyncio.sleep(0) # Skip the normal streaming loop stream = None else: # Other Mistral models use the standard chat.stream API stream = client.chat.stream( model=actual_model_id, messages=messages, max_tokens=10000 ) # Mistral models use the standard chat.stream API stream = client.chat.stream( model=actual_model_id, messages=messages, max_tokens=10000 ) # All other models use OpenAI-compatible API148 unmodified lines14915015115215215315415515615715815916016116216316416516690 unmodified lines25725825926026126224926326426526674 unmodified lines341342343330344345148 unmodified lines base_url="https://api.stepfun.com/v1" ) elif model_id == "codestral-2508" or model_id == "mistral-medium-2508" or model_id == "devstral-medium-2512": elif model_id == "devstral-medium-2512": # Use OpenRouter for Devstral return OpenAI( base_url="https://openrouter.ai/api/v1", api_key=os.getenv("OPENROUTER_API_KEY"), default_headers={ "HTTP-Referer": "https://anycoder.app", "X-Title": "AnyCoder", } ) elif model_id == "codestral-2508" or model_id == "mistral-medium-2508": # Use Mistral client for Mistral models return Mistral(api_key=os.getenv("MISTRAL_API_KEY")) 90 unmodified lines
def get_real_model_id(model_id: str) -> str: """Get the real model ID with provider suffixes if needed""" if model_id == "devstral-medium-2512": return "mistralai/devstral-2512:free" if model_id == "stealth-model-1": elif model_id == "stealth-model-1": # Get the real model ID from environment variable real_model_id = os.getenv("STEALTH_MODEL_1_ID") if not real_model_id:74 unmodified lines
def is_mistral_model(model_id: str) -> bool: """Check if model uses Mistral SDK""" return model_id in ["codestral-2508", "mistral-medium-2508", "devstral-medium-2512"] return model_id in ["codestral-2508", "mistral-medium-2508"]
backend_api.py+7-55
backend_models.py+17-3