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.4a1.tar.gz
.
File metadata
- Download URL: aiagentgraph-0.0.4a1.tar.gz
- Upload date:
- Size: 13.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14a1155a9bee3cf4ec9e41ef8067c04931eacc9a5da73c79d036fd8fbf72ae1d |
|
MD5 | a8e05790e1fc4916f374eb909011aef8 |
|
BLAKE2b-256 | 3e274fe3d07bd63010da9e21b816120034302eebff1246181f989db800e285e2 |
File details
Details for the file aiagentgraph-0.0.4a1-py3-none-any.whl
.
File metadata
- Download URL: aiagentgraph-0.0.4a1-py3-none-any.whl
- Upload date:
- Size: 14.9 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 | 3159f3394e9bfa2f52a7f3b6f36f7557543591f565a7dfaa0bb70a70e94a310e |
|
MD5 | 467abfca0de7bdeedc91430b5142b5ef |
|
BLAKE2b-256 | 0ec7dccb3cac328b2c65b9fdf982a490d43482913f4cce2e6df40f60b679b375 |