feat(docs): enhance LLM model configs API documentation with schema updates

- Add `model_type` field to database schema with supported values (text, vision, stt, tts, multimodal, embedding, reranking, other)
- Add `name` field as table-level column for indexing and filtering
- Add reserved JSONB fields for future extensibility
- Update API request/response schemas to include `model_type` and `name` fields
- Add new `LLMModelConfigWithSource` schema for detailed configuration responses
- Update usage examples to reflect new required fields
- Improve database constraints and indexing documentation
This commit is contained in:
YueGuobin committed 2026-03-03 17:54:30 +08:00
1 parent b968920a75
commit d42fff49a5
6 files changed
+272 -58

No files matched your search

+16 -4
View File
@@ -15,7 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from sqlalchemy import Column, Boolean, ForeignKey, CheckConstraint, UniqueConstraint, Index, Integer
from sqlalchemy import Column, Boolean, ForeignKey, CheckConstraint, UniqueConstraint, Index, Integer, String
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import relationship
@@ -35,12 +35,19 @@ class LLMModelConfig(BaseTable):
__tablename__ = "llm_model_configs"
config_id = Column(GUID, primary_key=True, default=generate_uuid)
config = Column(JSONB, nullable=False) # All config fields including name, provider, etc.
name = Column(String(100), nullable=False) # Configuration name (table-level for indexing)
model_type = Column(String(50), nullable=False) # Model type: text, vision, stt, tts, multimodal, etc.
config = Column(JSONB, nullable=False) # Config fields: provider, base_url, model, temperature, api_key, etc.
user_id = Column(GUID, ForeignKey("users.user_id", ondelete="CASCADE"), nullable=True)
group_id = Column(GUID, ForeignKey("user_groups.user_group_id", ondelete="CASCADE"), nullable=True)
is_default = Column(Boolean, default=False, nullable=False)
version = Column(Integer, default=0, nullable=False) # Optimistic locking version
# Reserved fields for future use (currently unused in code)
reserved_jsonb_1 = Column(JSONB, nullable=True)
reserved_jsonb_2 = Column(JSONB, nullable=True)
reserved_jsonb_3 = Column(JSONB, nullable=True)
# Relationships
user = relationship("User", backref="llm_model_configs")
group = relationship("UserGroup", backref="llm_model_configs")
@@ -53,6 +60,11 @@ class LLMModelConfig(BaseTable):
"(user_id IS NULL AND group_id IS NOT NULL)",
name="single_owner_check"
),
# Validate model_type values
CheckConstraint(
"model_type IN ('text', 'vision', 'stt', 'tts', 'multimodal', 'embedding', 'reranking', 'other')",
name="valid_model_type_check"
),
# Each user can have at most one default config
UniqueConstraint("user_id", "is_default", name="unique_user_default",
deferrable=True, initially="deferred",
@@ -64,10 +76,10 @@ class LLMModelConfig(BaseTable):
# Indexes for efficient queries
Index("idx_llm_model_configs_user_id", "user_id"),
Index("idx_llm_model_configs_group_id", "group_id"),
Index("idx_llm_model_configs_model_type", "model_type"),
Index("idx_llm_model_configs_config", "config", postgresql_using="gin"),
)
def __repr__(self):
config_name = self.config.get("name", "unnamed") if self.config else "unnamed"
owner = f"user_{self.user_id}" if self.user_id else f"group_{self.group_id}"
return f"<LLMModelConfig {config_name} for {owner} (default={self.is_default}, version={self.version})>"
return f"<LLMModelConfig {self.name} ({self.model_type}) for {owner} (default={self.is_default}, version={self.version})>"
+68 -26
View File
@@ -68,6 +68,8 @@ class LLMModelConfigsRepository(BaseRepository):
async def create_user_config(
self,
user_id: UUID,
name: str,
model_type: str,
config_data: Dict[str, Any],
is_default: bool = False
) -> models.LLMModelConfig:
@@ -83,6 +85,8 @@ class LLMModelConfigsRepository(BaseRepository):
raise
db_config = models.LLMModelConfig(
name=name,
model_type=model_type,
config=config_to_store,
user_id=user_id,
is_default=is_default
@@ -130,21 +134,36 @@ class LLMModelConfigsRepository(BaseRepository):
# Encrypt API key if present in updates
from gns3server.utils.encryption import encrypt
updates_copy = updates.copy()
if "api_key" in updates_copy and updates_copy["api_key"]:
try:
updates_copy["api_key"] = encrypt(updates_copy["api_key"])
except Exception as e:
log.error(f"Failed to encrypt API key: {e}")
raise
# Update config JSONB fields
# Table-level fields
if "name" in updates and updates["name"] is not None:
db_config.name = updates["name"]
if "model_type" in updates and updates["model_type"] is not None:
db_config.model_type = updates["model_type"]
if "is_default" in updates and updates["is_default"] is not None:
db_config.is_default = updates["is_default"]
# Config JSONB fields
config_fields = ["provider", "base_url", "model", "temperature", "api_key", "max_tokens"]
current_config = db_config.config.copy()
for key, value in updates_copy.items():
if key == "is_default":
db_config.is_default = value
elif value is not None:
current_config[key] = value
for field in config_fields:
if field in updates and updates[field] is not None:
if field == "api_key":
# Encrypt API key
try:
current_config[field] = encrypt(updates[field])
except Exception as e:
log.error(f"Failed to encrypt API key: {e}")
raise
else:
current_config[field] = updates[field]
# Handle extra config fields
for key, value in updates.items():
if key not in ["name", "model_type", "is_default", "expected_version"] + config_fields:
if value is not None:
current_config[key] = value
db_config.config = current_config
# Increment version for optimistic locking
@@ -225,6 +244,8 @@ class LLMModelConfigsRepository(BaseRepository):
async def create_group_config(
self,
group_id: UUID,
name: str,
model_type: str,
config_data: Dict[str, Any],
is_default: bool = False
) -> models.LLMModelConfig:
@@ -240,6 +261,8 @@ class LLMModelConfigsRepository(BaseRepository):
raise
db_config = models.LLMModelConfig(
name=name,
model_type=model_type,
config=config_to_store,
group_id=group_id,
is_default=is_default
@@ -287,21 +310,36 @@ class LLMModelConfigsRepository(BaseRepository):
# Encrypt API key if present in updates
from gns3server.utils.encryption import encrypt
updates_copy = updates.copy()
if "api_key" in updates_copy and updates_copy["api_key"]:
try:
updates_copy["api_key"] = encrypt(updates_copy["api_key"])
except Exception as e:
log.error(f"Failed to encrypt API key: {e}")
raise
# Update config JSONB fields
# Table-level fields
if "name" in updates and updates["name"] is not None:
db_config.name = updates["name"]
if "model_type" in updates and updates["model_type"] is not None:
db_config.model_type = updates["model_type"]
if "is_default" in updates and updates["is_default"] is not None:
db_config.is_default = updates["is_default"]
# Config JSONB fields
config_fields = ["provider", "base_url", "model", "temperature", "api_key", "max_tokens"]
current_config = db_config.config.copy()
for key, value in updates_copy.items():
if key == "is_default":
db_config.is_default = value
elif value is not None:
current_config[key] = value
for field in config_fields:
if field in updates and updates[field] is not None:
if field == "api_key":
# Encrypt API key
try:
current_config[field] = encrypt(updates[field])
except Exception as e:
log.error(f"Failed to encrypt API key: {e}")
raise
else:
current_config[field] = updates[field]
# Handle extra config fields
for key, value in updates.items():
if key not in ["name", "model_type", "is_default", "expected_version"] + config_fields:
if value is not None:
current_config[key] = value
db_config.config = current_config
# Increment version for optimistic locking
@@ -392,6 +430,8 @@ class LLMModelConfigsRepository(BaseRepository):
configs_with_source.append({
"config_id": config.config_id,
"name": config.name,
"model_type": config.model_type,
"source": "user",
"group_name": None,
"is_default": config.is_default,
@@ -416,6 +456,8 @@ class LLMModelConfigsRepository(BaseRepository):
configs_with_source.append({
"config_id": config.config_id,
"name": config.name,
"model_type": config.model_type,
"source": "group",
"group_name": group_names_map[group_id],
"is_default": config.is_default,