# **ChatGPT Prompt Engineering for Developers**
# Guideline
## LLM의 2가지 종류
### Base LLM
- 학습된 단어들에 기반해 다음 단어를 유추
### Instruction Tuned LLM
- 주어진 지시(Instruction)들을 따르려 함
- 지시들에 맞게 tune 되어 있으며 이에 따라가도록 설정되어 있음
- RLHM(Reinforcement Learning With Human Feedback)와 사용됨
- 최근 좋은 서비스들의 경우 이 모델을 대부분 사용 중(ChatGPT 역시 마찬가지)
## Chat GPT 사용 수칙
### 1. 명확하고 분명한 Instruction을 작성하라(Write clear and specific instructions)(clear≠short)
1. ``, ``` ````, ---, <>, <tag> </tag> 등등의 Delimiter를 활용해라
2. 구체화된 output structure를 지정하라(JSON, HTML 등)
3. 결과를 얻기 위하여 모델에게 주어진 조건들이 충분한지 확인하라
1. 주어진 일에 대한 결과가 만족스럽지 않다면 모델에게 주어진 조건들에 대해 확인시켜라
2. 가능한 edge case들에 대해 고려하여 모델들이 예상하지 못한 결과나 에러를 발생시키지 않도록 해라
4. 해야할 일에 대한 몇몇 성공적인 결과물을 예시로 보여주고 모델에게 일을 시켜라
### 2. 모델에게 생각할 시간을 제공해라(Give the model time to think)
1. 최종적인 결과물을 얻기 위해 스텝을 나눠서 제공해라
2. 모델에게 충분히 생각할 시간을 주어라
1. 결과를 바로 얻는 것이 아니라 모델이 해당 결과를 내기 위해 사유할 수 있는 시간을 제공해라(즉, Few Step을 더 밟거나 더 명확하게 지시를 내려라)
- 보통은 긴 Instruction이 더 Detail한 지시를 제공한다
## Halluciation - 모델의 한계
- 하는 말이 plausible(이성적이거나 맞다고 여겨짐)하지만 사실이 아닐 수 있다
- Halluciation을 줄이는 방법
- 관련된 정보를 먼저 찾도록 지시해라
- 관련된 정보에 기반하여 질문에 대답하도록 만들어라.
---
# Iterative Prompt Development

- Prompt를 이용한 어플리케이션 제작에서 첫 시도에서 내가 원하는 결과를 얻기는 힘들다.
- 이를 위해 다음과 같은 과정을 반복한다.
1. Prompt를 분명하고 자세하게 작성한다.
2. 결과물이 기대했던 것과 다른 이유를 분석한다.
3. 아이디어와 Prompt를 정제한다.
4. 반복한다.
- 팁:
- Instruction의 어떤 부분에서 내가 원하는 결과를 얻지 못했는지 분석해라.
- Instruction을 분명하게 하고 더 생각할 시간을 주어라.
- 여러 예시를 제공하며 Prompt를 정제해라.
---
# Temperature

- Model의 response를 결정짓는 Parameter
- 모델의 답변 random 정도를 결정짓는 다고 보면 된다.
- 같은 prompt를 제공할 때 마다 동일한 답변을 받고 싶다면 0, 더 다양한 종류의 답변을 받고 싶다면 더 높은 값의 temperature를 설정해야 한다.
---
# 활용 예시
## Summarizing
- 많은 summarizing 어플리케이션들이 LLM을 이용해 만들어진다.
- 다양한 방법을 이용하여 긴 글을 summarize할 수 있다.
- 사용 예시
```python
prod_review = """
Got this panda plush toy for my daughter's birthday, \
who loves it and takes it everywhere. It's soft and \
super cute, and its face has a friendly look. It's \
a bit small for what I paid though. I think there \
might be other options that are bigger for the \
same price. It arrived a day earlier than expected, \
so I got to play with it myself before I gave it \
to her.
"""
```
- shipping and delivery에 초점 맞추기
```python
prompt = f"""
Your task is to generate a short summary of a product \
review from an ecommerce site to give feedback to the \
Shipping deparmtment.
Summarize the review below, delimited by triple
backticks, in at most 30 words, and focusing on any aspects \
that mention shipping and delivery of the product.
Review: ```{prod_review}```
"""
response = get_completion(prompt)
print(response)
```
- price and value에 초점 맞추기
```python
prompt = f"""
Your task is to generate a short summary of a product \
review from an ecommerce site to give feedback to the \
pricing deparmtment, responsible for determining the \
price of the product.
Summarize the review below, delimited by triple
backticks, in at most 30 words, and focusing on any aspects \
that are relevant to the price and perceived value.
Review: ```{prod_review}```
"""
```
- “요약”이 아닌 정보 “추출”하기
```python
prompt = f"""
Your task is to extract relevant information from \
a product review from an ecommerce site to give \
feedback to the Shipping department.
From the review below, delimited by triple quotes \
extract the information relevant to shipping and \
delivery. Limit to 30 words.
Review: ```{prod_review}```
"""
```
## Inferring
- LLM을 이용하여 추론 작업을 시킬 수 있다.
- e.g. 리뷰에서 감정 추론, Article의 Topic 추론, etc…
## Transforming
- LLM은 어떤 형태를 다른 형태로 transforming 하는 것을 잘한다.
e.g. 번역(심지어 formal, informal way로도 번역 요청 가능!), JSON to HTML, 단위 변환, 스펠링 및 문법 체크, etc…
## Expanding
- 짧은 글을 더 긴 글로 늘리는 것으로 email, essay 등등에 사용할 수 있다.
- email 예시
- 고객의 리뷰 메일에 대하여 긍정적인 리뷰와 부정적인 리뷰에 따라 다른 메일 답변 전송 가능하다.
- Temperature를 이용하여 매번 다른 response를 생성해내도록 할 수도 있다.
## Chatbot
- ChatGPT는 multi turn conversation 형태로 설계되어 있다.
- 따라서 이전 메세지들을 input으로 다시 넣어줘야 context를 이해하고 답변이 가능하다.
- 이전 메세지들을 다시 input으로 넣지 않는다면 문맥 이해가 불가능하다.
- Web Interface에서 나의 답변들은 ‘user’ role로 계속해서 저장이 되는 것이다.
### role
- system
- conversation을 위한 high level instruction을 제공할 수 있다
- assistant
- system에게 제공된 high level instruction에 기반하여 Chat GPT가 제공하는 메세지(Response)
- user
- 사용자의 메세지