Skip to content

Commit 19c85e7

Browse files
committed
wip changes
1 parent 9f7bd6c commit 19c85e7

File tree

6 files changed

+64
-12
lines changed

6 files changed

+64
-12
lines changed

Diff for: app/controllers/documents_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def create
3838
@document = @planning_application.documents.build(document_params)
3939

4040
if @document.save
41-
flash[:notice] = "#{@document.name} has been uploaded."
41+
flash[:notice] = "#{@document.name} has been uploaded and queued for analysis."
4242
redirect_to planning_application_documents_path
4343
else
4444
render :new

Diff for: app/jobs/document_analyser_job.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
# frozen_string_literal: true
2+
13
class DocumentAnalyserJob < ApplicationJob
24
queue_as :high_priority
5+
retry_on(StandardError, attempts: 5, wait: 1.minute, jitter: 0)
36

47
def perform(document)
58
DocumentAnalyserService.new(document).call

Diff for: app/models/document.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@ class NotArchiveableError < StandardError; end
5353

5454
has_one_attached :file, dependent: :destroy
5555
after_create :create_audit!
56+
after_create :generate_ai_summary
5657
before_update :reset_replacement_document_validation_request_update_counter!, if: :owner_is_validation_request?
5758
after_update :audit_updated!
5859

59-
after_create :generate_ai_summary
60-
6160
DRAWING_TAGS = %w[
6261
elevations.existing
6362
elevations.proposed

Diff for: app/services/document_analyser_service.rb

+53-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require "openai"
24
require "base64"
35

@@ -19,12 +21,16 @@ def call
1921
return unless file_path
2022

2123
image = build_image(file_path)
22-
user_content = user_message(image)
24+
user_content = user_message_tags(image)
2325

2426
ai_summary = generate_ai_summary(user_content)
2527

28+
document_tags = generate_document_tags(user_content)
29+
30+
document.update!(tags: document_tags) if document_tags.any?
31+
2632
document.update!(ai_summary: ai_summary) if ai_summary.present?
27-
rescue StandardError => e
33+
rescue => e
2834
Rails.logger.error("Error analysing the document with id: #{document.id}: #{e.message}")
2935
false
3036
end
@@ -51,17 +57,17 @@ def download_file
5157

5258
def build_image(file_path)
5359
image_data = File.binread(file_path)
54-
base64_image = Base64.strict_encode64(image_data)
60+
Base64.strict_encode64(image_data)
5561
end
5662

57-
def generate_ai_summary(user_content)
63+
def generate_ai_summary(user_content)
5864
response = client.chat(
5965
parameters: {
6066
model: "gpt-4o-mini",
6167
messages: [
62-
{
63-
role: "system",
64-
content: "You are an expert, with experience in UK housing/planning, in interpreting and extracting a concise summary/description of a document"
68+
{
69+
role: "system",
70+
content: "You are an expert, with experience in UK housing/planning, in interpreting and extracting a concise summary/description of a document"
6571
},
6672
{
6773
role: "user",
@@ -78,11 +84,49 @@ def user_message(base64_image)
7884
[
7985
{
8086
type: "text",
81-
text: "Analyse this image/document and provide a short description/summary of its content in no more than 2 sentences."
87+
text: "Analyse this image/document and provide a short description/summary of its content in no more than 2 sentences."
88+
},
89+
{
90+
type: "image_url",
91+
image_url: {
92+
url: "data:image/jpeg;base64,#{base64_image}"
93+
}
94+
}
95+
]
96+
end
97+
98+
def generate_document_tags(user_content)
99+
response = client.chat(
100+
parameters: {
101+
model: "gpt-4o-mini",
102+
messages: [
103+
{
104+
role: "system",
105+
content: "You are an AI expert in reviewing and tagging documents with expertise in the UK housing/planning system. When tagging documents use ONLY the following predefined options:
106+
- **DRAWING TAGS**: #{Document::DRAWING_TAGS.join(", ")}
107+
- **EVIDENCE TAGS**: #{Document::EVIDENCE_TAGS.join(", ")}
108+
- **SUPPORTING DOCUMENT TAGS**: #{Document::SUPPORTING_DOCUMENT_TAGS.join(", ")}"
109+
},
110+
{
111+
role: "user",
112+
content: user_content
113+
}
114+
]
115+
}
116+
)
117+
response.dig("choices", 0, "message", "content").split(", ").map(&:strip)
118+
end
119+
120+
def user_message_tags(base64_image)
121+
[
122+
{
123+
type: "text",
124+
text: "Analyse this image/document and assign relevant document tags from the supplied lists.
125+
ONLY choose tags from these lists. You may choose more than one tag if relevant. Provide a comma separated list of ONLY the tags."
82126
},
83127
{
84128
type: "image_url",
85-
image_url: {
129+
image_url: {
86130
url: "data:image/jpeg;base64,#{base64_image}"
87131
}
88132
}

Diff for: app/views/documents/edit.html.erb

+4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@
7373
<p class="govuk-body">
7474
<%= created_by(@document) %>
7575
</p>
76+
77+
<p class="govuk-body govuk-!-margin-bottom-0">
78+
<strong>AI summary:</strong> <%= @document.ai_summary %>
79+
</p>
7680
</div>
7781
<div class="govuk-grid-column-full">
7882
<%= render "edit_and_upload" %>

Diff for: db/migrate/20250319102739_add_ai_summary_to_documents.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
class AddAiSummaryToDocuments < ActiveRecord::Migration[7.2]
24
def change
35
add_column :documents, :ai_summary, :text

0 commit comments

Comments
 (0)