-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from turingschool/feat/open-ai-gateway
Feat/open ai gateway
- Loading branch information
Showing
9 changed files
with
511 additions
and
1 deletion.
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
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,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 |
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 |
---|---|---|
@@ -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== |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.