Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 10
Integrate Config Management with Unified Api#447
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b2e05cb
Support config id + version in unified api
avirajsingh7 bfcdd26
Refactor config blob handling
avirajsingh7 ff5d3d1
fix test of config crud
avirajsingh7 9884fff
fix unit test for config
avirajsingh7 82707a2
Fix test llm module
avirajsingh7 07aa683
doc imporve
avirajsingh7 63b3cc1
rename callback to callback_response might be confusing
avirajsingh7 3715671
Enhance LLMCallRequest docstring for clarity on config usage; refacto…
avirajsingh7 591f202
Improve error logging in execute_job function for clarity on unknown …
avirajsingh7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,7 @@ | ||
| from app.models.llm.request import LLMCallRequest, CompletionConfig, QueryParams | ||
| from app.models.llm.request import ( | ||
| LLMCallRequest, | ||
| CompletionConfig, | ||
| QueryParams, | ||
| ConfigBlob, | ||
| ) | ||
| from app.models.llm.response import LLMCallResponse, LLMResponse, LLMOutput, Usage |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -6,8 +6,10 @@ | ||
| from sqlmodel import Session | ||
| from app.core.db import engine | ||
| from app.crud.config import ConfigVersionCrud | ||
| from app.crud.jobs import JobCrud | ||
| from app.models import JobStatus, JobType, JobUpdate, LLMCallRequest, LLMCallResponse | ||
| from app.models import JobStatus, JobType, JobUpdate, LLMCallRequest | ||
| from app.models.llm.request import ConfigBlob, LLMCallConfig | ||
| from app.utils import APIResponse, send_callback | ||
| from app.celery.utils import start_high_priority_job | ||
| from app.services.llm.providers.registry import get_llm_provider | ||
| @@ -76,6 +78,41 @@ def handle_job_error( | ||
| return callback_response.model_dump() | ||
| def resolve_config_blob( | ||
| config_crud: ConfigVersionCrud, config: LLMCallConfig | ||
| ) -> tuple[ConfigBlob | None, str | None]: | ||
| """Fetch and parse stored config version into ConfigBlob. | ||
| Returns: | ||
| (config_blob, error_message) | ||
| - config_blob: ConfigBlob if successful, else None | ||
| - error_message: human-safe error string if an error occurs, else None | ||
| """ | ||
| try: | ||
| config_version = config_crud.exists_or_raise(version_number=config.version) | ||
| except HTTPException as e: | ||
| return None, f"Failed to retrieve stored configuration: {e.detail}" | ||
| except Exception: | ||
| logger.error( | ||
| f"[resolve_config_blob] Unexpected error retrieving config version | " | ||
| f"config_id={config.id}, version={config.version}", | ||
| exc_info=True, | ||
| ) | ||
| return None, "Unexpected error occurred while retrieving stored configuration" | ||
| try: | ||
| return ConfigBlob(**config_version.config_blob), None | ||
| except (TypeError, ValueError) as e: | ||
| return None, f"Stored configuration blob is invalid: {str(e)}" | ||
nishika26 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| except Exception: | ||
| logger.error( | ||
| f"[resolve_config_blob] Unexpected error parsing config blob | " | ||
| f"config_id={config.id}, version={config.version}", | ||
| exc_info=True, | ||
| ) | ||
| return None, "Unexpected error occurred while parsing stored configuration" | ||
| def execute_job( | ||
| request_data: dict, | ||
| project_id: int, | ||
| @@ -93,53 +130,72 @@ def execute_job( | ||
| request = LLMCallRequest(**request_data) | ||
| job_id: UUID = UUID(job_id) | ||
| # one of (id, version) or blob is guaranteed to be present due to prior validation | ||
| config = request.config | ||
| provider = config.completion.provider | ||
| callback = None | ||
| callback_response = None | ||
| config_blob: ConfigBlob | None = None | ||
| logger.info( | ||
| f"[execute_job] Starting LLM job execution | job_id={job_id}, task_id={task_id}, " | ||
| f"provider={provider}" | ||
| ) | ||
| try: | ||
| # Update job status to PROCESSING | ||
| with Session(engine) as session: | ||
| # Update job status to PROCESSING | ||
| job_crud = JobCrud(session=session) | ||
| job_crud.update( | ||
| job_id=job_id, job_update=JobUpdate(status=JobStatus.PROCESSING) | ||
| ) | ||
| # if stored config, fetch blob from DB | ||
| if config.is_stored_config: | ||
| config_crud = ConfigVersionCrud( | ||
| session=session, project_id=project_id, config_id=config.id | ||
| ) | ||
| # blob is dynamic, need to resolve to ConfigBlob format | ||
| config_blob, error = resolve_config_blob(config_crud, config) | ||
| if error: | ||
| callback_response = APIResponse.failure_response( | ||
| error=error, | ||
| metadata=request.request_metadata, | ||
| ) | ||
| return handle_job_error( | ||
| job_id, request.callback_url, callback_response | ||
| ) | ||
| else: | ||
| config_blob = config.blob | ||
| try: | ||
| provider_instance = get_llm_provider( | ||
| session=session, | ||
| provider_type=provider, | ||
| provider_type=config_blob.completion.provider, | ||
| project_id=project_id, | ||
| organization_id=organization_id, | ||
| ) | ||
| except ValueError as ve: | ||
| callback = APIResponse.failure_response( | ||
| callback_response = APIResponse.failure_response( | ||
| error=str(ve), | ||
| metadata=request.request_metadata, | ||
| ) | ||
| if callback: | ||
| return handle_job_error(job_id, request.callback_url, callback) | ||
| return handle_job_error(job_id, request.callback_url, callback_response) | ||
| response, error = provider_instance.execute( | ||
| completion_config=config.completion, | ||
| completion_config=config_blob.completion, | ||
| query=request.query, | ||
| include_provider_raw_response=request.include_provider_raw_response, | ||
| ) | ||
| if response: | ||
| callback = APIResponse.success_response( | ||
| callback_response = APIResponse.success_response( | ||
| data=response, metadata=request.request_metadata | ||
| ) | ||
| if request.callback_url: | ||
| send_callback( | ||
| callback_url=request.callback_url, | ||
| data=callback.model_dump(), | ||
| data=callback_response.model_dump(), | ||
| ) | ||
| with Session(engine) as session: | ||
| @@ -152,21 +208,21 @@ def execute_job( | ||
| f"[execute_job] Successfully completed LLM job | job_id={job_id}, " | ||
| f"provider_response_id={response.response.provider_response_id}, tokens={response.usage.total_tokens}" | ||
| ) | ||
| return callback.model_dump() | ||
| return callback_response.model_dump() | ||
| callback = APIResponse.failure_response( | ||
| callback_response = APIResponse.failure_response( | ||
| error=error or "Unknown error occurred", | ||
| metadata=request.request_metadata, | ||
| ) | ||
| return handle_job_error(job_id, request.callback_url, callback) | ||
| return handle_job_error(job_id, request.callback_url, callback_response) | ||
| except Exception as e: | ||
| callback = APIResponse.failure_response( | ||
| callback_response = APIResponse.failure_response( | ||
| error=f"Unexpected error occurred", | ||
| metadata=request.request_metadata, | ||
| ) | ||
| logger.error( | ||
| f"[execute_job] {callback.error} {str(e)} | job_id={job_id}, task_id={task_id}", | ||
| f"[execute_job] Unknown error occurred: {str(e)} | job_id={job_id}, task_id={task_id}", | ||
| exc_info=True, | ||
| ) | ||
| return handle_job_error(job_id, request.callback_url, callback) | ||
| return handle_job_error(job_id, request.callback_url, callback_response) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.