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 |