Class: BuilderApm::Doctor::AiDoctor

Inherits:
Object
  • Object
show all
Defined in:
lib/builder_apm/doctor/ai_doctor.rb

Instance Method Summary collapse

Constructor Details

#initializeAiDoctor

Returns a new instance of AiDoctor.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/builder_apm/doctor/ai_doctor.rb', line 7

def initialize
    api_key = BuilderApm.configuration.api_key
    case BuilderApm.configuration.api
    when "OpenAi"
      @ai_client = OpenAIChatGPT.new(api_key, "gpt-3.5-turbo-16k")
    else
      @ai_client = BravoChatAi.new(api_key)
    end
    @role = "You are an AI trained to analyse and optimise web requests and database queries for a Ruby on Rails api. Your task is to identify any performance issues in the given request data, suggest possible solutions and show examples, Also show which files/methods should be looked into further"
    @ai_client.role(@role)
end

Instance Method Details

#deeper_analysis(request_data, diagnosis, files) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/builder_apm/doctor/ai_doctor.rb', line 87

def deeper_analysis(request_data, diagnosis, files)
    targeted_code = files.map do |file_n_line|
        parts = file_n_line&.split(':')
        next if parts&.length < 2

        file = parts.first
        line = parts.last.to_i
        extract_lines_from_file(file, line)
    end
    messages = [
        {
            role: "user",
            content: "Here is the request data, durations in milliseconds: \n#{reduced_backtrace(request_data)}"
        },
        {
            role: "assistant",
            content: diagnosis
        },
        {
            role: "user",
            content: "this is the code block mentioned in the list of files:
#{targeted_code.join("")}

#{deep_anaysis_call_to_action(request_data)}, no solution should include altering files that have a path folder called gems, As I am unable to edit those files
eg
<p>{description of what the fix is}</p>

<pre>{code fix 1}</pre>
<p>{notes / comments}</p>
[repeat for each code fix presented]"
        },
    ]

    @ai_client.chat(messages)
end

#diagnose(request_data) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/builder_apm/doctor/ai_doctor.rb', line 19

def diagnose(request_data) 
    diagnosis = if request_data["status"] == 500
        diagnose_error(reduced_backtrace(request_data), backtrace_error(request_data) )
    else
        diagnose_backtrace(reduced_backtrace(request_data))
    end
    diagnosis
end

#diagnose_backtrace(backtrace) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/builder_apm/doctor/ai_doctor.rb', line 55

def diagnose_backtrace(backtrace)
    
prompt = "Here is the request data, durations in milliseconds: \n#{backtrace}
Analyze this Stack trace and provide any potential performance issues and solutions.
The issues where more investigation into the code is required, include a list of files that are worth reviewing, unless the file path is part of gem or active_admin then skip that file and work out where in the rails app the trigger was and include that instead- path and file only (agregated, don't repeat a file even if its another line within that file).
eg
<h2>High Level Issues</h2>
<h3>{Issue type found}</h3>
<p>{times and where issue type was found}</p>
<p>{details of what this issue is and how it can effect the performance}</p>
{list of possibly solutions to fix said issue}

<h3>{Issue type found}</h3>
<p>{times and where issue type was found}</p>
<p>{details of what this issue is and how it can effect the performance}</p>
{list of possibly solutions to fix said issue}
[repeat Issue type found until no more issues type are left]


<dl><dt>Main Error File : </dt><dd class=\"main_file\"><input type=\"checkbox\" value=\"{path/filename4.ext:[line_number]}\" name=\"deeper_dive_filename\" />{path/filename.ext:line_number }</dd></dl>
<dl><dt>Addtional Files to review:{do not include the duplicate files regardless of different line number}</dt>
<dd class=\"other_file\"><input type=\"checkbox\" value=\"{path/filename1.ext:[line_number, line_number]}\" name=\"deeper_dive_filename\" />{path/filename1.ext:[line_number, line_number]}</dd>
<dd class=\"other_file\"><input type=\"checkbox\" value=\"{path/filename2.ext:[line_number, line_number, line_number, line_number]}\" name=\"deeper_dive_filename\" />{path/filename2.ext:[line_number, line_number, line_number, line_number]}</dd>
<dd class=\"other_file\"><input type=\"checkbox\" value=\"{path/filename3.ext:[line_number, line_number]}\" name=\"deeper_dive_filename\" />{path/filename3.ext:[line_number, line_number]}</dd>
<dd class=\"other_file\"><input type=\"checkbox\" value=\"{path/filename4.ext:[line_number]}\" name=\"deeper_dive_filename\" />{path/filename4.ext:[line_number]}</dd>
</dl>

"
    
    @ai_client.chat(prompt)
end

#diagnose_error(backtrace, error) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/builder_apm/doctor/ai_doctor.rb', line 28

def diagnose_error(backtrace, error)
    prompt = "Please review this error and stack trace for errors and provide feedback, followed by the path/filename of the rails app file causing the error and then any other files that need further investigation
eg
<h2>{the error}</h2>
<h3>Problem found:</h3>
<p>{details for the problem found}</p>

<h3>Solutions:</h3>
<p>{list of solutions}</p>

<dl><dt>Main Error File : </dt><dd class=\"main_file\"><input type=\"checkbox\" value=\"{path/filename4.ext:[line_number]}\" name=\"deeper_dive_filename\" />{path/filename.ext:line_number }</dd></dl>
<dl><dt>Addtional Files to review:{do not include the duplicate files regardless of different line number}</dt>
<dd class=\"other_file\"><input type=\"checkbox\" value=\"{path/filename1.ext:[line_number, line_number]}\" name=\"deeper_dive_filename\" />{path/filename1.ext:[line_number, line_number]}</dd>
<dd class=\"other_file\"><input type=\"checkbox\" value=\"{path/filename2.ext:[line_number, line_number, line_number, line_number]}\" name=\"deeper_dive_filename\" />{path/filename2.ext:[line_number, line_number, line_number, line_number]}</dd>
<dd class=\"other_file\"><input type=\"checkbox\" value=\"{path/filename3.ext:[line_number, line_number]}\" name=\"deeper_dive_filename\" />{path/filename3.ext:[line_number, line_number]}</dd>
<dd class=\"other_file\"><input type=\"checkbox\" value=\"{path/filename4.ext:[line_number]}\" name=\"deeper_dive_filename\" />{path/filename4.ext:[line_number]}</dd>
</dl>

Error:
#{error}

Stack Trace:
#{backtrace}"
    @ai_client.role("You are an AI trained to analyse and diagnose web requests for a Ruby on Rails api. Your task is to identify the error in the given request data, suggest possible solutions, Also show which files/methods should be looked into further")
    @ai_client.chat(prompt)
end