Skip to content

refactor: Improve Ollama integration and add Ollama URL option#9

Merged
psyray merged 3 commits intomasterfrom
feat/ollama-url-args
Mar 17, 2025
Merged

refactor: Improve Ollama integration and add Ollama URL option#9
psyray merged 3 commits intomasterfrom
feat/ollama-url-args

Conversation

@psyray
Copy link
Copy Markdown
Owner

@psyray psyray commented Mar 17, 2025

Fix #8
This change refactors the Ollama integration to improve performance and adds an option to specify the Ollama URL. The Ollama client is now initialized once and reused, and a new client is created for each process in parallel operations. An --ollama-url argument has been added to the CLI to allow users to specify the URL of their Ollama instance. Additionally, log messages related to Ollama connection have been improved.

This change refactors the Ollama integration to improve performance and adds an option to specify the Ollama URL. The Ollama client is now initialized once and reused, and a new client is created for each process in parallel operations. An --ollama-url argument has been added to the CLI to allow users to specify the URL of their Ollama instance. Additionally, log messages related to Ollama connection have been improved.
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Mar 17, 2025

Reviewer's Guide by Sourcery

This pull request refactors the Ollama integration to improve performance by reusing the Ollama client and adds an option to specify the Ollama URL via the CLI. It also improves logging for Ollama connection errors. The Ollama client is now initialized once and reused, and a new client is created for each process in parallel operations. An --ollama-url argument has been added to the CLI to allow users to specify the URL of their Ollama instance. Additionally, log messages related to Ollama connection have been improved.

Sequence diagram for getting Ollama client

sequenceDiagram
    participant OM as OllamaManager
    OM->OM: if not self.client
    OM->OM: try
    OM->OM: self.client = ollama.Client(self.api_url)
    OM->OM: self.client.list()
    OM->OM: return self.client
    OM->OM: except Exception as e
    OM->OM: self._log_connection_error(e)
    OM->OM: raise ConnectionError
    OM->OM: end
    OM->OM: return self.client
Loading

Sequence diagram for parallel file processing

sequenceDiagram
    participant P as Process
    participant OM as OllamaManager
    participant EF as Embedding Functions
    P->OM: ollama_manager = OllamaManager(args.api_url)
    P->EF: functions = extract_functions_from_file(args.input_path, content, ollama_manager)
    loop For each function
        P->EF: func_embedding = generate_content_embedding(func_content, args.embed_model, args.chunk_size, ollama_manager)
    end
    P->EF: file_embedding = generate_content_embedding(content, args.embed_model, args.chunk_size, ollama_manager)
Loading

Updated class diagram for EmbeddingManager

classDiagram
    class EmbeddingManager {
        -ollama_manager: OllamaManager
        -ollama_client: ollama.Client
        __init__(args, ollama_manager: OllamaManager)
        index_code_files(files: List[Path]) : None
        process_input_files(args) : bool
        build_vulnerability_embedding_prompt(vulnerability: Union[str, Dict]) : str
    }
Loading

Updated class diagram for SecurityAnalyzer

classDiagram
    class SecurityAnalyzer {
        -llm_model: str
        -embedding_manager: EmbeddingManager
        -ollama_manager: OllamaManager
        -client: ollama.Client
        __init__(llm_model: str, embedding_manager: EmbeddingManager, ollama_manager: OllamaManager)
        process_analysis_with_model(vulnerabilities, args, report)
        analyze_vulnerability(vulnerability: Dict, args, report)
        _build_prompt(vulnerability: Dict, code_snippet: str, args) : str
        _extract_relevant_info(vulnerability: Dict) : str
    }
Loading

Updated class diagram for EmbeddingAnalyzer

classDiagram
    class EmbeddingAnalyzer {
        -embedding_manager: EmbeddingManager
        -ollama_manager: OllamaManager
        -code_base: dict
        -embedding_model: str
        __init__(embedding_manager: EmbeddingManager, ollama_manager: OllamaManager)
        analyze_all_vulnerabilities(vulnerabilities: List[Dict]) : List[Dict]
        analyze_vulnerability(vulnerability: Dict) : Dict
        _prepare_analysis_args(vuln: Dict) : list
    }
Loading

File-Level Changes

Change Details Files
Introduces an option to specify the Ollama URL via the CLI.
  • Added an --ollama-url argument to the CLI using argparse.
  • The OllamaManager now accepts an optional api_url parameter during initialization, defaulting to http://localhost:11434.
  • The OLLAMA_URL constant is defined in config.py and used as the default value for the Ollama URL.
  • The OllamaManager now uses the configured api_url when checking the connection to the Ollama server.
  • The process_file_parallel and analyze_item_parallel functions now receive the api_url as an argument.
oasis/ollama_manager.py
oasis/oasis.py
oasis/config.py
oasis/embedding.py
oasis/analyze.py
Refactors the Ollama integration to improve performance by reusing the Ollama client.
  • The OllamaManager now initializes the Ollama client once and reuses it for subsequent calls.
  • A new Ollama client is created for each process in parallel operations to avoid conflicts.
  • The get_client method in OllamaManager now checks if a client already exists and returns it if it does, otherwise it creates a new client.
  • The process_file_parallel function now creates a new OllamaManager instance for each process.
oasis/ollama_manager.py
oasis/embedding.py
oasis/analyze.py
Improves logging for Ollama connection errors.
  • The _log_connection_error method in OllamaManager now includes the configured api_url in the error message.
  • The log level for the connection error message was changed from error to info.
oasis/ollama_manager.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@psyray psyray self-assigned this Mar 17, 2025
@psyray psyray linked an issue Mar 17, 2025 that may be closed by this pull request
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey @psyray - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a try-except block around the OllamaManager initialization in EmbeddingManager to handle potential connection errors early.
  • The _prepare_analysis_args function in EmbeddingAnalyzer duplicates the Ollama URL, which could lead to inconsistencies; consider passing the OllamaManager instance instead.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread oasis/ollama_manager.py
Comment thread oasis/embedding.py
psyray added 2 commits March 17, 2025 19:59
This change enhances the thread safety of the Ollama client initialization and handles cases where the Ollama manager might be missing. It also refactors the _init_ollama method to accept an optional ollama_url argument, improving flexibility. Additionally, explicit checks for a non-null ollama_manager are added before generating content embeddings and extracting functions.
@psyray psyray merged commit 3e196ef into master Mar 17, 2025
@psyray psyray deleted the feat/ollama-url-args branch March 17, 2025 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Ollama URL via env or arg

1 participant