Notebooks
W
Weaviate
Query Agent Get Started

Query Agent Get Started

vector-searchagentsvector-databaseretrieval-augmented-generationllm-frameworksfunction-callingweaviate-recipesPythongenerative-aiweaviate-services

Open In Colab

Build A Weaviate Query Agent - The E-Commerce Assistant

In this recipe, we will be building a simple e-commerce assistant agent with the Weaviate Query Agent. This agent will have access to a number of Weaviate collections, and will be capable of answering complex queries about brands and clothing items, accessing information from each collection.

๐Ÿ“š You can read and learn more about this service in our "Introducing the Weaviate Query Agent" blog.

To get started, we've prepared a few open datasets, available on Hugging Face. The first step will be walking through how to populate your Weaviate Cloud collections.

  • E-commerce: A dataset that lists clothing items, prices, brands, reviews etc.
  • Brands: A dataset that lists clothing brands and information about them such as their parent brand, child brands, average customer rating etc.

Additionally, we also have access to some other unrelated datasets which you can use to add more capabilities and variety to other agents later on in the recipe:

  • Financial Contracts: A dataset of financial contracts between indivicuals and/or companies, as well as information on the type of contract and who has authored them.
  • Weather: Daily weather information including temperature, wind speed, percipitation, pressure etc.

1. Setting Up Weaviate & Importing Data

To use the Weaviate Query Agent, first, create a Weaviate Cloud account๐Ÿ‘‡

  1. Create Serverless Weaviate Cloud account and setup a free Sandbox
  2. Go to 'Embedding' and enable it, by default, this will make it so that we use Snowflake/snowflake-arctic-embed-l-v2.0 as the embedding model
  3. Take note of the WEAVIATE_URL and WEAVIATE_API_KEY to connect to your cluster below

Info: We recommend using Weaviate Embeddings so you do not have to provide any extra keys for external embedding providers.

[ ]
[ ]
[3]

Prepare the Collections

In the following code blocks, we are pulling our demo datasets from Hugging Face and writing them to new collections in our Weaviate Serverless cluster.

โ—๏ธ The QueryAgent uses the descriptions of collections and properties to decide which ones to use when solving queries, and to access more information about properties. You can experiment with changing these descriptions, providing more detail, and more. It's good practice to provide property descriptions too. For example, below we make sure that the QueryAgent knows that prices are all in USD, which is information that would otherwise be unavailable.

[ ]
[ ]

2. Set Up the Query Agent

When setting up the query agent, we have to provide it a few things:

  • The client
  • The collection which we want the agent to have access to.
  • (Optionally) A system_prompt that describes how our agent should behave
  • (Optionally) Timeout - which for now defaults to 60s.

Let's start with a simple agent. Here, we're creating an agent that has access to our Brands & Ecommerce datasets.

[4]

3. Run the Query Agent

When we run the agent, it will first make a few decisions, depending on the query:

  1. The agent will decide which collection or collections to look up an answer in.
  2. The agent will also decide whether to perform a regular search query, what filters to use, whether to do an aggregation query, or all of them together!
  3. It will then provide a reponse within QueryAgentResponse. We will use the print_query_agent_response function for a nice display of various information provided in the response object.

Ask a Question

Let's start with a simple question: "I like the vintage clothes, can you list me some options that are less than $200?"

We can then also inspect how the agent responded, what kind of searches it performed on which collections, whether it has identified if the final answer is missing information or not, as well as the final answer ๐Ÿ‘‡

[15]


Ask a follow up question

The agent can also be provided with additional context. For example, we can provide the previous response as context and get a new_response

[6]


Now let's try a question that sholud require an aggregation. Let's see which brand lists the most shoes.

[7]


Search over multiple collections

In some cases, we need to combine the results of searches across multiple collections. From the result above, we can see that "Loom & Aura" lists the most shoes.

Let's imagine a scenario where the user would now want to find out more about this company, as well as the items that they sell.

[9]


Changing the System Prompt

In some cases, you may want to define a custom system_prompt for your agent. This can help you provide the agent with some default instructions as to how to behave. For example, let's create an agent that will always answer the query in the users language.

Let's also create a QueryAgent that has access to two more collections, Financial_contracts and Weather. Next, you can try out more queries yourself!

[10]

For example, this time lets ask something that is about weather!

[11]
Les vitesses de vent minimales, maximales et moyennes sont respectivement de 8,40 km/h, 94,88 km/h et 49,37 km/h. Ces donnรฉes offrent une vue d'ensemble des conditions de vent typiques mesurรฉes dans une pรฉriode ou un lieu donnรฉ.

Try More Questions

  • For example Let's try to find out more about the brans "Eko & Stitch"
[12]
Yes, Eko & Stitch has a branch in the UK. The brand is part of the broader company Nova Nest, which serves as Eko & Stitch's parent brand. Eko & Stitch itself operates in the UK and has its child brands, Eko & Stitch Active and Eko & Stitch Kids, also within the UK.
  • Our multi_lingual_agent also has access to a collection called "Financial_contracts". Let's try to find out some more information about this dataser.
[13]
The query seeks to identify the types of contracts listed and determine the most common type. Among the types of contracts provided in the results, the following were identified: employment contracts, sales agreements, invoice contracts, service agreements, and lease agreements. The most common type of contract found in the search results is the employment contract. However, when considering data from both search and aggregation results, the aggregation reveals that the invoice contract is the most common, followed by service agreements and lease agreements. While employment contracts appear frequently in the search results, they rank fourth in the aggregation data in terms of overall occurrences.