Skip to content
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

ChatGPTのAPIにリクエストをする実装 #11

Merged
merged 13 commits into from
Aug 26, 2023

Conversation

komaitaira
Copy link
Contributor

@komaitaira komaitaira commented Aug 26, 2023

概要

chatgptのapiにリクエストをして、返ってきたレスポンスを画面に表示するようにした

対応の詳細

  • libにopenapiのapiにリクエストをする関数を実装(lib/callChatGpt.js)
  • カスタムフック(hooks/useChatGpt.js)を実装し、その中でcallChatGptを呼び出すようにする
  • 返ってきたレスポンスをApp.jsに表示(レスポンスが返ってくるまでのローディングを仮実装)

レビュー観点

  • コミット順に見ていくとわかりやすいかもです https://github.com/Apocalyptic-Wonderboiled/wonder-app/pull/11/commits
  • 関数名もっと良さそうなのあれば意見いただきたいです!
  • libに実際にリクエストする処理を切り出したが、その辺問題なさそうか
    • 切り出した理由は、仮にテストを書くとしたらapiリクエスト部分だけ切り出された方が書きやすいのと、テストスコープが限定されると考えるため(なおテストは書いていない)
    • 使い回しが効きやすいようにしたかったため(なお使い回しする予定は今のところない)

動作確認

  • npm installをしてopenapiのライブラリをインストールする
  • .envをプロジェクトルートに配置し、apiキーを設定する
REACT_APP_OPENAI_API_KEY=ここにapiキーを貼る
2023-08-26.15.56.46.mov

その他

補足ですが、gpt4系から書き方が変わったようでモブプロの時と若干異なる実装になっています!
https://github.com/openai/openai-node#usage
delete configuration.baseOptions.headers['User-Agent'];という記述も不要になりました🙏
openai/openai-node#6 (comment)

@komaitaira komaitaira self-assigned this Aug 26, 2023
src/hooks/useChatGpt.js Outdated Show resolved Hide resolved
Copy link

@yuki-snow1823 yuki-snow1823 left a comment

Choose a reason for hiding this comment

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

動いた!!!すげ〜〜〜。

何個かコメントしました!

src/lib/callChatGpt.js Show resolved Hide resolved
src/lib/callChatGpt.js Outdated Show resolved Hide resolved
src/lib/callChatGpt.js Show resolved Hide resolved
Copy link

@yoshi0202 yoshi0202 left a comment

Choose a reason for hiding this comment

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

通りすがりなのでスルーでも!

src/lib/callChatGpt.js Outdated Show resolved Hide resolved
Copy link
Contributor

@kanp7 kanp7 left a comment

Choose a reason for hiding this comment

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

承認します!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants