Skip to content

Commit

Permalink
Merge pull request #150 from turingschool/feat/open-ai-gateway
Browse files Browse the repository at this point in the history
Feat/open ai gateway
  • Loading branch information
kaelinpsalazar authored Feb 26, 2025
2 parents 2f3322f + cb17d73 commit 243aefc
Show file tree
Hide file tree
Showing 9 changed files with 511 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ gem "jsonapi-serializer"
gem "rolify"
gem "pundit", "~> 2.4"

gem 'faraday'

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[ mri windows ]
gem "pry"
gem "rspec-rails"
gem "shoulda-matchers"
gem 'pundit-matchers', '~> 3.1'
gem 'webmock'
gem "vcr"
end

group :test do
Expand Down
27 changes: 27 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ GEM
mutex_m
securerandom (>= 0.3)
tzinfo (~> 2.0)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
base64 (0.2.0)
bcrypt (3.1.20)
benchmark (0.3.0)
Expand All @@ -88,6 +90,9 @@ GEM
coderay (1.1.3)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
crack (1.0.0)
bigdecimal
rexml
crass (1.0.6)
date (3.4.0)
debug (1.9.2)
Expand All @@ -97,14 +102,22 @@ GEM
docile (1.4.1)
drb (2.2.1)
erubi (1.13.0)
faraday (2.12.2)
faraday-net_http (>= 2.0, < 3.5)
json
logger
faraday-net_http (3.4.0)
net-http (>= 0.5.0)
globalid (1.2.1)
activesupport (>= 6.1)
hashdiff (1.1.2)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
io-console (0.7.2)
irb (1.14.1)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.10.1)
jsonapi-serializer (2.2.0)
activesupport (>= 4.2)
jwt (2.9.3)
Expand All @@ -124,6 +137,8 @@ GEM
minitest (5.25.1)
msgpack (1.7.3)
mutex_m (0.2.0)
net-http (0.6.0)
uri
net-imap (0.5.0)
date
net-protocol
Expand Down Expand Up @@ -152,6 +167,7 @@ GEM
method_source (~> 1.0)
psych (5.2.0)
stringio
public_suffix (6.0.1)
puma (6.4.3)
nio4r (~> 2.0)
pundit (2.4.0)
Expand Down Expand Up @@ -205,6 +221,7 @@ GEM
psych (>= 4.0.0)
reline (0.5.10)
io-console (~> 0.5)
rexml (3.4.1)
rolify (6.0.1)
rspec-core (3.13.2)
rspec-support (~> 3.13.0)
Expand Down Expand Up @@ -237,6 +254,13 @@ GEM
timeout (0.4.2)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
uri (1.0.2)
vcr (6.3.1)
base64
webmock (3.25.0)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
Expand All @@ -254,6 +278,7 @@ DEPENDENCIES
bcrypt (~> 3.1.7)
bootsnap
debug
faraday
jsonapi-serializer
jwt
pg (~> 1.1)
Expand All @@ -268,6 +293,8 @@ DEPENDENCIES
shoulda-matchers
simplecov
tzinfo-data
vcr
webmock

RUBY VERSION
ruby 3.2.2p53
Expand Down
46 changes: 46 additions & 0 deletions app/gateways/openai_gateway.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class OpenaiGateway

def generate_interview_questions(description)
prompt = build_prompt(description)

response = Faraday.post("https://api.openai.com/v1/chat/completions") do |req|
req.headers['Content-Type'] = 'application/json'
req.headers['Authorization'] = "Bearer #{Rails.application.credentials.dig(:open_ai, :key)}"
req.body = {
model: "gpt-4o-mini",
messages: [{ role: "user", content: prompt }],
max_tokens: 300,
temperature: 0.7
}.to_json
end

if response.status == 200
api_response = JSON.parse(response.body, symbolize_names: true)
raw_content = api_response.dig(:choices, 0, :message, :content)

cleaned_content = raw_content.match(/\[.*\]/m)&.to_s if raw_content

if cleaned_content
{
success: true,
id: api_response[:id],
data: JSON.parse(cleaned_content)
}
else
{ success: false, error: "Unexpected response format from OpenAI." }
end
else
{ success: false, error: "Failed to fetch interview questions." }
end
rescue => error
{ success: false, error: "An error occurred: #{error.message}" }

end

private

def build_prompt(description)
"Please generate 10 practice interview questions based on the following job description: #{description}.
ONLY return a JSON array of strings containing the questions, with no additional formatting or object keys."
end
end
2 changes: 1 addition & 1 deletion config/credentials.yml.enc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
db0KsbB6Dkqw1Gu0zGbmiTGfLnwfgaOQBqtwvVwm9hZ9sykROJBhMpbz1XDbbUzAOq/bn73a5Fa49FCumPyMU2ujopiuw9n5wfaSfxCfVAN8aEySG9Ds5BAKw8LrnzPSnQlEGkFjZ4h2YySb9rtlpHE2/FoHEzpL1hxCnSy8TG1GLXediBXEMOHmGlvuLrS9uW+P01gXBoI1Mry+bqcCxLRpRR5nQrC8JOqlm5mehLxY9GHayFJLLVP304SUre6ZXkKQnf+m+CAHvARw1a6qj8459Cjg8ByAgVs4poimsiShxZTr9855kpVOysIQgjkUXfTfvsSgGKVghYrPKY+Ozf/LDhr0Pw8lqQdWUNnTOHkuQ1JTnTD7EXz4/O9fKJ34z/CUtXxF1PuIkLAxnHl8nsy1N6FI--TAyYW5qBzS/O/Mf2--O46I5K8a+Om4hNE1bSjn4A==
ihxScc5DGq5kkzi/ZtqyFJVbq1hg/jQ07++IGNGuPuSBS9AXLlyr3ngqUP9sEegxGojrK7tOIqiGCxVWfLGfZ74XfAUK1LfNHqozQRYokzc+Q9b6rIjGZtAxVzxOqx/Jxhv596it9YzG4o8kBcbTsStPQaHPizi89Ccb24BlheZLVI1/nPL2asmjA/Ihsd1QDcQ3OBt5fg9/94Dj7fWHcrLRO+YQqWqgmBXraK/2LGOzaXClXS73LLDN6kpJNYgr0KNi7IW5+KEeREd1Fg9KpFDvDBEpaloP6GXyE5r2EGUHE/6imQskhJNWg6mHY/lCDFd9ySwl/SA8P5BvQtHx/2sJGXna9+fq6S7lrn0P46JevpV4bnzcla5UTcLbZ5637rd6XL1augKa316Qq3VLqrS+Dw0AnJ6YScI8xil5HxEO1IDaliE9bcC4dru0WD9o2CEL1MbCRTAFaitMSKh614bn+eHFmzHelW3FqKfhubHJXCUp+qGma7pDKIymVTPVg/M1q85kPprHBv1shdX4vARP4vmUaNWX/Xni2BrYXjgmD2F/B2BNtMEQz6cL56MOqw==--0fId3U/dHE/KKAka--SAhsQagzPr8bt/GIYu6YHg==
122 changes: 122 additions & 0 deletions spec/cassettes/openai_gateway_failure.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 243aefc

Please sign in to comment.