GCP – Building AI agents with Gen AI Toolbox for Databases and Dgraph
We recently announced the public beta of Gen AI Toolbox for Databases, and today we’re excited to expand its capabilities through a new partnership with Hypermode.
Gen AI Toolbox for Databases is an open source server that empowers application developers to connect production-grade, agent-based generative AI (gen AI) applications to databases. Toolbox streamlines the creation, deployment, and management of sophisticated gen AI tools capable of querying databases with secure access, robust observability, scalability, and comprehensive manageability.
Currently, Toolbox can be used to build tools for a large number of databases: AlloyDB for PostgreSQL (including AlloyDB Omni), Spanner, Cloud SQL for PostgreSQL, Cloud SQL for MySQL, Cloud SQL for SQL Server and self-managed MySQL and PostgreSQL. Additionally, Toolbox is open-source, including contributions and support from Google partners such as Neo4j.
Today we are excited to announce Gen AI Toolbox support for Dgraph in partnership with Hypermode.
Dgraph by Hypermode is the fully open source, built-for-scale graph database for AI apps.
What sets Dgraph apart:
-
Real-time performance: Designed for real-time workloads with distributed architecture that processes queries in parallel
-
Horizontal scalability: Easily scales to handle growing data volumes and user demands
-
AI-native primitives: Features vector indexing, search, and storage capabilities that allow development teams to store multiple embeddings on any node
-
Flexible data modeling: Supports property graph models that represent complex relationships crucial for recommendation systems and knowledge graphs
The integration between Dgraph and Toolbox delivers significant advantages for application developers:
1. Simplified configuration & development
- Straightforward setup: Just configure
kind: dgraph
in the source andkind: dgraph-dql
in Toolbox - Toolbox handles database connectivity while you focus on building AI features
2. Production-ready infrastructure
- Automated operational management including connection pooling, authentication, and resource allocation
- Zero-downtime deployments through config-driven approach
- Built-in support for common auth providers
3. Enterprise-grade observability
- Out-of-the-box insights via logging, metrics, and tracing
- Simplified debugging and monitoring for graph database operations
- aside_block
- <ListValue: [StructValue([(‘title’, ‘Try Google Cloud for free’), (‘body’, <wagtail.rich_text.RichText object at 0x3e89d5b8b8b0>), (‘btn_text’, ‘Get started for free’), (‘href’, ‘https://console.cloud.google.com/freetrial?redirectPath=/welcome’), (‘image’, None)])]>
Real-world use case: Building an e-commerce agent
To demonstrate the power of the Dgraph integration with Toolbox within the Gen AI ecosystem, let’s explore how to build a product search and recommendation agent for a large ecommerce platform.
Modern ecommerce platforms need to deliver personalized experiences that help customers discover relevant products quickly. This requires:
-
Efficient product search capabilities
-
Personalized product recommendations based on user behavior and preferences
-
Natural language interaction with product catalog and reviews
Our solution uses a polyglot database approach:
-
AlloyDB for PostgreSQL: Stores transactional data including product catalog, purchase history, and inventory
-
Dgraph: Powers the knowledge graph for personalized product recommendations and understanding user reviews
-
LangChain: Orchestrates the agent workflow and LLM interactions
-
Gen AI Toolbox for Databases: Connects our agent to both databases with production-ready infrastructure
Creating a product knowledge graph with Dgraph
A knowledge graph powers the foundation for our ecommerce agent. We’ll model products, users, and user reviews as a property graph in Dgraph. This approach captures not just the data, but the knowledge of how these data are connected.
The property graph model consists of:
-
Nodes with labels: Defining the type of the node (Product, User, Review)
-
Relationships: Connecting nodes (`purchased_by`, `reviewed_by`, `similar_to`)
-
Properties: Key-value pairs with attributes of each type of node.
Here we can see how we will model our product knowledge graph:
DQL (Dgraph Query Language) is Dgraph’s native query language. Inspired by GraphQL and optimized for graph operations, DQL offers intuitive syntax for expressing complex relationships with features like reverse edges, cascading filters, and efficient variable binding. Its key advantages include performance on relationship-heavy queries, simplified traversal of connected data, and the ability to execute complex graph operations in a single query – making it ideal for recommendation engines, social networks, and knowledge graphs where entity relationships are crucial.
We can traverse the product knowledge graph stored in Dgraph to generate personalized recommendations. This is a common use case for graph databases like Dgraph due to the performance optimizations of traversing the graph to find products purchased by similar users or products with similar features. This type of query can be expressed simply using DQL, for example:
- code_block
- <ListValue: [StructValue([(‘code’, ‘query ProductRecommendations($productId: string) {rn # Get the current product detailsrn product(func: uid($productId)) {rn uidrn namern categoryrn }rnrn # Find users who purchased this productrn var(func: uid($productId)) {rn ~purchased_by @cascade {rn uid as uidrn }rn }rnrn # Find other products these users purchasedrn recommendations(func: has(name), orderasc: name) @filter((has(purchased_by) AND uid_in(purchased_by, uid) AND NOT uid($productId))) {rn uidrn namern categoryrn pricern ratingrn purchase_count: count(purchased_by)rn rn # Sort by most frequently purchasedrn orderDesc: val(purchase_count)rn }rn}’), (‘language’, ”), (‘caption’, <wagtail.rich_text.RichText object at 0x3e89d42918b0>)])]>
Defining our tools with Toolbox
Tools are defined using YAML with Gen AI Toolbox and represent parameterized database queries. Each tool includes a description of the input and output, which helps the LLM understand and determine which tool to call and use. See the documentation for more details on tool definition.
We are able to define tools that leverage both PostgreSQL and Dgraph, allowing our agent to seamlessly work with both databases. Here we see defining the Dgraph source and the Dgraph tool for finding product reviews. Other tools used include searching the product catalog and querying Dgraph for personalized recommendations.
- code_block
- <ListValue: [StructValue([(‘code’, ‘sources:rn my-dgraph-source:rn kind: dgraphrn dgraphUrl: http://localhost:8080rntools:rn get-product-reviews:rn kind: dgraph-dqlrn source: my-dgraph-sourcern statement: |rn query all($asin: string){rn productReviews(func: type(Product), first: 10) @filter(eq(Product.asin, $asin )) {rn uidrn Product.asinrn Product.reviews {rn Review.titlern Review.textrn Review.ratingrn }rn }rn }rn isQuery: truern timeout: 20srn description: |rn Use this tool to find product reviews for a specific product.rn parameters:rn – name: asinrn type: stringrn description: The product ASIN’), (‘language’, ”), (‘caption’, <wagtail.rich_text.RichText object at 0x3e89d69f3df0>)])]>
Building your agent with LangChain
Now we’re ready to create our agent. We’ll use the LangChain framework, which implements a client for Gen AI Toolbox. We’ll connect to the Toolbox server and load the toolset, a description of the tools available to our agent, provided by Toolbox. Since we’ll be implementing a chat interface for our agent, we’ll use LangChain’s agent memory component. While LangChain supports many models, we’ll make use of Gemini Pro via Google’s Vertex AI cloud service.
- code_block
- <ListValue: [StructValue([(‘code’, ‘# Connect to Toolbox server and load the toolsetrntoolbox = ToolboxClient(“http://127.0.0.1:5000″)rntools = toolbox.load_toolset()rnrn# Initialize a memory componentrnmemory = ConversationBufferMemory(rn memory_key=”chat_history”,rn return_messages=Truern)rnrn# Agent promptrnprompt = ChatPromptTemplate.from_messages([rn (“system”, “””rn You are a helpful product search assistant. Your job is to help users find products they’re looking for using your tools.rn”””), rn MessagesPlaceholder(variable_name=”chat_history”),rn (“human”, “{input}”), rn MessagesPlaceholder(variable_name=”agent_scratchpad”)rn])rnrn# Use Gemini Pro LLMrnllm = ChatVertexAI(rntmodel=”gemini-1.5-pro-002″, rnttemperature=0, rntconvert_system_message_to_human=Falsern)rnrn# Create agent and agent executorrnagent = create_tool_calling_agent(llm, tools, prompt)rnagent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, memory=memory,tool_choice=”any” )’), (‘language’, ”), (‘caption’, <wagtail.rich_text.RichText object at 0x3e89d69f3d60>)])]>
Now when we run our Python script we have a natural language chat interface to our agent. With each interaction the agent will determine if it has enough information and context to respond to the query and if not will select a tool from the toolset provided by Toolbox. Toolbox also handles the logic of invoking the tool – which involves managing database connection pools, authentication to the database, and the query request/result lifecycle.
The result: A powerful gen AI-powered shopping assistant
With this implementation, we’ve created a natural language interface that can:
-
Help customers search for products using natural language
-
Provide detailed product information from PostgreSQL
-
Generate personalized product recommendations based on the knowledge graph in Dgraph
-
Surface relevant product reviews to aid purchase decisions
All of this is made possible by Gen AI Toolbox for Databases, which handles the complexity of database connectivity, authentication, and query execution while the agent focuses on delivering a seamless user experience.
Get started
Ready to build your own database-connected agents? Try Gen AI Toolbox with Dgraph support today:
Join the Hypermode community or to share your experiences and get help building your own solutions!
Read More for the details.