me1237guy@mincode:/mnt/c/Users/me123/python-llm-for-beginners-demo-code/basic-llm-demo$ python3 -m pip show python-dotenv


/usr/bin/python3: No module named pip

me1237guy@mincode:/mnt/c/Users/me123/python-llm-for-beginners-demo-code/basic-llm-demo$ sudo apt update
Get:1 http://archive.ubuntu.com/ubuntu resolute InRelease [136 kB]
Get:2 http://security.ubuntu.com/ubuntu resolute-security InRelease [137 kB]
Get:3 http://security.ubuntu.com/ubuntu resolute-security/main amd64 Packages [371 kB]
Get:4 http://archive.ubuntu.com/ubuntu resolute-updates InRelease [137 kB]
Get:5 http://security.ubuntu.com/ubuntu resolute-security/main Translation-en [89.6 kB]
Get:6 http://security.ubuntu.com/ubuntu resolute-security/main amd64 Components [32.7 kB]
Get:7 http://security.ubuntu.com/ubuntu resolute-security/main amd64 c-n-f Metadata [4616 B]
Get:8 http://security.ubuntu.com/ubuntu resolute-security/universe amd64 Packages [150 kB]
Get:9 http://security.ubuntu.com/ubuntu resolute-security/universe Translation-en [48.3 kB]
Get:10 http://security.ubuntu.com/ubuntu resolute-security/universe amd64 Components [43.2 kB]
Get:11 http://security.ubuntu.com/ubuntu resolute-security/universe amd64 c-n-f Metadata [3552 B]
Get:12 http://security.ubuntu.com/ubuntu resolute-security/restricted amd64 Packages [321 kB]
Get:13 http://security.ubuntu.com/ubuntu resolute-security/restricted Translation-en [61.2 kB]
Get:14 http://security.ubuntu.com/ubuntu resolute-security/restricted amd64 c-n-f Metadata [396 B]
Get:15 http://security.ubuntu.com/ubuntu resolute-security/multiverse amd64 Packages [9020 B]
Get:16 http://security.ubuntu.com/ubuntu resolute-security/multiverse Translation-en [2660 B]
Get:17 http://security.ubuntu.com/ubuntu resolute-security/multiverse amd64 Components [212 B]
Get:18 http://security.ubuntu.com/ubuntu resolute-security/multiverse amd64 c-n-f Metadata [120 B]
Get:19 http://archive.ubuntu.com/ubuntu resolute-backports InRelease [136 kB]
Get:20 http://archive.ubuntu.com/ubuntu resolute/main amd64 Packages [1480 kB]
Get:21 http://archive.ubuntu.com/ubuntu resolute/main Translation-en [524 kB]
Get:22 http://archive.ubuntu.com/ubuntu resolute/main amd64 Components [395 kB]
Get:23 http://archive.ubuntu.com/ubuntu resolute/main amd64 c-n-f Metadata [32.4 kB]
Get:24 http://archive.ubuntu.com/ubuntu resolute/universe amd64 Packages [16.0 MB]
Get:25 http://archive.ubuntu.com/ubuntu resolute/universe Translation-en [6329 kB]
Get:26 http://archive.ubuntu.com/ubuntu resolute/universe amd64 Components [4556 kB]
Get:27 http://archive.ubuntu.com/ubuntu resolute/universe amd64 c-n-f Metadata [313 kB]
Get:28 http://archive.ubuntu.com/ubuntu resolute/restricted amd64 Packages [152 kB]
Get:29 http://archive.ubuntu.com/ubuntu resolute/restricted Translation-en [25.8 kB]
Get:30 http://archive.ubuntu.com/ubuntu resolute/restricted amd64 Components [556 B]
Get:31 http://archive.ubuntu.com/ubuntu resolute/multiverse amd64 Packages [290 kB]
Get:32 http://archive.ubuntu.com/ubuntu resolute/multiverse Translation-en [127 kB]
Get:33 http://archive.ubuntu.com/ubuntu resolute/multiverse amd64 Components [50.0 kB]
Get:34 http://archive.ubuntu.com/ubuntu resolute/multiverse amd64 c-n-f Metadata [8276 B]
Get:35 http://archive.ubuntu.com/ubuntu resolute-updates/main amd64 Packages [446 kB]
Get:36 http://archive.ubuntu.com/ubuntu resolute-updates/main Translation-en [111 kB]
Get:37 http://archive.ubuntu.com/ubuntu resolute-updates/main amd64 Components [84.8 kB]
Get:38 http://archive.ubuntu.com/ubuntu resolute-updates/main amd64 c-n-f Metadata [5756 B]
Get:39 http://archive.ubuntu.com/ubuntu resolute-updates/universe amd64 Packages [235 kB]
Get:40 http://archive.ubuntu.com/ubuntu resolute-updates/universe Translation-en [76.9 kB]
Get:41 http://archive.ubuntu.com/ubuntu resolute-updates/universe amd64 Components [166 kB]
Get:42 http://archive.ubuntu.com/ubuntu resolute-updates/universe amd64 c-n-f Metadata [4704 B]
Get:43 http://archive.ubuntu.com/ubuntu resolute-updates/restricted amd64 Packages [331 kB]
Get:44 http://archive.ubuntu.com/ubuntu resolute-updates/restricted Translation-en [63.0 kB]
Get:45 http://archive.ubuntu.com/ubuntu resolute-updates/restricted amd64 c-n-f Metadata [392 B]
Get:46 http://archive.ubuntu.com/ubuntu resolute-updates/multiverse amd64 Packages [11.4 kB]
Get:47 http://archive.ubuntu.com/ubuntu resolute-updates/multiverse Translation-en [3032 B]
Get:48 http://archive.ubuntu.com/ubuntu resolute-updates/multiverse amd64 Components [216 B]
Get:49 http://archive.ubuntu.com/ubuntu resolute-updates/multiverse amd64 c-n-f Metadata [256 B]
Get:50 http://archive.ubuntu.com/ubuntu resolute-backports/main amd64 Components [212 B]
Get:51 http://archive.ubuntu.com/ubuntu resolute-backports/main amd64 c-n-f Metadata [112 B]
Get:52 http://archive.ubuntu.com/ubuntu resolute-backports/universe amd64 Packages [640 B]
Get:53 http://archive.ubuntu.com/ubuntu resolute-backports/universe Translation-en [300 B]
Get:54 http://archive.ubuntu.com/ubuntu resolute-backports/universe amd64 Components [216 B]
Get:55 http://archive.ubuntu.com/ubuntu resolute-backports/universe amd64 c-n-f Metadata [116 B]
Get:56 http://archive.ubuntu.com/ubuntu resolute-backports/restricted amd64 Components [216 B]
Get:57 http://archive.ubuntu.com/ubuntu resolute-backports/restricted amd64 c-n-f Metadata [120 B]
Get:58 http://archive.ubuntu.com/ubuntu resolute-backports/multiverse amd64 Components [216 B]
Get:59 http://archive.ubuntu.com/ubuntu resolute-backports/multiverse amd64 c-n-f Metadata [120 B]
Fetched 33.5 MB in 16s (2124 kB/s)
110 packages can be upgraded. Run 'apt list --upgradable' to see them.
me1237guy@mincode:/mnt/c/Users/me123/python-llm-for-beginners-demo-code/basic-llm-demo$ sudo apt install python3-pip
Installing:
python3-pip

Installing dependencies:
python3-wheel

Recommended packages:
build-essential python3-dev

Summary:
Upgrading: 0, Installing: 2, Removing: 0, Not Upgrading: 110
Download size: 1444 kB
Space needed: 10.5 MB / 1024 GB available

Continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu resolute/universe amd64 python3-wheel all 0.46.3-2 [27.9 kB]
Get:2 http://archive.ubuntu.com/ubuntu resolute/universe amd64 python3-pip all 25.1.1+dfsg-1ubuntu2 [1416 kB]
Fetched 1444 kB in 2s (682 kB/s)
Selecting previously unselected package python3-wheel.
(Reading database ... 35923 files and directories currently installed.)
Preparing to unpack .../python3-wheel_0.46.3-2_all.deb ...
Unpacking python3-wheel (0.46.3-2) ...
Selecting previously unselected package python3-pip.
Preparing to unpack .../python3-pip_25.1.1+dfsg-1ubuntu2_all.deb ...
Unpacking python3-pip (25.1.1+dfsg-1ubuntu2) ...
Setting up python3-wheel (0.46.3-2) ...
Setting up python3-pip (25.1.1+dfsg-1ubuntu2) ...
Processing triggers for man-db (2.13.1-1build1) ...

---------------------------------------------------------------------------------------------------------------------------

安裝虛擬環境套件

me1237guy@mincode:/mnt/c/Users/me123/python-llm-for-beginners-demo-code/basic-llm-demo$ sudo apt install python3-venv

Upgrading:
libpython3.14 libpython3.14-minimal libpython3.14-stdlib python3.14 python3.14-gdbm python3.14-minimal

Installing:
python3-venv

Installing dependencies:
python3-pip-whl python3-setuptools-whl python3.14-venv

Summary:
Upgrading: 6, Installing: 4, Removing: 0, Not Upgrading: 104
Download size: 12.0 MB
Space needed: 2993 kB / 1024 GB available

Continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu resolute-updates/main amd64 python3.14 amd64 3.14.4-1ubuntu0.1 [830 kB]
Get:2 http://archive.ubuntu.com/ubuntu resolute-updates/main amd64 libpython3.14 amd64 3.14.4-1ubuntu0.1 [2579 kB]
Get:3 http://archive.ubuntu.com/ubuntu resolute-updates/main amd64 libpython3.14-stdlib amd64 3.14.4-1ubuntu0.1 [2414 kB]
Get:4 http://archive.ubuntu.com/ubuntu resolute-updates/main amd64 python3.14-minimal amd64 3.14.4-1ubuntu0.1 [2580 kB]
Get:5 http://archive.ubuntu.com/ubuntu resolute-updates/main amd64 libpython3.14-minimal amd64 3.14.4-1ubuntu0.1 [916 kB]
Get:6 http://archive.ubuntu.com/ubuntu resolute-updates/main amd64 python3.14-gdbm amd64 3.14.4-1ubuntu0.1 [29.3 kB]
Get:7 http://archive.ubuntu.com/ubuntu resolute/universe amd64 python3-pip-whl all 25.1.1+dfsg-1ubuntu2 [1430 kB]
Get:8 http://archive.ubuntu.com/ubuntu resolute/universe amd64 python3-setuptools-whl all 78.1.1-0.1build1 [1174 kB]
Get:9 http://archive.ubuntu.com/ubuntu resolute-updates/universe amd64 python3.14-venv amd64 3.14.4-1ubuntu0.1 [5354 B]
Get:10 http://archive.ubuntu.com/ubuntu resolute/universe amd64 python3-venv amd64 3.14.3-0ubuntu2 [1032 B]
Fetched 12.0 MB in 8s (1414 kB/s)
(Reading database ... 36708 files and directories currently installed.)
Preparing to unpack .../0-python3.14_3.14.4-1ubuntu0.1_amd64.deb ...
Unpacking python3.14 (3.14.4-1ubuntu0.1) over (3.14.4-1) ...
Preparing to unpack .../1-libpython3.14_3.14.4-1ubuntu0.1_amd64.deb ...
Unpacking libpython3.14:amd64 (3.14.4-1ubuntu0.1) over (3.14.4-1) ...
Preparing to unpack .../2-libpython3.14-stdlib_3.14.4-1ubuntu0.1_amd64.deb ...
Unpacking libpython3.14-stdlib:amd64 (3.14.4-1ubuntu0.1) over (3.14.4-1) ...
Preparing to unpack .../3-python3.14-minimal_3.14.4-1ubuntu0.1_amd64.deb ...
Unpacking python3.14-minimal (3.14.4-1ubuntu0.1) over (3.14.4-1) ...
Preparing to unpack .../4-libpython3.14-minimal_3.14.4-1ubuntu0.1_amd64.deb ...
Unpacking libpython3.14-minimal:amd64 (3.14.4-1ubuntu0.1) over (3.14.4-1) ...
Preparing to unpack .../5-python3.14-gdbm_3.14.4-1ubuntu0.1_amd64.deb ...
Unpacking python3.14-gdbm (3.14.4-1ubuntu0.1) over (3.14.4-1) ...
Selecting previously unselected package python3-pip-whl.
Preparing to unpack .../6-python3-pip-whl_25.1.1+dfsg-1ubuntu2_all.deb ...
Unpacking python3-pip-whl (25.1.1+dfsg-1ubuntu2) ...
Selecting previously unselected package python3-setuptools-whl.
Preparing to unpack .../7-python3-setuptools-whl_78.1.1-0.1build1_all.deb ...
Unpacking python3-setuptools-whl (78.1.1-0.1build1) ...
Selecting previously unselected package python3.14-venv.
Preparing to unpack .../8-python3.14-venv_3.14.4-1ubuntu0.1_amd64.deb ...
Unpacking python3.14-venv (3.14.4-1ubuntu0.1) ...
Selecting previously unselected package python3-venv.
Preparing to unpack .../9-python3-venv_3.14.3-0ubuntu2_amd64.deb ...
Unpacking python3-venv (3.14.3-0ubuntu2) ...
Setting up python3-setuptools-whl (78.1.1-0.1build1) ...
Setting up python3-pip-whl (25.1.1+dfsg-1ubuntu2) ...
Setting up libpython3.14-minimal:amd64 (3.14.4-1ubuntu0.1) ...
Setting up python3.14-gdbm (3.14.4-1ubuntu0.1) ...
Setting up python3.14-minimal (3.14.4-1ubuntu0.1) ...
Setting up libpython3.14-stdlib:amd64 (3.14.4-1ubuntu0.1) ...
Setting up libpython3.14:amd64 (3.14.4-1ubuntu0.1) ...
Setting up python3.14 (3.14.4-1ubuntu0.1) ...
Setting up python3.14-venv (3.14.4-1ubuntu0.1) ...
Setting up python3-venv (3.14.3-0ubuntu2) ...
Processing triggers for systemd (259.5-0ubuntu3) ...
Processing triggers for man-db (2.13.1-1build1) ...
Processing triggers for libc-bin (2.43-2ubuntu2) ...

----------------------------------------------------------------------------------------------------------------------------------

建立虛擬環境:

python3 -m venv .venv

啟用它:

> source .venv/bin/activate

成功後,你的提示字元前面通常會多一個:


(.venv) me1237guy@mincode:...

(.venv) me1237guy@mincode:/mnt/c/Users/me123/python-llm-for-beginners-demo-code/basic-llm-demo$ python3 -m pip install --upgrade pip
Requirement already satisfied: pip in ./.venv/lib/python3.14/site-packages (25.1.1)
Collecting pip
Downloading pip-26.2.1-py3-none-any.whl.metadata (4.6 kB)
Downloading pip-26.2.1-py3-none-any.whl (1.8 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 4.2 MB/s eta 0:00:00
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 25.1.1
Uninstalling pip-25.1.1:
Successfully uninstalled pip-25.1.1
Successfully installed pip-26.2.1
(.venv) me1237guy@mincode:/mnt/c/Users/me123/python-llm-for-beginners-demo-code/basic-llm-demo$ python3 -m pip install python-dotenv
Requirement already satisfied: python-dotenv in ./.venv/lib/python3.14/site-packages (1.2.2)

(.venv) me1237guy@mincode:/mnt/c/Users/me123/python-llm-for-beginners-demo-code/basic-llm-demo$ python3 -m pip install openai
Requirement already satisfied: openai in ./.venv/lib/python3.14/site-packages (2.44.0)
Requirement already satisfied: anyio<5,>=3.5.0 in ./.venv/lib/python3.14/site-packages (from openai) (4.14.1)
Requirement already satisfied: distro<2,>=1.7.0 in ./.venv/lib/python3.14/site-packages (from openai) (1.9.0)
Requirement already satisfied: httpx<1,>=0.23.0 in ./.venv/lib/python3.14/site-packages (from openai) (0.28.1)
Requirement already satisfied: jiter<1,>=0.10.0 in ./.venv/lib/python3.14/site-packages (from openai) (0.16.0)
Requirement already satisfied: pydantic<3,>=1.9.0 in ./.venv/lib/python3.14/site-packages (from openai) (2.13.4)
Requirement already satisfied: sniffio in ./.venv/lib/python3.14/site-packages (from openai) (1.3.1)
Requirement already satisfied: tqdm>4 in ./.venv/lib/python3.14/site-packages (from openai) (4.68.3)
Requirement already satisfied: typing-extensions<5,>=4.14 in ./.venv/lib/python3.14/site-packages (from openai) (4.16.0)
Requirement already satisfied: idna>=2.8 in ./.venv/lib/python3.14/site-packages (from anyio<5,>=3.5.0->openai) (3.18)
Requirement already satisfied: certifi in ./.venv/lib/python3.14/site-packages (from httpx<1,>=0.23.0->openai) (2026.6.17)
Requirement already satisfied: httpcore==1.* in ./.venv/lib/python3.14/site-packages (from httpx<1,>=0.23.0->openai) (1.0.9)
Requirement already satisfied: h11>=0.16 in ./.venv/lib/python3.14/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai) (0.16.0)
Requirement already satisfied: annotated-types>=0.6.0 in ./.venv/lib/python3.14/site-packages (from pydantic<3,>=1.9.0->openai) (0.7.0)
Requirement already satisfied: pydantic-core==2.46.4 in ./.venv/lib/python3.14/site-packages (from pydantic<3,>=1.9.0->openai) (2.46.4)
Requirement already satisfied: typing-inspection>=0.4.2 in ./.venv/lib/python3.14/site-packages (from pydantic<3,>=1.9.0->openai) (0.4.2)

你可以用這個確認目前真的在虛擬環境:

(.venv) me1237guy@mincode:/mnt/c/Users/me123/python-llm-for-beginners-demo-code/basic-llm-demo$ which python
/mnt/c/Users/me123/python-llm-for-beginners-demo-code/basic-llm-demo/.venv/bin/python

再確認 dotenv:

  • 安裝完成後,重新載入 VS Code。
  • 再按:


    Ctrl + Shift + P


    搜尋:


    WSL: Connect to WSL



    WSL: Reopen Folder in WSL

----------------------------------------------------------------------------------------

先到 OpenRouter 官網登入,進入 API Keys 頁面,建立新的 key。OpenRouter 官方範例顯示,建立後的金鑰通常會是:


sk-or-v1-...

你可以直接到 OpenRouter:

OpenRouter 官網

> export OPEN_ROUTER_API_KEY="貼上你的_API_KEY"

先關閉原本已經開啟vscode, 因為這樣才會讀到上面設定的OPEN_ROUTER_API_KEY

> code .

import os
from dotenv import load_dotenv
from openai import OpenAI

load_dotenv()

api_key = os.getenv("OPEN_ROUTER_API_KEY")
print("有讀到 API key 嗎?", bool(api_key))
client = OpenAI(api_key=api_key, base_url="https://openrouter.ai/api/v1")

system_prompt = "你是一個會說繁體中文,而且會使用大量表情符號互動的可愛機器人"

response = client.chat.completions.create(
    model="deepseek/deepseek-chat-v3-0324",
    messages=[
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": "你好!你會說繁體中文嗎?你是誰呢?"},
    ],
    max_tokens=100,
    temperature=0.7,
)

print(response.choices[0].message.content)

測試結果:

如果你不想替 OpenAI API 加值,而是想繼續用前面提到的 OpenRouter,那就要把程式改成 OpenRouter 的:




client = OpenAI(
    base_url="https://openrouter.ai/api/v1",
    api_key=os.getenv("OPENROUTER_API_KEY"),
)


並使用 OpenRouter 支援的模型名稱,而不是直接走 OpenAI 的 API。

所以你現在有兩條路:

  1. 用 OpenAI API OpenAI Billing 加額度。

  1. 用 OpenRouter → 使用 OPENROUTER_API_KEY + base_url="https://openrouter.ai/api/v1"

ChatGPT Plus / Pro 訂閱,和 OpenAI API 額度是分開計費的。即使你有 ChatGPT 付費方案,也不代表 API 自動有額度





文章標籤:
openrouter
創作者介紹
創作者 天天向上 的頭像
me1237guy

天天向上

me1237guy 發表在 痞客邦 留言(0) 人氣( 17 )