본문 바로가기
AI, 논문, 데이터 분석

[LLM] llama3.2를 활용하여 Chatbot 만들기

by doraemin_dev 2025. 2. 4.

이경용 대표님 강의안

 

https://cafe.naver.com/aiclubcafe/1033

 

LLM - 05.라마3.2 + Fine Tuning + RAG

Fine Tuning 이란? Specific (private) 지식을 학습 시켜서 나만의 LLM을 만드는 것 RAG란? 사전 훈련된 대규모 언어 모델의 능력과 외부 데이터 소...

cafe.naver.com

 

https://cafe.naver.com/aiclubcafe/1037

 

LLM - Ngrok 터널링 (포트 포워드)

로컬 서버를 운영할 경우 외부에서 접속할 수 있는 URL을 제공 해주는 서비스 입니다. Ngrok (로컬 서버를 공개 서버로) https://ngrok.com/ https:/...

cafe.naver.com

 

FastAPI의 모델명을 수정해주자.

# 29번째 줄에 모델명 입력
    url = "http://host.docker.internal:11434/api/generate" # 모델명 수정 

# 선택사항
# 40번째 줄에 timeout 늘려주기
    async with httpx.AsyncClient(timeout=20.0) as client: # timeout=20.0으로 timeout 시간을 늘려주자.
# main.py 코드 전문
from fastapi import FastAPI, Form
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
import httpx
import json

app = FastAPI()

# CORS 설정
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

class RequestModel(BaseModel):
    model: str
    prompt: str

class Message(BaseModel):
    message: str

@app.post("/chat")
async def chat(message: Message):
    prompt = message.message
    # 요청할 URL
    url = "http://host.docker.internal:11434/api/generate" # 모델명 수정 
    # 요청에 포함할 데이터
    data = {
        "model": "llama3.2-kbo-v1.1:latest",
        "prompt": prompt
    }
    # 요청 헤더
    headers = {
        "Content-Type": "application/json"
    }
    
    async with httpx.AsyncClient(timeout=20.0) as client: # timeout=20.0으로 timeout 시간을 늘려주자.
        response = await client.post(url, headers=headers, data=json.dumps(data))

    if response.status_code == 200:
        # 개별 JSON 객체로 분할
        json_objects = response.content.decode().strip().split("\n")
        # 각 JSON 객체를 Python 사전으로 변환
        data = [json.loads(obj) for obj in json_objects]
        res_text = ''
        # 변환된 데이터 출력
        for item in data:
            print(item)
            res_text += item['response']
    else:
        res_text = f"Error Code : {response.status_code}"
    return {"reply": res_text}

 


ngrok를 통해 배포하였다.

 

만들어본 chatbot은, 개발자를 위한 취업 면접 답변해주는 것이다.

면접관이 JAVA에 대해 설명하시오 라고 질문했다면?!

 

결과 !

 

완료!