
RLHF는 "Reinforcement Learning from Human Feedback"의 약자로, 인간의 피드백을 활용한 강화 학습을 의미합니다. 이는 기계 학습, 특히 인공지능 모델의 학습에서 중요한 방법론 중 하나입니다.
RLHF의 기본 개념
- 강화 학습(Reinforcement Learning, RL): 강화 학습은 에이전트가 환경과 상호작용하면서 보상을 최대화하기 위해 행동을 학습하는 방법입니다. 에이전트는 행동(action)을 취하고, 그 행동에 대한 결과로 보상(reward)을 받으며, 이 보상을 최대화하는 방향으로 학습합니다.
- 인간의 피드백(Human Feedback): RLHF에서는 에이전트의 행동에 대해 사람이 직접 피드백을 제공합니다. 이 피드백은 에이전트가 올바른 행동을 학습하는 데 중요한 역할을 합니다. 사람이 직접 에이전트의 행동을 평가하고, 그에 따른 보상을 제공하여 에이전트가 더 나은 의사 결정을 할 수 있도록 유도합니다.
RLHF의 적용
RLHF는 특히 자연어 처리(NLP) 모델의 훈련에 많이 사용됩니다. 예를 들어, 대화형 AI 모델이 특정 질문에 대한 응답을 생성할 때, 인간 피드백을 통해 응답의 적절성, 유용성, 정확성을 평가하고, 이를 바탕으로 모델을 개선할 수 있습니다. 이를 통해 모델은 더 자연스럽고 정확한 응답을 생성하게 됩니다.
요약
RLHF는 인간의 피드백을 사용하여 강화 학습을 수행하는 방법입니다.
강화 학습과 인간의 피드백을 결합하여, 에이전트가 더 나은 의사 결정을 할 수 있도록 학습을 진행합니다.
주로 자연어 처리와 같은 분야에서 모델의 성능을 개선하는 데 사용됩니다.
ChatGPT 4의 답변 -
대규모 언어 모델에 효과적인 프롬프트를 작성하기 위해 두 가지 프롬프트 원칙과 관련 전략을 연습
pip install openai

이렇게 하면 설치 완료!
직접 해보실 거면 env파일 등록을 해줘야합니다!
```python
import openai
import os
from dotenv import load_dotenv, find_dotenv_ = load_dotenv(find_dotenv())
openai.api_key = os.getenv('OPENAI_API_KEY')→ .env 파일에 본인의 key로 설정
```
그리고 버전도 다운그레이드 해줘야 합니다!
```shell
pip install python-dotenv
```
• OpenAI API 키는 https://platform.openai.com/account/api-keys 에서 생성 및 확인 가능

GPT 3.5 Turbo 모델과 Chat completions 엔드포인트를 사용하며 Chat completions endpoint의 형식과 입력에 대한 더 자세한 내용은 이후에 다룸
Helper Function을 사용하면 프롬프트를 사용하기 쉬워지고, 생성된 출력을 확인할 수 있음
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0, # this is the degree of randomness of the model's output
)
return response.choices[0].message["content"]
, """, < >, , :`# 원본
text = f"""
You should express what you want a model to do by \
providing instructions that are as clear and \
specific as you can possibly make them. \
This will guide the model towards the desired output, \
and reduce the chances of receiving irrelevant \
or incorrect responses. Don't confuse writing a \
clear prompt with writing a short prompt. \
In many cases, longer prompts provide more clarity \
and context for the model, which can lead to \
more detailed and relevant outputs.
"""
prompt = f"""
Summarize the text delimited by triple backticks \
into a single sentence.
```{text}```
"""
response = get_completion(prompt)
print(response)
# 번역
text = f"""
가능한 한 명확하고 구체적인 지침을 제공하여 모델이 수행하기를 원하는 작업을 표현해야 합니다.
이렇게 하면 모델을 원하는 출력으로 안내하고 관련성이 없거나 잘못된 응답을 받을 가능성을 줄일 수 있습니다.
명확한 프롬프트를 작성하는 것과 짧은 프롬프트를 작성하는 것을 혼동하지 마세요.
대부분의 경우, 긴 프롬프트가 모델에 더 명확하고 맥락을 제공하므로 다음과 같은 결과를 얻을 수 있습니다.
더 상세하고 관련성 높은 출력을 얻을 수 있습니다.
"""
prompt = f"""
백틱 세 개로 구분된 텍스트를 한 문장으로 요약하세요.
```{text}```
"""
response = get_completion(prompt)
print(response)
결과

우리가 하고자 하는 작업은 text를 요약하는 것
그래서 프롬프트에 세 개의 백틱으로 구분된 텍스트를 한 문장으로 요약하라고 명령
그 후 응답을 얻기 위해 getCompletion 헬퍼 함수를 사용 후 응답 출력
모델의 작업을 명확하고 구체적으로 표현하고, 긴 프롬프트를 사용하여 모델이 더 명확하고 관련성 높은 출력을 얻을 수 있도록 해야 함
구분자를 사용하는 것은 Prompt Injection을 피하는 유용한 기법
RateLimitError 문제 발생 시

# 영어 원문
prompt = f"""
Generate a list of three made-up book titles along \
with their authors and genres.
Provide them in JSON format with the following keys:
book_id, title, author, genre.
"""
response = get_completion(prompt)
print(response)
# 한글 번역문
prompt = f"""
저자 및 장르와 함께 세 개의 구성 도서 제목 목록을 생성합니다.
다음 키와 함께 JSON 형식으로 제공하세요:
book_id, 제목, 저자, 장르.
"""
response = get_completion(prompt)
print(response)

# 차 한잔을 만드는 프롬프트 예시
text_1 = f"""
Making a cup of tea is easy! First, you need to get some \
water boiling. While that's happening, \
grab a cup and put a tea bag in it. Once the water is \
hot enough, just pour it over the tea bag. \
Let it sit for a bit so the tea can steep. After a \
few minutes, take out the tea bag. If you \
like, you can add some sugar or milk to taste. \
And that's it! You've got yourself a delicious \
cup of tea to enjoy.
"""
# 지시 사항 순서를 포함하고 있으면 다음 형식으로 지시 사항을 재작성하고 단계를 적음
# 만약 지시 사항 순서를 포함하지 않는다면 "No steps provided" 출력
prompt = f"""
You will be provided with text delimited by triple quotes.
If it contains a sequence of instructions, \
re-write those instructions in the following format:
Step 1 - ...
Step 2 - …
…
Step N - …
If the text does not contain a sequence of instructions, \
then simply write \"No steps provided.\"
\"\"\"{text_1}\"\"\"
"""
response = get_completion(prompt)
print("Completion for Text 1:")
print(response)
실행 결과 (올바르게 출력)

text_2 = f"""
The sun is shining brightly today, and the birds are \
singing. It's a beautiful day to go for a \
walk in the park. The flowers are blooming, and the \
trees are swaying gently in the breeze. People \
are out and about, enjoying the lovely weather. \
Some are having picnics, while others are playing \
games or simply relaxing on the grass. It's a \
perfect day to spend time outdoors and appreciate the \
beauty of nature.
"""
prompt = f"""
You will be provided with text delimited by triple quotes.
If it contains a sequence of instructions, \
re-write those instructions in the following format:
Step 1 - ...
Step 2 - …
…
Step N - …
If the text does not contain a sequence of instructions, \
then simply write \"No steps provided.\"
\"\"\"{text_2}\"\"\"
"""
response = get_completion(prompt)
print("Completion for Text 2:")
print(response)

# 모델에게 일관된 어조로 답변하도록 지시
prompt = f"""
Your task is to answer in a consistent style.
<child>: Teach me about patience.
<grandparent>: The river that carves the deepest \
valley flows from a modest spring; the \
grandest symphony originates from a single note; \
the most intricate tapestry begins with a solitary thread.
<child>: Teach me about resilience.
"""
response = get_completion(prompt)
print(response)

text = f"""
In a charming village, siblings Jack and Jill set out on \
a quest to fetch water from a hilltop \
well. As they climbed, singing joyfully, misfortune \
struck—Jack tripped on a stone and tumbled \
down the hill, with Jill following suit. \
Though slightly battered, the pair returned home to \
comforting embraces. Despite the mishap, \
their adventurous spirits remained undimmed, and they \
continued exploring with delight.
"""
# example 1 (단계 제시)
prompt_1 = f"""
Perform the following actions:
1 - Summarize the following text delimited by triple \
backticks with 1 sentence.
2 - Translate the summary into French.
3 - List each name in the French summary.
4 - Output a json object that contains the following \
keys: french_summary, num_names.
Separate your answers with line breaks.
Text:
```{text}```
"""
response = get_completion(prompt_1)
print("Completion for prompt 1:")
print(response)

prompt_2 = f"""
Your task is to perform the following actions:
1 - Summarize the following text delimited by
<> with 1 sentence.
2 - Translate the summary into French.
3 - List each name in the French summary.
4 - Output a json object that contains the
following keys: french_summary, num_names.
Use the following format:
Text: <text to summarize>
Summary: <summary>
Translation: <summary translation>
Names: <list of names in summary>
Output JSON: <json with summary and num_names>
Text: <{text}>
"""
response = get_completion(prompt_2)
print("\nCompletion for prompt 2:")
print(response)

prompt = f"""
Determine if the student's solution is correct or not.
Question:
I'm building a solar power installation and I need \
help working out the financials.
- Land costs $100 / square foot
- I can buy solar panels for $250 / square foot
- I negotiated a contract for maintenance that will cost \
me a flat $100k per year, and an additional $10 / square \
foot
What is the total cost for the first year of operations
as a function of the number of square feet.
Student's Solution:
Let x be the size of the installation in square feet.
Costs:
1. Land cost: 100x
2. Solar panel cost: 250x
3. Maintenance cost: 100,000 + 100x
Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000
"""
response = get_completion(prompt)
print(response)
실행 결과 (학생의 잘못된 풀이: 유지비용이 10만 달러 + 100x인데 실제로는 10x가 되어야 함)
그래서 실제로는 Total Cost가 450x + 100,000이 아닌 360x + 100,000이 되어야 함

하지만 올바르다고 함
사람도 학생의 풀이만 읽었을 때는 계산을 잘못할 가능성이 큼
모델에게 자신만의 풀이를 찾아내고 학생의 풀이와 비교하도록 만듦
prompt = f"""
Your task is to determine if the student's solution \
is correct or not.
To solve the problem do the following:
- First, work out your own solution to the problem including the final total.
- Then compare your solution to the student's solution \
and evaluate if the student's solution is correct or not.
Don't decide if the student's solution is correct until
you have done the problem yourself.
Use the following format:
Question:
question here
Student's solution:
student's solution here
Actual solution:
steps to work out the solution and your solution here
Is the student's solution the same as actual solution \
just calculated:
yes or no
Student grade:
correct or incorrect
Question:
I'm building a solar power installation and I need help \
working out the financials.
Student's solution:
Let x be the size of the installation in square feet.
Costs:
1. Land cost: 100x
2. Solar panel cost: 250x
3. Maintenance cost: 100,000 + 100x
Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000
Actual solution:
"""
response = get_completion(prompt)
print(response)
지시에 따라 모델이 자체적으로 계산 과정을 진행
올바르게 360x를 사용한 값을 얻음
모델에게 직접 계산을 요청하고 작업을 단계별로 분해하며 모델에게 더 많은 시간을 주어 생각하게 함

prompt = f"""
Tell me about AeroGlide UltraSlim Smart Toothbrush by Boie
"""
response = get_completion(prompt)
print(response)
가상의 제품에 대한 현실적인 설명을 제공함
이런 것들은 현실적으로 들리기 때문에 위험
