Second-Me/lpm_kernel/api/services/user_llm_config_service.py
ryangyuan 9fe511f0f2
Feature/0416/add thinking mode (#264)
* fix: modify thinking_model loading configuration

* feat: realize thinkModel ui

* feat:store

* feat: add combined_llm_config_dto

* add thinking_model_config & database migration

* directly add thinking model to user_llm_config

* delete thinking model repo dto service

* delete thinkingmodel table migration

* add is_cot config

* feat: allow define  is_cot

* feat: simplify logs info

* feat: add training model

* feat: fix is_cot problem

* fix: fix chat message

* fix: fix progress error

* fix: disable no settings thinking

* feat: add thinking warning

* fix: fix start service error

* feat:fix init trainparams problem

* feat: change playGround prompt

* feat: Add Dimension Mismatch Handling for ChromaDB (#157) (#207)

* Fix Issue #157

Add chroma_utils.py to manage chromaDB and added docs for explanation

* Add logging and debugging process

- Enhanced the`reinitialize_chroma_collections` function in`chroma_utils.py` to properly check if collections exist before attempting to delete them, preventing potential errors when collections don't exist.
- Improved error handling in the`_handle_dimension_mismatch` method in`embedding_service.py` by adding more robust exception handling and verification steps after reinitialization.
- Enhanced the collection initialization process in`embedding_service.py` to provide more detailed error messages and better handle cases where collections still have incorrect dimensions after reinitialization.
- Added additional verification steps to ensure that collection dimensions match the expected dimension after creation or retrieval.
- Improved logging throughout the code to provide more context in error messages, making debugging easier.

* Change topics_generator timeout to 30 (#263)

* quick fix

* fix: shade -> shade_merge_info (#265)

* fix: shade -> shade_merge_info

* add convert array

* quick fix import error

* add log

* add heartbeat

* new strategy

* sse version

* add heartbeat

* zh to en

* optimize code

* quick fix convert function

* Feat/new branch management (#267)

* feat: new branch management

* feat: fix multi-upload

* optimize contribute management

---------

Co-authored-by: Crabboss Mr <1123357821@qq.com>
Co-authored-by: Ye Xiangle <yexiangle@mail.mindverse.ai>
Co-authored-by: Xinghan Pan <sampan090611@gmail.com>
Co-authored-by: doubleBlack2 <108928143+doubleBlack2@users.noreply.github.com>
Co-authored-by: kevin-mindverse <kevin@mindverse.ai>
Co-authored-by: KKKKKKKevin <115385420+kevin-mindverse@users.noreply.github.com>
2025-04-24 14:19:23 +08:00

81 lines
2.9 KiB
Python

from typing import Optional
from lpm_kernel.api.repositories.user_llm_config_repository import UserLLMConfigRepository
from lpm_kernel.api.dto.user_llm_config_dto import (
UserLLMConfigDTO,
UpdateUserLLMConfigDTO
)
from datetime import datetime
class UserLLMConfigService:
"""User LLM Configuration Service"""
def __init__(self):
self.repository = UserLLMConfigRepository()
def get_available_llm(self) -> Optional[UserLLMConfigDTO]:
"""Get available LLM configuration
Since we only have one default configuration now (ID=1), just return it
"""
return self.repository.get_default_config()
def update_config(
self,
config_id: int,
dto: UpdateUserLLMConfigDTO
) -> UserLLMConfigDTO:
"""Update configuration or create if not exists
This method ensures that only one configuration record exists in the database.
If the configuration with the given ID doesn't exist, it will be created.
Args:
config_id: Configuration ID (should be 1)
dto: UpdateUserLLMConfigDTO object
Returns:
Updated or created configuration
"""
# Check if we need to clean up extra records
self._ensure_single_record()
# Update or create the configuration
return self.repository.update(config_id, dto)
def delete_key(self, config_id: int = 1) -> Optional[UserLLMConfigDTO]:
"""Delete API key from the configuration
This method removes the API key and related fields from the configuration.
Args:
config_id: Configuration ID (default is 1)
Returns:
Updated configuration with key removed
"""
# Check if we need to clean up extra records
self._ensure_single_record()
# Get the current configuration
config = self.repository.get_default_config()
if not config:
# If no configuration exists, return None
return None
# delete
return self.repository.delete(config_id)
def _ensure_single_record(self):
"""Ensure that only one configuration record exists in the database"""
# This is a safety measure to ensure we only have one record
# In normal operation, this should never be needed
count = self.repository.count()
if count != 1:
# If we have more than one record, we need to clean up
# This is a rare case that should not happen in normal operation
# Implementation would depend on how we want to handle this case
# For now, we'll just log a warning
from lpm_kernel.common.logging import logger
logger.warning(f"Found {count} LLM configurations in the database. Only one should exist.")
# Future implementation could delete extra records