Skip to main content

胖貓專屬嘴臭LLM推理API

Project description

🐈 FatCat GPT API

這是一個基於 FastAPI 與 fatcat-gpt 套件建構的大型語言模型 (LLM) 推理伺服器。 模型被設定為一個名叫咪咪的 Discord 活躍群友,講話直接,而且專門給出讓人心情更糟的超臭回覆。

透過這個專案,模型只會在伺服器啟動時載入顯示卡一次,之後便可在背景待命,隨時接收 API 請求並瞬間給出回覆,可以用來串接 Discord 機器人。


🛠️ 環境與安裝清單

請確保你的環境中已經安裝了 fatcat-gpt 核心套件以及 API 所需的依賴模組:

# 確保已安裝 fatcat-gpt
pip install fatcat-gpt

🚀 快速啟動指南

這個專案分為「伺服器端 (Server)」與「測試端 (Client)」兩個部分。

第一步:啟動背景伺服器 (server.py)

伺服器負責將幾十 GB 的模型載入 GPU 中並保持待命。請在終端機中執行以下指令來啟動 API 伺服器:

uvicorn server:app --host 0.0.0.0 --port 8000

💡 提示 (多顯卡環境): > 如果你的機器有多張顯卡,且你想把模型強制綁定在特定的 GPU (例如 GPU 0),請在指令前面加上環境變數:

CUDA_VISIBLE_DEVICES=0 uvicorn server:app --host 0.0.0.0 --port 8000

server.py

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List, Dict
from contextlib import asynccontextmanager
from fatcat_gpt import FatCatGPT

bot = None

# =========================================================
# start server
# =========================================================
@asynccontextmanager
async def lifespan(app: FastAPI):
    global bot
    print("[伺服器啟動中] 正在將模型載入到背景,請稍候...")
    bot = FatCatGPT()
    print("[伺服器已就緒] 胖貓模型載入完成,等待呼叫!")
    yield
    print("[伺服器關閉中] 正在釋放顯示卡記憶體...")
    bot = None

app = FastAPI(title="FatCat GPT API", lifespan=lifespan)

class ChatRequest(BaseModel):
    messages: List[Dict[str, str]]
    max_new_tokens: int = 128
    temperature: float = 0.3
    top_p: float = 0.95

# =========================================================
# Endpoint: /chat
# =========================================================
@app.post("/chat")
async def chat_endpoint(request: ChatRequest):
    if bot is None:
        raise HTTPException(status_code=500, detail="模型尚未載入完成")
    
    try:
        response = bot.chat(
            messages=request.messages,
            max_new_tokens=request.max_new_tokens,
            temperature=request.temperature,
            top_p=request.top_p
        )
        return {"status": "success", "response": response}
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

等待終端機印出 [伺服器已就緒] 或類似的成功訊息後,就代表伺服器已經成功啟動並駐留在背景。請將這個終端機視窗保持開啟,不要關閉。

第二步:發送測試請求 (send.py)

請開啟另一個新的終端機視窗,執行測試腳本,向剛才開好的伺服器發送對話內容:

python send.py

send.py

import requests

API_URL = "http://127.0.0.1:8000/chat"

payload = {
    "messages": [
        {
          "role": "user",
          "content": "[聊天頻道名稱] user_id_a: 今天晚餐吃什麼好?\n[聊天頻道名稱] user_id_b: 不知道欸"
        }
    ],
    "temperature": 0.3 
}

print("發送請求中...")
response = requests.post(API_URL, json=payload)

if response.status_code == 200:
    data = response.json()
    print("FatCat:", data["response"])
else:
    print("發生錯誤:", response.text)

如果一切順利,你將會瞬間在終端機看到來自咪咪的無情臭人回覆!

📡 API 規格說明

伺服器啟動後,預設會監聽 http://127.0.0.1:8000。

POST /chat 接收對話歷史並回傳咪咪生成的文字。

請求格式 (JSON payload):

{
  "messages": [
    {
      "role": "user",
      "content": "[聊天頻道名稱] user_id_a: 今天晚餐吃什麼好?\n[聊天頻道名稱] user_id_b: 不知道欸"
    }
  ],
  "max_new_tokens": 128,
  "temperature": 0.3,
  "top_p": 0.95
}

成功回應格式 (JSON):

{
  "status": "success",
  "response": "咪咪生成的無情回覆字串"
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fatcat_gpt-0.1.4.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fatcat_gpt-0.1.4-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file fatcat_gpt-0.1.4.tar.gz.

File metadata

  • Download URL: fatcat_gpt-0.1.4.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for fatcat_gpt-0.1.4.tar.gz
Algorithm Hash digest
SHA256 794855b0e40f298ba81488d99382448b495d864ac625988cedfc941173b95d58
MD5 3c7820db472f25f3eed7528b40844b6e
BLAKE2b-256 44862ef1568d894c75c3230dbf6813dc6ce77a559121daac98c881817aef763f

See more details on using hashes here.

File details

Details for the file fatcat_gpt-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: fatcat_gpt-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for fatcat_gpt-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a9c624e83130a9454ffac4d2b4d556d30649a433b3aa133870b2b8c84f887e3f
MD5 09aca9fec75d0832118b951050cd097e
BLAKE2b-256 01b834adde332c3587e123cb3b6ee02c7dce88a988bedaccd6d424c2ab5dc670

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page