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: 今天晚餐吃什麼好? [聊天頻道名稱] 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: 今天晚餐吃什麼好? [聊天頻道名稱] 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.2.tar.gz (10.2 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.2-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fatcat_gpt-0.1.2.tar.gz
  • Upload date:
  • Size: 10.2 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.2.tar.gz
Algorithm Hash digest
SHA256 de02a2343093930899c05794e651240dc37e7f40c7b74f6b8010f91eb6749e81
MD5 e7f9c0b73f2e04ad641f7beb0c6c46d1
BLAKE2b-256 5277ec75d77342eb83c19815b860b9dcf2a0c9395352ac3bf9903e65f5ce9b1f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fatcat_gpt-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.3 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4f4a76e2e6374c6436f934311762a4ef63ae28f8020201527e373c314767d815
MD5 3623f7120e7c0d4fe6acc229088d08bf
BLAKE2b-256 ba714bb6790bd292ebbaee38b4f1b8bd7af04fd1e7d1ddfbb149189fcb5fea8c

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