-
-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create InsightJob to process insights asynchronously, triggered from an after_commit callback on Insight to queue processing. Add client and models Insight class methods model which calls out to Ollama. Add prompt generation substituting in the initial request body in place of a template variable.
- Loading branch information
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
## | ||
# InsightJob is responsible for generating InfoRequest insights using an AI | ||
# model run via Ollama. | ||
# | ||
class InsightJob < ApplicationJob | ||
queue_as :insights | ||
|
||
delegate :model, :temperature, :prompt, to: :@insight | ||
|
||
def perform(insight) | ||
@insight = insight | ||
|
||
insight.update(output: results.first) | ||
end | ||
|
||
private | ||
|
||
def results | ||
client.generate( | ||
{ model: model, prompt: prompt, temperature: temperature, stream: false } | ||
) | ||
end | ||
|
||
def client | ||
Ollama.new( | ||
credentials: { address: ENV['OLLAMA_URL'] }, | ||
options: { server_sent_events: true } | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe InsightJob, type: :job do | ||
let(:insight) do | ||
FactoryBot.build('insight', model: 'gpt-3.5-turbo', temperature: 0.7) | ||
end | ||
|
||
let(:client) { instance_double('Ollama::Controllers::Client') } | ||
|
||
let(:job) { InsightJob.new } | ||
|
||
before do | ||
allow(job).to receive(:client).and_return(client) | ||
allow(insight).to receive(:prompt).and_return('Test prompt') | ||
allow(insight).to receive(:update) | ||
end | ||
|
||
describe '#perform' do | ||
it 'updates the insight with the generated output' do | ||
expect(client).to receive(:generate).with( | ||
hash_including( | ||
model: 'gpt-3.5-turbo', | ||
temperature: 0.7, | ||
prompt: 'Test prompt', | ||
stream: false | ||
) | ||
).and_return(['Generated output']) | ||
|
||
expect(insight).to receive(:update).with(output: 'Generated output') | ||
|
||
job.perform(insight) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters