Self-Hosted LLMs for Enterprise #4

Self-Hosted LLMs for Enterprise #4

ตอนสุดท้ายแล้วนะครับ สำหรับการ deploy llm model ใช้งานเอง หลังจากที่แล้ว Setup ตัว service และ tools ต่างๆที่ต้องการครบถ้วนแล้ว เรามาลุยกันต่อเลยครับในการ download model และทำ API Endpoint

สำหรับใครที่เพิ่งเข้ามาอ่านตอนนี้เป็นตอนแรก สามารถติดตามตอนก่อนหน้าได้ที่

https://blog.float16.cloud/self-hosted-llms-for-enterprise-/

https://blog.float16.cloud/self-hosted-llms-for-enterprise-2/

https://blog.float16.cloud/self-hosted-llms-for-enterprise-3/

ไปเริ่มตอนที่ 4 กันเลย !!

1.สร้างโปรเจกต์และดาวน์โหลดโมเดล

# 1. สร้างโฟลเดอร์สำหรับโปรเจกต์
mkdir -p llm-chat-api
cd llm-chat-api
# 2. ดาวน์โหลดโมเดล (Llama 3.2 1B Q8_0)
huggingface-cli download bartowski/Llama-3.2-1B-Instruct-GGUF \
Llama-3.2-1B-Instruct-Q8_0.gguf --local-dir model

ไฟล์โมเดลจะถูกเก็บไว้ใน ./model/Llama-3.2-1B-Instruct-Q8_0.gguf

2.สร้าง Python ไฟล์เพื่อรันโมเดล

สร้างไฟล์ main.py

# main.py
from llama_cpp import Llama
llm = Llama(
    model_path="model/Llama-3.2-1B-Instruct-Q8_0.gguf",
    n_gpu_layers=-1,
    verbose=False,
    chat_format='llama-3'
)
output = llm.create_chat_completion(
    messages=[
        {"role": "system", "content": "You are an best assistant"},
        {"role": "user", "content": "Introduce yourself"}
    ]
)
print(output["choices"][0]["message"]["content"])

ทดสอบรันด้วย

python3 main.py

3.เปิด API ด้วย FastAPI

ติดตั้ง FastAPI และ Uvicorn (server)

pip install fastapi uvicorn pydantic

อัปเดต main.py ให้เป็น REST API แบบ POST

# main.py
from fastapi import FastAPI
from pydantic import BaseModel
from llama_cpp import Llama

app = FastAPI()

llm = Llama(
    model_path="model/Llama-3.2-1B-Instruct-Q8_0.gguf",
    n_gpu_layers=-1,
    verbose=False,
    chat_format='llama-3'
)

class PromptRequest(BaseModel):
    prompt: str

@app.post("/chat")
def chat(req: PromptRequest):
    response = llm.create_chat_completion(
        messages=[
            {"role": "system", "content": "You are an assistant who can help to answer general question"},
            {"role": "user", "content": req.prompt}
        ]
    )
    return {"response": response["choices"][0]["message"]["content"]}

4.รัน API Server

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

API จะเปิดให้ใช้งานที่ http://localhost:8000/chat

โดยเราสามารถทดลองเรียกใช้งานได้ผ่าน curl

curl -X POST http://localhost:8000/chat \
  -H "Content-Type: application/json" \
  -d '{"prompt": "Describe a sunset over the ocean."}'

บทสรุปของทั้งหมด

จากทุกตอนที่ผ่านเรา เราจะสามารถทำ llm endpoint เอาไว้ใช้งานง่ายๆในทีม ไม่ว่าจะเอามาใช้ทดสอบ หรือนำไปพัฒนาเป็น product ต่างๆต่อไป สุดท้ายนี้อยากฝากทุกคนให้ติดตามบทความอื่นต่อไปด้วยนะครับ รับรองว่ามีอะไรที่น่าสนใจมาให้ติดตามต่อแน่นอนครับ

Read more

ทำ Data Extraction จาก image โดยใช้ LLM Multimodal

ทำ Data Extraction จาก image โดยใช้ LLM Multimodal

ในการทำ Data Extraction จากรูปภาพ เช่น สลิปใบเสร็จ, บัตรประชาชน, หรือแบบฟอร์มกระดาษ วิธีดั้งเดิมมักใช้ OCR (Optical Character Recognition) ร่วมกับการเขียน rule หรือ regex เพื่อแยกข้อมูลออกมา ซึ่งยุ่งยากหรือเมื่อรูปแบบข้อมูลเปลี่ยน ซึ่งจริงๆแล้วเรามี

By Thanawat Kittichaikarn
Self-Hosted LLMs for Enterprise #3

Self-Hosted LLMs for Enterprise #3

สำหรับใครที่เพิ่งเข้ามาเจอตอนนี้สามารถย้อนกลับไปอ่าน 2 ตอนก่อนหน้าได้ที่ https://blog.float16.cloud/self-hosted-llms-for-enterprise-1/ https://blog.float16.cloud/self-hosted-llms-for-enterprise-2/ 2 ตอนที่ผ่านมา เราได้ติดตั้ง GPU Driver และเชื่อม GPU เข้ากับ Docker ได้เรียบร้อยแล้ว บทนี้เราจะมาติดตั้งเครื

By Weerasak Suwannapong
Self-Hosted LLMs for Enterprise #2

Self-Hosted LLMs for Enterprise #2

ในตอนที่แล้ว เราได้ติดตั้ง NVIDIA GPU Driver ให้พร้อมใช้งานบนเครื่อง EC2 (g5g.xlarge) ที่ใช้ Ubuntu 24.04 LTS บทนี้เราจะทำให้ GPU ที่ติดตั้งไว้สามารถใช้งานได้ภายใน Docker container เพื่อเตรียมต่อยอดไปยังการสร้าง LLM API ด้วย llama.cpp อ่านตอนแรกได้

By Weerasak Suwannapong
Self-Hosted LLMs for Enterprise #1

Self-Hosted LLMs for Enterprise #1

ในยุคที่ Generative AI กลายเป็นผู้ช่วยประจำวันของใครหลายคน ไม่ว่าจะเขียนโค้ด ตอบคำถาม หรือสรุปรายงาน หลายองค์กรเริ่มสนใจจะ ติดตั้งและใช้งาน LLM ภายในเอง เพื่อความเป็นส่วนตัว ความยืดหยุ่น และควบคุมต้นทุน ซีรีส์นี้จะพาไปตั้งค่าระบบทีละขั้น ตั้งแต่

By Weerasak Suwannapong