LangChain历史聊天记录的实现

import ChatGLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain.tools import Tool 
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_community.tools import WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper
from langchain_community.tools import ArxivQueryRun
from langchain.memory import ConversationBufferMemory
from langchain.agents import initialize_agent 
from langchain.agents import AgentType 
from langchain.chains import LLMChain 
from langchain.prompts import PromptTemplate
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
from langchain.pydantic_v1 import BaseModel, Field
from langchain.tools import BaseTool, StructuredTool, tool
from langchain.tools import BaseTool
from langchain.agents import load_tools
from math import pi
from typing import Union
from typing import Optional, Type
from langchain.callbacks.manager import (
    AsyncCallbackManagerForToolRun,
    CallbackManagerForToolRun,
)
from langchain.tools import BaseTool
from math import pi
from typing import Union
from typing import Optional
from math import sqrt, cos, sin
from langchain.chains import LLMMathChain
from langchain.agents import Tool
from dotenv import load_dotenv
import os
    
llm = ChatGLM.ChatGLM_LLM()



# # 加载 .env 文件
load_dotenv()
tools = load_tools(["serpapi", "llm-math"], llm=llm)

# print(tools[0])

memory = ConversationBufferMemory(memory_key="chat_history")
# reinitialize the agent
agent = initialize_agent(
     agent = AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
    tools=tools,
    llm=llm,
    verbose=True,
    max_iterations=30,
    memory=memory
)
agent.invoke("你是谁")
agent.invoke("openai是什么")  
a = agent.invoke("我的第一个问题是什么") 
print(a.get("output"))  

会输出: 你是谁

你可能感兴趣的:(LangChain,langchain,java,前端)