chain.invoke()

์ƒค์›Œ์‹ค์˜ ๋ฐ”๋ณดยท2025๋…„ 2์›” 5์ผ

GPT

๋ชฉ๋ก ๋ณด๊ธฐ
9/11

๐Ÿ” 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๊ฐ€์ง€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์—ฐ๊ฒฐํ•จ.

  1. template โ†’ ChatPromptTemplate(ํ”„๋กฌํ”„ํŠธ ํ…œํ”Œ๋ฆฟ)

  2. chat โ†’ OpenAI ๋ชจ๋ธ(ChatOpenAI)

  3. CommaOutputParser() โ†’ ์‘๋‹ต์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํŒŒ์„œ(Parser)

๐Ÿ”น 3๏ธโƒฃ .invoke() ์‹คํ–‰

chain.invoke(
    {"max_items": 5, "question": "What are the 5 largest cities in the world?"}
)

๐Ÿ“Œ ์ด ๋ถ€๋ถ„์—์„œ ์‹คํ–‰๋˜๋Š” ๊ณผ์ •

  1. {"max_items": 5, "question": "What are the 5 largest cities in the world?"} ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ChatPromptTemplate์— ์‚ฝ์ž….
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?
  1. LangChain์˜ chat ๋ชจ๋ธ์ด ์‹คํ–‰๋จ (OpenAI ๋ชจ๋ธ ํ˜ธ์ถœ)

โ€ข ์˜ˆ์ œ ์‘๋‹ต:

"NEW YORK, TOKYO, SHANGHAI, MUMBAI, SAO PAULO"
  1. CommaOutputParser()๊ฐ€ ์‘๋‹ต์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜
["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))
  1. strip() โ†’ ๋ฌธ์ž์—ด ์–‘์ชฝ ๊ณต๋ฐฑ ์ œ๊ฑฐ

  2. split(",") โ†’ ์ฝค๋งˆ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ

  3. 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

๐Ÿ“Œ ์ด ๊ฒฝ์šฐ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ

  1. CommaOutputParser()๋Š” AI ์‘๋‹ต์ด ์žˆ์–ด์•ผ ๋™์ž‘ ๊ฐ€๋Šฅ โ†’ ํ•˜์ง€๋งŒ ์•„์ง AI ์‘๋‹ต์ด ์—†์Œ.

  2. chat ๋ชจ๋ธ์ด ์‹คํ–‰๋˜๊ธฐ ์ „์— CommaOutputParser()๊ฐ€ ์‹คํ–‰๋˜๋ฏ€๋กœ ์ž…๋ ฅ๊ฐ’์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Œ.

  3. template์ด ๋งˆ์ง€๋ง‰์— ์œ„์น˜ โ†’ ํ…œํ”Œ๋ฆฟ์ด ์•„์ง ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์€ ์ƒํƒœ์—์„œ AI๊ฐ€ ์‹คํ–‰๋จ.

โœ… 3๏ธโƒฃ ์˜ฌ๋ฐ”๋ฅธ ์ฒด์ด๋‹ ์ˆœ์„œ

chain = template | chat | CommaOutputParser()

๐Ÿ“Œ ์˜ฌ๋ฐ”๋ฅธ ์‹คํ–‰ ์ˆœ์„œ

  1. template: {max_items} ๋ฐ {question} ๋ณ€์ˆ˜๋ฅผ ์ฑ„์›Œ๋„ฃ์–ด ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ƒ์„ฑ.

  2. chat: AI ๋ชจ๋ธ์ด ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์‘๋‹ต์„ ์ƒ์„ฑ.

  3. CommaOutputParser(): AI์˜ ์‘๋‹ต์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜.

โœ… 4๏ธโƒฃ ์ฒด์ด๋‹ ์ˆœ์„œ๋ฅผ ์‹œ๊ฐ์ ์œผ๋กœ ํ‘œํ˜„

์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ๋ฐ์ดํ„ฐ๊ฐ€ ํ๋ฅด๋Š” ๋ฐฉํ–ฅ์„ ์œ ์ง€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

(1) template โ†’ (2) chat โ†’ (3) CommaOutputParser()
์ˆœ์„œ๋™์ž‘์„ค๋ช…
1๏ธโƒฃ templateํ”„๋กฌํ”„ํŠธ ํ…œํ”Œ๋ฆฟ ์ ์šฉ{max_items} ๋ฐ {question}์„ ์ฑ„์›€
2๏ธโƒฃ chatOpenAI ๋ชจ๋ธ ์‹คํ–‰ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ  ์‘๋‹ต์„ ์ƒ์„ฑ
3๏ธโƒฃ CommaOutputParser()์‘๋‹ต์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜AI์˜ ์ถœ๋ ฅ๊ฐ’์„ ๋ฆฌ์ŠคํŠธ๋กœ ๊ฐ€๊ณต

โœ… 5๏ธโƒฃ ๊ฒฐ๋ก : ์ฒด์ด๋‹ ์ˆœ์„œ๋Š” ์ค‘์š”ํ•˜๋‹ค

์˜ฌ๋ฐ”๋ฅธ ์ฒด์ด๋‹ (โœ…)์ž˜๋ชป๋œ ์ฒด์ด๋‹ (โŒ)
`templatechat
`templatechat`
`chatCommaOutputParser()`

๐Ÿš€ ๊ฒฐ๋ก : ์ˆœ์„œ๊ฐ€ ์œ ์ง€๋˜์ง€ ์•Š์œผ๋ฉด ์˜ค๋ฅ˜ ๋ฐœ์ƒ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์•„์ง€๋ฏ€๋กœ, ์˜ฌ๋ฐ”๋ฅธ ์ฒด์ด๋‹ ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค! ๐Ÿš€

profile
๊ณต๋ถ€ํ•˜๋Š” ๊ฐœ๋ฐœ์ž

0๊ฐœ์˜ ๋Œ“๊ธ€