<์ถ์ฒ ๋ฐ ์ฐธ๊ณ ์๋ฃ>
- Langchain QuickStart
https://python.langchain.com/docs/get_started/quickstart
Quickstart | ๐ฆ๏ธ๐ Langchain
Installation
python.langchain.com
- Quickstart์ ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์ฃผ์์ผ๋ก ์ค๋ช
- Quickstart์ค LLMs๋ฅผ ๊ธฐ์ค์ผ๋ก ํจ.
0. ์ ๋ฆฌ
- LLM์ ๋ค์ํ Tools์ ์จ์ ํจ๊ณผ์ ์ผ๋ก ํ์ฉ ๊ฐ๋ฅ(์ฑ๋ด ๋ฑ)
- LLM์ผ๋ก GPT + plug_in + with Bing ํจ๊ณผ๋ฅผ ๋ผ ์ ์์.
- ๊ณต๋ถํ ๋๋ OpenAI Api์ฌ์ฉ ํ๋ ๊ฒ์ด ์ ์ผ ํธํจ
- Python 3.8์ด์ ๋ฒ์ ์์ ์ฌ์ฉ ํ ๊ฒ
- Template + tool + agent๋ฅผ chain์ ์ฌ์ฉํด์ ๋ฌถ์ด ์ค.
- OpenAI API Key๊ฐ ์์ด์ผ ํจ.
1. ๊ธฐ๋ณธ ์ฌ์ฉ
import os
os.environ["OPENAI_API_KEY"] = "API_KEY"
from langchain.llms import OpenAI
llm = OpenAI(temperature=0.9) #temperature 0~1๊น์ง 1์ ๊ฐ๊น์ธ ์๋ก ๋ฌด์์์ฑ ์ฆ๊ฐ
text = "๋ค์ํ ์์์ ์๋ง์ ๋ง๋๋ ํ์ฌ์ ์ด๋ฆ์ ๋ฌด์์ผ๋ก ํ๋ฉด ์ข์๊น?"
print(llm(text))
2. Prompt Template
- ์ถ๋ ฅ ์์์ ์ ์: ์ด๋ป๊ฒ ์ถ๋ ฅ์ ํ ๊ฒ์ธ๊ฐ?
from langchain.prompts import PromptTemplate
prompt = PromptTemplate(
input_variables = ['product'], #A
template = "{product}์ ๋ง๋๋ ํ์ฌ์ ์ด๋ฆ์ ๋ฌด์์ผ๋ก ํ๋ฉด ์ข์๊น?"
)
#format์์ product(A์ ์ง์ )์ ๋ณ์๋ฅผ ๋ฃ์ผ๋ฉด prompt์ ๋ค์ด๊ฐ.
print(prompt.format(product='๋ง์๋ ์ผ์ต'))
#์ถ๋ ฅ: ๋ง์๋ ์ผ์ต์ ๋ง๋๋ ํ์ฌ์ ์ด๋ฆ์ ๋ฌด์์ผ๋ก ํ๋ฉด ์ข์๊น?
3. LLMs + Prompt
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
llm = OpenAI(temperature=0.9)
prompt = PromptTemplate(
input_variables=['product'],
template="{product}์ ๋ง๋๋ ํ์ฌ์ ์ด๋ฆ์ ๋ฌด์์ผ๋ก ํ๋ฉด ์ข์๊น?"
)
#LLMChain์ ์ฌ์ฉํด์ ์ฐ๊ฒฐํด์ค
from langchain.chains import LLMChain
chain = LLMChain(llm=llm, prompt=prompt)
#์คํ
chain.run("๊ต์ฅํ ํ๊ดด๋ ฅ์ ๋ฌด๊ธฐ")
#์ถ๋ ฅ: '\n\n์ฃผ๋ํ๋ ๋ฌด๊ธฐ(Leading Weapon)์ด๋ผ๋ ์ด๋ฆ์ด ์ด์ธ๋ฆฌ๋ ๊ฒ ๊ฐ์ต๋๋ค.'
4. Agent ์ฌ์ฉ
import os
#APIํธ์ถ์ด ํ์ํ tool์ Key๊ฐ ์์ด์ผ ํจ.
os.environ["SERPAPI_API_KEY"] = "API_Key"
from langchain.agents import load_tools #tool ๋ถ๋ฌ์ค๊ธฐ
from langchain.agents import initialize_agent #agent ์ด๊ธฐ์
from langchain.agents import AgentType #agent type ์ง์
from langchain.llms import OpenAI
#agent๋ฅผ ์ฝํธ๋กคํ๋๋ฐ ์ฌ์ฉํ LLM ๋ถ๋ฌ์ค๊ธฐ
llm = OpenAI(temperature=0) #๋ฌด์์ X
#์ฌ์ฉํ Tool ๋ถ๋ฌ์ค๊ธฐ
tools = load_tools(['serpapi', 'llm-math'], llm=llm)
#llm,tools, agent, agent_type ์ด๊ธฐํ
agent = initialize_agent(tools,
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True)
#์คํ
agent.run("์์ธ์ ์ด์ ์ต๊ณ ๊ธฐ์จ์ ํ์จ๋ก ๋ช๋์ผ? .023์ ๊ฑฐ๋ญ ์ ๊ณฑํ ์ซ์๋ ์ผ๋ง์ผ?")
#์ถ๋ ฅ: '84 °F (at 12:00 pm) and 0.000529'
5. Memory:
- ์ฑ๋ด ๋ฑ์์ ์์ ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์ถ๋ ฅ์ ํด์ผ ํ ๋ ํ์
- Long-term
- Short-term
from langchain import OpenAI, ConversationChain
llm = OpenAI(temperature=0)
#ConversationChain ์ด์ ์ ์
/์ถ๋ ฅ์ ๊ธฐ์ตํ๊ณ , ์ ๋ฌ๋ ์ปจํ
์คํธ์ ์ถ๊ฐํ๋ ๊ฐ๋จํ ์ ํ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ฐ์ง๊ณ ์์.
conversation = ConversationChain(llm=llm, verbose=True)
output = conversation.predict(input='์๋
')
print(output)
#์ถ๋ ฅ: ์๋
ํ์ธ์! ๋ฌด์์ ๋์๋๋ฆด๊น์?
output = conversation.predict(input="๊ทธ๋ฅ AI๋ ๋ํํ๊ณ ์ถ์์ด")
print(output)
#์ถ๋ ฅ: ๋ค, ๋ง์ํ์ธ์! ๋๋ ์ธ๊ณต์ง๋ฅ์
๋๋ค. ๋๋ ์ธ๊ฐ๊ณผ ๋ํํ๋ ๊ฒ์ ์ข์ํฉ๋๋ค. ๋ฌด์์ ๋ฌผ์ด๋ณด๊ณ ์ถ์ผ์ ๊ฐ์?
'NLP > LangChain' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๊ตฌ๋ฆIDE์์ Alpaca ์ฌ์ฉ(LangChain์์ OpenAI Credit ์์ง์ ๋์ฒด) (0) | 2023.06.09 |
---|