前面八天,我們已經學會:
什麼是 GenAI 與雲端平台
Azure AI Foundry & GCP Vertex AI 的使用方式
API 呼叫與兩者的比較
今天,我們來實際動手,做一個 小型 AI 應用 Demo:
👉「AI 問答小幫手 (Chatbot)」
1️⃣ Demo 概念
這個小應用的目標是:
使用者輸入一段問題
後端呼叫雲端 GenAI API(Azure 或 GCP)
回傳 AI 生成的回覆
我們可以用最簡單的 Python + FastAPI 來實作。
2️⃣ 範例程式碼 (以 Azure AI Foundry 為例)
from fastapi import FastAPI
from pydantic import BaseModel
import openai
import os
app = FastAPI()
openai.api_type = "azure"
openai.api_base = os.getenv("AZURE_OPENAI_ENDPOINT")
openai.api_version = "2023-05-15"
openai.api_key = os.getenv("AZURE_OPENAI_KEY")
class Question(BaseModel):
text: str
@app.post("/ask")
def ask_ai(q: Question):
response = openai.ChatCompletion.create(
engine="gpt-35-turbo", # 這裡是你在 Azure 部署的模型名稱
messages=[{"role": "user", "content": q.text}]
)
return {"answer": response["choices"][0]["message"]["content"]}
只要啟動 FastAPI,前端(或 Postman)就能呼叫 /ask API,送入問題,得到 AI 回覆。
3️⃣ GCP Vertex AI 版本 (簡化版)
from fastapi import FastAPI
from pydantic import BaseModel
from google.cloud import aiplatform
app = FastAPI()
project_id = "your-project-id"
location = "us-central1"
model_name = "chat-bison" # 或 gemini-pro
aiplatform.init(project=project_id, location=location)
chat_model = aiplatform.ChatModel.from_pretrained(model_name)
chat_session = chat_model.start_chat()
class Question(BaseModel):
text: str
@app.post("/ask")
def ask_ai(q: Question):
response = chat_session.send_message(q.text)
return {"answer": response.text}
4️⃣ Demo 的價值
實際落地:不只是 Playground 測試,而是能在應用程式中使用。
平台可切換:同樣的架構,換成 Azure 或 GCP 就能跑。
未來擴展:可加上前端(React / Vue)或整合 Slack、LINE Bot。
5️⃣ 小結
透過今天的練習,我們完成了:
一個簡單的「AI 問答小幫手」
學會如何把雲端 GenAI 平台整合到應用程式中
體驗 API 開發 → Demo 應用的完整流程