๐ LangChain์ ChatPromptTemplate + chat + CommaOutputParser()์ ๋์ ๊ณผ์ ์์ธ ์ค๋ช
์ด ์ฝ๋๋ LangChain์์ ํ๋กฌํํธ ํ ํ๋ฆฟ์ ์์ฑํ๊ณ , OpenAI ๋ชจ๋ธ๊ณผ ์ฐ๊ฒฐํ ํ, ๊ฒฐ๊ณผ๋ฅผ CommaOutputParser๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ๊ณตํ๋ ํ์ดํ๋ผ์ธ์ ๋๋ค.
โ 1๏ธโฃ ์ฝ๋ ๋ถ์
template = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a list generator. Everything you are asked will be answered with a comma separated list of max {max_items} in uppercase. DO NOT reply with anything else.",
),
(
"human",
"{question}",
),
]
)
chain = template | chat | CommaOutputParser()
chain.invoke(
{"max_items": 5, "question": "What are the 5 largest cities in the world?"}
)
โ 2๏ธโฃ ๋์ ๊ณผ์ (์ ์ฒด ํ๋ฆ)
| ๋จ๊ณ | ์ค๋ช |
|---|---|
| 1๏ธโฃ ChatPromptTemplate ์์ฑ | {max_items}์ {question}์ ํฌํจํ ํ๋กฌํํธ ํ ํ๋ฆฟ ์ ์ |
| 2๏ธโฃ ํ ํ๋ฆฟ์ chat ๋ชจ๋ธ๊ณผ ์ฐ๊ฒฐ | chat ๋ชจ๋ธ(OpenAI GPT-3.5)์๊ฒ ๋ณํ๋ ํ ์คํธ๋ฅผ ์ ๋ฌ |
| 3๏ธโฃ CommaOutputParser() ์ฐ๊ฒฐ | ๋ชจ๋ธ์ ์๋ต์ ํ์ฑํ์ฌ ์ฝค๋ง(,)๋ก ๊ตฌ๋ถ๋ ๋ฆฌ์คํธ๋ก ๋ณํ |
| 4๏ธโฃ chain.invoke() ์คํ | ์ ๋ ฅ ๋ฐ์ดํฐ๋ฅผ ํ ํ๋ฆฟ์ ์ฝ์ โ OpenAI ๋ชจ๋ธ์ด ์๋ต ์์ฑ โ ์๋ต์ CommaOutputParser()๊ฐ ๊ฐ๊ณต |
โ 3๏ธโฃ ์ฝ๋์ ์ธ๋ถ ๊ณผ์
๐น 1๏ธโฃ ChatPromptTemplate ์ ์
template = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a list generator. Everything you are asked will be answered with a comma separated list of max {max_items} in uppercase. DO NOT reply with anything else.",
),
(
"human",
"{question}",
),
]
)
๐ ๋์ ์๋ฆฌ
โข system ๋ฉ์์ง๋ AI์ ์ญํ ์ ์ ์:
"You are a list generator. Everything you are asked will be answered with a comma separated list of max {max_items} in uppercase. DO NOT reply with anything else."
โข human ๋ฉ์์ง๋ ์ฌ์ฉ์์ ์ง๋ฌธ์ ํฌํจ:
"{question}"
โข {max_items} ๋ฐ {question} โ ์ดํ .invoke() ์คํ ์ ๊ฐ์ด ์ฑ์์ง.
๐น 2๏ธโฃ chain ์์ฑ
chain = template | chat | CommaOutputParser()
๐ ์ด ๊ตฌ๋ฌธ์ LangChain์ ์ฒด์ด๋ ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ฌ 3๊ฐ์ง ์ปดํฌ๋ํธ๋ฅผ ์ฐ๊ฒฐํจ.
template โ ChatPromptTemplate(ํ๋กฌํํธ ํ ํ๋ฆฟ)
chat โ OpenAI ๋ชจ๋ธ(ChatOpenAI)
CommaOutputParser() โ ์๋ต์ ๋ฆฌ์คํธ๋ก ๋ณํํ๋ ํ์(Parser)
๐น 3๏ธโฃ .invoke() ์คํ
chain.invoke(
{"max_items": 5, "question": "What are the 5 largest cities in the world?"}
)
๐ ์ด ๋ถ๋ถ์์ ์คํ๋๋ ๊ณผ์
SYSTEM: You are a list generator. Everything you are asked will be answered with a comma separated list of max 5 in uppercase. DO NOT reply with anything else.
HUMAN: What are the 5 largest cities in the world?
โข ์์ ์๋ต:
"NEW YORK, TOKYO, SHANGHAI, MUMBAI, SAO PAULO"
["NEW YORK", "TOKYO", "SHANGHAI", "MUMBAI", "SAO PAULO"]
โ 4๏ธโฃ CommaOutputParser()์ ์ญํ
๐ CommaOutputParser() ๋ด๋ถ ๋์
class CommaOutputParser(BaseOutputParser):
def parse(self, text):
items = text.strip().split(",")
return list(map(str.strip, items))
strip() โ ๋ฌธ์์ด ์์ชฝ ๊ณต๋ฐฑ ์ ๊ฑฐ
split(",") โ ์ฝค๋ง๋ก ๋ถ๋ฆฌํ์ฌ ๋ฆฌ์คํธ ์์ฑ
map(str.strip, items) โ ๊ฐ ๋ฆฌ์คํธ ์์์ ๊ณต๋ฐฑ ์ ๊ฑฐ
๐ ์ต์ข ๋ณํ ๊ฒฐ๊ณผ
["NEW YORK", "TOKYO", "SHANGHAI", "MUMBAI", "SAO PAULO"]
โ 5๏ธโฃ ์ต์ข ์์ฝ
| ๋จ๊ณ | ๋์ |
|---|---|
| 1๏ธโฃ ํ๋กฌํํธ ํ ํ๋ฆฟ ์์ฑ | ChatPromptTemplate.from_messages()๋ก {max_items}, {question} ํฌํจํ ํ ํ๋ฆฟ ์ ์ |
| 2๏ธโฃ ์ฒด์ธ ์์ฑ | `ChatPromptTemplate |
| 3๏ธโฃ .invoke() ์คํ | ํ ํ๋ฆฟ ๋ณ์์ ๊ฐ ์ฝ์ ํ OpenAI ๋ชจ๋ธ ํธ์ถ |
| 4๏ธโฃ AI ์๋ต ์์ฑ | "NEW YORK, TOKYO, SHANGHAI, MUMBAI, SAO PAULO" ๋ฐํ |
| 5๏ธโฃ CommaOutputParser() ๋ณํ | ["NEW YORK", "TOKYO", "SHANGHAI", "MUMBAI", "SAO PAULO"] ๋ฆฌ์คํธ๋ก ๋ณํ |
๐ ์ฒด์ด๋(|)์์ ์์๊ฐ ์ค์ํ๊ฐ?
chain = template | chat | CommaOutputParser()
โ ์์๊ฐ ์ค์ํฉ๋๋ค!
๊ฐ ๊ตฌ์ฑ ์์๊ฐ ๋ฐ์ดํฐ๋ฅผ ์์ฐจ์ ์ผ๋ก ์ฒ๋ฆฌํ๊ธฐ ๋๋ฌธ์ ์ฌ๋ฐ๋ฅธ ์์๋ก ์ฐ๊ฒฐํด์ผ ํฉ๋๋ค.
โ 1๏ธโฃ ์ฒด์ด๋(|)์ ๋์ ๋ฐฉ์
LangChain์์๋ | ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ๋ฌ ๊ฐ์ ์ฒด์ธ์ ์ฐ๊ฒฐํ ์ ์์ต๋๋ค.
์์๊ฐ ์ค์ํ ์ด์ ?
๋ฐ์ดํฐ ํ๋ฆ์ด ์๋์ ๊ฐ์ด ์งํ๋๊ธฐ ๋๋ฌธ์ ๋๋ค.
1๏ธโฃ `template`: ํ๋กฌํํธ ํ
ํ๋ฆฟ ์์ฑ โ {๋ณ์} ์ฑ์๋ฃ๊ธฐ
2๏ธโฃ `chat`: AI ๋ชจ๋ธ์ ํธ์ถํ์ฌ ๊ฒฐ๊ณผ ์์ฑ
3๏ธโฃ `CommaOutputParser()`: AI์ ์๋ต์ ํ์ฑํ์ฌ ๋ฆฌ์คํธ๋ก ๋ณํ
๊ฐ ๋จ๊ณ๊ฐ ์ด์ ๋จ๊ณ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋์ํ๋ฏ๋ก, ์์๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ์ ์ง๋์ด์ผ ํฉ๋๋ค.
โ 2๏ธโฃ ์ฒด์ด๋ ์์๋ฅผ ์๋ชป ์ค์ ํ ๊ฒฝ์ฐ
์๋ฅผ ๋ค์ด, ์์๋ฅผ ์๋ชป ์ค์ ํ๋ฉด ์ค๋ฅ๊ฐ ๋ฐ์ํ๊ฑฐ๋ ์๋ชป๋ ๊ฒฐ๊ณผ๊ฐ ๋์ฌ ์ ์์ต๋๋ค.
# โ ์๋ชป๋ ์ฒด์ด๋ ์์
chain = CommaOutputParser() | chat | template
๐ ์ด ๊ฒฝ์ฐ ๋ฐ์ํ๋ ๋ฌธ์
CommaOutputParser()๋ AI ์๋ต์ด ์์ด์ผ ๋์ ๊ฐ๋ฅ โ ํ์ง๋ง ์์ง AI ์๋ต์ด ์์.
chat ๋ชจ๋ธ์ด ์คํ๋๊ธฐ ์ ์ CommaOutputParser()๊ฐ ์คํ๋๋ฏ๋ก ์ ๋ ฅ๊ฐ์ด ์ฌ๋ฐ๋ฅด์ง ์์.
template์ด ๋ง์ง๋ง์ ์์น โ ํ ํ๋ฆฟ์ด ์์ง ์ฒ๋ฆฌ๋์ง ์์ ์ํ์์ AI๊ฐ ์คํ๋จ.
โ 3๏ธโฃ ์ฌ๋ฐ๋ฅธ ์ฒด์ด๋ ์์
chain = template | chat | CommaOutputParser()
๐ ์ฌ๋ฐ๋ฅธ ์คํ ์์
template: {max_items} ๋ฐ {question} ๋ณ์๋ฅผ ์ฑ์๋ฃ์ด ํ๋กฌํํธ๋ฅผ ์์ฑ.
chat: AI ๋ชจ๋ธ์ด ํ๋กฌํํธ๋ฅผ ์ ๋ ฅ๋ฐ์ ์๋ต์ ์์ฑ.
CommaOutputParser(): AI์ ์๋ต์ ๋ฆฌ์คํธ๋ก ๋ณํ.
โ 4๏ธโฃ ์ฒด์ด๋ ์์๋ฅผ ์๊ฐ์ ์ผ๋ก ํํ
์๋ ๊ทธ๋ฆผ์ฒ๋ผ ๋ฐ์ดํฐ๊ฐ ํ๋ฅด๋ ๋ฐฉํฅ์ ์ ์งํด์ผ ํฉ๋๋ค.
(1) template โ (2) chat โ (3) CommaOutputParser()
| ์์ | ๋์ | ์ค๋ช |
|---|---|---|
| 1๏ธโฃ template | ํ๋กฌํํธ ํ ํ๋ฆฟ ์ ์ฉ | {max_items} ๋ฐ {question}์ ์ฑ์ |
| 2๏ธโฃ chat | OpenAI ๋ชจ๋ธ ์คํ | ํ๋กฌํํธ๋ฅผ ์ ๋ ฅ๋ฐ๊ณ ์๋ต์ ์์ฑ |
| 3๏ธโฃ CommaOutputParser() | ์๋ต์ ๋ฆฌ์คํธ๋ก ๋ณํ | AI์ ์ถ๋ ฅ๊ฐ์ ๋ฆฌ์คํธ๋ก ๊ฐ๊ณต |
โ 5๏ธโฃ ๊ฒฐ๋ก : ์ฒด์ด๋ ์์๋ ์ค์ํ๋ค
| ์ฌ๋ฐ๋ฅธ ์ฒด์ด๋ (โ ) | ์๋ชป๋ ์ฒด์ด๋ (โ) |
|---|---|
| `template | chat |
| `template | chat` |
| `chat | CommaOutputParser()` |
๐ ๊ฒฐ๋ก : ์์๊ฐ ์ ์ง๋์ง ์์ผ๋ฉด ์ค๋ฅ ๋ฐ์ ๊ฐ๋ฅ์ฑ์ด ๋์์ง๋ฏ๋ก, ์ฌ๋ฐ๋ฅธ ์ฒด์ด๋ ์์๋ฅผ ์ ์งํด์ผ ํฉ๋๋ค! ๐