這裡是因為 Colab 改版, 目前才需要這麼複雜的安裝。
!pip install -U tensorflow-probability
!pip install typing-extensions --upgrade
!pip install gradio
!pip install langchain
!pip install openai
Requirement already satisfied: tensorflow-probability in /usr/local/lib/python3.10/dist-packages (0.22.0) Collecting tensorflow-probability Downloading tensorflow_probability-0.23.0-py2.py3-none-any.whl (6.9 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.9/6.9 MB 18.2 MB/s eta 0:00:00 Requirement already satisfied: absl-py in /usr/local/lib/python3.10/dist-packages (from tensorflow-probability) (1.4.0) Requirement already satisfied: six>=1.10.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow-probability) (1.16.0) Requirement already satisfied: numpy>=1.13.3 in /usr/local/lib/python3.10/dist-packages (from tensorflow-probability) (1.23.5) Requirement already satisfied: decorator in /usr/local/lib/python3.10/dist-packages (from tensorflow-probability) (4.4.2) Requirement already satisfied: cloudpickle>=1.3 in /usr/local/lib/python3.10/dist-packages (from tensorflow-probability) (2.2.1) Requirement already satisfied: gast>=0.3.2 in /usr/local/lib/python3.10/dist-packages (from tensorflow-probability) (0.5.4) Requirement already satisfied: dm-tree in /usr/local/lib/python3.10/dist-packages (from tensorflow-probability) (0.1.8) Installing collected packages: tensorflow-probability Attempting uninstall: tensorflow-probability Found existing installation: tensorflow-probability 0.22.0 Uninstalling tensorflow-probability-0.22.0: Successfully uninstalled tensorflow-probability-0.22.0 Successfully installed tensorflow-probability-0.23.0 Requirement already satisfied: typing-extensions in /usr/local/lib/python3.10/dist-packages (4.5.0) Collecting typing-extensions Downloading typing_extensions-4.9.0-py3-none-any.whl (32 kB) Installing collected packages: typing-extensions Attempting uninstall: typing-extensions Found existing installation: typing_extensions 4.5.0 Uninstalling typing_extensions-4.5.0: Successfully uninstalled typing_extensions-4.5.0 Successfully installed typing-extensions-4.9.0 Collecting gradio Downloading gradio-4.10.0-py3-none-any.whl (16.6 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 16.6/16.6 MB 54.7 MB/s eta 0:00:00 Collecting aiofiles<24.0,>=22.0 (from gradio) Downloading aiofiles-23.2.1-py3-none-any.whl (15 kB) Requirement already satisfied: altair<6.0,>=4.2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (4.2.2) Collecting fastapi (from gradio) Downloading fastapi-0.105.0-py3-none-any.whl (93 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 93.1/93.1 kB 16.0 MB/s eta 0:00:00 Collecting ffmpy (from gradio) Downloading ffmpy-0.3.1.tar.gz (5.5 kB) Preparing metadata (setup.py) ... done Collecting gradio-client==0.7.3 (from gradio) Downloading gradio_client-0.7.3-py3-none-any.whl (304 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 304.8/304.8 kB 41.1 MB/s eta 0:00:00 Collecting httpx (from gradio) Downloading httpx-0.25.2-py3-none-any.whl (74 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 75.0/75.0 kB 11.6 MB/s eta 0:00:00 Requirement already satisfied: huggingface-hub>=0.19.3 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.19.4) Requirement already satisfied: importlib-resources<7.0,>=1.3 in /usr/local/lib/python3.10/dist-packages (from gradio) (6.1.1) Requirement already satisfied: jinja2<4.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.1.2) Requirement already satisfied: markupsafe~=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.1.3) Requirement already satisfied: matplotlib~=3.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.7.1) Requirement already satisfied: numpy~=1.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (1.23.5) Collecting orjson~=3.0 (from gradio) Downloading orjson-3.9.10-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (138 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 138.7/138.7 kB 7.7 MB/s eta 0:00:00 Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from gradio) (23.2) Requirement already satisfied: pandas<3.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (1.5.3) Requirement already satisfied: pillow<11.0,>=8.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (9.4.0) Collecting pydantic>=2.0 (from gradio) Downloading pydantic-2.5.2-py3-none-any.whl (381 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 381.9/381.9 kB 50.1 MB/s eta 0:00:00 Collecting pydub (from gradio) Downloading pydub-0.25.1-py2.py3-none-any.whl (32 kB) Collecting python-multipart (from gradio) Downloading python_multipart-0.0.6-py3-none-any.whl (45 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 45.7/45.7 kB 6.4 MB/s eta 0:00:00 Requirement already satisfied: pyyaml<7.0,>=5.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (6.0.1) Collecting semantic-version~=2.0 (from gradio) Downloading semantic_version-2.10.0-py2.py3-none-any.whl (15 kB) Collecting tomlkit==0.12.0 (from gradio) Downloading tomlkit-0.12.0-py3-none-any.whl (37 kB) Requirement already satisfied: typer[all]<1.0,>=0.9 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.9.0) Requirement already satisfied: typing-extensions~=4.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (4.9.0) Collecting uvicorn>=0.14.0 (from gradio) Downloading uvicorn-0.24.0.post1-py3-none-any.whl (59 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 59.7/59.7 kB 10.4 MB/s eta 0:00:00 Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from gradio-client==0.7.3->gradio) (2023.6.0) Collecting websockets<12.0,>=10.0 (from gradio-client==0.7.3->gradio) Downloading websockets-11.0.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (129 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 129.9/129.9 kB 18.1 MB/s eta 0:00:00 Requirement already satisfied: entrypoints in /usr/local/lib/python3.10/dist-packages (from altair<6.0,>=4.2.0->gradio) (0.4) Requirement already satisfied: jsonschema>=3.0 in /usr/local/lib/python3.10/dist-packages (from altair<6.0,>=4.2.0->gradio) (4.19.2) Requirement already satisfied: toolz in /usr/local/lib/python3.10/dist-packages (from altair<6.0,>=4.2.0->gradio) (0.12.0) Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.3->gradio) (3.13.1) Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.3->gradio) (2.31.0) Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.3->gradio) (4.66.1) Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (1.2.0) Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (0.12.1) Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (4.46.0) Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (1.4.5) Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (3.1.1) Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (2.8.2) Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0,>=1.0->gradio) (2023.3.post1) Collecting annotated-types>=0.4.0 (from pydantic>=2.0->gradio) Downloading annotated_types-0.6.0-py3-none-any.whl (12 kB) Collecting pydantic-core==2.14.5 (from pydantic>=2.0->gradio) Downloading pydantic_core-2.14.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.1/2.1 MB 77.3 MB/s eta 0:00:00 Requirement already satisfied: click<9.0.0,>=7.1.1 in /usr/local/lib/python3.10/dist-packages (from typer[all]<1.0,>=0.9->gradio) (8.1.7) Collecting colorama<0.5.0,>=0.4.3 (from typer[all]<1.0,>=0.9->gradio) Downloading colorama-0.4.6-py2.py3-none-any.whl (25 kB) Collecting shellingham<2.0.0,>=1.3.0 (from typer[all]<1.0,>=0.9->gradio) Downloading shellingham-1.5.4-py2.py3-none-any.whl (9.8 kB) Requirement already satisfied: rich<14.0.0,>=10.11.0 in /usr/local/lib/python3.10/dist-packages (from typer[all]<1.0,>=0.9->gradio) (13.7.0) Collecting h11>=0.8 (from uvicorn>=0.14.0->gradio) Downloading h11-0.14.0-py3-none-any.whl (58 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 58.3/58.3 kB 9.1 MB/s eta 0:00:00 Requirement already satisfied: anyio<4.0.0,>=3.7.1 in /usr/local/lib/python3.10/dist-packages (from fastapi->gradio) (3.7.1) Collecting starlette<0.28.0,>=0.27.0 (from fastapi->gradio) Downloading starlette-0.27.0-py3-none-any.whl (66 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 67.0/67.0 kB 10.1 MB/s eta 0:00:00 Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx->gradio) (2023.11.17) Collecting httpcore==1.* (from httpx->gradio) Downloading httpcore-1.0.2-py3-none-any.whl (76 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 76.9/76.9 kB 9.6 MB/s eta 0:00:00 Requirement already satisfied: idna in /usr/local/lib/python3.10/dist-packages (from httpx->gradio) (3.6) Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from httpx->gradio) (1.3.0) Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<4.0.0,>=3.7.1->fastapi->gradio) (1.2.0) Requirement already satisfied: attrs>=22.2.0 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (23.1.0) Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (2023.11.2) Requirement already satisfied: referencing>=0.28.4 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (0.32.0) Requirement already satisfied: rpds-py>=0.7.1 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (0.13.2) Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib~=3.0->gradio) (1.16.0) Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.10/dist-packages (from rich<14.0.0,>=10.11.0->typer[all]<1.0,>=0.9->gradio) (3.0.0) Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.10/dist-packages (from rich<14.0.0,>=10.11.0->typer[all]<1.0,>=0.9->gradio) (2.16.1) Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.19.3->gradio) (3.3.2) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.19.3->gradio) (2.0.7) Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/dist-packages (from markdown-it-py>=2.2.0->rich<14.0.0,>=10.11.0->typer[all]<1.0,>=0.9->gradio) (0.1.2) Building wheels for collected packages: ffmpy Building wheel for ffmpy (setup.py) ... done Created wheel for ffmpy: filename=ffmpy-0.3.1-py3-none-any.whl size=5579 sha256=00aa266b7affb906e664f30017d2db8771f2632ef4021f1aa372f33e407d3e43 Stored in directory: /root/.cache/pip/wheels/01/a6/d1/1c0828c304a4283b2c1639a09ad86f83d7c487ef34c6b4a1bf Successfully built ffmpy Installing collected packages: pydub, ffmpy, websockets, tomlkit, shellingham, semantic-version, python-multipart, pydantic-core, orjson, h11, colorama, annotated-types, aiofiles, uvicorn, starlette, pydantic, httpcore, httpx, fastapi, gradio-client, gradio Attempting uninstall: pydantic Found existing installation: pydantic 1.10.13 Uninstalling pydantic-1.10.13: Successfully uninstalled pydantic-1.10.13 ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. lida 0.0.10 requires kaleido, which is not installed. llmx 0.0.15a0 requires cohere, which is not installed. llmx 0.0.15a0 requires openai, which is not installed. llmx 0.0.15a0 requires tiktoken, which is not installed. Successfully installed aiofiles-23.2.1 annotated-types-0.6.0 colorama-0.4.6 fastapi-0.105.0 ffmpy-0.3.1 gradio-4.10.0 gradio-client-0.7.3 h11-0.14.0 httpcore-1.0.2 httpx-0.25.2 orjson-3.9.10 pydantic-2.5.2 pydantic-core-2.14.5 pydub-0.25.1 python-multipart-0.0.6 semantic-version-2.10.0 shellingham-1.5.4 starlette-0.27.0 tomlkit-0.12.0 uvicorn-0.24.0.post1 websockets-11.0.3 Collecting langchain Downloading langchain-0.0.351-py3-none-any.whl (794 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 794.3/794.3 kB 11.8 MB/s eta 0:00:00 Requirement already satisfied: PyYAML>=5.3 in /usr/local/lib/python3.10/dist-packages (from langchain) (6.0.1) Requirement already satisfied: SQLAlchemy<3,>=1.4 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.0.23) Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /usr/local/lib/python3.10/dist-packages (from langchain) (3.9.1) Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in /usr/local/lib/python3.10/dist-packages (from langchain) (4.0.3) Collecting dataclasses-json<0.7,>=0.5.7 (from langchain) Downloading dataclasses_json-0.6.3-py3-none-any.whl (28 kB) Collecting jsonpatch<2.0,>=1.33 (from langchain) Downloading jsonpatch-1.33-py2.py3-none-any.whl (12 kB) Collecting langchain-community<0.1,>=0.0.2 (from langchain) Downloading langchain_community-0.0.4-py3-none-any.whl (1.5 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.5/1.5 MB 20.9 MB/s eta 0:00:00 Collecting langchain-core<0.2,>=0.1 (from langchain) Downloading langchain_core-0.1.1-py3-none-any.whl (190 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 190.6/190.6 kB 24.0 MB/s eta 0:00:00 Collecting langsmith<0.1.0,>=0.0.70 (from langchain) Downloading langsmith-0.0.72-py3-none-any.whl (46 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 46.3/46.3 kB 6.2 MB/s eta 0:00:00 Requirement already satisfied: numpy<2,>=1 in /usr/local/lib/python3.10/dist-packages (from langchain) (1.23.5) Requirement already satisfied: pydantic<3,>=1 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.5.2) Requirement already satisfied: requests<3,>=2 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.31.0) Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /usr/local/lib/python3.10/dist-packages (from langchain) (8.2.3) Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (23.1.0) Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.4) Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.4) Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.4.0) Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1) Collecting marshmallow<4.0.0,>=3.18.0 (from dataclasses-json<0.7,>=0.5.7->langchain) Downloading marshmallow-3.20.1-py3-none-any.whl (49 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 49.4/49.4 kB 7.7 MB/s eta 0:00:00 Collecting typing-inspect<1,>=0.4.0 (from dataclasses-json<0.7,>=0.5.7->langchain) Downloading typing_inspect-0.9.0-py3-none-any.whl (8.8 kB) Collecting jsonpointer>=1.9 (from jsonpatch<2.0,>=1.33->langchain) Downloading jsonpointer-2.4-py2.py3-none-any.whl (7.8 kB) Requirement already satisfied: anyio<5,>=3 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2,>=0.1->langchain) (3.7.1) Requirement already satisfied: packaging<24.0,>=23.2 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2,>=0.1->langchain) (23.2) Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain) (0.6.0) Requirement already satisfied: pydantic-core==2.14.5 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain) (2.14.5) Requirement already satisfied: typing-extensions>=4.6.1 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain) (4.9.0) Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (3.3.2) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (3.6) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (2.0.7) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (2023.11.17) Requirement already satisfied: greenlet!=0.4.17 in /usr/local/lib/python3.10/dist-packages (from SQLAlchemy<3,>=1.4->langchain) (3.0.2) Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3->langchain-core<0.2,>=0.1->langchain) (1.3.0) Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3->langchain-core<0.2,>=0.1->langchain) (1.2.0) Collecting mypy-extensions>=0.3.0 (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain) Downloading mypy_extensions-1.0.0-py3-none-any.whl (4.7 kB) Installing collected packages: mypy-extensions, marshmallow, jsonpointer, typing-inspect, jsonpatch, langsmith, dataclasses-json, langchain-core, langchain-community, langchain Successfully installed dataclasses-json-0.6.3 jsonpatch-1.33 jsonpointer-2.4 langchain-0.0.351 langchain-community-0.0.4 langchain-core-0.1.1 langsmith-0.0.72 marshmallow-3.20.1 mypy-extensions-1.0.0 typing-inspect-0.9.0 Collecting openai Downloading openai-1.5.0-py3-none-any.whl (223 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 223.7/223.7 kB 7.4 MB/s eta 0:00:00 Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.10/dist-packages (from openai) (3.7.1) Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from openai) (1.7.0) Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from openai) (0.25.2) Requirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/lib/python3.10/dist-packages (from openai) (2.5.2) Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from openai) (1.3.0) Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.10/dist-packages (from openai) (4.66.1) Requirement already satisfied: typing-extensions<5,>=4.5 in /usr/local/lib/python3.10/dist-packages (from openai) (4.9.0) Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai) (3.6) Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai) (1.2.0) Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai) (2023.11.17) Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai) (1.0.2) Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai) (0.14.0) Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->openai) (0.6.0) Requirement already satisfied: pydantic-core==2.14.5 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->openai) (2.14.5) Installing collected packages: openai ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. llmx 0.0.15a0 requires cohere, which is not installed. llmx 0.0.15a0 requires tiktoken, which is not installed. Successfully installed openai-1.5.0
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
我們先來安裝 openai
套件, 還有快速打造 Web App 的 gradio
。
# 本來在這裡裝 openai 和 gradio, 但前面已經裝了
#
#!pip install openai
#!pip install gradio
首先使用 openai
套件。
from openai import OpenAI
把自己的金鑰貼上。
client = OpenAI(
# defaults to os.environ.get("OPENAI_API_KEY")
api_key="你的 API 金鑰",
)
ChatGPT API 的重點是要把之前對話的內容送給 ChatGPT, 然後他就會有個適當的回應!
角色 (role
) 一共有三種, 分別是:
system
: 這是對話機器人的「人設」user
: 使用者assistant
: ChatGPT 的回應prompt = "我今天心情很不好。"
messages = [{"role":"system", "content":"你是一個非常溫暖的對話機器人,回應都簡短,儘量不要超過二十個字,而且有同理心。"},
{"role": "user", "content": prompt}]
messages
[{'role': 'system', 'content': '你是一個非常溫暖的對話機器人,回應都簡短,儘量不要超過二十個字,而且有同理心。'}, {'role': 'user', 'content': '我今天心情很不好。'}]
現在我們來接 ChatGPT 的回應。
chat_completion = client.chat.completions.create(
messages=messages,
model="gpt-3.5-turbo",
)
chat_completion
ChatCompletion(id='chatcmpl-8XT4spSlQ6esL4VNGZQlbmEy39i50', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='怎麼了?可以跟我分享嗎?', role='assistant', function_call=None, tool_calls=None))], created=1702987290, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=16, prompt_tokens=77, total_tokens=93))
chat_completion.choices[0].message.content
'怎麼了?可以跟我分享嗎?'
reply = chat_completion.choices[0].message.content
reply
'怎麼了?可以跟我分享嗎?'
messages.append({"role":"assistant", "content":reply})
messages
[{'role': 'system', 'content': '你是一個非常溫暖的對話機器人,回應都簡短,儘量不要超過二十個字,而且有同理心。'}, {'role': 'user', 'content': '我今天心情很不好。'}, {'role': 'assistant', 'content': '怎麼了?可以跟我分享嗎?'}]
icon = "(*゚▽゚*): "
messages = [{"role":"system", "content":"你是一個非常溫暖的對話機器人,回應都簡短,儘量不要超過二十個字,而且有同理心。"}]
while True:
prompt = input('> ')
if 'bye' in prompt:
print('再見, 下次再聊!')
break
messages.append({"role": "user", "content": prompt})
chat_completion = client.chat.completions.create(
messages=messages,
model="gpt-3.5-turbo",
)
reply = chat_completion.choices[0].message.content
print(icon + reply)
print()
messages.append({"role": "assistant", "content": reply})
> 我覺得很難過 (*゚▽゚*): 為什麼呢?可以跟我說說嗎? > Python 都不會寫 (*゚▽゚*): 沒關係,學習一門程式語言需要時間和練習。你可以從基礎開始,慢慢學習,我相信你能掌握Python的!加油! > 同學都排擠我 (*゚▽゚*): 這讓你感到不舒服。試著與他們溝通,或尋找其他人與你有共同興趣的朋友。你不是孤單的,我在這裡支持你。 > 先這樣, bye 再見, 下次再聊!
gradio
打造 ChatGPT 網路應用程式!¶import gradio as gr
messages = [{"role":"system", "content":"你是一個非常溫暖的對話機器人,回應都簡短,儘量不要超過二十個字,而且有同理心。"}]
def pipi(prompt, history):
history = history or []
global messages
messages.append({"role": "user", "content": prompt})
chat_completion = client.chat.completions.create(
messages=messages,
model="gpt-3.5-turbo",
)
reply = chat_completion.choices[0].message.content
messages.append({"role": "assistant", "content": reply})
history = history + [[prompt, reply]]
return history, history
chatbot = gr.Chatbot()
iface = gr.Interface(pipi,
inputs=["text", "state"],
outputs=[chatbot, "state"],
title="療癒系拍拍機器人",
description="你有什麼心事, 歡迎來找我聊聊 :)")
iface.launch(share=True)
Colab notebook detected. To show errors in colab notebook, set debug=True in launch() Running on public URL: https://a070a50d7289189f1f.gradio.live This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)