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の調査 #5

Closed
sora-uzu opened this issue Aug 11, 2023 · 1 comment
Closed

ChatGPTの調査 #5

sora-uzu opened this issue Aug 11, 2023 · 1 comment
Assignees

Comments

@sora-uzu
Copy link
Member

No description provided.

@komaitaira komaitaira self-assigned this Aug 11, 2023
@komaitaira
Copy link
Contributor

調査したこと

  • chatgpt apiでできること
  • chatgpt apiのAPIキー取得方法
  • APIキーを用いたchatgpt apiへのリクエスト方法

chatgpt apiでできること

https://aismiley.co.jp/ai_news/what-is-the-chatgpt-api/
↑を参考に、主にできることとしては下記になることがわかった

  • Eメールなどの文章作成・要約・添削
  • 言語翻訳
  • 情報検索
  • Pythonなどのプログラミングコード作成
  • チャットボットシステムや会話型エージェントの開発
  • etc..

今回は普段使っているような「ユーザーが何か問いかけたらそれに対してchatgptが返答してくれる」ようなchatの機能が使いたいため、問題なくできそうである。

chatgpt apiのAPIキー取得方法

https://aismiley.co.jp/ai_news/what-is-the-chatgpt-api/
↑またまたこちらを参考にまずOpenAIのアカウントを作成し、キーを取得。詳細はリンクより

APIキーを用いたchatgpt apiへのリクエスト方法

APIリファレンスがあるので、そちらを参考に進めた。
言語としてはPython、Node.jsの2つがあるようでした。(今回はjsで完結できるとbetterなのでnode.jsを選択)
https://platform.openai.com/docs/api-reference/chat

リクエスト例としてはリファレンスにも記載あるが、下記のようになる

const { Configuration, OpenAIApi } = require("openai");

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);

const completion = await openai.createChatCompletion({
  model: "gpt-3.5-turbo",
  messages: [{"role": "system", "content": "You are a helpful assistant."}, {role: "user", content: "Hello world"}],
});
console.log(completion.data.choices[0].message);

そのときのレスポンスが下記のようになる

{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "gpt-3.5-turbo-0613",
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "\n\nHello there, how may I assist you today?",
    },
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21
  }
}

エラーについて

上記を参考に実装すると、Refused to set unsafe header "User-Agent"というエラーがコンソールに吐き出される。
調べてみると、バックエンドからではなくフロントエンドから直接API呼び出しを行ったときに発生するとのこと。やはりフロントから直接API叩くのはセキュアでないため想定されないっぽい。
これを解消するには下記のようにリクエストヘッダーにあるUser-Agentというプロパティをdeleteするととりあえずは解決するっぽい。
openai/openai-node#6 (comment)

今回は公開はつどい内に閉じるつもりであり、開発合宿が終わって一定期間経ったらAPIキー削除してリクエストが行かないようにしたりなど何らかの対応をすればいいかなと思っているので、とりあえずは上記で対応で良いと思っている。

その他

OpenAIのAPI料金の節約方法という記事が参考になりそうなので、もし個人で使ってみたいという方がいたら参考にしてみるといいかも。
これをみるとやはり日本語よりも英語の方が節約できるっぽいし、質問文を端的にするとか、コツが書いてて良い感じ。
https://zenn.dev/umi_mori/books/chatbot-chatgpt/viewer/how_to_save_openai_api_prices

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

No branches or pull requests

2 participants