Skip to content

Commit 3343418

Browse files
benbaumann95rebeccaoneill
authored andcommitted
wip changes
1 parent 9f7bd6c commit 3343418

File tree

6 files changed

+64
-14
lines changed

6 files changed

+64
-14
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-11
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,15 @@ def call
1921
return unless file_path
2022

2123
image = build_image(file_path)
22-
user_content = user_message(image)
24+
user_summary_content = user_message(image)
25+
user_content = user_message_tags(image)
2326

24-
ai_summary = generate_ai_summary(user_content)
27+
ai_summary = generate_ai_summary(user_summary_content)
28+
document_tags = generate_document_tags(user_content)
2529

30+
document.update!(tags: document_tags) if document_tags.any?
2631
document.update!(ai_summary: ai_summary) if ai_summary.present?
27-
rescue StandardError => e
32+
rescue => e
2833
Rails.logger.error("Error analysing the document with id: #{document.id}: #{e.message}")
2934
false
3035
end
@@ -51,17 +56,17 @@ def download_file
5156

5257
def build_image(file_path)
5358
image_data = File.binread(file_path)
54-
base64_image = Base64.strict_encode64(image_data)
59+
Base64.strict_encode64(image_data)
5560
end
5661

57-
def generate_ai_summary(user_content)
62+
def generate_ai_summary(user_content)
5863
response = client.chat(
5964
parameters: {
6065
model: "gpt-4o-mini",
6166
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"
67+
{
68+
role: "system",
69+
content: "You are an expert, with experience in UK housing/planning, in interpreting and extracting a concise summary/description of a document"
6570
},
6671
{
6772
role: "user",
@@ -70,19 +75,56 @@ def generate_ai_summary(user_content)
7075
]
7176
}
7277
)
73-
7478
response.dig("choices", 0, "message", "content")
7579
end
7680

7781
def user_message(base64_image)
7882
[
7983
{
8084
type: "text",
81-
text: "Analyse this image/document and provide a short description/summary of its content in no more than 2 sentences."
85+
text: "Analyse this image/document and provide a short description/summary of its contents in no more than 2 sentences."
86+
},
87+
{
88+
type: "image_url",
89+
image_url: {
90+
url: "data:image/jpeg;base64,#{base64_image}"
91+
}
92+
}
93+
]
94+
end
95+
96+
def generate_document_tags(user_content)
97+
response = client.chat(
98+
parameters: {
99+
model: "gpt-4o-mini",
100+
messages: [
101+
{
102+
role: "system",
103+
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:
104+
- **DRAWING TAGS**: #{Document::DRAWING_TAGS.join(", ")}
105+
- **EVIDENCE TAGS**: #{Document::EVIDENCE_TAGS.join(", ")}
106+
- **SUPPORTING DOCUMENT TAGS**: #{Document::SUPPORTING_DOCUMENT_TAGS.join(", ")}"
107+
},
108+
{
109+
role: "user",
110+
content: user_content
111+
}
112+
]
113+
}
114+
)
115+
response.dig("choices", 0, "message", "content").split(", ").map(&:strip)
116+
end
117+
118+
def user_message_tags(base64_image)
119+
[
120+
{
121+
type: "text",
122+
text: "Analyse this image/document and assign relevant document tags from the supplied lists.
123+
ONLY choose tags from these lists. You may choose more than one tag if relevant. Provide a comma separated list of ONLY the tags."
82124
},
83125
{
84126
type: "image_url",
85-
image_url: {
127+
image_url: {
86128
url: "data:image/jpeg;base64,#{base64_image}"
87129
}
88130
}

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)