generated from teamhide/fastapi-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
store and load model connection config #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cfa52cb
add tinydb and a model config store
kaushik-himself 9b2ed05
use the model config store in the ui adapter
kaushik-himself 195d943
chore: save and list profiles
vinikatyal 169dee4
chore: List past runs , selected profile / config etc
vinikatyal 10622c5
chore: fix build error
vinikatyal fbc8a54
chore: remove print
vinikatyal 16a908c
chore: fix re runs
vinikatyal a485419
chore: allow reseting to defaults
vinikatyal bcea970
fix provider name
kaushik-himself 962f243
fix tests
kaushik-himself 8ed547b
add pytest-asyncio to pyproject.toml
kaushik-himself 398aa88
update README and docs
kaushik-himself File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| from pathlib import Path | ||
| from tinydb import TinyDB, Query | ||
| from typing import List, Dict, Any, Optional | ||
| import uuid | ||
|
|
||
| DB_DIR = Path.home() / ".compliant-llm" | ||
| CONFIG_DB_FILE = DB_DIR / "model_config.json" | ||
|
|
||
| # Ensure the .compliant-llm directory exists | ||
| DB_DIR.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| _db_instance = None | ||
|
|
||
| def _get_table(): | ||
| global _db_instance | ||
| if _db_instance is None: | ||
| _db_instance = TinyDB(CONFIG_DB_FILE) | ||
| return _db_instance.table('model_config') | ||
|
|
||
| def save_config(runner_config_data: Dict[str, Any], profile_name: str | None = None) -> None: | ||
kaushik-himself marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Saves or updates a model configuration profile.""" | ||
| table = _get_table() | ||
|
|
||
| # Ensure 'id' exists, add if not | ||
| if 'id' not in runner_config_data: | ||
| runner_config_data['id'] = str(uuid.uuid4()) | ||
|
|
||
| # Ensure 'past_runs' exists if it's a new config or not present | ||
| if 'past_runs' not in runner_config_data: | ||
| runner_config_data['past_runs'] = [] | ||
|
|
||
| # Add/update profile_name within the document for easier access if needed | ||
| if profile_name is not None: | ||
| runner_config_data['profile_name'] = profile_name | ||
| document_to_store = runner_config_data | ||
|
|
||
| ConfigQuery = Query() | ||
| table.upsert(document_to_store, ConfigQuery.id == runner_config_data['id']) | ||
| print(f"Config '{profile_name}' saved.") | ||
|
|
||
| def get_config(id: str) -> Optional[Dict[str, Any]]: | ||
| """Retrieves a specific model configuration profile.""" | ||
| table = _get_table() | ||
| ConfigQuery = Query() | ||
| return table.get(ConfigQuery.id == id) | ||
|
|
||
| def list_configs() -> List[Dict[str, Any]]: | ||
| """Lists all saved model configuration profiles.""" | ||
| table = _get_table() | ||
| return table.all() | ||
|
|
||
| def delete_config(id: str) -> bool: | ||
| """Deletes a model configuration profile. Returns True if deleted.""" | ||
| table = _get_table() | ||
| ConfigQuery = Query() | ||
| deleted_ids = table.remove(ConfigQuery.id == id) | ||
| return len(deleted_ids) > 0 | ||
|
|
||
| def add_report_to_config(id: str, report_file_path: str) -> bool: | ||
| """Adds a report file path to the 'past_runs' list of a specific config.""" | ||
| table = _get_table() | ||
| ConfigQuery = Query() | ||
| config_doc = table.get(ConfigQuery.id == id) | ||
|
|
||
| if not config_doc: | ||
| print(f"Error: Config profile '{id}' not found.") | ||
| return False | ||
|
|
||
| # Ensure past_runs is a list | ||
| if 'past_runs' not in config_doc or not isinstance(config_doc['past_runs'], list): | ||
| config_doc['past_runs'] = [] | ||
|
|
||
| # Avoid duplicate entries | ||
| if report_file_path not in config_doc['past_runs']: | ||
| config_doc['past_runs'].append(report_file_path) | ||
| table.upsert(config_doc, ConfigQuery.id == id) | ||
| print(f"Report '{report_file_path}' added to config '{id}'.") | ||
| return True | ||
| else: | ||
| print(f"Report '{report_file_path}' already exists in config '{id}'.") | ||
| return False | ||
|
|
||
| def close_db(): | ||
| """Closes the database connection.""" | ||
| global _db_instance | ||
| if _db_instance: | ||
| _db_instance.close() | ||
| _db_instance = None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.