Skip to content

Commit

Permalink
feat(ollama): enhance endpoint extraction with JSON response handling…
Browse files Browse the repository at this point in the history
… and fix typo in ignore_extensions method

Signed-off-by: HAHWUL <[email protected]>
  • Loading branch information
hahwul committed Dec 31, 2024
1 parent b08bb3a commit 5e529b6
Showing 1 changed file with 49 additions and 11 deletions.
60 changes: 49 additions & 11 deletions src/analyzer/analyzers/llm_analyzers/ollama.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,59 @@ module Analyzer::AI

relative_path = get_relative_path(base_path, path)

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

begin
prompt = <<-PROMPT
!! You must only report JSON results. Don't explain anything and don't decorate it with Markdown. !!
---
Analyze the following code and extract endpoint and parameter information.
Return the result strictly as a JSON object with the following structure:
[
{
"url": "string / e.g. /api/v1/users",
"method": "string / e.g. GET, POST, PUT, DELETE",
"params": [
{
"name": "string / e.g. id",
"param_type": "string / e.g. query, json, form, header, cookie, path",
"value": "string / e.g. hahwul"
}
]
}
]
file.each_line do |_|
# 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
Code:
#{content}
PROMPT

response = ollama.request(prompt)
logger.debug "Ollama response (#{relative_path}):"
logger.debug_sub response

response_json = JSON.parse(response.to_s)
response_json.as_a.each do |endpoint|
url = endpoint["url"].as_s
method = endpoint["method"].as_s
params = endpoint["params"].as_a.map do |param|
Param.new(
param["name"].as_s,
param["value"].as_s,
param["param_type"].as_s
)
end
details = Details.new(PathInfo.new(path))
result << Endpoint.new(url, method, params, details)
end
rescue ex : Exception
puts "Error processing file: #{path}"
puts "Error: #{ex.message}"
end
end
end
Expand All @@ -51,7 +89,7 @@ module Analyzer::AI
result
end

def ingnore_extensions
def ignore_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
Expand Down

0 comments on commit 5e529b6

Please sign in to comment.