【超簡単】PythonでChatGPTを使ってAIチャットボットを作ろう!ChatGPTとPythonの最強コンビ!~プログラム公開中~

大福ちゃんねる
23 Apr 202317:56

Summary

TLDRThis video script outlines a tutorial on integrating ChatGPT with Python via API for efficient business use. It discusses the importance of API usage for privacy, as free ChatGPT use may lead to data collection by OpenAI. The script provides a step-by-step guide on setting up an API key, calculating token usage, and creating a Python program to interact with ChatGPT. It also demonstrates building a simple ChatGPT application using tkinter, allowing users to ask questions and receive responses within a GUI. The tutorial is practical, showing how to maintain context across multiple interactions and track token consumption, encouraging viewers to try it for potential applications.

Takeaways

  • 😀 The video introduces using ChatGPT through a Python program with an API connection.
  • 🏢 There's an increasing trend of businesses, governments, and municipalities considering using ChatGPT for operational efficiency.
  • 🔒 Concerns are raised about privacy as free use of ChatGPT may lead to information being used for AI training by OpenAI.
  • 📄 For sensitive information, using ChatGPT via API is recommended as OpenAI states they won't use the data for learning.
  • 🤖 The video demonstrates how to integrate ChatGPT into various tasks for business automation using Python and API.
  • 💰 The cost of using ChatGPT's GPT3.5 API as of April 2023 is $0.002 per 1000 tokens, which is approximately 0.2 to 0.3 Japanese Yen.
  • 🔡 Tokens are calculated with English words as 1 token and Japanese characters as 1 token, with some characters consuming 2 tokens.
  • ⏱ The API's token consumption includes both the questions asked and the responses given by ChatGPT.
  • 🆓 Personal use of ChatGPT API comes with a free tier for the first three months, which amounts to $18 worth of usage.
  • 🔑 The process of obtaining an API key from OpenAI involves creating an account, setting up payment information, and generating a key through the developer portal.
  • 🛠️ The video provides a step-by-step guide on setting up a Python program to use the ChatGPT API, including installing necessary libraries and setting API keys.
  • 📊 The script includes a practical example of creating a simple ChatGPT application using Python's tkinter for GUI, demonstrating a question-and-answer interface with token count.

Q & A

  • Why would someone use ChatGPT via a Python program with API connection instead of using it for free?

    -Using ChatGPT via a Python program with API connection is beneficial for organizations like businesses and government offices aiming to improve operational efficiency. It also ensures that the information used does not contribute to OpenAI's learning data, which can be a concern with free usage due to potential privacy issues with sensitive information.

  • What is a potential risk of using the free version of ChatGPT?

    -The free version of ChatGPT may collect user data, which could potentially be used for AI learning purposes. This could be problematic if sensitive or confidential information is processed through the service.

  • How does the API connection with ChatGPT help with privacy concerns?

    -When using ChatGPT via API connection, OpenAI states that the information utilized will not be used for learning purposes, thus addressing privacy concerns and making it a preferred method for enterprises and government entities.

  • What are the potential benefits of integrating ChatGPT into various business tasks through a Python program?

    -Integrating ChatGPT via a Python program allows for the automation of various tasks and the potential for more efficient business operations by leveraging the AI's capabilities within specific workflows.

  • What was the cost of using the GPT3.5 API as of April 2023?

    -As of April 2023, the cost for using the GPT3.5 API was $0.002 per 1000 tokens, which roughly translates to 0.2 to 0.3 Japanese Yen.

  • How are tokens calculated in the context of ChatGPT's API?

    -Tokens are calculated with English words being one token each, while in Japanese, one character equals one token, and certain characters like Kanji may consume two tokens. This means that 500 to 1000 characters would equate to 1000 tokens.

  • Why might the consumption of tokens be higher than expected when using ChatGPT's API?

    -Token consumption is not only counted for the questions asked but also includes the responses from ChatGPT. Therefore, longer answers can lead to a higher consumption of tokens per interaction.

  • What is the significance of the 'max_tokens' parameter in the ChatGPT API?

    -The 'max_tokens' parameter in the ChatGPT API allows users to limit the response length to a specified number of tokens, ensuring that the answer does not exceed the desired length.

  • What is the purpose of the 'temperature' parameter in the ChatGPT API?

    -The 'temperature' parameter, which can be set between 0 and 2, influences the diversity of the responses. A higher value yields more varied answers, while a lower value results in more consistent responses.

  • How does the script describe the process of obtaining an API key from OpenAI?

    -The script outlines the process of obtaining an API key by navigating to the 'API Keys' section in the OpenAI dashboard, clicking 'CreateNewSecretKey', naming the key, and then copying and saving the generated key.

  • What is the script's approach to creating a user interface for interacting with ChatGPT?

    -The script suggests creating a simple user interface using tkinter in Python, with input fields for questions, a text area for displaying answers, and a button to submit questions to ChatGPT.

  • How does the script handle the continuation of a conversation with ChatGPT in the Python program?

    -The script demonstrates how to append each new question and the corresponding ChatGPT response to a 'messages' list, ensuring that the context of the conversation is maintained across multiple interactions.

  • What is the script's final application design for interacting with ChatGPT?

    -The final application design includes a user interface where users can input questions at the top and receive answers in a text box below, with the ability to continue the conversation and view a running count of token usage.

Outlines

00:00

🤖 Introduction to Using ChatGPT with Python API

This paragraph introduces the concept of integrating ChatGPT with Python programs via API connections. It discusses the reasons why organizations might prefer using ChatGPT through API connections to maintain privacy, as free usage could lead to data being utilized for AI training. The paragraph also covers the potential of automating tasks by embedding ChatGPT into various business processes. The cost of using the GPT3.5 API is detailed, with an explanation of token consumption for both questions and answers. The video script provides a step-by-step guide on setting up an account, navigating the OpenAI platform, and understanding the billing process, including setting payment methods and limits.

05:00

🔑 Obtaining and Securing API Keys for ChatGPT

The second paragraph focuses on the process of obtaining API keys from the OpenAI platform. It explains how to set up payment methods and create new secret keys, emphasizing the importance of saving these keys securely. The script then transitions into creating a Python program using Visual Studio Code, installing the OpenAI library, and writing code to interact with the ChatGPT API. It demonstrates how to import the library, set the API key, and structure the API request to send messages and receive responses from ChatGPT. Additionally, the paragraph suggests enhancing the program's usability by retrieving the API key from the computer's environment variables instead of hardcoding it into the program.

10:02

🛠️ Enhancing the Python Program with ChatGPT API

This paragraph delves into enhancing the Python program to make it more user-friendly and efficient. It discusses the structure of the JSON response from the ChatGPT API and how to extract the desired information, such as the response content and token consumption. The script also introduces additional parameters like 'max_tokens' and 'temperature' that can be used to control the API's behavior. The paragraph outlines the steps to execute the program and observe the effects of these parameters on the output. The goal is to create a simple application that allows users to input questions and receive responses from ChatGPT, while also tracking the token usage.

15:04

🌐 Building a Chat Application with ChatGPT API in Python

The final paragraph describes the process of building a simple chat application using the ChatGPT API and Python's tkinter library. It outlines the steps to design the GUI, including creating input and output text boxes, and a button to submit questions. The script explains how to define a function that processes user input, sends it to ChatGPT, and displays the response along with the accumulated token count. The paragraph also addresses how to maintain the context of a conversation by appending previous questions and responses to subsequent API calls. The video concludes by demonstrating the completed application in action, showcasing its ability to continue a conversation with ChatGPT and track token usage effectively.

Mindmap

Keywords

💡API

API stands for Application Programming Interface, which is a set of rules and protocols for building and interacting with software applications. In the context of the video, the API is used to connect a Python program with ChatGPT, allowing the integration of ChatGPT's capabilities into various business processes and automating tasks. The script discusses using the API to securely utilize ChatGPT without exposing sensitive information to OpenAI's learning algorithms.

💡ChatGPT

ChatGPT is an advanced language model developed by OpenAI that can generate human-like text based on given prompts. The video's theme revolves around using ChatGPT through a Python program via API, highlighting its potential for business efficiency and automation. The script mentions that while using ChatGPT freely might expose information to OpenAI, using it via API ensures the information is not used for learning purposes.

💡Python

Python is a widely used high-level programming language known for its readability and versatility. In the video, Python is chosen as the programming language to create a program that interfaces with ChatGPT's API. The script provides a step-by-step guide on setting up and using Python to build an application that can interact with ChatGPT, demonstrating its role in automating and integrating AI capabilities.

💡Token

In the context of the video, a 'token' refers to the unit of measurement for the usage of the ChatGPT API. The cost of using the API is calculated based on the number of tokens consumed, with each token representing a character or word in the interaction. The script explains that tokens are consumed not only for user inputs but also for ChatGPT's responses, emphasizing the importance of being mindful of token usage to avoid high costs.

💡OpenAI

OpenAI is a research and development company that creates AI technologies, including ChatGPT. The video discusses using OpenAI's ChatGPT service through its API for business and government purposes. The script mentions that OpenAI does not use data from API connections for learning, which is a key point for organizations concerned about data privacy.

💡Business Efficiency

Business efficiency refers to the optimization of processes to reduce waste and increase productivity. The video script discusses how integrating ChatGPT via API into business processes can lead to increased efficiency, as it can automate tasks such as summarizing documents or answering queries, which would otherwise require manual effort.

💡Data Privacy

Data privacy is the protection of information from unauthorized access or disclosure. The script highlights the importance of data privacy by explaining that using ChatGPT's API, as opposed to the free version, ensures that the information provided by businesses or governments is not used for AI training, thus maintaining confidentiality.

💡Automation

Automation refers to the use of technology to perform tasks without human intervention. The video's main theme includes the potential of ChatGPT's API to automate various business tasks. The script provides examples of how a Python program can be used to automate interactions with ChatGPT, leading to more efficient workflows.

💡tkinter

Tkinter is a Python library used for creating graphical user interfaces (GUIs). In the script, tkinter is mentioned as the library used to build the GUI for the ChatGPT application. The video demonstrates creating a simple interface that allows users to input questions and receive answers from ChatGPT, showcasing tkinter's role in building interactive applications.

💡JSON

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and for machines to parse and generate. The video script refers to the JSON structure of the output data from the ChatGPT API, explaining how to extract information such as the response from ChatGPT and the number of tokens used, which is crucial for understanding the API's response and managing costs.

Highlights

Introduction of using ChatGPT via API connection in Python programs.

Reasons for using Python and API for ChatGPT include business efficiency and privacy concerns.

Free use of ChatGPT may lead to information being used for AI training by OpenAI.

API connection assures that the utilized information is not used for OpenAI's learning.

Automating various tasks by integrating ChatGPT into Python programs through API.

Cost of using ChatGPT's GPT3.5 API as of April 2023 is $0.002 per 1000 tokens.

Token consumption varies with language, with Japanese characters sometimes consuming 2 tokens per character.

Long responses from ChatGPT can lead to significant token consumption in a single interaction.

Continued conversations with ChatGPT require previous interactions to be fed into the system, consuming more tokens.

Potential for high token consumption with repeated interactions, exceeding the API's token limit.

Free tier for individual users for the first 3 months, with a limit of $18.

Instructions on accessing OpenAI's homepage and navigating to the Developers section.

Guide on setting up initial configurations such as organization name and payment methods.

Process of obtaining an API key from OpenAI's platform.

Installation of OpenAI's library in Python using pip and the importance of updating to the latest version.

Creating a Python program to utilize ChatGPT's API with steps to import OpenAI and set the API key.

Explanation of the JSON structure of the output data from ChatGPT's API.

Incorporating parameters like max_tokens and temperature to control the response from ChatGPT.

Building a simple GUI application using tkinter to interact with ChatGPT.

Developing a function to handle user inputs and display responses from ChatGPT within the GUI.

Implementation of a feature to continue conversations with ChatGPT by appending previous interactions.

Completed program allows for a series of questions and responses while tracking token consumption.

Encouragement for viewers to try creating their own applications using ChatGPT's API.

Transcripts

play00:04

以前の動画でChatGPTの使い方をご紹介しましたが、

play00:08

今回は、PythonプログラムからAPI接続で

play00:12

ChatGPTを使ってみようと思います。

play00:16

せっかく、無料で使えるChatGPTを、なぜ、 PythonプログラムでAPI接続で使うのか

play00:23

についてですが、最近、企業や政府や市町村などの 役所も、業務効率化の為に、ChatGPTを活用しようと

play00:31

話題になっています。

play00:33

しかし、ChatGPTを無料で使うと、その情報は、 OpenAIに取得され、AIの学習で利用される

play00:41

可能性があると言われています。

play00:43

もちろん、ChatGPTに質問をするだけであれば、 単に、Google検索で調べものをすることと変わりませんので

play00:51

問題ありません。

play00:53

ただ、企業の社内文章をChatGPTに投げて要約してもらうと、 場合によっては、機密情報や個人情報が、

play01:00

学習に使われると、困ります。

play01:04

一方、ChatGPTにAPIで接続して、利用した情報は、 OpenAIは学習には活用しないと言っています。

play01:13

それらの理由から、企業や政府、市町村では、 ChatGPTをAPIで利用するケースが多いようです。

play01:20

さらに、PythonプログラムからAPI接続でChatGPTを 利用できれば、様々な業務の中に ChatGPTを組み込むことが

play01:29

出来ますので、業務の自動化も可能になってきます。

play01:33

それらの理由から、今回は、ChatGPTをPythonプログラム からAPI接続で利用する方法をご紹介したいと思います。

play01:44

まず、ChatGPTのAPIの利用料についてご紹介します。

play01:49

2023年4月時点での、ChatGPTの GPT3.5の API利用料は 1000トークンあたり、0.002ドルです。

play01:59

日本円で0.2~0.3円といったところです。

play02:02

1000トークンとは、英単語1つで1トークンと言われて いますが、日本語の場合、1文字で1トークン、漢字などの場合、

play02:10

2トークン消費することもあるようですので500~1000文字 で1000トークンという計算になります。

play02:17

また、トークンの消費は、質問だけではなく、ChatGPTからの 回答もカウントに入りますので、長い回答が返ってくると、

play02:25

1回のやりとりで 結構消費してしまいます。

play02:29

実際のAPI処理でトークン数を出力した結果がこちらです。

play02:34

「ChatGPTについて教えてください。」という同じ質問を、 日本語と英語で実施したところ、

play02:40

英語では、240トークン消費したことに対して、日本語では、 349トークンの消費となり、およそ1.5倍消費してしまいました。

play02:50

さらに、ChatGPTに対して、続きの質問をする場合、 前の話を引き継いで回答を得るためには、2回目以降は、

play02:59

それまでの質問や、回答も与える必要があり、 そこでもトークンを消費します。

play03:05

例えば、1回あたり、300トークンだとします。

play03:09

それに対して、2回目は、1回目の結果も投げる為、600トークン を消費し、合わせて900トークン消費したことになります。

play03:18

そして、それを繰り返すと、5回目で、GPT3.5のAPI接続における 上限トークンの4096を超えてしまいます。

play03:27

ですので、たとえ1000トークンが0.2円だとしても、ちょっと使うと、 すぐに1円分が課金されるので、注意が必要です。

play03:37

ただし、個人利用の場合、最初から3カ月間は、 18ドル分が無料で使えるようです。

play03:44

私の場合、2月に利用を開始したので、5月1日まで無料枠が 使えるようですので、せっかくなので、使ってみようと思います。

play03:55

まず、OpenAIのトップページに接続します。

play03:58

そして、上部のメニューの「Developers」をクリックし、 「Overview」をクリックします。

play04:05

そして、既に、ChatGPTのアカウントがある方は、Loginをします。

play04:10

また、まだアカウントが無い場合は、「サインアップ」を クリックして、先にアカウントを作成します。

play04:17

アカウントの作成方法などは、以前の動画をご覧ください。

play04:27

ログインが完了すると、まずは、初期設定をします。

play04:32

画面、左側のSettingsをクリックすると、 このような画面が表示されます。

play04:37

ここでは、組織名を設定します。

play04:41

個人の場合は、Personalのままで良いです。

play04:44

そして、次に、支払い方法を設定します。

play04:48

左メニューの「Billing」の中の「Overview」をクリックします。

play04:52

そして、SetupPaidAcountをクリックします。

play04:56

そして、I'm an individual」を選択します。

play05:00

そして、クレジットカード番号などの情報を入力します。

play05:04

支払い方法の設定が完了すると、 支払い限度額を設定することも出来ます。

play05:10

例えば、初期設定では96ドルで通知メールが届き、 120ドルが上限になっているようです。

play05:18

これで、支払い方法の設定は完了です。

play05:21

続いて、APIキーを取得します。

play05:24

左メニューの、API Keysをクリックし、画面中央の、 CreateNewSecretKeyのボタンをクリックします。

play05:32

そして、適当な名前を入力して、 CreateSecretKeyをクリックします。

play05:38

そして、表示されたキーをコピーして、 メモ帳などに保存し、Doneをクリックします。

play05:44

APIキーは、複数取得できるので、利用目的に 応じて設定するのが良いと思います。

play05:51

これで、APIキーの取得が完了です。

play05:56

では、Pythonプログラムを作成していきたいと思います。

play06:00

Pythonプログラムの作成はVSCodeでしますので、 まずは、VSCodeを起動します。

play06:07

そして、ファイルを新規作成し、Pythonプログラムとして 適当な名前で保存します。

play06:20

まず、最初に、OpenAIのライブラリーをインストールします。

play06:24

pip install OpenAI でインストールします。

play06:35

ここで、注意が必要なのは、過去にOpenAIのライブラリーを インストールしたことがある場合は、

play06:41

最新のライブラリーをインストールする為に、 --upgradeオプションを付けて、インストールしてください。

play06:48

pip install --upgrade OpenAI となります。

play06:59

2023年4月時点のOpenAIのライブラリーの バージョンは、0.27.4になります。

play07:06

それでは、プログラムを作成していきたいと思います。

play07:10

まず、import OpenAIでOpenAIをインポートします。

play07:15

次に、APIキーをセットします。

play07:18

APIキーは、先ほど取得した文字コードになります。

play07:22

次に、response = openai.ChatCompletion .createと入力します。

play07:27

そして、model="gpt-3.5-turbo"とします。

play07:32

次に、ChatGPTに送信する messagesを設定していきます。

play07:37

このように、roleはuser、contentに、 ChatGPTについて教えてください。とセットします。

play07:46

そして、カッコを閉じます。

play07:48

そして、結果をprint文で出力します。

play07:52

このように記載します。

play07:55

それでは、処理を実行します。

play07:59

すると、しばらく時間がかかって、 このように、答えが出力されました。

play08:07

ここまで、わずか10行のプログラムで、ChatGPTがAPIで 使えるようになりましたが、ここからは、

play08:14

プログラムを使いやすいように改造していきたいと思います。

play08:18

まず、プログラムの最初の、APIキーを設定する箇所ですが、 プログラムの中で 丸見えなのは良くありませんので、

play08:25

この部分を、各パソコンの環境変数から 取得出来るようにしたいと思います。

play08:31

まず、プログラムの中では、このように、 「os.getenv("OPENAI_API_KEY")」と書き換えます。

play08:39

また、import osで OSをインポートしておきます。

play08:45

そして、環境変数に登録するには、Windowsの場合、 コントロールパネルから、ユーザーアカウントをクリックします。

play08:53

そして、「環境変数の変更」をクリックします。

play08:57

そして、「新規」をクリックして、「変数名」にOPENAI_API_KEYを 入力し、変数値にAPIキーの文字列を入力します。

play09:07

これによって、プログラムの中では、 APIキーを表示する必要が無くなります。

play09:14

次に、OpenAIの公式サイトのChatGPTの 解説ページを見てみます。

play09:20

DevelopersのOverViewから「Chat」をクリックします。

play09:27

すると、画面の中盤に、出力データのJSON形式の 構造についての記載が有ります。

play09:35

先ほど、ChatGPTからの回答結果を取り出すのに、 このような記載をしていたのは、このJSON形式の構造の中から、

play09:42

この部分を取り出すのに、response.choices[0] ["message"]["content"].strip()と記載していました。

play09:50

ここで、strip()は、前後の空白を取り除くという意味になります。

play09:55

そして、この構造を見ると、この部分で利用した トークン数を取得できることが分かります。

play10:02

最初がprompt_tokensで質問で使ったトークン数、 2つ目が、completion_tokensで、ChatGPTからの

play10:10

回答によるトークン数、最後がその合計だと分かります。

play10:14

そこで、プログラムの中で、このように記述して、 トークン数を出力するようにしたいと思います。

play10:21

また、処理のパラメータには、max_tokensという パラメータがあり、例えば100を指定すると、

play10:27

回答が100トークンで停止してくれます。

play10:32

さらに、temperatureというパラメータは、0~2の値を 設定でき、値が大きくなると、多様な回答が得られ、

play10:39

値が低いと、一貫性のある回答をしてくれるそうです。

play10:45

初期値は1だということで、0.5を設定したいと思います。

play10:53

では、これで実行してみたいと思います。

play10:57

処理を実行すると、このように、回答結果が100トークン の途中で停止し、消費したトークン数が

play11:04

このように表示されました。

play11:06

また、temperatureを0.5に設定した為か、 回答内容が若干、硬くなった気がします。

play11:15

では、ここからは、先ほどのプログラムを応用して、 ちょっとしたアプリを作成してみたいと思います。

play11:22

完成イメージは、このように、画面上部に質問を 入力して、下部に、その結果を表示する構成にします。

play11:30

また、通常のChatと同様に、何度か質問を 繰り返せるようにします。

play11:35

さらに、トークン数もカウント出来るようにしたいと思います。

play11:42

では、プログラムを作成していきます。

play11:45

まず、第1ステップとして、1回の質問が 出来る構成にしたいと思います。

play11:50

画面は、tkinterで作成したいと思いますので、 import tkinter as tkでtkinterをインポートします。

play12:00

画面設計としては、このようにroot = tk.Tk()で 画面全体を宣言します。

play12:06

アプリのタイトルをroot.title ("ChatGPTアプリ")で設定します。

play12:11

そして、質問のラベルをセットします。

play12:15

そして、質問を入力する為の、 インプットボックスを配置します。

play12:20

input_box=tk.Text(root, width=100, height=5) とし、横幅を100、高さを5で設定します。

play12:31

次に、回答のラベルを配置します。

play12:34

そして、回答の結果をセットする、 テキストボックスを配置します。

play12:39

output_box=tk.Text(root, width=100, height=20) とし、幅を100、高さを20に設定します。

play12:50

そして、質問ボタンを配置します。

play12:53

コマンドとして、answer関数を呼び出すようにします。

play12:57

そして、一番下に、トークン数を表示するラベルをセットします。

play13:02

また、最後にmainloopをセットし、 画面を入力待ちでループするようにします。

play13:08

これで、画面設計は完了です。

play13:11

続いて、質問ボタンを押した際に処理される answer関数を作成していきます。

play13:21

まず、質問用のインプットボックスに入力された 文字列を、question変数に代入します。

play13:27

そして、ChatGPTに投げる為に、 messages変数に、LIST型変数をセットします。

play13:34

そして、先ほど作成したプログラムの 構成でAPIの処理を記述します。

play13:40

今回のmax_tokensの上限は 400にしておきたいと思います。

play13:44

messagesパラメータには、先ほどの messages変数をセットします。

play13:49

ChatGPTからの回答は、変数respに代入します。

play13:54

また、トークン数は変数tokenに代入します。

play14:02

また、一度入力した質問は、入力欄からは消して、 回答用のテキストボックスに質問として記述し、

play14:10

その後に改行を挟んで、回答を記述するようにします。

play14:14

また、一番下に配置したトークン数も更新します。

play14:18

では、これで、一旦、実行してみたいと思います。

play14:32

すると、このように、質問に対して、結果が、 回答用のテキストボックスにセットされました。

play14:39

また、トークン数も表示されています。

play14:42

ここまでは、良い感じですので、引き続き、質問を 継続できるように機能追加していきたいと思います。

play14:51

ChatGPTに対して、1回目の内容を引き継いで、 2回目の質問をするには、1回目の質問と回答も

play14:58

2回目のメッセージにセットする必要があります。

play15:03

OpenAIのサイトにも、このような形式で、 メッセージをセットするよう記載されています。

play15:10

質問文は、roleをuserとし、ChatGPTの回答は、 roleをassistantとする必要があるようです。

play15:18

それらを踏まえて、プログラムを書き換えていきます。

play15:21

まず、最初に変数messagesをリスト型で空で宣言します。

play15:27

また、変数tokenも最初に0でリセットしておきます。

play15:32

そして、answer関数では、その2つの変数を global変数として宣言します。

play15:38

そして、代入していたmessagesをappendに することで、質問をするたびに、追加する構成にします。

play15:46

また、変数tokenは、今回、消費した トークン数を加算するようにします。

play15:52

そして、ChatGPTからの回答は、次回の質問の為に、 messages変数の後にroleをassistantにして、

play15:59

追加しておきます。

play16:01

これで、プログラムの変更は完了ですので、 処理を実行したいと思います。

play16:09

まず、1つ目の質問として、 「ChatGPTは何が得意ですか」と聞きます。

play16:18

すると、回答がこのように出力されました。

play16:22

トークン数も、177と計算され、正常に動作しています。

play16:27

そして、続きの質問として、 「プログラムレビューも出来ますか」と聞きます。

play16:33

すると、2回目の質問では、ChatGPTという言葉を質問文に 入れていないのに、回答は、ChatGPTに対する

play16:41

質問であることを理解して回答してくれていますので、 1回目の質問を継続してくれています。

play16:48

また、トークン数も578と累計をカウント できていますので、プログラムは完成です。

play16:58

以上、今回は、ChatGPTを、Pythonプログラムから API接続で使ってみました。

play17:05

わずか40行程度の短いプログラムで、見た目はあまり 良くないですが、ChatGPTの基本機能は

play17:12

使えるようになっており、意外と簡単に出来たと思います。

play17:16

そして、このプログラムをベースに、様々な応用が 可能ではないかと思います。

play17:22

また、このプログラムを作成するにあたって、ChatGPTの APIを何度も利用しましたが、実際の利用料は

play17:29

0.02ドルと 約3円程度で、さらに無料期間でしたので、 実際には料金はかかりませんでした。

play17:38

皆さんも、一度、トライしてみては如何でしょうか

play17:42

最後まで、ご視聴、ありがとうございました。

Rate This

5.0 / 5 (0 votes)

Related Tags
ChatGPTAPI IntegrationPython ProgrammingBusiness EfficiencyAI LearningData PrivacyToken UsageAutomated ResponsesCoding TutorialTechnical Guide