프롬프트 인젝션 방어 추가
This commit is contained in:
1 parent
903c88b8bd
commit
9a5a120892
3 files changed
+30
-4
No files matched your search
+5
-3
@@ -1,7 +1,7 @@
|
||||
"""structured output 공용 래퍼.
|
||||
|
||||
OpenAI 호환 /chat/completions를 쓰는 프로바이더면 base_url만 바꿔 그대로 쓴다.
|
||||
출력은 항상 한국어로 받는다.
|
||||
출력은 항상 한국어로 받는다. 사용자 메시지는 데이터로만 다루도록 감싸고, 응답의 프롬프트 유출을 검사한다.
|
||||
"""
|
||||
from typing import TypeVar
|
||||
|
||||
@@ -9,6 +9,7 @@ from openai import AsyncOpenAI
|
||||
from pydantic import BaseModel
|
||||
|
||||
from settings import settings
|
||||
from utils.prompt_guard import GUARD_RULE, check_leak, wrap_data
|
||||
|
||||
T = TypeVar("T", bound=BaseModel)
|
||||
|
||||
@@ -28,10 +29,10 @@ class StructuredLLM:
|
||||
system: str | None = None,
|
||||
temperature: float = 0.0,
|
||||
) -> T:
|
||||
system_content = f"{system}\n\n{KOREAN_OUTPUT_RULE}" if system else KOREAN_OUTPUT_RULE
|
||||
system_content = "\n\n".join(rule for rule in (system, KOREAN_OUTPUT_RULE, GUARD_RULE) if rule)
|
||||
messages = [
|
||||
{"role": "system", "content": system_content},
|
||||
{"role": "user", "content": prompt},
|
||||
{"role": "user", "content": wrap_data(prompt)},
|
||||
]
|
||||
|
||||
msg = (await self._client.chat.completions.parse(
|
||||
@@ -41,6 +42,7 @@ class StructuredLLM:
|
||||
temperature=temperature,
|
||||
)).choices[0].message
|
||||
|
||||
check_leak(msg.content)
|
||||
if msg.refusal:
|
||||
raise RuntimeError(f"{self.model} 거절: {msg.refusal}")
|
||||
if msg.parsed is None:
|
||||
|
||||
Reference in new issue
Block a user