Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 93 additions & 24 deletions app/core/main.py
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


Expand Down Expand Up @@ -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}'")
Comment thread
qodo-code-review[bot] marked this conversation as resolved.
Comment thread
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()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

3. Api key cli not applied 🐞 Bug ☼ Reliability

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
### Issue description
`--api-key` is parsed but not used when creating the LLM instances via `llm_creation()`. This can break local CLI usage when the environment variables are not set.

### Issue Context
`llm_creation(api_key=None, ...)` is designed to accept an explicit key. The CLI already exposes `--api-key`.

### Fix Focus Areas
- app/core/main.py[262-265]
- app/core/main.py[292-294]

### Suggested fix
Change `models = llm_creation()` to `models = llm_creation(api_key=args.api_key)` (or equivalent provider-aware wiring) so the CLI-provided key is honored during model creation.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

# 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:
Expand Down
Loading