Thursday, October 3, 2024

What is prebuilt react agents and how to use memory in them

from langgraph.prebuilt import create_react_agent

prebuilt_doc_agent = create_react_agent(model, [execute_sql],

  state_modifier = system_prompt)


from langgraph.checkpoint.sqlite import SqliteSaver

memory = SqliteSaver.from_conn_string(":memory:")


prebuilt_doc_agent = create_react_agent(model, [execute_sql], 

  checkpointer=memory)



class SQLAgent:

  def __init__(self, model, tools, system_prompt = ""):

    <...>

    self.graph = graph.compile(checkpointer=memory)

    <...>



# defining thread

thread = {"configurable": {"thread_id": "1"}}

messages = [HumanMessage(content="What info do we have in ecommerce_db.users table?")]


for event in prebuilt_doc_agent.stream({"messages": messages}, thread):

    for v in event.values():

        v['messages'][-1].pretty_print()



followup_messages = [HumanMessage(content="I would like to know the column names and types. Maybe you could look it up in database using describe.")]


for event in prebuilt_doc_agent.stream({"messages": followup_messages}, thread):

    for v in event.values():

        v['messages'][-1].pretty_print()



new_thread = {"configurable": {"thread_id": "42"}}

followup_messages = [HumanMessage(content="I would like to know the column names and types. Maybe you could look it up in database using describe.")]


for event in prebuilt_doc_agent.stream({"messages": followup_messages}, new_thread):

    for v in event.values():

        v['messages'][-1].pretty_print()



references:

https://towardsdatascience.com/from-basics-to-advanced-exploring-langgraph-e8c1cf4db787




No comments:

Post a Comment