Skip to content

Commit

Permalink
feat(analyzer): add Ollama AI analyzer and integrate with analysis en…
Browse files Browse the repository at this point in the history
…dpoints

Signed-off-by: HAHWUL <[email protected]>
  • Loading branch information
hahwul committed Dec 25, 2024
1 parent ec0f27e commit 804755f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/analyzer/analyzer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def initialize_analyzers(logger : NoirLogger)
{"rust_axum", Rust::Axum},
{"rust_rocket", Rust::Rocket},
{"rust_actix_web", Rust::ActixWeb},
{"ai_ollama", AI::Ollama},
])

logger.success "#{analyzers.size} Analyzers initialized"
Expand All @@ -67,6 +68,11 @@ def analysis_endpoints(options : Hash(String, YAML::Any), techs, logger : NoirLo
logger.info "Analysis Started"
logger.sub "➔ Code Analyzer: #{techs.size} in use"

if (options["ollama"].to_s != "") && (options["ollama_model"].to_s != "")
logger.sub "➔ AI Analyzer: Ollama in use"
techs << "ai_ollama"
end

techs.each do |tech|
if analyzer.has_key?(tech)
if NoirTechs.similar_to_tech(options["exclude_techs"].to_s).includes?(tech)
Expand Down
56 changes: 56 additions & 0 deletions src/analyzer/analyzers/llm_analyzers/ollama.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require "../../../utils/utils.cr"
require "../../../models/analyzer"
require "../../../llm/ollama"

module Analyzer::AI
class Ollama < Analyzer
@llm_url : String
@model : String

def initialize(options : Hash(String, YAML::Any))
super(options)
@llm_url = options["ollama"].as_s
@model = options["ollama_model"].as_s
end

def analyze
# Init LLM Instance
ollama = LLM::Ollama.new(@llm_url, @model)

# Source Analysis
begin
Dir.glob("#{base_path}/**/*") do |path|
next if File.directory?(path)

relative_path = get_relative_path(base_path, path)

if File.exists?(path) && !(ingnore_extensions.includes? File.extname(path))
File.open(path, "r", encoding: "utf-8", invalid: :skip) do |file|
params_query = [] of Param
params_body = [] of Param
methods = [] of String

file.each_line do |line|
# TODO
# puts ollama.request("Hi! This is prompt text.")
# details = Details.new(PathInfo.new(path))
# result << Endpoint.new("/#{relative_path}", method, params_body, details)
rescue
next
end
end
end
end
rescue e
logger.debug e
end
Fiber.yield

result
end

def ingnore_extensions
[".js", ".css", ".html", ".xml", ".json", ".yml", ".yaml", ".md", ".jpg", ".jpeg", ".png", ".gif", ".svg", ".ico", ".eot", ".ttf", ".woff", ".woff2", ".otf", ".mp3", ".mp4", ".avi", ".mov", ".webm", ".zip", ".tar", ".gz", ".7z", ".rar", ".pdf", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".txt", ".csv", ".log", ".sql", ".bak", ".swp"]
end
end
end

0 comments on commit 804755f

Please sign in to comment.