티스토리 뷰
Ollama + CrewAI + FastAPI를 활용한 한국 주식 분석 블로그 - 프로젝트 성능 최적화 및 유지보수
octo54 2025. 3. 7. 17:23Ollama + CrewAI + FastAPI를 활용한 한국 주식 분석 블로그
6. 프로젝트 성능 최적화 및 유지보수
이 글에서는 한국 주식 분석 블로그의 성능을 최적화하고 유지보수하는 방법을 다룹니다.
✅ FastAPI 성능 최적화
✅ CrewAI 모델 최적화
✅ 데이터베이스 최적화 및 캐싱 도입
✅ API 로깅 및 모니터링 구축
6.1 FastAPI 성능 최적화
6.1.1 비동기(Async) 처리 적용
FastAPI는 비동기(Async) 기능을 지원하여 API 응답 속도를 개선할 수 있습니다.
아래처럼 async def를 사용하여 비동기 방식으로 API 요청을 처리합니다.
비동기 FastAPI 예제 (api.py)
from fastapi import FastAPI, HTTPException
import os
app = FastAPI()
REPORT_PATH = "weekly_korean_stock_report.md"
@app.get("/report/")
async def get_report():
"""비동기 방식으로 CrewAI 리포트 반환"""
if os.path.exists(REPORT_PATH):
with open(REPORT_PATH, "r", encoding="utf-8") as file:
report_content = file.read()
return {"title": "주간 한국 주식 시장 분석", "content": report_content}
else:
raise HTTPException(status_code=404, detail="Report not found")
6.1.2 Gunicorn + Uvicorn을 활용한 성능 개선
FastAPI는 기본적으로 싱글 스레드로 동작합니다.
Gunicorn과 Uvicorn을 함께 사용하여 멀티 스레드 환경에서 실행하면 성능이 개선됩니다.
Gunicorn + Uvicorn 실행 예제
pip install gunicorn
gunicorn -w 4 -k uvicorn.workers.UvicornWorker src.stock_analysis.api:app --bind 0.0.0.0:8000
🔹 -w 4: 4개의 워커(worker) 프로세스 실행
🔹 -k uvicorn.workers.UvicornWorker: Gunicorn이 Uvicorn을 사용하도록 설정
6.2 CrewAI 모델 최적화
CrewAI의 성능을 최적화하기 위해 모델의 메모리 사용량을 줄이고, 실행 속도를 개선합니다.
6.2.1 Ollama 모델 설정 최적화
DeepSeek 모델(deepseek-r1:8b)은 큰 모델이므로 GPU 최적화가 필요합니다.
아래와 같이 Ollama 설정을 수정하여 메모리 사용을 최적화합니다.
Ollama 설정 변경 (ollama.yaml)
model: deepseek-r1:8b
parameters:
num_threads: 8
num_gpu_layers: 10
🔹 num_threads: 8 → CPU 병렬 처리를 활성화
🔹 num_gpu_layers: 10 → 10개의 GPU 레이어를 사용하여 속도 향상
6.2.2 CrewAI 실행 최적화
CrewAI 실행 시 작업을 순차적(sequential)에서 병렬(parallel)로 변경하여 성능을 개선할 수 있습니다.
아래와 같이 Process.parallel 설정을 적용합니다.
from crewai import Process
crew = Crew(
agents=[stock_market_analyst, financial_sentiment_analyst, investment_report_writer],
tasks=[stock_market_analysis, financial_sentiment_analysis, investment_report_generation],
process=Process.parallel, # 병렬 실행 적용
verbose=True,
)
6.3 데이터베이스 최적화 및 캐싱 도입
데이터베이스 성능을 개선하고, API 요청 속도를 높이기 위해 Redis 캐싱을 도입할 수 있습니다.
6.3.1 Redis 설치 및 실행
sudo apt update && sudo apt install redis
sudo systemctl start redis
6.3.2 FastAPI에서 Redis 캐싱 적용
import redis
from fastapi import FastAPI
app = FastAPI()
# Redis 연결 설정
redis_client = redis.Redis(host='localhost', port=6379, db=0)
@app.get("/report/")
async def get_report():
"""Redis 캐싱을 활용한 리포트 제공"""
cached_report = redis_client.get("weekly_report")
if cached_report:
return {"title": "주간 한국 주식 시장 분석", "content": cached_report.decode('utf-8')}
# 리포트 파일 읽기 (캐싱되지 않은 경우)
if os.path.exists(REPORT_PATH):
with open(REPORT_PATH, "r", encoding="utf-8") as file:
report_content = file.read()
redis_client.setex("weekly_report", 3600, report_content) # 1시간 동안 캐싱
return {"title": "주간 한국 주식 시장 분석", "content": report_content}
raise HTTPException(status_code=404, detail="Report not found")
6.4 API 로깅 및 모니터링 구축
운영 환경에서 API의 상태를 실시간으로 모니터링하기 위해 로깅 및 분석 시스템을 구축합니다.
6.4.1 FastAPI 로그 기록 설정
import logging
# 로깅 설정
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
@app.get("/report/")
async def get_report():
"""API 요청 로깅"""
logger.info("Report API 요청 수신됨")
if os.path.exists(REPORT_PATH):
with open(REPORT_PATH, "r", encoding="utf-8") as file:
report_content = file.read()
return {"title": "주간 한국 주식 시장 분석", "content": report_content}
logger.error("리포트 파일을 찾을 수 없음")
raise HTTPException(status_code=404, detail="Report not found")
6.4.2 Prometheus + Grafana를 활용한 API 모니터링
1) Prometheus 설치
wget https://github.com/prometheus/prometheus/releases/latest/download/prometheus-*.linux-amd64.tar.gz
tar xvfz prometheus-*.linux-amd64.tar.gz
cd prometheus-*
./prometheus --config.file=prometheus.yml
2) FastAPI에서 Prometheus 메트릭 제공
pip install prometheus_client
from prometheus_client import Counter, generate_latest
from fastapi import Response
request_counter = Counter("api_requests", "Number of API requests received")
@app.get("/metrics")
def metrics():
"""Prometheus 메트릭 제공"""
return Response(content=generate_latest(), media_type="text/plain")
3) Grafana에서 대시보드 설정
- Prometheus를 데이터 소스로 추가
- API 요청 수, 평균 응답 시간, 오류 발생률 등 시각화
📌 정리 및 다음 단계
이제 한국 주식 분석 블로그의 성능을 최적화하고, 운영 환경에서 안정적으로 서비스할 수 있도록 설정했습니다.
✅ FastAPI 비동기 처리 및 Gunicorn 최적화
✅ CrewAI 모델 실행 속도 개선
✅ Redis 캐싱을 활용한 API 응답 속도 향상
✅ Prometheus + Grafana를 활용한 API 모니터링 구축
🔜 다음 글 예고: 7. 프로젝트 완성 및 최종 테스트
다음 글에서는 전체 프로젝트를 최종적으로 테스트하고, 실전에서 어떻게 활용할 수 있을지 정리합니다. 🚀
'project > Ollama + CrewAI + FastAPI를 활용한 한국 주식 분석' 카테고리의 다른 글
- Total
- Today
- Yesterday
- rag
- 백엔드
- 프론트엔드
- nodejs
- Python
- LangChain
- github
- 개발블로그
- Docker
- Ktor
- fastapi
- 관리자
- babel
- Webpack
- 백엔드개발
- Page
- nextJS
- kotlin
- llm
- Next.js
- REACT
- AI챗봇
- til
- PostgreSQL
- Project
- 웹개발
- 챗봇개발
- 로컬LLM
- 리액트
- 페이지
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |