Skip to content

Commit fbf4e38

Browse files
committed
Add inference happy path
Uses the OpenAI client SDK to call a real inference service. The values in .env.example work for a local Ollama instance.
1 parent abe6c2b commit fbf4e38

6 files changed

Lines changed: 75 additions & 2 deletions

‎.env.example‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,8 @@ SCRATCH_ASSET_IMPORT_BASE_URL=https://example.com/assets/
7070

7171
HOSTNAME=localhost
7272
LEARNER_EXPERIENCE_TESTS_DISPATCH_TOKEN=changeme
73+
74+
# Friendly error messages
75+
FEM_INFERENCE_API_URL='http://localhost:11434/v1/'
76+
FEM_INFERENCE_API_KEY=''
77+
BYPASS_FEM_INFERENCE=true

‎Gemfile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,5 @@ group :test do
8787
gem 'webdrivers'
8888
gem 'webmock'
8989
end
90+
91+
gem 'openai', '~> 0.115.0'

‎Gemfile.lock‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ GEM
324324
stringio
325325
time
326326
uri
327+
openai (0.115.0)
328+
connection_pool (>= 2.2.3)
329+
logger
327330
paper_trail (17.0.0)
328331
activerecord (>= 7.1)
329332
request_store (~> 1.4)
@@ -603,6 +606,7 @@ DEPENDENCIES
603606
omniauth-rails_csrf_protection (~> 2.0.1)
604607
omniauth-rpi!
605608
open-uri
609+
openai (~> 0.115.0)
606610
paper_trail
607611
pg (~> 1.7)
608612
postmark-rails
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
# frozen_string_literal: true
22

33
class FriendlyErrorMessagesInferenceClient
4-
def get_message(_message, _trace, _runtime, _code)
5-
'This is a placeholder friendly error message.'
4+
def initialize
5+
@client = OpenAI::Client.new(
6+
base_url: ENV.fetch('FEM_INFERENCE_API_URL'),
7+
api_key: ENV.fetch('FEM_INFERENCE_API_KEY')
8+
)
9+
end
10+
11+
def get_message(message, trace, runtime, code)
12+
return 'This is a placeholder friendly error message.' if ENV.fetch('BYPASS_FEM_INFERENCE', nil) == 'true'
13+
14+
@client.responses.create(model: 'qwen3.5:9b', input:
15+
JSON.dump({
16+
message:,
17+
trace:,
18+
runtime:,
19+
code:
20+
})).output_text
621
end
722
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe FriendlyErrorMessagesInferenceClient do
6+
subject(:client) { described_class.new }
7+
8+
let(:openai_client) { instance_double(OpenAI::Client) }
9+
10+
context 'when BYPASS_FEM_INFERENCE is false' do
11+
around do |example|
12+
ClimateControl.modify(BYPASS_FEM_INFERENCE: 'false') do
13+
example.run
14+
end
15+
end
16+
17+
it 'calls the inference server' do
18+
responses = instance_double(OpenAI::Resources::Responses)
19+
response = instance_double(OpenAI::Models::Responses::Response, output_text: 'A friendly error message')
20+
allow(OpenAI::Client).to receive(:new).and_return(openai_client)
21+
allow(openai_client).to receive(:responses).and_return(responses)
22+
allow(responses).to receive(:create).and_return(response)
23+
24+
result = client.get_message('', '', '', '')
25+
expect(result).to eq 'A friendly error message'
26+
end
27+
end
28+
29+
context 'when BYPASS_FEM_INFERENCE is true' do
30+
around do |example|
31+
ClimateControl.modify(BYPASS_FEM_INFERENCE: 'true') do
32+
example.run
33+
end
34+
end
35+
36+
it 'returns a mock response' do
37+
result = client.get_message('', '', '', '')
38+
expect(result).to eq 'This is a placeholder friendly error message.'
39+
end
40+
end
41+
end

‎spec/requests/friendly_error_messages_spec.rb‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
Flipper.enable(:ai_friendly_errors)
1313
end
1414

15+
around do |example|
16+
ClimateControl.modify(BYPASS_FEM_INFERENCE: 'true') do
17+
example.run
18+
end
19+
end
20+
1521
it 'does not allow logged-out users' do
1622
post '/api/fem'
1723

0 commit comments

Comments
 (0)