LangChain์ด๋? โ๏ธ
1. LangChain ์ ์
LangChain์ ๋ํ ์ธ์ด ๋ชจ๋ธ(LLM, Large Language Model)์ ํ์ฉ์ ์ฝ๊ฒ ๋ง๋ค์ด์ฃผ๋ ํ๋ ์์ํฌ๋ก,
OpenAI GPT, Hugging Face์ Transformers, Cohere ๋ฑ์ ๋ค์ํ LLM์ ์ฐ๊ฒฐํ์ฌ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ตฌ์ถํ ์ ์๋๋ก ๋์์ค๋ค.
ํนํ, ํ๋กฌํํธ ์์ง๋์ด๋ง, ์ฒด์ด๋(Chaining), ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ, ๋ฐ์ดํฐ ๊ฒ์(Augmented Generation) ๋ฑ์ ํฌํจํ ๋ค์ํ ๊ธฐ๋ฅ์
์ ๊ณตํ๋ค.
2. LangChain์ ์ฃผ์ ๊ฐ๋
1) LLMs (Large Language Models)
LangChain์ ๋ค์ํ LLM์ ์ง์ํ๋ฉฐ, ์ด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์์ฐ์ด ์ฒ๋ฆฌ(NLP) ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ฐ๋ฐํ ์ ์๋ค.
from langchain.llms import OpenAI
llm = OpenAI(model_name="text-davinci-003", openai_api_key="your-api-key")
response = llm("Hello, how are you?")
print(response)
2) ํ๋กฌํํธ ํ ํ๋ฆฟ (Prompt Templates)
ํ๋กฌํํธ๋ฅผ ๋์ ์ผ๋ก ์์ฑํ์ฌ LLM์ ์ ๋ ฅํ ์ ์๋๋ก ํ๋ค.
from langchain.prompts import PromptTemplate
template = PromptTemplate(input_variables=["product"], template= "Tell me about {product}.")
print(template.format(product="iPhone"))
3) ์ฒด์ธ (Chains)
์ฌ๋ฌ ๊ฐ์ ํ๋กฌํํธ์ LLM ํธ์ถ์ ์ฐ๊ฒฐํ์ฌ ๋ ๋ณต์กํ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ง๋ค ์ ์๋ค.
from langchain.chains import LLMChain
chain = LLMChain(llm=llm, prompt=template)
print(chain.run("iPhone"))
ํ์ดํ ์ฐ์ฐ์ (`|`)๋ฅผ ์ด์ฉํ ์ฒด์ธ ๊ฒฐํฉ
LangChain์์๋ `|` ์ฐ์ฐ์๋ฅผ ํ์ฉํ์ฌ ์ฌ๋ฌ ์ฒด์ธ์ ์ฐ๊ฒฐํ ์๋ ์๋ค.
from langchain.chains import SimpleSequentialChain
chain1 = LLMChain(llm=llm, prompt=PromptTemplate(input_variables=["text"], template="Summarize: {text}"))
chain2 = LLMChain(llm=llm, prompt=PromptTemplate(input_variables=["summary"], template="Explain: {summary}"))
final_chain = chain1 | chain2
result = final_chain.invoke({"text": "LangChain is an amazing framework for LLMs."})
print(result)
์ ์ฝ๋์์๋ ์ฒซ ๋ฒ์งธ ์ฒด์ธ์ด ํ ์คํธ๋ฅผ ์์ฝํ๊ณ , ๋ ๋ฒ์งธ ์ฒด์ธ์ด ํด๋น ์์ฝ์ ์ค๋ช ํ๋ ๋ฐฉ์์ผ๋ก ์ฒด์ธ์ด ์ฐ๊ฒฐ๋๋ค.
4) ๋ฉ๋ชจ๋ฆฌ (Memory)
LangChain์ ๋ํํ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ง๋ค ๋, ์ด์ ๋ํ ๊ธฐ๋ก์ ์ ์ฅํ๊ณ ํ์ฉํ ์ ์๋๋ก ๋ฉ๋ชจ๋ฆฌ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
memory.save_context({"input": "Hello"}, {"output": "Hi there!"})
print(memory.load_memory_variables({}))
5) ์์ด์ ํธ (Agents)
LangChain์ ์์ด์ ํธ๋ ๋ค์ํ ๋๊ตฌ(tool)์ ์ํธ์์ฉํ ์ ์์ผ๋ฉฐ, ๋์ ์ผ๋ก ์์ ์ ์ํํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
from langchain.agents import AgentType, initialize_agent
from langchain.tools import Tool
# ์์ ๋๊ตฌ ์์ฑ
def fake_search(query):
return f"Search result for {query}"
tool = Tool(name="Search Tool", func=fake_search, description="A fake search tool")
# ์์ด์ ํธ ์ด๊ธฐํ
agent = initialize_agent([tool], llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
response = agent.run("What is LangChain?")
print(response)
6) ๋ฐ์ดํฐ ๊ฒ์ (Retrieval-Augmented Generation, RAG)
LangChain์ ์ธ๋ถ ๋ฌธ์์์ ์ ๋ณด๋ฅผ ๊ฒ์ํ์ฌ LLM์ ์๋ต์ ํฅ์์ํค๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
from langchain.document_loaders import TextLoader
from langchain.indexes import VectorstoreIndexCreator
loader = TextLoader("data.txt")
index = VectorstoreIndexCreator().from_loaders([loader])
response = index.query("What is LangChain?")
print(response)
3. LangChain์ ํ์ฉ ์ฌ๋ก
- ์ฑ๋ด(Chatbots) ๊ตฌ์ถ
- ์๋ํ๋ ๋ฌธ์ ์ฒ๋ฆฌ
- AI ๊ธฐ๋ฐ ๊ฒ์ ์์คํ
- ๋ฐ์ดํฐ ๋ถ์ ๋ฐ ๋ณด๊ณ ์ ์์ฑ
- ์ฝ๋ ์์ฑ ๋ฐ ๋ฒ์ญ
4. LangChain์ ์ฅ์
- ๋ค์ํ LLM ๋ฐ ๋๊ตฌ์์ ์ ์ฐํ ์ฐ๋
- ์ฒด์ธ ๋ฐ ๋ฉ๋ชจ๋ฆฌ ๊ธฐ๋ฅ์ ํ์ฉํ ๋ณต์กํ ์ํฌํ๋ก์ฐ ๊ตฌ์ฑ ๊ฐ๋ฅ
- ๊ฒ์ ๊ธฐ๋ฐ ์๋ต ์์ฑ(RAG)์ผ๋ก ์ ๋ณด ๊ฒ์ ๋ฅ๋ ฅ ๊ฐํ
- ๋น ๋ฅธ ํ๋กํ ํ์ดํ ๋ฐ ๋ฐฐํฌ ์ง์
๋ญ์ฒด์ธ ์ ๋ฆฌ:
1๏ธโฃ LangChain์ LLM์ ์ฝ๊ฒ ํ์ฉํ ์ ์๋ ํ๋ ์์ํฌ์ด๋ค~
2๏ธโฃ ํ๋กฌํํธ, ์ฒด์ธ, ๋ฉ๋ชจ๋ฆฌ, ์์ด์ ํธ ๋ฑ์ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค~
3๏ธโฃ ๋ค์ํ ํ์ฉ ์ฌ๋ก์ ๊ฐ๋ ฅํ ํ์ฅ์ฑ์ ์ ๊ณตํ๋ค~
4๏ธโฃ AI ์ ํ๋ฆฌ์ผ์ด์ ๊ฐ๋ฐ์ ๋ ๋น ๋ฅด๊ณ ํจ์จ์ ์ผ๋ก ํ ์ ์๋๋ก ์ง์ํ๋ค~
'AI ๐ค > ML & DL ๐ง ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[250226] ์๋ฒ ๋ฉ(Embedding)์ด๋ (2) | 2025.02.26 |
---|---|
[250214] Parser ์ ๋ฆฌ (0) | 2025.02.14 |
[250210] LLM ํน๊ฐ ๋๋ฒ์งธ ์๊ฐ~ (1) | 2025.02.10 |
[250207] ํผ์ ํธ๋ก (Perceptron)์ด๋? (0) | 2025.02.07 |
[250207] LLM ํน๊ฐ ์ ๋ฆฌ~ (0) | 2025.02.07 |