Prompt Engineering

프롬프트 평가 [4]

HeyTeddy 2024. 7. 26. 16:48
반응형

LLM 모델을 사용하여 질문을 통하여 얻은 답변을 평가하는 시간을 가죠보겠습니다.

이 프롬프트가 좋은지 아닌지 확인할 필요가 있습니다.

우선 평가 방법은 총 3가지로 구분할 수 있습니다.

  • 사람
  • 모델
  • 코드

평가 기준은 LLM이 어느 정도 성능을 가진 모델인지 정량 or 정성적으로 확인하는 객관적인 지표로 나타냅니다.

 

태스크에 적합한 평가 지표 설정이 필요한 이유는, 실사용 입장에선 사실 위 지표들간의 미미한 우위는 의미 없고 특정 태스크 점수가 중요합니다. 지표가 높다고 특정 태스크에서 무조건 높은건 절대 아닙니다.

 

사람을 활용한 평가가 필요한 이유는?

  1. 전문가 평가(주관적)
  2. 블라인드 평가
  3. 명확한 기준
  4. 더 좋은 답변을 선택하는 테스트

모델을 활용한 평가가 필요한 이유

  1. GPT-4 강한 LLM을 통해 평가하는 방법 (가장 유사하다)
  2. G-Eval, MT-Bench

코드로 모델을 활용한 평가를 알아보겠습니다.

# OpenAI API 호출
from openai import OpenAI

OPENAI_API_KEY = "API KEY 입력하세요"

# client 호출
client = OpenAI(api_key=OPENAI_API_KEY)

# 질문
question = "왜 숲은 초록색인가요?"

# gpt-3.5 모델을 통해 질문 후, 답변(answer3_5)을 얻음
completion = client.chat.completions.create(
    model='gpt-3.5-turbo-0125',
    messages=[{'role': 'user', 'content': question}],
    temperature=0.0
)

answer3_5 = completion.choices[0].message.content
print(answer3_5)

## 숲이 초록색인 이유는 주로 나무의 잎이나 식물들이 엽록소라는 녹색 색소를 함유하고 있기 때문입니다.
## 엽록소는 빛을 흡수하여 에너지를 생산하는 역할을 하며, 이 과정에서 초록색을 반사하기 때문에 숲은 우리 눈에 초록색으로 보입니다.
## 또한, 숲의 식물들이 서로 경쟁하며 자라는 과정에서 초록색이 우세해지게 되어 숲이 전체적으로 초록색으로 보이게 됩니다.

# gpt-4 모델을 통해 질문 후, 답변(answer4)을 얻음
completion = client.chat.completions.create(
    model='gpt-4-1106-preview', # 최근 모델
    messages=[{'role': 'user', 'content': question}],
    temperature=0.0
)

answer4 = completion.choices[0].message.content
print(answer4)

## 숲이 초록색으로 보이는 주된 이유는 나무와 식물의 잎에 포함된 엽록소(chlorophyll) 때문입니다.
## 엽록소는 식물이 광합성을 하는 데 필수적인 역할을 합니다.
## 광합성 과정에서 식물은 태양광을 에너지로 변환하여 탄수화물을 만들고, 이 과정에서 산소를 방출합니다.
## 엽록소는 태양광의 빨간색과 파란색 파장을 흡수하고, 초록색 파장을 반사합니다.
## 우리 눈은 반사된 빛을 감지하기 때문에, 이 반사된 초록색 빛이 우리에게 식물의 잎이 초록색으로 보이게 만듭니다. 숲이 많은 나무와 식물로 이루어져 있기 때문에, 우리는 숲을 초록색으로 인식하게 됩니다.
## 또한, 다른 색소들도 잎에서 발견될 수 있지만, 엽록소가 가장 우세하기 때문에 초록색이 가장 두드러지게 나타납니다. 가을철에는 일부 나무들이 엽록소를 분해하고 다른 색소들이 더 잘 보이게 되면서 잎이 노란색, 주황색, 빨간색으로 변하는 것을 볼 수 있습니다.

 

평가 프롬프트에 넣어보겠습니다.

 

prompt = f"""[System]
Please act as an impartial judge and evaluate the quality of the responses provided by two
AI assistants to the user question displayed below. You should choose the assistant that
follows the user’s instructions and answers the user’s question better. Your evaluation
should consider factors such as the helpfulness, relevance, accuracy, depth, creativity,
and level of detail of their responses. Begin your evaluation by comparing the two
responses and provide a short explanation. Avoid any position biases and ensure that the
order in which the responses were presented does not influence your decision. Do not allow
the length of the responses to influence your evaluation. Do not favor certain names of
the assistants. Be as objective as possible. After providing your explanation, output your
final verdict by strictly following this format: "[[A]]" if assistant A is better, "[[B]]"
if assistant B is better, and "[[C]]" for a tie.

[User Question]
{question}

[The Start of Assistant A’s Answer]
{answer3_5}
[The End of Assistant A’s Answer]

[The Start of Assistant B’s Answer]
{answer4}
[The End of Assistant B’s Answer]"""

# 평가
# 최신 모델 'gpt-4-turbo-2024-04-09'에 위 prompt를 넣어보자.
completion = client.chat.completions.create(
    model='gpt-4-turbo-2024-04-09', # 최신 모델
    messages=[{'role': 'user', 'content': prompt}],
    temperature=0.0
)

print(completion.choices[0].message.content)

## Comparing the responses from Assistant A and Assistant B, both assistants provide accurate and relevant explanations for why forests appear green, focusing on the role of chlorophyll in plant leaves. However, there are differences in the depth and detail of their explanations.

## Assistant A's response is concise and correctly identifies chlorophyll as the reason for the green color, mentioning the absorption of light and the reflection of green light. It also touches on the competitive aspect of plant growth in forests contributing to the predominance of green.

## Assistant B's response, on the other hand, goes into greater detail about the process of photosynthesis and the specific wavelengths of light absorbed and reflected by chlorophyll. This assistant also expands on how chlorophyll's dominance over other pigments in leaves leads to the green color, and further explains the seasonal color changes in leaves, which adds depth and educational value to the answer.

## In terms of creativity, both responses stick to scientific explanations and do not venture into creative interpretations, which is appropriate for the factual nature of the question.

## Overall, Assistant B provides a more detailed and comprehensive explanation that not only addresses the primary question but also enriches the user's understanding of related phenomena (like seasonal changes in leaf color). This makes Assistant B's response more helpful and informative.

## Final verdict: [[B]]

여기서 A는 answer3_5(gpt-3.5), B는 answer4(gpt-4)이다.

 

결론적으로 B예시가 더 좋은 예시라고 평가했다.

 

간략히 요약하자면,

  1. 평가 프롬프트에 'gpt-3.5'와 'gpt-4'의 질문한 답변을 넣는다
  2. 넣은 '평가 프롬프트'를 최신 gpt모델 'gpt-4-turbo-2024-04-09'에 평가한다.
  3. 결론적으로 B예시가 더 좋은 예시라고 하였다.

코드를 활용한 평가

  • Recall, Accuracy, Precision
  • BLEU
  • ROUGE
## 사람, 모델, 코드를 활용한 평가의 각각 장단점
### 1. 사람을 활용한 평가
- 장점
1. 복잡한 맥락과 뉘앙스를 사람은 이해할 수 있기 때문에, 심층적 이해와 맥락을 평가하는데 유리함
2. 통제된 환경일 경우, 사람이 직접 평가한 방법이라 신뢰를 할 수 있음
3. 실제 사용해보고 경험과 만족도를 반영할 수 있어, 사용자 중심의 평가 가능

- 단점
1. 다수의 의한 노이즈 발생 가능성이 있음
2. 전문가가 아닐시 정확성이 떨어짐
3. 시간과 비용이 높을 수 있음
 
### 2. 모델을 활용한 평가
-장점
1. 정량적 지표를 통해 객관적으로 성능을 평가할 수 있음
2. 자동으로 대규모 데이터셋을 평가할 수 있어, 시간과 비용을 절약할 수 있음
3. 평가의 일관성과 신뢰성을 높일 수 있음

- 단점
1. 모델은 복잡한 맥락이나 뉘앙스를 완벽히 이해하지 못할 수 있음
2. 편향이 평가 결과에 미칠 수 있음
 
### 3. 코드를 활용한 평가
- 장점
1. 무료로 평가할 수 있음
2. 신속하고 정확하게 평가할 수 있음
3. 복잡한 계산 가능

- 단점
1. 신뢰성이 떨어질 수 있음
2. 기술적 제약 발생

## 요약
1. 태스크에 적합한 사람이 평가하는 방법이 제일 좋음
2. 현실적으로 사람이 평가하는 방법은 쉽지 않아, 모델이 평가하는 방법도 충분히 사용함
3. 코드를 활용한 평가를 권장, 현실적으로 고려하는 점이 '비용'이기 때문에
4. 가장 이상적인 평가 방법은 정량적 + 정성적 평가 모두 하는 법
5. 최종 지표는 사용자의 피드백

## 정리
- 평가 기준은 총 3가지
1. 사람을 활용한 평가
2. 모델을 활용한 평가
3. 코드를 활용한 평가

 

반응형