-
Notifications
You must be signed in to change notification settings - Fork 2
add CLI file input support for FILE_ANALYZER tool when running locally #172
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 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,17 @@ | ||
| import os | ||
| import shutil | ||
| import argparse | ||
| from typing import Optional | ||
| import configparser | ||
| from typing import Optional, List | ||
| from pathlib import Path | ||
|
|
||
| from dotenv import load_dotenv | ||
| from langsmith import Client | ||
| from pathlib import Path | ||
| from langchain_community.chat_models import ChatOpenAI, ChatLiteLLM | ||
|
|
||
| from app.core.workflow.langraph_workflow import create_workflow, process_workflow | ||
| from app.core.session import create_user_session, initialize_session_context | ||
| from app.core.utils import IntRange, setup_logger | ||
| import configparser | ||
| from langchain_community.chat_models import ChatOpenAI, ChatLiteLLM | ||
| from app.core.questions import standard_questions | ||
|
|
||
|
|
||
|
|
@@ -198,52 +202,117 @@ def langsmith_setup() -> Optional[Client]: | |
| return None | ||
|
|
||
|
|
||
| def _prepare_session_files(session_id: str, file_paths: List[str]) -> Path: | ||
| """ | ||
| Copy user-supplied local files into the session's input directory so that | ||
| the FILE_ANALYZER tool can discover them at runtime. | ||
|
|
||
| Args: | ||
| session_id: Active session identifier. | ||
| file_paths: List of local file paths provided via the CLI. | ||
|
|
||
| Returns: | ||
| Path to the session input directory. | ||
|
|
||
| Raises: | ||
| FileNotFoundError: If any of the supplied paths do not exist. | ||
| """ | ||
| input_dir = create_user_session(session_id, input_dir=True) | ||
|
|
||
| for raw_path in file_paths: | ||
| src = Path(raw_path).resolve() | ||
| if not src.exists(): | ||
| raise FileNotFoundError(f"File not found: {src}") | ||
| dest = input_dir / src.name | ||
| shutil.copy2(str(src), str(dest)) | ||
| logger.info(f"Copied '{src}' -> '{dest}'") | ||
|
qodo-code-review[bot] marked this conversation as resolved.
|
||
|
|
||
| return input_dir | ||
|
|
||
|
|
||
| def main(): | ||
| """Main function to run the workflow.""" | ||
| # Define command line arguments | ||
|
|
||
| parser = argparse.ArgumentParser(description="Process a workflow with a predefined question number.") | ||
| parser.add_argument('-q', '--question', type=int, choices=IntRange(1, len(standard_questions)), | ||
| help=f"Choose a standard question number from 1 to {len(standard_questions)}.") | ||
| parser.add_argument('-c', '--custom', type=str, | ||
| help="Provide a custom question.") | ||
| parser.add_argument('-e', '--evaluation', action='store_true', | ||
| help="Enable evaluation mode") | ||
| parser.add_argument('--api-key', type=str, | ||
| help="OpenAI API key (optional, defaults to environment variable)") | ||
| parser.add_argument('--endpoint', type=str, | ||
| help="Knowledge graph endpoint URL (optional)") | ||
| """ | ||
| CLI entry-point for running the MetaboT workflow. | ||
|
|
||
| Usage examples: | ||
| python -m app.core.main -q 1 | ||
| python -m app.core.main -c "Describe my dataset" -f data.csv | ||
| python -m app.core.main -c "Compare files" -f file1.csv file2.tsv | ||
| """ | ||
| parser = argparse.ArgumentParser( | ||
| description="Process a workflow with a predefined question number." | ||
| ) | ||
| parser.add_argument( | ||
| '-q', '--question', type=int, | ||
| choices=IntRange(1, len(standard_questions)), | ||
| help=f"Choose a standard question number from 1 to {len(standard_questions)}.", | ||
| ) | ||
| parser.add_argument( | ||
| '-c', '--custom', type=str, | ||
| help="Provide a custom question.", | ||
| ) | ||
| parser.add_argument( | ||
| '-f', '--file', type=str, nargs='+', | ||
| help="One or more local file paths to make available for the FILE_ANALYZER tool.", | ||
| ) | ||
| parser.add_argument( | ||
| '-e', '--evaluation', action='store_true', | ||
| help="Enable evaluation mode.", | ||
| ) | ||
| parser.add_argument( | ||
| '--api-key', type=str, | ||
| help="OpenAI API key (optional, defaults to environment variable).", | ||
| ) | ||
| parser.add_argument( | ||
| '--endpoint', type=str, | ||
| help="Knowledge graph endpoint URL (optional).", | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| # Resolve the question | ||
| if args.question: | ||
| question = standard_questions[args.question - 1] | ||
| elif args.custom: | ||
| question = args.custom | ||
| else: | ||
| print("You must provide either a standard question number or a custom question.") | ||
| print("You must provide either a standard question number (-q) or a custom question (-c).") | ||
| return | ||
|
|
||
| # Initialize LangSmith if available | ||
| langsmith_client = langsmith_setup() | ||
| langsmith_setup() | ||
|
|
||
| # Get endpoint URL from arguments or environment | ||
| # Resolve endpoint URL | ||
| endpoint_url = ( | ||
| args.endpoint | ||
| or os.environ.get("KG_ENDPOINT_URL") | ||
| or "https://enpkg.commons-lab.org/graphdb/repositories/ENPKG" | ||
| ) | ||
|
|
||
| # Initialize language models | ||
| models = llm_creation() | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3. Api key cli not applied The CLI accepts --api-key but does not pass it to llm_creation(), so model initialization may still rely on environment variables and fail even when the user supplies a key. Agent Prompt
|
||
| # Create a user session (mirrors the Streamlit session lifecycle) | ||
| session_id = create_user_session() | ||
| initialize_session_context(session_id) | ||
|
|
||
| # Stage user-provided files into the session's input directory | ||
| if args.file: | ||
| try: | ||
| _prepare_session_files(session_id, args.file) | ||
| except FileNotFoundError as exc: | ||
| logger.error(str(exc)) | ||
| print(f"Error: {exc}") | ||
| return | ||
|
|
||
| try: | ||
| # Create and process workflow | ||
| workflow = create_workflow( | ||
| models=models, | ||
| session_id=session_id, | ||
| endpoint_url=endpoint_url, | ||
| evaluation=False, | ||
| api_key=args.api_key | ||
| api_key=args.api_key, | ||
| ) | ||
|
|
||
| process_workflow(workflow, question) | ||
|
|
||
| except Exception as e: | ||
|
|
||
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.