Cloud Blog: Building AI agents with Gen AI Toolbox for Databases and Dgraph

Source URL: https://cloud.google.com/blog/topics/partners/expanding-gen-ai-toolbox-for-databases-with-hypermode/
Source: Cloud Blog
Title: Building AI agents with Gen AI Toolbox for Databases and Dgraph

Feedly Summary: 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 and kind: 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
), (‘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) {\r\n # Get the current product details\r\n product(func: uid($productId)) {\r\n uid\r\n name\r\n category\r\n }\r\n\r\n # Find users who purchased this product\r\n var(func: uid($productId)) {\r\n ~purchased_by @cascade {\r\n uid as uid\r\n }\r\n }\r\n\r\n # Find other products these users purchased\r\n recommendations(func: has(name), orderasc: name) @filter((has(purchased_by) AND uid_in(purchased_by, uid) AND NOT uid($productId))) {\r\n uid\r\n name\r\n category\r\n price\r\n rating\r\n purchase_count: count(purchased_by)\r\n \r\n # Sort by most frequently purchased\r\n orderDesc: val(purchase_count)\r\n }\r\n}’), (‘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:\r\n my-dgraph-source:\r\n kind: dgraph\r\n dgraphUrl: http://localhost:8080\r\ntools:\r\n get-product-reviews:\r\n kind: dgraph-dql\r\n source: my-dgraph-source\r\n statement: |\r\n query all($asin: string){\r\n productReviews(func: type(Product), first: 10) @filter(eq(Product.asin, $asin )) {\r\n uid\r\n Product.asin\r\n Product.reviews {\r\n Review.title\r\n Review.text\r\n Review.rating\r\n }\r\n }\r\n }\r\n isQuery: true\r\n timeout: 20s\r\n description: |\r\n Use this tool to find product reviews for a specific product.\r\n parameters:\r\n – name: asin\r\n type: string\r\n 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 toolset\r\ntoolbox = ToolboxClient(“http://127.0.0.1:5000")\r\ntools = toolbox.load_toolset()\r\n\r\n# Initialize a memory component\r\nmemory = ConversationBufferMemory(\r\n memory_key="chat_history",\r\n return_messages=True\r\n)\r\n\r\n# Agent prompt\r\nprompt = ChatPromptTemplate.from_messages([\r\n ("system", """\r\n You are a helpful product search assistant. Your job is to help users find products they\’re looking for using your tools.\r\n"""), \r\n MessagesPlaceholder(variable_name="chat_history"),\r\n ("human", "{input}"), \r\n MessagesPlaceholder(variable_name="agent_scratchpad")\r\n])\r\n\r\n# Use Gemini Pro LLM\r\nllm = ChatVertexAI(\r\n\tmodel="gemini-1.5-pro-002", \r\n\ttemperature=0, \r\n\tconvert_system_message_to_human=False\r\n)\r\n\r\n# Create agent and agent executor\r\nagent = create_tool_calling_agent(llm, tools, prompt)\r\nagent_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:

Check out Gen AI Toolbox on GitHub

Watch Hypermode Live: Building Agents with Dgraph using Google’s Gen AI Toolbox for Databases

Read Dgraph + Gen AI Toolbox blog post

Check out the code from the ecommerce demo app

Learn more about Hypermode

Join the Hypermode community or to share your experiences and get help building your own solutions!

AI Summary and Description: Yes

**Summary:** The text discusses the introduction and capabilities of the Gen AI Toolbox for Databases, which allows developers to create generative AI applications that connect to various databases. It emphasizes its integration with the Hypermode’s Dgraph, a graph database tailored for AI applications, detailing the advantages of using this combination for building powerful AI-driven tools, particularly for e-commerce.

**Detailed Description:**
– **Overview of Gen AI Toolbox for Databases:**
– Released as an open-source server designed to streamline development of generative AI applications.
– Enables connection to multiple databases: AlloyDB, Spanner, Cloud SQL for MySQL/PostgreSQL/SQL Server, and self-managed databases.
– Recently added support for Dgraph, enhancing capabilities for AI applications.

– **Key Features of Dgraph:**
– Real-time performance and distributed architecture for handling large workloads.
– Horizontal scalability to support increasing data needs.
– AI-native capabilities such as vector indexing for managing embeddings.
– Flexible data modeling for complex relationships that are essential for features like recommendation systems.

– **Integration Benefits:**
– Simplified setup and development process where Toolbox manages database connectivity.
– Production-ready infrastructure with automated operational management such as authentication and resource allocation.
– Enterprise-grade observability tools for logging, metrics, and tracing that help in debugging.

– **Real-world Application Scenario:**
– Example of an e-commerce agent demonstrating the use of a polyglot database integrating AlloyDB and Dgraph.
– Focuses on personalized product searches and recommendations based on user behavior and inventory data.

– **Knowledge Graph Creation:**
– Uses a property graph model to represent products, users, and reviews, facilitating complex queries and relationships.
– DQL (Dgraph Query Language) allows for efficient graph operations critical in recommendation engines.

– **Tool Definition and Usage with Toolbox:**
– Tools are defined in YAML for executing parameterized queries on databases.
– Agent created using LangChain framework implements a client to interact with Gen AI Toolbox.

– **Final Outcome:**
– A natural language interface for an AI-powered customer service assistant capable of efficient product searches and personalized recommendations supported by Dgraph’s knowledge graph integration.
– The Gen AI Toolbox simplifies handling database complexities, letting developers concentrate on delivering user experiences.

This text serves as an insight into innovation at the intersection of AI and database management, highlighting the practical implications of integrating generative AI with robust database solutions, which is crucial for security and compliance professionals focusing on data management and application security in cloud and AI environments.