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": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import gradio as gr\n", + "from langchain_ollama import ChatOllama\n", + "from langchain_core.messages import HumanMessage\n", + "\n", + "llm = ChatOllama(\n", + " model=\"gemma4:26b\",\n", + " base_url=\"http://localhost:11434\",\n", + " temperature=0.7\n", + ")\n", + "\n", + "def chat_with_ollama(user_input):\n", + " try:\n", + " if not user_input.strip():\n", + " return \"Please enter a message.\"\n", + "\n", + " response = llm.invoke([\n", + " HumanMessage(content=user_input)\n", + " ])\n", + "\n", + " return response.content\n", + "\n", + " except Exception as e:\n", + " return f\"Error: {type(e).__name__}\\n\\n{str(e)}\"\n", + "\n", + "demo = gr.Interface(\n", + " fn=chat_with_ollama,\n", + " inputs=gr.Textbox(lines=5, label=\"Your prompt\"),\n", + " outputs=gr.Textbox(lines=10, label=\"Ollama response\"),\n", + " title=\"Ollama + LangChain + Gradio\"\n", + ")\n", + "\n", + "demo.launch()" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -266,8 +431,22 @@ } ], "metadata": { + "kernelspec": { + "display_name": "llm_course_env", + "language": "python", + "name": "python3" + }, "language_info": { - "name": "python" + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.11" } }, "nbformat": 4, diff --git a/Class1/class1/hw_output/brave.png b/Class1/class1/hw_output/brave.png new file mode 100644 index 0000000..ce67c2d Binary files /dev/null and b/Class1/class1/hw_output/brave.png differ diff --git a/Class1/class1/hw_output/example.png b/Class1/class1/hw_output/example.png new file mode 100644 index 0000000..dbbc455 Binary files /dev/null and b/Class1/class1/hw_output/example.png differ diff --git a/Class1/class1/hw_output/github.png b/Class1/class1/hw_output/github.png new file mode 100644 index 0000000..9ef09f3 Binary files /dev/null and b/Class1/class1/hw_output/github.png differ diff --git a/Class1/class1/hw_output/hello.txt b/Class1/class1/hw_output/hello.txt new file mode 100644 index 0000000..89860de --- /dev/null +++ b/Class1/class1/hw_output/hello.txt @@ -0,0 +1 @@ +Hello MCP! \ No newline at end of file diff --git a/Class1/class1/hw_output/notion.txt b/Class1/class1/hw_output/notion.txt new file mode 100644 index 0000000..7960e8f --- /dev/null +++ b/Class1/class1/hw_output/notion.txt @@ -0,0 +1 @@ +https://www.notion.so/MCP-Automation-Test-36970be48b76812fbfeaca71d47e808d?source=copy_link \ No newline at end of file diff --git a/Class1/class1/hw_output/ollama.png b/Class1/class1/hw_output/ollama.png new file mode 100644 index 0000000..d9af479 Binary files /dev/null and b/Class1/class1/hw_output/ollama.png differ diff --git a/Class1/class1/hw_output/table_no_outline_h2.csv b/Class1/class1/hw_output/table_no_outline_h2.csv new file mode 100644 index 0000000..c896618 --- /dev/null +++ b/Class1/class1/hw_output/table_no_outline_h2.csv @@ -0,0 +1,6 @@ +Day,Time,Location +Wednesday,3-6 pm,Cal Poly Campus (follow U-Pick Signs) +Thursday,2:30-5pm,Morro Bay Farmer's Market +Thursday,6:10-9pm,Downtown SLO Farmer's Market +Saturday,8-10:30am,Farmer's Market new Embassy Suites +Saturday,11am-2pm,Cal Poly Campus (follow U-Pick signs) diff --git a/Class1/class1/hw_output/table_no_outline_h2.md b/Class1/class1/hw_output/table_no_outline_h2.md new file mode 100644 index 0000000..7c6673f --- /dev/null +++ b/Class1/class1/hw_output/table_no_outline_h2.md @@ -0,0 +1,13 @@ +# Table No Outline - H2 + +> **Source:** https://afd.calpoly.edu/web/sample-tables +> **Caption:** No Style Table Listing +> **Extracted by:** Puppeteer MCP — 2026-05-23 + +| Day | Time | Location | +|-----|------|----------| +| Wednesday | 3-6 pm | Cal Poly Campus (follow U-Pick Signs) | +| Thursday | 2:30-5pm | Morro Bay Farmer's Market | +| Thursday | 6:10-9pm | Downtown SLO Farmer's Market | +| Saturday | 8-10:30am | Farmer's Market new Embassy Suites | +| Saturday | 11am-2pm | Cal Poly Campus (follow U-Pick signs) | diff --git a/Class1/class1/hw_output/thinking.png b/Class1/class1/hw_output/thinking.png new file mode 100644 index 0000000..ed8d499 Binary files /dev/null and b/Class1/class1/hw_output/thinking.png differ