ChatGPTAutomation は松尾研のHPでも紹介されている、ChatGptを操作するライブラリです。
ChatGPTAutomationを実行すると、次のようにGoogle Chromeが起動します。
起動するだけのコードは次の通りです。
from chatgpt_automation.chatgpt_automation import ChatGPTAutomation
chat_bot = ChatGPTAutomation(
username="xxxxx@gmail.com", # Optional
password="xxxxx" # Optional
)
これを実行した時に、次のようにエラーとなることがあります。
noricgeographic@MacBook-Air-6 % python3 ./src/main.py
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/chatgpt_automation/chatgpt_automation.py", line 77, in __init__
chrome_driver_path = ChromeDriverManager().install()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/webdriver_manager/chrome.py", line 39, in install
driver_path = self._get_driver_path(self.driver)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/webdriver_manager/core/manager.py", line 30, in _get_driver_path
file = self._download_manager.download_file(driver.get_url())
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/webdriver_manager/core/download_manager.py", line 28, in download_file
response = self._http_client.get(url)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/webdriver_manager/core/http.py", line 33, in get
self.validate_response(resp)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/webdriver_manager/core/http.py", line 16, in validate_response
raise ValueError(f"There is no such driver by url {resp.url}")
ValueError: There is no such driver by url https://chromedriver.storage.googleapis.com/126.0.6478/chromedriver_mac_arm64.zip
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/noricgeographic/xxxx/./src/main.py", line 6, in <module>
chat_bot = ChatGPTAutomation(
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/chatgpt_automation/chatgpt_automation.py", line 78, in __init__
except ChromeDriverManagerException as e:
NameError: name 'ChromeDriverManagerException' is not define
ChromeDriverManager().install() をした時に、ブラウザに対応するChromeDriverが見つからないというエラーです。
こちらは、下記コマンドによって解消できました。
noricgeographic@MacBook-Air-6 ~ % pip install -U webdriver-manager
Requirement already satisfied: webdriver-manager in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (3.8.5)
Collecting webdriver-manager
Downloading webdriver_manager-4.0.1-py2.py3-none-any.whl.metadata (12 kB)
Requirement already satisfied: requests in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from webdriver-manager) (2.28.2)
Requirement already satisfied: python-dotenv in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from webdriver-manager) (0.21.1)
Requirement already satisfied: packaging in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from webdriver-manager) (23.0)
Requirement already satisfied: charset-normalizer<4,>=2 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from requests->webdriver-manager) (3.0.1)
Requirement already satisfied: idna<4,>=2.5 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from requests->webdriver-manager) (3.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from requests->webdriver-manager) (1.26.13)
Requirement already satisfied: certifi>=2017.4.17 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from requests->webdriver-manager) (2024.6.2)
Downloading webdriver_manager-4.0.1-py2.py3-none-any.whl (27 kB)
Installing collected packages: webdriver-manager
Attempting uninstall: webdriver-manager
Found existing installation: webdriver-manager 3.8.5
Uninstalling webdriver-manager-3.8.5:
Successfully uninstalled webdriver-manager-3.8.5
Successfully installed webdriver-manager-4.0.1
webdriver-manager をアップデートすれば良いようです。
Selenium系は動かすまでが一苦労ですね。。
続けて次のように書くことで、ChatGptからの回答を取得できるようです。
chat_bot.send_prompt_to_chatgpt("元気?")
response = chat_bot.return_last_response()
しかし、現在のChatGPTの仕様が変わったためか、次のようなエラーとなり回答を得ることができませんでした。
An unexpected error occurred: Message: invalid selector
from javascript error: {"status":32,"value":"An invalid or illegal selector was specified"}
(Session info: chrome=126.0.6478.115)
Stacktrace:
0 chromedriver 0x00000001005cfa20 chromedriver + 4389408
1 chromedriver 0x00000001005c832c chromedriver + 4358956
2 chromedriver 0x00000001001e4afc chromedriver + 281340
3 chromedriver 0x00000001001e8fcc chromedriver + 298956
4 chromedriver 0x00000001001eae44 chromedriver + 306756
5 chromedriver 0x00000001001eaebc chromedriver + 306876
6 chromedriver 0x00000001002270e4 chromedriver + 553188
7 chromedriver 0x000000010025fcec chromedriver + 785644
8 chromedriver 0x000000010021bed0 chromedriver + 507600
9 chromedriver 0x000000010021c8a8 chromedriver + 510120
10 chromedriver 0x00000001005973a4 chromedriver + 4158372
11 chromedriver 0x000000010059be08 chromedriver + 4177416
12 chromedriver 0x000000010057d064 chromedriver + 4051044
13 chromedriver 0x000000010059c6f4 chromedriver + 4179700
14 chromedriver 0x0000000100570064 chromedriver + 3997796
15 chromedriver 0x00000001005ba0bc chromedriver + 4300988
16 chromedriver 0x00000001005ba238 chromedriver + 4301368
17 chromedriver 0x00000001005c7f24 chromedriver + 4357924
18 libsystem_pthread.dylib 0x0000000185cb2f94 _pthread_start + 136
19 libsystem_pthread.dylib 0x0000000185cadd34 thread_start + 8
ChatGptの回答欄は<textarea>となっていますが、ChatGPTAutomationが追いついていないのでしょう。
今はChatGPTAutomationによる自動化は諦めました。
問題なく動かせてる人がいたら、環境等教えてくださいm(_ _)m