Skip to content

FrontEnd Merge#142

Merged
lfnothias merged 3 commits intodevfrom
dev_frontend_merge
Feb 19, 2025
Merged

FrontEnd Merge#142
lfnothias merged 3 commits intodevfrom
dev_frontend_merge

Conversation

@lucaspradi
Copy link
Contributor

@lucaspradi lucaspradi commented Feb 17, 2025

User description

Added the streamlit functionalities 'streamlit_webapp/*' to the main MetaboT repository.

Modified workflow construction on app/core/workflow to be seamlessly integrated with Streamlit, keeping the python script compatible with the format needed from streamlit while still working on it's own.
Adapted app/core/main.py and app/core/evaluation.py to use the new workflow
Created a new llm handler file at app/core/llm_handler to avoid circular imports in the workflow and main files.


PR Type

enhancement, other


Description

  • Integrate Streamlit functionalities into MetaboT

  • Create new LLM handler to avoid circular imports

  • Update workflow for seamless Streamlit integration

  • Implement database cleanup and file management utilities


Changes walkthrough 📝

Relevant files
Enhancement
10 files
evaluation.py
Update evaluation logic and workflow integration                 
+50/-44 
llm_handler.py
Create LLM handler to manage models                                           
+47/-0   
main.py
Refactor main for Streamlit compatibility                               
+65/-250
langraph_workflow.py
Enhance workflow management and agent handling                     
+114/-110
cleanup_database.py
Implement database cleanup functionality                                 
+18/-0   
cleanup_files.py
Add file cleanup utility for temporary files                         
+44/-0   
postgres_database.py
Manage PostgreSQL database connections                                     
+95/-0   
postgres_tool_database.py
Tool-specific database operations                                               
+76/-0   
streamlit_app.py
Main Streamlit application setup                                                 
+486/-0 
streamlit_utils.py
Utility functions for Streamlit app                                           
+306/-0 
Documentation
1 files
README.md
Update documentation for new features                                       
+114/-0 
Additional files
1 files
__init__.py [link]   

Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • @github-actions
    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🔒 Security concerns

    Sensitive information exposure:
    The code handles API keys and database URLs, which are sensitive information. Ensure these are securely managed and not exposed in logs or error messages. Additionally, consider using environment variables or a secure vault for managing these secrets.

    ⚡ Recommended focus areas for review

    Environment Variable Handling

    The code sets environment variables for LangSmith without checking if they are already set, which could lead to unexpected behavior if these variables are used elsewhere in the application.

    # Set environment variables for LangSmith
    os.environ["LANGCHAIN_TRACING_V2"] = "true"
    os.environ["LANGCHAIN_PROJECT"] = (
        os.environ.get("LANGCHAIN_PROJECT")
        or os.environ.get("LANGSMITH_PROJECT")
        or "MetaboT evaluation"
    )
    os.environ["LANGCHAIN_ENDPOINT"] = (
        os.environ.get("LANGCHAIN_ENDPOINT")
        or os.environ.get("LANGSMITH_ENDPOINT")
        or os.environ.get("LANGSMITH_BASE_URL")
        or "https://api.smith.langchain.com"
    )
    API Key Handling

    The function llm_creation raises a ValueError if the OpenAI API key is not found. Consider providing a more user-friendly error message or handling this scenario more gracefully.

    # Get the OpenAI API key from the argument or environment variables
    openai_api_key = api_key if api_key else os.getenv("OPENAI_API_KEY")
    if not openai_api_key:
        raise ValueError("OpenAI API key not found in environment variables or parameters")
    Hardcoded Path

    The code contains a hardcoded path to a local directory, which may not be portable across different environments or systems.

    sys.path.insert(0, '/Users/yzhouchen001/Desktop/research/kgbot_webapp')

    @github-actions
    Copy link
    Contributor

    github-actions bot commented Feb 17, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Add error handling for workflow invocation

    Ensure that the workflow.invoke method handles exceptions gracefully to prevent the
    application from crashing if an error occurs during message processing.

    app/core/evaluation.py [101-104]

    -response = workflow.invoke(
    -    message,
    -    {"configurable": {"thread_id": thread_id}},
    -)
    +try:
    +    response = workflow.invoke(
    +        message,
    +        {"configurable": {"thread_id": thread_id}},
    +    )
    +except Exception as e:
    +    logger.error(f"Error invoking workflow: {e}")
    +    response = {"error": str(e)}
    Suggestion importance[1-10]: 8

    __

    Why: Adding exception handling around the workflow.invoke method is crucial for preventing the application from crashing due to unhandled exceptions during message processing. This enhances the robustness and reliability of the application.

    Medium
    Add error handling for workflow execution

    Add exception handling around the create_workflow and process_workflow calls to
    ensure that any errors are logged and do not cause the application to terminate
    unexpectedly.

    app/core/main.py [156-162]

    -workflow = create_workflow(
    -    api_key=args.api_key,
    -    endpoint_url=endpoint_url,
    -    evaluation=args.evaluation
    -)
    -process_workflow(workflow, args.question)
    +try:
    +    workflow = create_workflow(
    +        api_key=args.api_key,
    +        endpoint_url=endpoint_url,
    +        evaluation=args.evaluation
    +    )
    +    process_workflow(workflow, args.question)
    +except Exception as e:
    +    logger.error(f"Error in workflow processing: {e}")
    +    raise
    Suggestion importance[1-10]: 8

    __

    Why: Implementing exception handling around the create_workflow and process_workflow calls ensures that any errors are logged and do not cause the application to terminate unexpectedly, improving the application's stability.

    Medium
    Validate configuration file existence

    Validate the configuration file path before attempting to read it to avoid potential
    file not found errors.

    app/core/llm_handler.py [21-23]

     config_path = Path(__file__).resolve().parent.parent / "config" / "params.ini"
    +if not config_path.exists():
    +    raise FileNotFoundError(f"Configuration file not found: {config_path}")
     logger.info(f"Loading configuration from {config_path}")
     config.read(config_path)
    Suggestion importance[1-10]: 7

    __

    Why: Checking for the existence of the configuration file before attempting to read it is a good practice to prevent runtime errors and ensure that the application can handle missing files gracefully.

    Medium
    General
    Validate non-empty API key input

    Implement a check to ensure that the user_provided_key is not empty before
    attempting to validate it, to avoid unnecessary API calls and potential errors.

    streamlit_webapp/streamlit_app.py [158-162]

    -if validate_key_button and user_provided_key:
    -    if check_characters_api_key(user_provided_key) == True:
    +if validate_key_button:
    +    if not user_provided_key:
    +        st.error("API Key cannot be empty.")
    +    elif check_characters_api_key(user_provided_key) == True:
             if test_openai_key(user_provided_key) == True:
    Suggestion importance[1-10]: 6

    __

    Why: Adding a check to ensure that the user_provided_key is not empty before validation prevents unnecessary API calls and potential errors, enhancing the user experience by providing immediate feedback.

    Low

    @lfnothias lfnothias merged commit 86c2dbc into dev Feb 19, 2025
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    2 participants