Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /DeepselSystems_deepsel-cms /backend /apps /cms /routers /autocomplete.py
| from fastapi import APIRouter, Depends, HTTPException | |
| from pydantic import BaseModel | |
| from sqlalchemy.orm import Session | |
| from db import get_db | |
| from apps.core.utils.models_pool import models_pool | |
| from ..models.organization import CMSSettingsModel | |
| import requests | |
| import logging | |
| from deepsel.utils.api_router import create_api_router | |
| router = create_api_router("autocomplete", tags=["Autocomplete APIs"]) | |
| logger = logging.getLogger(__name__) | |
| class AutocompleteRequest(BaseModel): | |
| text: str | |
| cursor_position: int | |
| class AutocompleteResponse(BaseModel): | |
| suggestions: list[str] | |
| completion: str = "" | |
| def get_openrouter_client(organization_id: int, db: Session): | |
| """Get OpenRouter client configuration from organization settings""" | |
| try: | |
| # Get organization settings | |
| settings_model = ( | |
| db.query(CMSSettingsModel) | |
| .filter(CMSSettingsModel.id == organization_id) | |
| .first() | |
| ) | |
| if not settings_model: | |
| logger.error( | |
| f"No organization settings found for org_id: {organization_id}" | |
| ) | |
| raise HTTPException( | |
| status_code=404, detail="Organization settings not found" | |
| ) | |
| if not settings_model.openrouter_api_key: | |
| logger.error("No OpenRouter API key configured") | |
| raise HTTPException( | |
| status_code=400, detail="OpenRouter API key not configured" | |
| ) | |
| # Use the relationship like in page.py | |
| autocomplete_model = settings_model.ai_autocomplete_model | |
| if not autocomplete_model: | |
| logger.error("No autocomplete model configured") | |
| raise HTTPException( | |
| status_code=400, detail="AI autocomplete model not configured" | |
| ) | |
| model_string_id = getattr(autocomplete_model, "string_id", None) | |
| if not model_string_id: | |
| model_string_id = getattr( | |
| autocomplete_model, "canonical_slug", "google/gemini-flash-1.5-8b" | |
| ) | |
| logger.warning(f"string_id not found, using fallback: {model_string_id}") | |
| config = { | |
| "api_key": settings_model.openrouter_api_key, | |
| "model": model_string_id, | |
| "organization_id": organization_id, | |
| } | |
| return config | |
| except HTTPException: | |
| raise | |
| except Exception as e: | |
| logger.error(f"Error getting OpenRouter client: {e}") | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def get_autocomplete_suggestions( | |
| request: AutocompleteRequest, | |
| db: Session = Depends(get_db), | |
| organization_id: int = 1, # TODO: Get from auth context | |
| ): | |
| """ | |
| Get AI-powered autocomplete suggestions for text | |
| """ | |
| try: | |
| # Get OpenRouter configuration | |
| config = get_openrouter_client(organization_id, db) | |
| # Extract text before cursor | |
| text_before_cursor = request.text[: request.cursor_position] | |
| # Find the current sentence from the last dot/exclamation/question mark | |
| sentence_delimiters = [".", "!", "?"] | |
| last_sentence_start = 0 | |
| for delimiter in sentence_delimiters: | |
| last_pos = text_before_cursor.rfind(delimiter) | |
| if last_pos > last_sentence_start: | |
| last_sentence_start = last_pos + 1 | |
| current_sentence = text_before_cursor[last_sentence_start:].strip() | |
| # Only proceed if we have a meaningful sentence to complete (at least 3 characters) | |
| if len(current_sentence) < 3: | |
| logger.info("Sentence too short, returning empty suggestions") | |
| return AutocompleteResponse(suggestions=[]) | |
| # Determine if cursor is after a space | |
| text_before_cursor = request.text[: request.cursor_position] | |
| cursor_after_space = ( | |
| text_before_cursor.endswith(" ") and len(text_before_cursor.strip()) > 0 | |
| ) | |
| # Create comprehensive prompt with full context | |
| prompt = f"""You are an AI writing assistant. Complete the current sentence naturally based on the context. | |
| Full text context: | |
| {request.text} | |
| Current sentence fragment to complete: | |
| {current_sentence} | |
| SPACING RULE: | |
| - If cursor is NOT after a space, start completion with a space | |
| - If cursor IS after a space, continue completing without adding space | |
| Current cursor position: {'AFTER SPACE - continue without space' if cursor_after_space else 'NOT AFTER SPACE - start with space'} | |
| Provide only the completion text, nothing else. Be concise and natural. | |
| Completion:""" | |
| # Call OpenRouter API | |
| headers = { | |
| "Authorization": f"Bearer {config['api_key']}", | |
| "Content-Type": "application/json", | |
| "HTTP-Referer": "https://deepsel.com", | |
| "X-Title": "Deepsel CMS Autocomplete", | |
| } | |
| data = { | |
| "model": config["model"], | |
| "messages": [{"role": "user", "content": prompt}], | |
| "max_tokens": 2000, | |
| "temperature": 0.7, | |
| "stop": ["\n", ".", "!", "?"], | |
| } | |
| response = requests.post( | |
| "https://openrouter.ai/api/v1/chat/completions", | |
| headers=headers, | |
| json=data, | |
| timeout=15, | |
| ) | |
| if response.status_code != 200: | |
| logger.error( | |
| f"OpenRouter API request failed with status code: {response.status_code}" | |
| ) | |
| return AutocompleteResponse(suggestions=[]) | |
| try: | |
| result = response.json() | |
| # Extract the completion using the same pattern as other OpenRouter calls | |
| if "choices" in result and len(result["choices"]) > 0: | |
| completion = result["choices"][0]["message"]["content"].strip() | |
| # Clean up the completion - remove any common prefixes | |
| if completion.lower().startswith("completion:"): | |
| completion = completion[11:].strip() | |
| elif completion.lower().startswith("the completion is:"): | |
| completion = completion[18:].strip() | |
| if completion: | |
| # Generate some variations as suggestions | |
| suggestions = [ | |
| completion, | |
| completion + " ", | |
| completion.split()[0] if completion.split() else completion, | |
| ] | |
| # Remove duplicates and empty strings | |
| suggestions = list(set([s for s in suggestions if s])) | |
| return AutocompleteResponse( | |
| suggestions=suggestions[:3], completion=completion | |
| ) | |
| return AutocompleteResponse(suggestions=[]) | |
| except Exception as e: | |
| logger.error(f"Error parsing OpenRouter response: {e}") | |
| return AutocompleteResponse(suggestions=[]) | |
| except requests.exceptions.Timeout: | |
| logger.error("OpenRouter API timeout") | |
| return AutocompleteResponse(suggestions=[]) | |
| except requests.exceptions.RequestException as e: | |
| logger.error(f"OpenRouter API request error: {e}") | |
| return AutocompleteResponse(suggestions=[]) | |
| except Exception as e: | |
| logger.error(f"Error in autocomplete: {e}") | |
| raise HTTPException( | |
| status_code=500, detail="Failed to get autocomplete suggestions" | |
| ) | |
Xet Storage Details
- Size:
- 7.45 kB
- Xet hash:
- 502847b54d2e20dd191732e1e4e0f4483280e73c843ca42204757b1c1267dd12
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.