@gakeppuchi

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

ruby on rails 画面遷移のスピードを速くしたいです。

解決したいこと

Openaiのapiを利用し、チャットアプリを作成中です。
現在ローカル環境で、質問をしてからフリーズし、13秒ほどかかって画面遷移しています。
質問内容も複雑なものではなく、今夜の晩御飯どうしようかな??の質問で10秒以上時間がかかっています。
本来実装したい質問は更に複雑なものでなので、高速で解答をもらって画面遷移をできるようにしたいです。

発生している問題・エラー

画面遷移に時間がかかりすぎている

 Parameters: {"authenticity_token"=>"ZAWWmcjBBJwCukTwM5dmPd/krMtMV6zl3zWhgnYS7gIf0NqM18OPk41PHdMJoEpkE0Pot3obc6cYZ07dGP2JWA==", "question"=>{"language"=>"あいうえお", "framework"=>"か", "hobby"=>"さ", "former_job"=>"た", "desired_engineer"=>"デー タエンジニ ア"}, "commit"=>"聞く!"}
  User Load (2.7ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
   (1.2ms)  BEGIN
  ↳ app/controllers/questions_controller.rb:22:in `create'
  User Load (1.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
  ↳ app/controllers/questions_controller.rb:22:in `create'
  Question Create (1.8ms)  INSERT INTO `questions` (`language`, `framework`, `hobby`, `former_job`, `desired_engineer`, `user_id`, `created_at`, `updated_at`) VALUES ('あいうえお', 'か', 'さ', 'た', 'データエンジニア', 2, '2023-04-07 12:26:19.766549', '2023-04-07 12:26:19.766549')
  ↳ app/controllers/questions_controller.rb:22:in `create'
   (10.0ms)  COMMIT
  ↳ app/controllers/questions_controller.rb:22:in `create'
   (1.1ms)  BEGIN
  ↳ app/controllers/questions_controller.rb:34:in `create'
  Answer Create (2.0ms)  INSERT INTO `answers` (`user_id`, `question_id`, `answer`, `created_at`, `updated_at`) VALUES (2, 31, '私には食欲がありませんが、穀物や野菜をたくさん含んだ健康的な食事をお勧めします。例えば、野菜炒めや鶏胸肉のグリルなどが良いでしょう。また、スープやサラダなども健康的でおいしい選択肢です。どうぞお召し上がりください。', '2023-04-07 12:26:32.806025', '2023-04-07 12:26:32.806025')
  ↳ app/controllers/questions_controller.rb:34:in `create'
   (15.1ms)  COMMIT
  ↳ app/controllers/questions_controller.rb:34:in `create'
Redirected to http://localhost:3000/
Completed 200 OK in 13105ms (ActiveRecord: 35.1ms | Allocations: 9792)


Started GET "/" for ::1 at 2023-04-07 21:26:32 +0900
Processing by QuestionsController#index as HTML
  User Load (0.7ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
   (0.4ms)  BEGIN
  Impression Create (0.5ms)  INSERT INTO `impressions` (`impressionable_type`, `user_id`, `controller_name`, `action_name`, `request_hash`, `ip_address`, `session_hash`, `referrer`, `created_at`, `updated_at`) VALUES ('Question', 2, 'questions', 'index', 'c10de02b87a69bfc66b8fddd53b4b4be93097bbef9b2bf6a6be323a97a871326', '::1', '33524f41f350a04a567d4d968238f371', 'http://localhost:3000/questions/new', '2023-04-07 12:26:32.892504', '2023-04-07 12:26:32.892504')
   (4.0ms)  COMMIT
   (0.5ms)  SELECT COUNT(*) FROM `users`
  ↳ app/controllers/questions_controller.rb:8:in `index'
   (0.4ms)  SELECT COUNT(*) FROM `questions`
  ↳ app/controllers/questions_controller.rb:10:in `index'
  Rendering questions/index.html.erb within layouts/application
  Rendered shared/_header.html.erb (Duration: 0.2ms | Allocations: 127)
  Rendered shared/_footer.html.erb (Duration: 0.0ms | Allocations: 5)
  Rendered questions/index.html.erb within layouts/application (Duration: 0.7ms | Allocations: 336)
[Webpacker] Everything's up-to-date. Nothing to do
[Webpacker] Everything's up-to-date. Nothing to do
Completed 200 OK in 24ms (Views: 8.1ms | ActiveRecord: 6.5ms | Allocations: 14066)



ActionController::RoutingError (No route matches [GET] "/assets/dist/js/bootstrap.bundle.min.js"):

該当するソースコード

def create
    require 'ruby/openai'

    @question = Question.new(question_params)
    @question.save
    client = OpenAI::Client.new(access_token:ENV["OPENAI_API_KEY"])

      response = client.chat(
      parameters: {
          model: "gpt-3.5-turbo",
          messages: [{ role: "user", content: "今日の晩御飯どうしようかな??" }],
      })
    
    #response.dig("choices", 0, "message", "content")
    #@messages = response.dig("choices", 0, "message", "content")
    @answer = Answer.new(answer: response.dig("choices", 0, "message", "content"), question: @question, user_id: current_user.id)
    @answer.save
    redirect_to root_path
  end

自分で試したこと

ActionController::RoutingError (No route matches [GET] "/assets/dist/js/bootstrap.bundle.min.js"):

このエラーが画面遷移が遅くなっている理由かもしれないと考え、jsのファイルを作ったり編集したりしましたが解決にはいたりませんでした。
環境は
rails 0_6_0
bootstrap 5.2.0
使っています。
何卒宜しくお願い致します。

0 likes

2Answer

OpenAIのレスポンスタイムによるものなので難しいと思います。

以下根拠
questionsの作成:12:26:19
answersの作成:12:26:32
であることからボトルネック箇所は以下であることがわかります

@question.save #12:26:19
client = OpenAI::Client.new(access_token:ENV["OPENAI_API_KEY"])
response = client.chat(
  parameters: {
    model: "gpt-3.5-turbo",
    messages: [{ role: "user", content: "今日の晩御飯どうしようかな??" }],
  }
)
@answer = Answer.new(answer: response.dig("choices", 0, "message", "content"), question: @question, user_id: current_user.id)
@answer.save #12:26:32

以上からclient#chatの処理で13秒かかっていることがほぼ見えてきます。

以下発行されたSQL

INSERT INTO `questions` (`language`, `framework`, `hobby`, `former_job`, `desired_engineer`, `user_id`, `created_at`, `updated_at`) VALUES ('あいうえお', 'か', 'さ', 'た', 'データエンジニア', 2, '2023-04-07 12:26:19.766549', '2023-04-07 12:26:19.766549')
INSERT INTO `answers` (`user_id`, `question_id`, `answer`, `created_at`, `updated_at`) VALUES (2, 31, '私には食欲がありませんが、穀物や野菜をたくさん含んだ健康的な食事をお勧めします。例えば、野菜炒めや鶏胸肉のグリルなどが良いでしょう。また、スープやサラダなども健康的でおいしい選択肢です。どうぞお召し上がりください。', '2023-04-07 12:26:32.806025', '2023-04-07 12:26:32.806025')
1Like

Comments

  1. @gakeppuchi

    Questioner

    @h_kono0707様
    いつもありがとうございます、本当に助かります!
    なるほど、created_atの時間を見ればいいのですね!時間がかかっていることと、値がちゃんと保存されているかにしか目がいかず、ここまで見れてませんでした!

    ひとまずajax通信で動画再生したりとかでごまかせるところはごまかしてみますー!

”推測するな、計測せよ”という言葉もあるので、経過時間を測る処理を入れてどこがボトルネックなのか実際に計測してみましょう。

OpenAIの部分で時間がかかっているなら、質問の登録と回答の登録を分け、回答は非同期処理にする方法は検討できないでしょうか。

0Like

Comments

  1. @gakeppuchi

    Questioner

    @blue32a様
    経過時間を測る処理があることを初めて知りました、ありがとうございます!やり方を調べてどこが悪いのか、数値で表せられるように努力します!

    質問と解答の登録分け。。。ちとまだイメージがわかないので、ひとまずajax処理をやってみます!
    ご回答ありがとうございますvv

Your answer might help someone💌