728x90
Default
import os
os.environ['OPENAI_API_KEY'] = 'sk-...'
Building Custom Tools
1) Package Import
from langchain import OpenAI
from langchain.chat_models import ChatOpenAI
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
# callback cost
from langchain.callbacks import get_openai_callback
2) llm model ์ค์
turbo_llm = ChatOpenAI(
temperature = 0,
model_name = 'gpt-3.5-turbo'
)
3) ๊ฒ์ Langchain pakage load
from langchain.tools import DuckDuckGoSearchRun
from langchain.agents import Tool
from langchain.tools import BaseTool
4) Search (๋ถ๊ฐ๊ธฐ๋ฅ)
Define Custom Tool
search = DuckDuckGoSearchRun()
tools = [
Tool(
name = 'search',
func = search.run,
description="useful for when you need to answer questions about current events. You should ask targeted questions"
# current event์ ๋ํด ๋์์ด ๋๋ ๋ต์ ํด๋ผ. ๋ชฉํ๋ก ํ๋ ์ง๋ฌธ์ ํด์ผํ๋ค.
)
]
5) LLM ๋ชจ๋ธ์ ๋ต๋ณ์ด ๋ฐ๋์ง ์๋๋ก ๊ณ ์
์ด๋ค ๋ต์ด ๋์๋ ์ถ์ ๋ฌ๊ฑ๋ก ๋ต๋ณ ํ๋ ํจ์ ์ ์
def meaning_of_life(input=""):
return '์ถ์ ๋ฌ๊ฑ'
life_tool = Tool(
name='์ถ์ด๋ ๋ฌด์์ธ๊ฐ?',
func= meaning_of_life,
description="Useful for when you need to answer questions about the meaning of life. input should be MOL "
)
6) Agent ์์ฑ
from langchain.agents import initialize_agent
tools = [search, life_tool]
๋ต๋ณ์ ์ฐธ์กฐํ ์ด์ memory ๊ฐ์(k=3) ์ค์
memory = ConversationBufferWindowMemory(
memory_key='chat_history',
k=3,
return_messages=True
)
conversational_agent = initialize_agent(
agent = 'chat-conversational-react-description',
tools = tools,
llm = turbo_llm,
verbose = True,
max_iterations = 3,
early_stopping_method = 'generate',
memory = memory
)
7) Agent ์คํ
conversational_agent("์ถ์ด๋ ๋ฌด์์ธ๊ฐ?")
LLM์ด ํญ์ Agent Tool์ ๋ชจ๋ํฐ๋งํ ์ํ์์ Task๋ฅผ ์ ์ํ๋ค.
๋ฐ์ํ
'๐ฃ๏ธ Natural Language Processing' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Mac] Transformer model downloaded path (0) | 2023.05.28 |
---|---|
[LangChain] Retrieval PDF (0) | 2023.05.26 |
small scale text data classification (0) | 2023.05.16 |
[M1 Transformers] M1 Mac Transformers Install Error (0) | 2022.06.19 |
[BERT TOKENIZE]๋จ์ด ํ ํฐํ (1)- ๋ฐ์ดํธ ์ ์ธ์ฝ๋ฉ (0) | 2022.05.09 |