Repository Public
1# =============================================================================2# SP BOUCHER | INFRASTRUCTURE & AUTOMATION3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# Project : MacLustr LLM API7# File : src/maclustr_llm/schemas.py8# Description : Schémas de requêtes de l'API (compatibles OpenAI).9# Module : maclustr_llm10# Repository : https://git.spboucher.ai/maclustr-llm11# Created : 2026-11-1912# Last Modified : 2026-11-1913#14# Author : Simon-Pierre Boucher15# Position : Professor of Finance16# Institution : Université du Québec en Outaouais (UQO)17# Organization : SP Boucher18# Website : https://spboucher.ai19# Git : https://git.spboucher.ai20#21# Preferred Contact: admin@spboucher.ai22# Technical Contact: dev@spboucher.ai23# Security/Admin : admin@spboucher.ai24#25# Copyright (c) 202626# Simon-Pierre Boucher27# All rights reserved where applicable. This file is governed by the28# applicable project license, copyright notices and relevant law.29# Existing third-party rights and notices remain unaffected.30#31# License : None specified (no license file in this repository)32#33# Do not embed credentials, private keys, access tokens,34# certificates or confidential personal information.35#36# =============================================================================37# END OF SP BOUCHER SOURCE HEADER38# =============================================================================3940"""Schémas de requêtes de l'API (compatibles OpenAI). Les champs inconnus sont ignorés, comme chez OpenAI,41sauf ceux qui changeraient le sens de la requête et ne sont pas pris en charge (rejet explicite)."""4243from __future__ import annotations4445from typing import Any, Literal4647from pydantic import BaseModel, ConfigDict, Field, field_validator484950class FunctionDef(BaseModel):51 model_config = ConfigDict(extra="ignore")52 name: str = Field(min_length=1, max_length=64, pattern=r"^[a-zA-Z0-9_.-]+$")53 description: str | None = None54 parameters: dict[str, Any] | None = None55 strict: bool | None = None565758class Tool(BaseModel):59 model_config = ConfigDict(extra="ignore")60 type: Literal["function"]61 function: FunctionDef626364class ToolCallFunction(BaseModel):65 model_config = ConfigDict(extra="ignore")66 name: str67 arguments: str686970class ToolCall(BaseModel):71 model_config = ConfigDict(extra="ignore")72 id: str | None = None73 type: Literal["function"] = "function"74 function: ToolCallFunction757677class Message(BaseModel):78 model_config = ConfigDict(extra="ignore")79 role: Literal["system", "developer", "user", "assistant", "tool"]80 content: str | list[dict[str, Any]] | None = None81 name: str | None = None82 tool_calls: list[ToolCall] | None = None83 tool_call_id: str | None = None8485 @field_validator("content")86 @classmethod87 def _flatten(cls, v: Any) -> Any:88 if isinstance(v, list):89 parts: list[str] = []90 for part in v:91 kind = part.get("type") if isinstance(part, dict) else None92 if kind not in ("text", "input_text", "output_text"):93 raise ValueError(f"type de contenu non pris en charge : {kind!r} (texte uniquement)")94 parts.append(str(part.get("text", "")))95 return "".join(parts)96 return v979899class JsonSchemaSpec(BaseModel):100 model_config = ConfigDict(extra="ignore", populate_by_name=True)101 name: str = "response"102 description: str | None = None103 schema_: dict[str, Any] | None = Field(default=None, alias="schema")104 strict: bool | None = None105106107class ResponseFormat(BaseModel):108 model_config = ConfigDict(extra="ignore")109 type: Literal["text", "json_object", "json_schema"]110 json_schema: JsonSchemaSpec | None = None111112113class StreamOptions(BaseModel):114 model_config = ConfigDict(extra="ignore")115 include_usage: bool = False116117118class _Sampling(BaseModel):119 model_config = ConfigDict(extra="ignore")120 model: str = Field(min_length=1, max_length=128)121 stream: bool = False122 stream_options: StreamOptions | None = None123 max_tokens: int | None = Field(default=None, ge=1)124 temperature: float | None = Field(default=None, ge=0, le=2)125 top_p: float | None = Field(default=None, gt=0, le=1)126 top_k: int | None = Field(default=None, ge=0, le=1000)127 min_p: float | None = Field(default=None, ge=0, le=1)128 presence_penalty: float | None = Field(default=None, ge=-2, le=2)129 frequency_penalty: float | None = Field(default=None, ge=-2, le=2)130 repetition_penalty: float | None = Field(default=None, ge=0, le=2)131 stop: str | list[str] | None = None132 seed: int | None = None133 n: int | None = Field(default=None, ge=1, le=1)134 user: str | None = None135136 @field_validator("stop")137 @classmethod138 def _stop(cls, v: Any) -> Any:139 if isinstance(v, list):140 if len(v) > 8:141 raise ValueError("au plus 8 séquences d'arrêt")142 if any(not isinstance(s, str) or not s or len(s) > 128 for s in v):143 raise ValueError("séquences d'arrêt : chaînes non vides de 128 caractères au plus")144 return v145146147class ChatRequest(_Sampling):148 messages: list[Message] = Field(min_length=1)149 max_completion_tokens: int | None = Field(default=None, ge=1)150 tools: list[Tool] | None = Field(default=None, max_length=128)151 tool_choice: str | dict[str, Any] | None = None152 response_format: ResponseFormat | None = None153 enable_thinking: bool | None = None154 reasoning_effort: str | None = None155 logprobs: bool | None = None156 top_logprobs: int | None = Field(default=None, ge=0, le=10)157158159class CompletionRequest(_Sampling):160 prompt: str | list[str]161162 @field_validator("prompt")163 @classmethod164 def _prompt(cls, v: Any) -> Any:165 if isinstance(v, list):166 if len(v) != 1:167 raise ValueError("un seul prompt par requête")168 return v[0]169 return v170