Custom Langchain agent/tools with memory video code file:
Added 2023-02-15 13:48:25 +0000 UTCUPDATE: This code would redefine the agent memory each time with streamlit erasing the memory. I have uploaded a file with the fixed code so that when using this with streamlit, agent's memory doesn't reset. I am leaving the old code file as reference
CODE THAT NEEDS TO CHANGE:
if "agent_memory" not in st.session_state:
st.session_state["agent_memory"] =
ConversationBufferMemory(memory_key="chat_history")
llm=OpenAI(temperature=0, verbose=True)
agent_chain = initialize_agent(tools, llm, agent="conversational-react-description", memory=st.session_state["agent_memory"], verbose=True)
this is for video: https://youtu.be/NIG8lXk0ULg
Turn any python function into a custom langchain agent with tools including short term conversational memory
Comments
Sorry for the late reply. I must have missed this one. It automatically reads the API key from a user environment variable named as "OPENAI_API_KEY" if you have that setup, then you are good to go. Otherwise you would have to explicitly define it.
Echo Hive
2023-02-17 18:57:28 +0000 UTCHelp me understand how this is connecting to OpenAI API? I don't see the call from the code. Is it pulling my API key from the environment or do I not need one? Great work. Thank you.
Jim McMillan
2023-02-16 17:33:50 +0000 UTCI haven’t tried with lambda but you can load those built in tools easily with load_tools import from langchain as such: tools = load_tools(["serpapi", "llm-math", "wolfram-alpha"], llm=llm, serpapi_api_key=os.getenv("SERPAPI_API_KEY"), wolfram_alpha_appid=os.getenv("WOLFRAM_ALPHA_APPID"). It is in the code from “gpt-3 searches the internet” post and video
Echo Hive
2023-02-15 14:27:57 +0000 UTCThanks for making this great video. Question, have you tried using the lambda function to import the tools i.e wolframalpha? See below. func=lambda llm, serpapi_api_key, wolfram_alpha_appid: load_tools(["serpapi", "wolfram-alpha"], llm=llm, serpapi_api_key=serpapi_api_key, wolfram_alpha_appid=wolfram_alpha_appid)
Mark
2023-02-15 14:18:31 +0000 UTC