RAG
Want an agent that can do this for you? We’ll soon have a DocumentAgent available to take the hassle out of RAG.
Retrieval-Augmented Generation is a technique to improve LLM-generated responses by giving it additional knowledge. This typically involves gathering the information and injecting it into an agent’s system message for use by their LLM.
There are a number of ways to incorporate RAG into your AG2 workflow and agents:
- Use an AG2 reference agent, DocumentAgent (Coming Soon!)
- Add RAG capabilities to an agent
- Add RAG tools to an agent
- Incorporate context into an agent’s system message (manually and automatically)
1. DocumentAgent
Coming soon is our DocumentAgent, built specifically for RAG, and it will take the hassle out of loading, parsing, storing, and querying documents/web pages.
2. Add RAG capabilities to an agent
AG2 allows you to add capabilities to agents and an example of a capability is RAG using a graph database.
It only takes two steps to do this:
- Create the capability
- Add it to the agent
See the notebooks associated with the capabilities below for walk-throughs.
RAG Capability: Neo4j GraphRAG
Based on AG2’s base GraphRAG capability, this Neo4j GraphRAG capability allows the embedding and querying of information with a Neo4j graph database.
See the Using Neo4j’s graph database with AG2 agents for Q&A notebook.
RAG Capability: FalkorDB GraphRAG
Also based on AG2’s base GraphRAG capability, this capability uses a FalkorDB GraphRAG database.
See the Trip planning with a FalkorDB GraphRAG agent using a Swarm notebook.
If you need a capability for a different GraphRAG database, consider building a capability similar to these using our GraphRagCapability base class.
3. Add RAG tools to an agent
Together with our upcoming DocumentAgent, will be the ability to add RAG tools directly to your agents. Watch this space!
4. Incorporating context into an Agent’s system message
ConversableAgent has a number of hooks that get run before an agent replies. You can utilise the update_agent_state
hook to run a function that updates your agent’s system message with some context before it goes to the LLM.
Within the function use the ConversableAgent.update_system_message method to update the system message.
Let’s walk-through a simple example that puts a listing of the files in the current directory into an agent’s system message and asks an LLM to explain them.
We start with our imports, LLM configuration, and the system message template, which we’ll inject the file listing in to.
Here’s the function we’ll attach to the hook, it gets all files in the current directory and updates the associated agent’s system message accordingly.
Now we create the agent and attach the hook.
Finally we create a human-in-the-loop agent and ask our files_agent
about the files.
And we can see the LLM now knows about the files and directories in the current folder and is able to provide some information about them.