[PYTHON/LANGCHAIN] RunnableWithMessageHistory 클래스 : invoke 메소드를 사용해 채팅하기 1
■ RunnableWithMessageHistory 클래스의 invoke 메소드를 사용해 채팅하는 방법을 보여준다. ※ OPENAI_API_KEY 환경 변수 값은 .env 파일에 정의한다. ▶ main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
from dotenv import load_dotenv from langchain_community.tools.tavily_search import TavilySearchResults from langchain_community.document_loaders import WebBaseLoader from langchain_text_splitters import RecursiveCharacterTextSplitter from langchain_openai import OpenAIEmbeddings from langchain_community.vectorstores import FAISS from langchain.tools.retriever import create_retriever_tool from langchain_openai import ChatOpenAI from langchain import hub from langchain.agents import create_tool_calling_agent from langchain.agents import AgentExecutor from langchain_core.chat_history import BaseChatMessageHistory from langchain_community.chat_message_histories import ChatMessageHistory from langchain_core.runnables.history import RunnableWithMessageHistory load_dotenv() chatOpenAI = ChatOpenAI(model = "gpt-4o") tavilySearchResults = TavilySearchResults(max_results = 2) webBaseLoader = WebBaseLoader("https://docs.smith.langchain.com/overview") documentList = webBaseLoader.load() recursiveCharacterTextSplitter = RecursiveCharacterTextSplitter(chunk_size = 1000, chunk_overlap = 200) splitDocumentList = recursiveCharacterTextSplitter.split_documents(documentList) openAIEmbeddings = OpenAIEmbeddings() faiss = FAISS.from_documents(splitDocumentList, openAIEmbeddings) vectorStoreRetriever = faiss.as_retriever() vectorStoreRetrieverTool = create_retriever_tool( vectorStoreRetriever, "langsmith_search", "Search for information about LangSmith. For any questions about LangSmith, you must use this tool!", ) toolList = [tavilySearchResults, vectorStoreRetrieverTool] chatPromptTemplate = hub.pull("hwchase17/openai-functions-agent") runnableSequence = create_tool_calling_agent(chatOpenAI, toolList, chatPromptTemplate) agentExecutor = AgentExecutor(agent = runnableSequence, tools = toolList) sessionIDDictionary = {} def getChatMessageHistory(sessionID : str) -> BaseChatMessageHistory: if sessionID not in sessionIDDictionary: sessionIDDictionary[sessionID] = ChatMessageHistory() return sessionIDDictionary[sessionID] runnableWithMessageHistory = RunnableWithMessageHistory( agentExecutor, getChatMessageHistory, input_messages_key = "input", history_messages_key = "chat_history", ) responseDictionary1 = runnableWithMessageHistory.invoke( {"input" : "hi! I'm bob"}, config = {"configurable" : {"session_id" : "<foo>"}} ) print(responseDictionary1) print("-" * 100) responseDictionary2 = runnableWithMessageHistory.invoke( {"input" : "what's my name?"}, config = {"configurable" : {"session_id" : "<foo>"}} ) print(responseDictionary2) print("-" * 100) """ {'input': "hi! I'm bob", 'chat_history': [], 'output': 'Hello Bob! How can I assist you today?'} |
{'input': "what's