6.8 Update Copilot With GraphRAG¶
The next step is to update your API to use GraphRAG for data retrieval. You will update how your copilot finds and retrieves unpaid invoices for this.
Review the function¶
Following the function calling pattern used by your LangChain agent to retrieve data from the database, you will use a Python function to execute openCypher queries against your graph database from your copilot. Within the src/api/app/functions/chat_functions.py
file, the get_unpaid_invoices_for_vendor
function has been provided for executing cypher queries. Open it now in Visual Studio Code and explore the code with the function. You can also expand the section below to see the code inline.
GraphRAG code
src/api/app/functions/chat_functions.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
-
Define grapy query (line 6): Creates the cypher query that will be used to look up unpaid invoices for the specified
vendor_id
-
Execute cypher query (lines 12): The cyper query is sent to the database for execution, using the
__execute_graph_query()
function. -
The
__execute_graph_query()
function, starting on line 25 on thechat_functions.py
file, runs the query against theag_catalog
schema, which contains the graph database. To enable this, it also includes aSET
query prior to running the graph query to addag_catalog
to thesearch_path
in the connection. -
Return the results (lines 13): The query results are extracted and returned to the LLM.
Implement GraphRAG¶
To implement GraphRAG functionality in your copilot, you must include the get_unpaid_invoices_for_vendor
function in your LangChain agent's tools
collection. You will add this function to the list of available tools to your agent.
-
In the VS Code Explorer, navigate to the
src/api/app/routers
folder and open thecompletions.py
file. -
Within the
tools
array, locate the following line (line 75):Python 1
StructuredTool.from_function(coroutine=cf.get_invoices),
-
Insert the following code on the line just below that:
Insert the following Python code to add the GraphRAG function!
Python 1
StructuredTool.from_function(coroutine=cf.get_unpaid_invoices_for_vendor),
-
Your new
tools
array should look like this:Python 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# Define tools for the agent to retrieve data from the database tools = [ # Hybrid search functions StructuredTool.from_function(coroutine=cf.find_invoice_line_items), StructuredTool.from_function(coroutine=cf.find_invoice_validation_results), StructuredTool.from_function(coroutine=cf.find_milestone_deliverables), StructuredTool.from_function(coroutine=cf.find_sow_chunks_with_semantic_ranking), StructuredTool.from_function(coroutine=cf.find_sow_validation_results), # Get invoice data functions StructuredTool.from_function(coroutine=cf.get_invoice_id), StructuredTool.from_function(coroutine=cf.get_invoice_line_items), StructuredTool.from_function(coroutine=cf.get_invoice_validation_results), StructuredTool.from_function(coroutine=cf.get_invoices), StructuredTool.from_function(coroutine=cf.get_unpaid_invoices_for_vendor), # Get SOW data functions StructuredTool.from_function(coroutine=cf.get_sow_chunks), StructuredTool.from_function(coroutine=cf.get_sow_id), StructuredTool.from_function(coroutine=cf.get_sow_milestones), StructuredTool.from_function(coroutine=cf.get_milestone_deliverables), StructuredTool.from_function(coroutine=cf.get_sow_validation_results), StructuredTool.from_function(coroutine=cf.get_sows), # Get vendor data functions StructuredTool.from_function(coroutine=cf.get_vendors) ]
-
Save the
completions.py
file.
Test with VS Code¶
As you have done previously, you will test your updates using Visual Studio Code.
Start the API¶
Follow the steps below to start a debug session for the API in VS Code.
-
In Visual Studio Code Run and Debug panel, select the API Debugger option for your OS from the debug configurations dropdown list.
-
Select the Start Debugging button (or press F5 on your keyboard).
-
Wait for the API application to start completely, indicated by an
Application startup complete.
message in the terminal output.
Start the Portal¶
With the API running, you can start a second debug session in VS Code for the Portal project.
-
Return to the Run and Debug panel in Visual Studio Code and select the Portal Debugger option from the debug configurations dropdown list.
-
Select the Start Debugging button (or press F5 on your keyboard).
-
This should launch the Woodgrove Bank Contract Management Portal in a new browser window (http://localhost:3000/).
-
In the copilot chat on the Dashboard page, enter the following message and send it:
Paste the following prompt into the copilot chat box!
1
Tell me about the accuracy of unpaid invoices from Adatum.
-
Observe the results provided using GraphRAG.
GraphRAG improves accuracy
Add a breakpoint in the
get_unpaid_invoices_for_vendor
function in thechat_functions.py
file. The breakpoint will allow you to see the graph query executing and enable you to step through the remaining function calls to observe that the invoice validation results are only retrieved for the unpaid invoices. This precision reduces the data returned from the database and allows the RAG pattern to only receive the data it needs to generate a response.
Congratulations! You just learned how to leverage the GraphRAG capabilities of Azure Database for PostgreSQL and AGE!