diff --git a/Class1/class1/.mcp.json b/Class1/class1/.mcp.json new file mode 100644 index 0000000..1ec8501 --- /dev/null +++ b/Class1/class1/.mcp.json @@ -0,0 +1,55 @@ +{ + "mcpServers": { + "filesystem": { + "type": "stdio", + "command": "npx", + "args": [ + "-y", + "@modelcontextprotocol/server-filesystem", + "/Users/ming/Desktop/Learning/MLE_in_Gen_AI-Course/Class1/class1" + ] + }, + "sequential-thinking": { + "type": "stdio", + "command": "npx", + "args": [ + "-y", + "@modelcontextprotocol/server-sequential-thinking" + ] + }, + "puppeteer": { + "type": "stdio", + "command": "npx", + "args": [ + "-y", + "@modelcontextprotocol/server-puppeteer" + ] + }, + "brave-search": { + "type": "stdio", + "command": "npx", + "args": [ + "-y", + "@modelcontextprotocol/server-brave-search" + ], + "env": { + "BRAVE_API_KEY": "YOUR_BRAVE_API_KEY" + } + }, + "github": { + "type": "stdio", + "command": "npx", + "args": [ + "-y", + "@modelcontextprotocol/server-github" + ], + "env": { + "GITHUB_PERSONAL_ACCESS_TOKEN": "YOUR_GITHUB_PERSONAL_ACCESS_TOKEN" + } + }, + "notion": { + "type": "http", + "url": "https://mcp.notion.com/mcp" + } + } +} diff --git a/Class1/class1/Class 1 Homework.ipynb b/Class1/class1/Class 1 Homework.ipynb index d64e539..fa9e066 100644 --- a/Class1/class1/Class 1 Homework.ipynb +++ b/Class1/class1/Class 1 Homework.ipynb @@ -118,12 +118,16 @@ }, { "cell_type": "raw", - "metadata": {}, + "metadata": { + "vscode": { + "languageId": "raw" + } + }, "source": [ "curl http://localhost:11434/v1/chat/completions \\\n", " -H \"Content-Type: application/json\" \\\n", " -d '{\n", - " \"model\": \"llama2\",\n", + " \"model\": \"gemma4:26b\",\n", " \"messages\": [\n", " {\n", " \"role\": \"system\",\n", @@ -146,9 +150,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The 2020 World Series was played at **Globe Life Field** in Arlington, Texas. \n", + "\n", + "Because of the COVID-19 pandemic, Major League Baseball decided to host the entire postseason at a single site to minimize travel and reduce the risk of virus transmission.\n" + ] + } + ], "source": [ "from openai import OpenAI\n", "\n", @@ -158,7 +172,7 @@ ")\n", "\n", "response = client.chat.completions.create(\n", - " model=\"llama2\",\n", + " model=\"gemma4:26b\",\n", " messages=[\n", " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", " {\"role\": \"user\", \"content\": \"Who won the world series in 2020?\"},\n", @@ -169,6 +183,69 @@ "print(response.choices[0].message.content)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Note\n", + "# `base_url` 筆記\n", + "## 1. 把 `base_url` 拆成四段看\n", + "```python\n", + "base_url=\"http://localhost:11434/v1\"\n", + "```\n", + "可以拆成:\n", + "```text\n", + "http://localhost:11434/v1\n", + "│ │ │ │\n", + "│ │ │ └─ OpenAI-compatible API 路徑\n", + "│ │ └────── Ollama 預設 port\n", + "│ └──────────────── 你的本機電腦\n", + "└─────────────────────── HTTP 協定\n", + "```\n", + "意思是:這段網址不是連到 OpenAI 官方 API,而是連到你自己電腦上正在執行的 Ollama server。\n", + "\n", + "## 2. `base_url` 像是模型倉庫的地址\n", + "可以把:\n", + "```python\n", + "base_url=\"http://localhost:11434/v1\"\n", + "```\n", + "想成「模型倉庫的地址」。而:\n", + "\n", + "```python\n", + "model=\"gemma4:26b\"\n", + "model=\"qwen3.6:35b\"\n", + "```\n", + "\n", + "則是「你要叫哪一個模型出來工作」。\n", + "所以如果 `gemma4:26b` 和 `qwen3.6:35b` 都在同一個 Ollama 裡面,只需要一個 `base_url`:\n", + "```python\n", + "client = OpenAI(\n", + " base_url=\"http://localhost:11434/v1\",\n", + " api_key=\"ollama\",\n", + ")\n", + "```\n", + "\n", + "每次呼叫時,只要換 `model` 名稱就好:\n", + "```python\n", + "response = client.chat.completions.create(\n", + " model=\"gemma4:26b\",\n", + " messages=[{\"role\": \"user\", \"content\": \"Hello!\"}],\n", + ")\n", + "\n", + "response = client.chat.completions.create(\n", + " model=\"qwen3.6:35b\",\n", + " messages=[{\"role\": \"user\", \"content\": \"Hello!\"}],\n", + ")\n", + "```\n", + "\n", + "## 3. 核心觀念\n", + "\n", + "* 同一個 Ollama server 裡有多個模型時,通常只需要一個 `base_url`。\n", + "* `base_url` 決定要連到哪一個模型服務。\n", + "* `model` 決定要使用該服務裡面的哪一個模型。\n", + "* 只有當模型放在不同服務或不同機器上時,才需要不同的 `base_url`。\n" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -220,9 +297,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "User prompt: 'What is the capital of Germany?'\n", + "Model answer: The capital of Germany is **Berlin**.\n" + ] + } + ], "source": [ "# Example: Using LCEL to reproduce a \"Basic Prompting\" scenario\n", "from langchain_core.prompts import PromptTemplate\n", @@ -236,7 +322,7 @@ ")\n", "\n", "# 3. Define the model\n", - "model = ChatOllama(model = [\"llama2\"]) # Using Ollama \n", + "model = ChatOllama(model = \"gemma4:26b\") # Using Ollama \n", "\n", "# 4. Chain the components together using LCEL\n", "chain = (\n", @@ -253,6 +339,85 @@ "print(\"Model answer:\", result)\n" ] }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "#%pip install -U langchain-ollama\n", + "#%pip show langchain langchain-core langchain-community langchain-ollama" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "* Running on local URL: http://127.0.0.1:7862\n", + "* To create a public link, set `share=True` in `launch()`.\n" + ] + }, + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "