-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/open ai gateway #150
Feat/open ai gateway #150
Changes from all commits
bad26a5
9a571a9
f09201e
27f8d45
59a0200
5694eac
cb17d73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is built really cleanly. Easy to follow logic. Easy to follow errors. |
||
end | ||
end |
Original file line number |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U/dHE/KKAka--SAhsQagzPr8bt/GIYu6YHg== |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got it to pull down sample interview questions, after a little issue on my end with the key. Great job!