feat: 챗봇 AI 연결 - #13
Conversation
📝 Walkthrough변경 개요OpenAI와 연동된 채팅 API 기능이 추가됩니다. 요청/응답 데이터 모델, 프롬프트 생성, OpenAI HTTP 호출, FastAPI 라우터, 앱 통합 및 환경 설정이 포함됩니다. 변경 사항OpenAI 채팅 API 전체 구현
예상 코드 리뷰 난이도🎯 3 (중간) | ⏱️ ~25분 관련 PR
제안 레이블
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 5
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@app/main.py`:
- Around line 1-7: Move the call to load_dotenv() so all imports are grouped
first; currently load_dotenv() is between imports causing Ruff E402. In main.py,
gather the import statements (from dotenv import load_dotenv, from fastapi
import FastAPI, from app.routers import chat, health, newsletters) at the top,
then call load_dotenv() immediately after those imports (before any other
runtime code) to preserve behavior and satisfy the linter.
In `@app/schemas.py`:
- Around line 108-112: The ChatRequest model accepts free-form strings for
language and chat_type which lets clients send invalid values (breaking API
contract and causing silent fallback in app/services/chat_prompt.py); define
explicit Enum classes (e.g., Language and ChatType) with the allowed values and
change ChatRequest.language: Language and ChatRequest.chat_type: ChatType so
pydantic validates inputs and returns 422 on invalid values; update any code
that reads these fields (e.g., functions in app/services/chat_prompt.py that
currently fallback) to use the enum members rather than raw strings.
- Around line 103-110: Change ChatMessageItem.role from an unconstrained str to
a strict enum/Literal limiting values to only "user" or "assistant" so clients
cannot inject "system" or other roles; update the ChatMessageItem definition
(and import types) to use typing.Literal["user","assistant"] or a small Enum and
ensure ChatRequest.history remains a list[ChatMessageItem] (and remove/avoid
using a mutable default like [] if applicable) so downstream code in
chat_prompt.py that forwards item.role to OpenAI only ever receives validated
"user" or "assistant" values.
In `@app/services/chat_prompt.py`:
- Around line 57-59: The CI fails due to a >100-char string on the second prompt
line in app/services/chat_prompt.py; locate the long Korean sentence in the
prompt definition (the multi-line prompt constant or list of prompt strings) and
break it into shorter string literals under 100 chars each using implicit
concatenation or by splitting into two strings joined with a space so the
runtime text is unchanged (e.g., split the long sentence into two shorter quoted
segments in the same prompt variable such as the system prompt constant used by
the chat prompt builder).
In `@app/services/chat_service.py`:
- Around line 14-18: chat currently only checks get_openai_settings().api_key
and ignores the OpenAISettings.enabled flag; update chat to first inspect
get_openai_settings().enabled (the OpenAISettings.enabled property) and if it's
False raise or return an OpenAIConfigurationError (same error type) before any
API calls or api_key checks, so no OpenAI traffic occurs when disabled; locate
the check in the chat function and modify the control flow to validate
settings.enabled prior to using settings.api_key.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 45c57693-eb6c-4716-ab98-bfca682be94f
📒 Files selected for processing (6)
.gitignoreapp/main.pyapp/routers/chat.pyapp/schemas.pyapp/services/chat_prompt.pyapp/services/chat_service.py
deli-minju
left a comment
There was a problem hiding this comment.
시스템 프롬프트와 OpenAI 호출 흐름이 잘 정리되어 있고, 낮은 temperature 설정과 불확실한 내용에 대한 확인 안내도 서비스 성격에 맞게 적절해 보입니다.
chat type 필드로 추후 문서 챗봇 확장까지 고려된 점도 확인했습니다. 고생하셨습니다!
📌 작업 요약
🌿 브랜치 정보
feat/#12-chatbot-openAIdevelop(기본)✅ 체크리스트
feat/refac/hotfix/chore/design/bugfix)feat/fix/refactor/docs/style/chore)🧪 테스트 결과
Summary by CodeRabbit
릴리스 노트
새로운 기능
개선 사항