> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreweave.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 가드레일로 사용하는 Scorer

> W&B Weave에서 Scorer를 가드레일로 사용하는 방법을 알아보세요

> [!NOTE]
>
> 이 문서는 대화형 노트북입니다. 로컬에서 실행하거나 아래 링크를 사용할 수 있습니다.
>
> * [Google Colab에서 열기](https://colab.research.google.com/github/wandb/examples/blob/main/colabs/weave/scorers_as_guardrails.ipynb)
> * [GitHub에서 소스 보기](https://github.com/wandb/examples/blob/main/colabs/weave/scorers_as_guardrails.ipynb)

Weave Scorer는 호출의 성능을 평가하는 `score` 방법을 갖춘 클래스입니다. 기본 규칙부터 LLM 평가자까지 다양한 형태로 구현할 수 있습니다.

이 노트북은 실행 시점에 Scorer를 적용하여 원치 않는 모델 출력을 차단하거나 필터링하려는 Weave 사용자를 위한 문서입니다. 안내를 따라 LLM 응답을 가로채고, Scorer로 평가한 뒤, Scorer가 응답을 안전하지 않은 것으로 표시하면 대체 메시지를 반환하는 콘텐츠 안전 가드레일을 구축합니다. Scorer를 이렇게 사용하면 오프라인 실험에 사용하는 평가 로직을 재사용하여 프로덕션에서 안전 정책을 강제 적용할 수 있습니다.

이 노트북을 마치면 LLM Call을 감싸고 Scorer의 판정에 따라 원래 응답 또는 안전한 대체 응답을 반환하는 `ContentSafetyScorer`를 구현할 수 있습니다.

```python lines theme={"system"}
%pip install weave --quiet
python
"""
Example demonstrating how to implement guardrails in Weave.
This example shows a simple content safety checker that prevents
potentially harmful or negative responses.
"""

import weave

# 알아보기 쉬운 프로젝트 이름으로 Weave를 초기화합니다
weave.init("content-safety-guardrails")

class ContentSafetyScorer(weave.Scorer):
    """A scorer that evaluates content safety based on presence of specified phrases."""

    unsafe_phrases: list[str]
    case_sensitive: bool = False

    @weave.op
    def score(self, output: str) -> bool:
        """
        Evaluate output safety based on presence of unsafe phrases.

        Args:
            output: The text output to evaluate

        Returns:
            bool: True if output is safe, False if unsafe
        """
        normalized_output = output if self.case_sensitive else output.lower()

        for phrase in self.unsafe_phrases:
            normalized_phrase = phrase if self.case_sensitive else phrase.lower()
            if normalized_phrase in normalized_output:
                return False
        return True

@weave.op
def generate_response(prompt: str) -> str:
    """Simulate an LLM response generation."""
    if "test" in prompt.lower():
        return "I'm sorry, I cannot process that request."
    elif "help" in prompt.lower():
        return "I'd be happy to help you with that!"
    else:
        return "Here's what you requested: " + prompt

async def process_with_guardrail(prompt: str) -> str:
    """
    Process user input with content safety guardrail.
    Returns the response if safe, or a fallback message if unsafe.
    """
    # 안전성 Scorer를 초기화합니다
    safety_scorer = ContentSafetyScorer(
        name="Content Safety Checker",
        unsafe_phrases=["sorry", "cannot", "unable", "won't", "will not"],
    )

    # 응답을 생성하고 Call 객체를 조회합니다
    response, call = generate_response.call(prompt)

    # 안전성 점수화를 적용합니다
    evaluation = await call.apply_scorer(safety_scorer)

    # 안전성 검사 결과에 따라 응답 또는 대체 메시지를 반환합니다
    if evaluation.result:
        return response
    else:
        return "I cannot provide that response."
python
"""Example usage of the guardrail system."""
test_prompts = [
    "Please help me with my homework",
    "Can you run a test for me?",
    "Tell me a joke",
]

print("Testing content safety guardrails:\n")

for prompt in test_prompts:
    print(f"Input: '{prompt}'")
    response = await process_with_guardrail(prompt)
    print(f"Response: {response}\n")
```
