Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OPENAI_ACCESS_TOKEN=dummy
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ node_modules

# Ignore master key for decrypting credentials and more.
/config/master.key

.env
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ group :development do
end

gem "active_model_serializers", "~> 0.10.13"

gem "dotenv-rails", groups: %i[development test]
gem "ruby-openai"
gem "pry"
22 changes: 22 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ GEM
builder (3.2.4)
case_transform (0.2)
activesupport
coderay (1.1.3)
concurrent-ruby (1.2.0)
crass (1.0.6)
database_cleaner (2.0.2)
Expand All @@ -91,7 +92,17 @@ GEM
irb (>= 1.5.0)
reline (>= 0.3.1)
diff-lcs (1.5.0)
dotenv (2.8.1)
dotenv-rails (2.8.1)
dotenv (= 2.8.1)
railties (>= 3.2)
erubi (1.12.0)
faraday (2.7.10)
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday-multipart (1.0.4)
multipart-post (~> 2)
faraday-net_http (3.0.2)
globalid (1.1.0)
activesupport (>= 5.0)
haml (6.1.1)
Expand All @@ -117,6 +128,7 @@ GEM
mini_mime (1.1.2)
minitest (5.17.0)
msgpack (1.6.0)
multipart-post (2.3.0)
net-imap (0.3.4)
date
net-protocol
Expand All @@ -133,6 +145,9 @@ GEM
racc (~> 1.4)
pg (1.4.5)
prettier_print (1.2.1)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
puma (5.6.5)
nio4r (~> 2.0)
racc (1.6.2)
Expand Down Expand Up @@ -186,6 +201,10 @@ GEM
rspec-mocks (~> 3.12)
rspec-support (~> 3.12)
rspec-support (3.12.1)
ruby-openai (4.2.0)
faraday (>= 1)
faraday-multipart (>= 1)
ruby2_keywords (0.0.5)
syntax_tree (6.1.1)
prettier_print (>= 1.2.0)
syntax_tree-haml (4.0.3)
Expand Down Expand Up @@ -218,11 +237,14 @@ DEPENDENCIES
bootsnap
database_cleaner
debug
dotenv-rails
pg (~> 1.1)
prettier_print
pry
puma (~> 5.0)
rails (~> 7.0.4, >= 7.0.4.2)
rspec-rails
ruby-openai
syntax_tree
syntax_tree-haml
syntax_tree-rbs
Expand Down
38 changes: 38 additions & 0 deletions app/services/exercise_recommender_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class ExerciseRecommenderService
attr_reader :client

def initialize
@client = OpenAI::Client.new
end

def recommend_next_exercise(current_exercises)
response =
@client.chat(
parameters: {
model: "gpt-3.5-turbo",
messages: messages(current_exercises)
}
)

body = JSON.parse(response["choices"][0]["message"]["content"])

return body["name"], body["instructions"]
end

def messages(current_exercises)
exercise_names = current_exercises.pluck(:name).join(", ")

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👇 This is really where the meat of the interaction is

return [
{
"role" => "system",
"content" =>
"You will be provided a list of exercise names. Your task is to suggest the next exercise to do. Make the response in the JSON format: {\"name\": \"exercise name\", \"instructions\": \"exercise instructions\"}."
},
{
"role" => "user",
"content" =>
"My current exercises are: #{exercise_names}. What should my next exercise be?"
}
]
end
end
3 changes: 3 additions & 0 deletions config/initializers/openai.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
OpenAI.configure do |config|
config.access_token = ENV.fetch("OPENAI_ACCESS_TOKEN")
end
2 changes: 1 addition & 1 deletion package-lock.json

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