LoginSignup
4
8

More than 1 year has passed since last update.

【Python】chatGPTを使ってチャットができるデスクトップアプリを作りたい

Posted at

概要

pythonとGPTの勉強がてら、
Windowsのデスクトップアプリケーションとして、
chatGPTを利用できるアプリを作ろうかな、と思ったので手順メモ

環境

  • OS: Windows 10
  • python3.11
  • GUIフレームワーク:pyQt5

GPT3については、Azure OpenAI Serviceを利用する

手順

Pythonインストール

こちらを参考に導入
https://www.python.jp/install/windows/install.html

「C:¥python」にインストールしました。手順は省略します。

pyQt5の導入

PowerShellで下記を実行

> C:¥python¥Scripts¥pip3.exe install pyQt5

Qt Designerのインストール

下記をインストール。
https://build-system.fman.io/qt-designer-download

OpenAIモジュールのインストール

> C:¥python¥Scripts¥pip3.exe install openai

Qt DesignerでGUIをデザインする

ひとまずこんなイメージの画面を作成してみた。
image.png

Qt Desingerでデザインをして、uiファイルとして保存する。
その後、pyファイルに変換をかける

> C:¥python¥Scripts¥pyuic5 .¥chabot.ui -o .¥output.py

pythonプログラム側

下記のように作成

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QStyleFactory
from output import Ui_MainWindow

# Azure OpenAI Serviceからコードを取得する
import openai
openai.api_type = "azure"
openai.api_base = ""
openai.api_version = ""
openai.api_key = ""

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

        # 送信ボタン
        self.pushButton.clicked.connect(self.buttonClicked)
        # 閉じるボタン
        self.pushButton_2.clicked.connect(self.closeWindow)

    # ---------------------------
    # 送信ボタンを押したときの動作
    # ---------------------------
    def buttonClicked(self):
        # テキストエリアに入力された値を取得
        input_text = self.plainTextEdit.toPlainText()

        self.textBrowser.append("あなた: " + input_text)
        self.textBrowser.append('')
        
        # -------------------------------------
        # この辺はAzure OpenAI Serviceから値を取得する
        response = openai.ChatCompletion.create(
  engine="",
  messages = [{"role":"system","content":"[AIの人格とかがあれば指示する]"},
              {"role":"user","content":input_text}],
  temperature=0.7,
  max_tokens=800,
  top_p=0.95,
  frequency_penalty=0,
  presence_penalty=0,
  stop=None)
        # -----------------------------------

        # chatGPTからのレスポンスを取得
        message = response.choices[0].message.content
        # 画面に表示
        self.textBrowser.append("彩香:" + message)
        self.textBrowser.append('')

    # ---------------------------
    # 閉じるボタンを押したときの動作
    # ---------------------------
    def closeWindow(self):
        self.close()

# ---------------------------
# メイン処理
# ---------------------------
if __name__ == '__main__':
    app = QApplication(sys.argv)
    QApplication.setStyle(QStyleFactory.create('Material'))
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec_())

せっかくなのでAIの人格はかわいい女の子にする(重要)

テスト

ひとまずテストをする。

image.png

いい感じ。

会話できるようにする

このままだと、一問一答でしか答えられず、会話ができないため、継続的な会話をできるようにする。
chatGPTに処理を投げるときのbodyで、messagesの内容に会話を保持させる。

・ユーザーの入力

input_text = self.plainTextEdit.toPlainText()

user_content = {"role": "user", "content": input_text}
message_json_array.append(user_content)

・chatGPTからの返答

input_text = self.plainTextEdit.toPlainText()

assistant_content = {"role": "assistant", "content": message}
message_json_array.append(assistant_content)

会話できるか確かめる

しばらく会話をしてみて、途中で最初に話していたことを聞いてみる。
image.png
ちゃんとはじめに喋っていたことを覚えているので、これで会話できるようになった。

今後改良していきたい点など

  • もう少し使いやすいUIにする
  • UIのデザインをオサレにする

この辺も出来たら記事にしていきたいなぁ、、

おわり

今回のコードを作るに当たって結構chatGPTを使ったけど、
やはり学習の初期導入にはかなり便利。
とはいえ、間違った情報を平気で与えてくるから、予備知識はやっぱり必要かな。
今後もchatGPTとうまく付き合っていきたい。

4
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
8