No project description provided
Project description
Introduction
Developed a cutting-edge Research Assistant Agent that utilizes a search tool to fetch up-to-date information, generating informed responses to user queries. Built on top of LangGraph AgentState, our agent leverages the powerful GPT-4O LLM model and integrates seamlessly with the TavillySearch API for external search capabilities.
Key Features:
- Contextual Understanding: The agent comprehends user queries and decides when to initiate search actions, ensuring optimal responses.
- Parallel Search Calls: Equipped to handle multiple searches simultaneously, enhancing efficiency.
- Rate Limiting: Implemented rate limiting wrappers to prevent excessive resource calls.
- Human-in-the-Loop: Incorporated breakpoints for human intervention, ensuring accuracy and control.
Installation for Mac
The following steps are required to save graph rendering localing on the repository.
Step 1. Follow instructions to install brew to install packages/tools that are not pre-installed on your laptop. Step 2. Run
brew install graphviz
Step 3. Per PyGraphviz run
pip install --config-settings="--global-option=build_ext" \
--config-settings="--global-option=-I$(brew --prefix graphviz)/include/" \
--config-settings="--global-option=-L$(brew --prefix graphviz)/lib/" \
pygraphviz
LLM-Agents
Simple ReAct Agent
A ReAct model is a an Agent that performs both Reasoning and Actions task, based on [1]. This project implements as simplified ReAct pattern from scratch using python. The main essense of this framework is as follows:
- LLM thinks on what to do
- Decides on what action to take
- Execute that actions (or calls an external tool to take the action)
- An observations is returned
- Repeat from (2)
The Simple ReAct Agent directory showcases two approches.
agent.py
: The ReAct framework from scratch utilizing prompting and output parsing.agentoptimized.py
: Use native langchain modules with AgentExecutor.
LangGraph Agent
Visual of the simple Langgraph Agent utilizing a search tool that leverages the TavillySearch API.
Known Issues
The take_action()
method/node has a failsafe to invoke the LLM and have it pass the required arguments for the tool call. In langchain-openai>=0.1.21rc1
there is a new kwarg struct=True
to enforce the tools argument schema.
Persistance ("memory") To Graph
Some Applications need memory to share context across multiple interactions/sessions. In LangGraph, memory is provided for any StateGraph through Checkpointers such as SqliteSaver
. This checkpointer is used as an in-memory database, if your machine memory is a concern LangGraph we can use Redis as our cache application
This is an important feature, for example, when building a customer facing chatbot and the requirements are to have the chatbot recall previous states of the conversation and have the user resume from that state.
State Memory
With state memory the Agent can be invoked from any section of its graph with the use of its history and checkpoints.
Mid-Execution Failures
Programs fail, it happens. Fortunately, with the use of our memory/checkpoint we can make necessary updates to our code, re-initiate our class, and start from a saved checkpoint.
Suppose you are working with the Agent
and you have some "bad code" in the take_action
method:
Class Agent:
...
def take_action(self, state: AgentState):
raise()
...
...
Depending on the size of the graph and/or costs it does not make sense to start the execution from the beginning. Instead, we would like to resume from the faulty state after making the neccessary fixes. This is made possible with the thread config and memory/checkpoint.
# Fix method
Class Agent:
...
def take_action(self, state: AgentState):
#raise()
...
...
# Initiate Agent class
abot = Agent(model, [tool], system=prompt, checkpointer=memory)
# Current/Next stage
print(f"Graph will start from current configuration and run the following stage in the graph: {abot.graph.get_state(thread).next}")
# Run from latest checkpoint
for event in abot.graph.stream(input=None, config=thread):
for v in event.values():
print(v)
Agent Updates (Time-Travel)
Additionally, Agent states allow us to time-travel to a particular point on the graph with the use of the thread_ts
. This key identifies a place in the state within the current thread.
We can use the iterator graph.get_state_history(thread)
to obtain the thread_ts
and use it to invoke/stream our agent from that state on the graph.
for event in abot.graph.stream(input=None, config={"configurable": {"thread_id": "1", "thread_ts": {value}}):
for v in event.values():
print(v)
This is useful for a few reasons updates to future nodes in the graph and modification of intermediate stages within the graph. For the latter you can get the state, with the use of the thread_ts
, modify it, and then update the history using:
current_values = abot.graph.get_state(thread)
# ... modify current_values
graph.update_state(thread, current_values.values)
Breakpoints (Human-In-The-Loop)
Human in the Loop are essential interations between person(s) and LLM-Agents. Adding breakpoints in the graph, allow it to stop at specific nodes, seek human intervention/approval before proceeding. This can be used when there is a sensitive step that will happen next, and we require final approval/modification from a live person.
Graph Visualization
Run python script with flag --savegraph
.
Reference
[1]: Google Research. "React: Synergizing Reasoning and Acting in Language Models." Google AI Blog, 4 Aug. 2023, link.
[2]: Madaan, A. et.al. "Self-Refine: Iterative Reginement with Self-Feedback." 2023, link
[3]: Ridnik, T. et al. "Code Generation with AlphaCodium: From Prompt Engineering to Flow Engineering" 16 Jan. 2024, link
Contributions
This repo follows the deeplearning.ai
course "AI Agents in LangGraph", with some modifications.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file aiagentgraph-0.0.1.tar.gz
.
File metadata
- Download URL: aiagentgraph-0.0.1.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3253dee77f39a5c3d325a1a2faf4c54f358492fc927f77d9fde7d4c999fde230 |
|
MD5 | 05757e0c0aa7e04bd2b223f1a7e7bcc1 |
|
BLAKE2b-256 | 428d56220348d59015f82054c6eb17da20b1595e15d37aab13e96ad4fd87ba1e |
File details
Details for the file aiagentgraph-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: aiagentgraph-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3cf4d565723e3a9245fe22fabae9b210da159c5e42d37baaaa18b0f72d605a75 |
|
MD5 | 55be0fe1d0c6de0094287f8c9a4d3bf8 |
|
BLAKE2b-256 | c68ff3b18f00365a831851a9c1ceedfd7270515da0cc003808ba684ee349a01e |