Skip to content

Commit

Permalink
restore tests, fix bug, thanks scott
Browse files Browse the repository at this point in the history
  • Loading branch information
epugh committed Feb 6, 2025
1 parent a7e8a34 commit ca99a9a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
13 changes: 7 additions & 6 deletions app/services/llm_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def make_user_prompt query_doc_pair
end

# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize
def get_llm_response user_prompt, system_prompt
conn = Faraday.new(url: 'https://api.openai.com') do |f|
f.request :json
Expand All @@ -56,12 +57,6 @@ def get_llm_response user_prompt, system_prompt
interval: 2,
interval_randomness: 0.5,
backoff_factor: 2,
# exceptions: [
# Faraday::ConnectionFailed,
## Faraday::TimeoutError,
# 'Timeout::Error',
# 'Error::TooManyRequests'
# ],
retry_statuses: [ 429 ],
}
end
Expand All @@ -84,6 +79,11 @@ def get_llm_response user_prompt, system_prompt
if response.success?
begin
chat_response = response.body
if chat_response.is_a?(String)
# in our tests the body is a String, but in real use it's already formatted as Json by Faraday
chat_response = JSON.parse(chat_response)
end

content = chat_response['choices']&.first&.dig('message', 'content')

parsed_content = JSON.parse(content)
Expand All @@ -100,4 +100,5 @@ def get_llm_response user_prompt, system_prompt
end
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize
end
21 changes: 16 additions & 5 deletions test/services/llm_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,29 @@ class LlmServiceTest < ActiveSupport::TestCase
TEXT

describe 'Hacking with Scott' do
test 'can we make it run' do
# WebMock.allow_net_connect!
user_prompt = DEFAULT_USER_PROMPT
system_prompt = AiJudgesController::DEFAULT_SYSTEM_PROMPT
result = service.get_llm_response(user_prompt, system_prompt)
puts result

assert_kind_of Numeric, result[:judgment]
assert_not_nil result[:explanation]

# WebMock.disable_net_connect!
end
test 'making a user prompt' do
user_prompt = service.make_user_prompt query_doc_pair
assert_includes user_prompt, query_doc_pair.query_text
end

test 'creating a judgement' do
Judgement.new(query_doc_pair: query_doc_pair, user: judge)
# service.perform_judgement judgement
judgement = Judgement.new(query_doc_pair: query_doc_pair, user: judge)
service.perform_judgement judgement

# assert_instance_of Float, judgement.rating
# assert_not_nil judgement.explanation
assert true
assert_instance_of Float, judgement.rating
assert_not_nil judgement.explanation
end
end

Expand Down

0 comments on commit ca99a9a

Please sign in to comment.