Skip to content

Prompts

OptimizeRequest

Bases: BaseModel

Schema for prompt optimization request.

Attributes:

  • instruction (str) –

    The user instruction to optimize.

  • connector_ids (List[str]) –

    List of connector IDs to provide context for optimization.

Source code in app/api/v1/endpoints/prompts.py
19
20
21
22
23
24
25
26
27
28
29
30
31
class OptimizeRequest(BaseModel):
    """
    Schema for prompt optimization request.

    Attributes:
        instruction: The user instruction to optimize.
        connector_ids: List of connector IDs to provide context for optimization.
    """

    instruction: str = Field(..., min_length=1, max_length=5000, description="The user instruction to optimize")
    connector_ids: List[str] = Field(
        default_factory=list, description="List of connector IDs to provide context for optimization"
    )

OptimizeResponse

Bases: BaseModel

Schema for prompt optimization response.

Attributes:

  • optimized_instruction (str) –

    The optimized version of the instruction.

Source code in app/api/v1/endpoints/prompts.py
34
35
36
37
38
39
40
41
42
class OptimizeResponse(BaseModel):
    """
    Schema for prompt optimization response.

    Attributes:
        optimized_instruction: The optimized version of the instruction.
    """

    optimized_instruction: str = Field(..., description="The optimized version of the instruction")

optimize_prompt(request, service, current_user) async

Optimizes a user instruction using LLM and context from connectors.

Parameters:

  • request (OptimizeRequest) –

    The optimization request containing instruction and connector IDs.

  • service (Annotated[PromptService, Depends(get_prompt_service)]) –

    The prompt service instance.

  • current_user (Annotated[User, Depends(get_current_user)]) –

    The currently authenticated user.

Returns:

  • OptimizeResponse ( OptimizeResponse ) –

    The response containing the optimized instruction.

Raises:

  • FunctionalError

    If there's a functional error during optimization.

  • TechnicalError

    If there's a technical error during optimization.

Source code in app/api/v1/endpoints/prompts.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
@router.post("/optimize", response_model=OptimizeResponse)
async def optimize_prompt(
    request: OptimizeRequest,
    service: Annotated[PromptService, Depends(get_prompt_service)],
    current_user: Annotated[User, Depends(get_current_user)],  # 🛡️ SECURITY: Require Auth
) -> OptimizeResponse:
    """
    Optimizes a user instruction using LLM and context from connectors.

    Args:
        request: The optimization request containing instruction and connector IDs.
        service: The prompt service instance.
        current_user: The currently authenticated user.

    Returns:
        OptimizeResponse: The response containing the optimized instruction.

    Raises:
        FunctionalError: If there's a functional error during optimization.
        TechnicalError: If there's a technical error during optimization.
    """
    start_time = time.time()
    func_name = "optimize_prompt"
    logger.info(
        "START | %s | User: %s | Params: {'connector_count': %d}",
        func_name,
        current_user.email,
        len(request.connector_ids),
    )

    try:
        # Note: connector_ids currently unused in service but kept for future RAG context injection
        optimized_text = await service.optimize_instruction(request.instruction)

        elapsed = round((time.time() - start_time) * 1000, 2)
        logger.info("FINISH | %s | Status: Success | Duration: %sms", func_name, elapsed)

        return OptimizeResponse(optimized_instruction=optimized_text)

    except (FunctionalError, TechnicalError) as e:
        logger.error("❌ FAIL | %s | Known Error: %s (Code: %s)", func_name, e.message, e.error_code, exc_info=True)
        raise e
    except Exception as e:
        logger.error("❌ FAIL | %s | Error: %s", func_name, str(e), exc_info=True)
        raise TechnicalError(message=f"Failed to optimize prompt: {e}", error_code="PROMPT_OPTIMIZATION_FAILED")