使用查询代理构建电商助手
在本教程中,我们将使用 Weaviate 查询代理 构建一个简单的电商助手代理。该代理将可以访问多个 Weaviate 集合,并能够回答有关品牌和服装项目的复杂查询,从每个集合中访问信息。
我们准备了一些公共数据集,您可以使用它们来探索查询代理。它们在 HuggingFace 上可用
简介:什么是查询代理?
Weaviate 查询代理是一种预构建的代理服务,旨在根据存储在 Weaviate Cloud 中的数据回答自然语言查询。用户只需提供自然语言的提示/问题,查询代理就会负责所有中间步骤以提供答案。

查询代理
- 接收任务,形式为问题或指令。
- 使用合适的生成模型(例如大型语言模型)来分析任务并确定要执行的确切查询。
- 将查询发送到 Weaviate,Weaviate 会根据需要使用指定的向量化器集成(我们将使用 Weaviate Embeddings)对查询进行向量化。
- 接收来自 Weaviate 的结果,并使用合适的生成模型生成对用户提示/查询的最终响应。
先决条件
要使用 Weaviate 代理和 Weaviate Embedding 服务,您需要拥有一个 Weaviate Cloud 帐户。
步骤 1:设置 Weaviate
现在,让我们开始通过设置一个 Weaviate Cloud 实例来完成本教程,并将其连接到 Python 客户端。
1.1 创建 Weaviate Cloud 集群
- 在 Weaviate Cloud 中创建一个 免费的 Sandbox 集群。
- 请注意
REST Endpoint和AdminAPI 密钥,以便连接到您的集群。(有关更多信息,请查看 快速入门)
在本教程中,我们使用 Weaviate Embeddings 服务作为向量化器,因此您无需为外部嵌入提供程序提供任何额外的密钥。Weaviate Embeddings 使用 Snowflake/snowflake-arctic-embed-l-v2.0 作为默认嵌入模型。
如果您想使用另一个向量化器,请查看支持的 模型提供程序列表。
1.2 安装 Python 库
为了安装 Weaviate Python 客户端以及 agents 组件,请运行
pip install "weaviate-client[agents]"
您还需要 datasets,这是一个轻量级库,可提供对 HuggingFace 上托管的公共数据集的访问权限。
pip install datasets
故障排除:强制 pip 安装最新版本
对于现有安装,即使 pip install -U "weaviate-client[agents]" 也可能无法将 weaviate-agents 升级到 最新版本。如果发生这种情况,请尝试显式升级 weaviate-agents 包
1.3 连接到您的实例
现在,您终于可以使用第一步中的参数连接到您的 Weaviate Cloud 实例了
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
import os
import weaviate
from weaviate.auth import Auth
# Best practice: store your credentials in environment variables
weaviate_url = os.environ["WEAVIATE_URL"]
weaviate_api_key = os.environ["WEAVIATE_API_KEY"]
client = weaviate.connect_to_weaviate_cloud(
cluster_url=weaviate_url,
auth_credentials=Auth.api_key(weaviate_api_key),
)
print(client.is_ready()) # Should print: `True`
# Your work goes here!
client.close() # Free up resources
运行此代码段后,您应该看到消息 True 打印出来,这意味着您已成功连接到您的实例。
步骤 2:准备集合
在以下代码块中,我们从 Hugging Face 拉取我们的演示数据集,并将它们写入 Weaviate Sandbox 集群中的新集合。在开始将数据导入 Weaviate 之前,我们需要 定义集合,这意味着设置数据模式并选择向量化器/嵌入服务。
2.1 定义集合
下面可以看到数据集 E-Commerce 和 Brands 中的对象是什么样的。

对于集合 Brands,我们将使用 auto-schema 选项,该选项会根据导入的数据自动创建属性。但是,这不是导入数据的最佳方式。
查询代理使用 集合和属性的描述 来决定在解决查询时使用哪些集合。您可以尝试更改这些描述并提供更多详细信息。这就是为什么建议手动定义数据模式并添加属性描述的原因。
例如,下面我们确保查询代理知道所有价格均以美元为单位,这是否则无法获得的信息。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.config import Configure, Property, DataType
# Using `auto-schema` to infer the data schema during import
client.collections.create(
"Brands",
description="A dataset that lists information about clothing brands, their parent companies, average rating and more.",
vector_config=Configure.Vectors.text2vec_weaviate(),
)
# Explicitly defining the data schema
client.collections.create(
"ECommerce",
description="A dataset that lists clothing items, their brands, prices, and more.",
vector_config=Configure.Vectors.text2vec_weaviate(),
properties=[
Property(name="collection", data_type=DataType.TEXT),
Property(
name="category",
data_type=DataType.TEXT,
description="The category to which the clothing item belongs",
),
Property(
name="tags",
data_type=DataType.TEXT_ARRAY,
description="The tags that are assocciated with the clothing item",
),
Property(name="subcategory", data_type=DataType.TEXT),
Property(name="name", data_type=DataType.TEXT),
Property(
name="description",
data_type=DataType.TEXT,
description="A detailed description of the clothing item",
),
Property(
name="brand",
data_type=DataType.TEXT,
description="The brand of the clothing item",
),
Property(name="product_id", data_type=DataType.UUID),
Property(
name="colors",
data_type=DataType.TEXT_ARRAY,
description="The colors on the clothing item",
),
Property(name="reviews", data_type=DataType.TEXT_ARRAY),
Property(name="image_url", data_type=DataType.TEXT),
Property(
name="price",
data_type=DataType.NUMBER,
description="The price of the clothing item in USD",
),
],
)
2.2 填充数据库
现在,我们可以将预向量化的数据(E-Commerce 和 Brands)导入到我们的 Weaviate Cloud 实例
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from datasets import load_dataset
brands_dataset = load_dataset(
"weaviate/agents", "query-agent-brands", split="train", streaming=True
)
ecommerce_dataset = load_dataset(
"weaviate/agents", "query-agent-ecommerce", split="train", streaming=True
)
brands_collection = client.collections.use("Brands")
ecommerce_collection = client.collections.use("ECommerce")
with brands_collection.batch.fixed_size(batch_size=200) as batch:
for item in brands_dataset:
batch.add_object(properties=item["properties"], vector=item["vector"])
with ecommerce_collection.batch.fixed_size(batch_size=200) as batch:
for item in ecommerce_dataset:
batch.add_object(properties=item["properties"], vector=item["vector"])
failed_objects = brands_collection.batch.failed_objects
if failed_objects:
print(f"Number of failed imports: {len(failed_objects)}")
print(f"First failed object: {failed_objects[0]}")
print(f"Size of the ECommerce dataset: {len(ecommerce_collection)}")
print(f"Size of the Brands dataset: {len(brands_collection)}")
通过调用 len() 来获取我们的集合,我们可以检查导入是否已成功完成,并查看我们的集合的大小。
Size of the E-Commerce dataset: 448
Size of the Brands dataset: 104
步骤 3:设置查询代理
3.1 基本查询代理
在设置查询代理时,我们需要提供一些内容
client- 我们希望代理访问的
collections。 - (可选) 描述我们的代理应该如何行为的
system_prompt - (可选) 超时时间 - 目前默认为 60 秒。
让我们从一个简单的代理开始。在这里,我们创建一个 qa 代理,它可以访问我们的 Brands 和 ECommerce 数据集。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.agents.query import QueryAgent
qa = QueryAgent(
client=client,
collections=["ECommerce", "Brands"],
)
就这样!现在,您可以开始使用查询代理了。
3.2 自定义查询代理(带有系统提示)
在某些情况下,您可能希望为您的代理定义一个自定义 system_prompt。这可以帮助您为代理提供一些默认指令,说明如何行为。例如,让我们创建一个始终以用户语言回答查询的代理。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
multi_lingual_qa = QueryAgent(
client=client,
collections=["ECommerce", "Brands"],
system_prompt="You are a helpful assistant that always generates the final response in the user's language."
"You may have to translate the user query to perform searches. But you must always respond to the user in their own language.",
)
我们将在 步骤 4.5 中使用此自定义代理 multi_lingual_qa。
步骤 4:运行查询代理
当我们运行代理时,它会首先根据查询做出一些决定
- 代理将决定查看答案的集合(或多个集合)。
- 代理还将决定是执行常规 搜索查询、使用哪些 过滤器、是否执行 聚合查询,还是全部一起执行。
- 然后,它将在
QueryAgentResponse中提供响应。
4.1 提问
让我们从一个简单的问题开始
- “我喜欢复古服装,你能列出一些价格低于 200 美元的产品吗?”
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
response = qa.ask(
"I like vintage clothes, can you list me some options that are less than $200?"
)
response.display()
代理的响应是
original_query='I like vintage clothes, can you list me some options that are less than $200?' collection_names=['ECommerce'] searches=[[QueryResultWithCollection(queries=['vintage clothes'], filters=[[IntegerPropertyFilter(property_name='price', operator=<ComparisonOperator.LESS_THAN: '<'>, value=200.0)]], filter_operators='AND', collection='ECommerce')]] aggregations=[] usage=Usage(requests=3, request_tokens=7681, response_tokens=428, total_tokens=8109, details=None) total_time=11.588264226913452 aggregation_answer=None has_aggregation_answer=False has_search_answer=True is_partial_answer=False missing_information=[] final_answer='Here are some vintage-inspired clothing options under $200:\n\n1. **Vintage Philosopher Midi Dress ($125.00)** - A deep green velvet fabric dress with antique gold button detailing, perfect for an academic or sophisticated setting. [Echo & Stitch]\n\n2. **Vintage Gale Pleated Dress ($120.00)** - A burgundy pleated dress capturing the Dark Academia aesthetic, great for poetry readings or contemplative afternoons. [Solemn Chic]\n\n3. **Retro Groove Flared Pants ($59.00)** - Electric blue flared pants capturing early 2000s millennial pop culture chic. [Vivid Verse]\n\n4. **Vintage Scholar Tote ($90.00)** - A deep green canvas tote with brown leather accents, ideal for carrying scholarly essentials. [Echo & Stitch]\n\n5. **Retro Futura Tee ($29.98)** - A bold graphic tee with early 2000s pop culture references, perfect for a nostalgic throwback. [Vivid Verse]\n\n6. **Electric Velvet Trousers ($60.00)** - Neon green velvet trousers with a flared leg, inspired by turn-of-the-century fashion icons. [Vivid Verse]\n\n7. **Vintage Ivy Loafers ($120.00)** - Burgundy leather loafers offering timeless sophistication, suitable for academic settings. [Solemn Chic]' sources=[Source(object_id='6040c373-9cce-421d-8f4e-e01346cbf29f', collection='ECommerce'), Source(object_id='06a83da4-92ff-4cbc-9cc6-c55fc6ed49b9', collection='ECommerce'), Source(object_id='a5a0927d-2859-47e6-8455-d77e0190aa53', collection='ECommerce'), Source(object_id='a5f3c394-d207-4064-a2bc-b9f655d09ddc', collection='ECommerce'), Source(object_id='5d7bb719-2df5-4a7c-9bbc-275450ab29c6', collection='ECommerce'), Source(object_id='50f4c89c-faf9-4430-b393-c39ede5d511d', collection='ECommerce'), Source(object_id='5b6d5a1f-e218-4400-ac9f-df59466f3c97', collection='ECommerce')]
但是这个输出有点太多了,所以让我们只返回字段 final_answer
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
# Only print the final answer of the response
print(response.final_answer)
输出是
Here are some vintage-inspired clothing options under $200:
1. **Vintage Philosopher Midi Dress ($125.00)** - A deep green velvet fabric dress with antique gold button detailing, perfect for an academic or sophisticated setting. [Echo & Stitch]
2. **Vintage Gale Pleated Dress ($120.00)** - A burgundy pleated dress capturing the Dark Academia aesthetic, great for poetry readings or contemplative afternoons. [Solemn Chic]
3. **Retro Groove Flared Pants ($59.00)** - Electric blue flared pants capturing early 2000s millennial pop culture chic. [Vivid Verse]
4. **Vintage Scholar Tote ($90.00)** - A deep green canvas tote with brown leather accents, ideal for carrying scholarly essentials. [Echo & Stitch]
5. **Retro Futura Tee ($29.98)** - A bold graphic tee with early 2000s pop culture references, perfect for a nostalgic throwback. [Vivid Verse]
6. **Electric Velvet Trousers ($60.00)** - Neon green velvet trousers with a flared leg, inspired by turn-of-the-century fashion icons. [Vivid Verse]
7. **Vintage Ivy Loafers ($120.00)** - Burgundy leather loafers offering timeless sophistication, suitable for academic settings. [Solemn Chic]
通过使用 QueryAgentResponse 上的 display() 方法,您可以获得以下信息的友好输出
- 🔍 原始查询 -
original_query - 📝 最终答案 -
final_answer - 🔭 执行的搜索 -
searches - 📊 运行的聚合 -
aggregations - 📚 来源 -
sources - ⚠️ 缺少信息 -
missing_information - 📊 使用统计信息 -
usage - 总耗时 -
total_time
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
# Print the whole response in a user-friendly format
response.display()
响应是
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────── 🔍 Original Query ────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ I like vintage clothes, can you list me some options that are less than $200? │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────── 📝 Final Answer ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ Here are some vintage-inspired clothing options under $200: │
│ │
│ 1. **Vintage Philosopher Midi Dress ($125.00)** - A deep green velvet fabric dress with antique gold button detailing, perfect for an academic or sophisticated setting. [Echo & Stitch] │
│ │
│ 2. **Vintage Gale Pleated Dress ($120.00)** - A burgundy pleated dress capturing the Dark Academia aesthetic, great for poetry readings or contemplative afternoons. [Solemn Chic] │
│ │
│ 3. **Retro Groove Flared Pants ($59.00)** - Electric blue flared pants capturing early 2000s millennial pop culture chic. [Vivid Verse] │
│ │
│ 4. **Vintage Scholar Tote ($90.00)** - A deep green canvas tote with brown leather accents, ideal for carrying scholarly essentials. [Echo & Stitch] │
│ │
│ 5. **Retro Futura Tee ($29.98)** - A bold graphic tee with early 2000s pop culture references, perfect for a nostalgic throwback. [Vivid Verse] │
│ │
│ 6. **Electric Velvet Trousers ($60.00)** - Neon green velvet trousers with a flared leg, inspired by turn-of-the-century fashion icons. [Vivid Verse] │
│ │
│ 7. **Vintage Ivy Loafers ($120.00)** - Burgundy leather loafers offering timeless sophistication, suitable for academic settings. [Solemn Chic] │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────── 🔭 Searches Executed 1/1 ─────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ QueryResultWithCollection(queries=['vintage clothes'], filters=[[IntegerPropertyFilter(property_name='price', operator=<ComparisonOperator.LESS_THAN: '<'>, value=200.0)]], filter_operators='AND', collection='ECommerce') │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ 📊 No Aggregations Run │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 📚 Sources ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ - object_id='6040c373-9cce-421d-8f4e-e01346cbf29f' collection='ECommerce' │
│ - object_id='06a83da4-92ff-4cbc-9cc6-c55fc6ed49b9' collection='ECommerce' │
│ - object_id='a5a0927d-2859-47e6-8455-d77e0190aa53' collection='ECommerce' │
│ - object_id='a5f3c394-d207-4064-a2bc-b9f655d09ddc' collection='ECommerce' │
│ - object_id='5d7bb719-2df5-4a7c-9bbc-275450ab29c6' collection='ECommerce' │
│ - object_id='50f4c89c-faf9-4430-b393-c39ede5d511d' collection='ECommerce' │
│ - object_id='5b6d5a1f-e218-4400-ac9f-df59466f3c97' collection='ECommerce' │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
📊 Usage Statistics
┌────────────────┬──────┐
│ LLM Requests: │ 3 │
│ Input Tokens: │ 7681 │
│ Output Tokens: │ 428 │
│ Total Tokens: │ 8109 │
└────────────────┴──────┘
Total Time Taken: 11.59s
4.2 提出后续问题
代理还可以提供额外的上下文。例如,我们可以使用 ChatMessage 对象将之前的响应作为上下文提供,以创建一个对话并获得 new_response
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.agents.classes import ChatMessage
# Create a conversation with the previous response
conversation = [
ChatMessage(
role="user",
content="I like vintage clothes, can you list me some options that are less than $200?",
),
ChatMessage(role="assistant", content=response.final_answer),
ChatMessage(
role="user", content="What about some nice shoes, same budget as before?"
),
]
new_response = qa.ask(conversation)
new_response.display()
响应是
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────── 🔍 Original Query ────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ What about some nice shoes, same budget as before? │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────── 📝 Final Answer ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ Here are some great vintage-style shoes priced under $200 from various collections that blend style with nostalgia: │
│ │
│ 1. **Vintage Noir Loafers** ($125): These dress shoes, part of the Dark Academia collection, come in black and grey, offering a timeless look with subtle modern twists. They have received positive reviews for style and comfort, once broken │
│ in (Source: 1). │
│ │
│ 2. **Vintage Ivy Loafers** ($120): Another classic from the Dark Academia line, these deep burgundy loafers combine elegance with comfort, as appreciated by users in their reviews (Source: 2). │
│ │
│ 3. **Glide Platforms** ($90): For a more playful option, these high-shine pink platform sneakers offer a nod to the Y2K era, receiving praise for their comfort and unique look (Source: 3). │
│ │
│ 4. **Celestial Step Platform Sneakers** ($90): With a dreamy sky blue color, these Y2K-inspired sneakers provide comfort and a touch of whimsy (Source: 4). │
│ │
│ 5. **Garden Stroll Loafers** ($90): From the Cottagecore collection, these cream loafers feature delicate floral motifs for a chic, countryside-inspired look (Source: 5). │
│ │
│ 6. **Bramble Brogues** ($120): In dusky green, these suede brogues add pastoral elegance to any outfit, admired for their craftsmanship (Source: 7). │
│ │
│ 7. **Garden Fête Heels** ($125): These cottagecore heels feature floral embroidery for a touch of elegance perfect for garden parties (Source: 10). │
│ │
│ 8. **Garden Gala T-Straps** ($125): Elegant cream and sage T-strap heels featuring floral embellishments, perfect for a whimsical yet classy touch (Source: 18). │
│ │
│ All these options fall under the $200 budget, offering a variety of styles from classic and elegant to colorful and playful, accommodating different fashion tastes and preferences. │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────── 🔭 Searches Executed 1/1 ─────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ QueryResultWithCollection(queries=['vintage shoes'], filters=[[IntegerPropertyFilter(property_name='price', operator=<ComparisonOperator.LESS_THAN: '<'>, value=200.0)]], filter_operators='AND', collection='Ecommerce') │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────── 📊 Aggregations Run 1/1 ─────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ AggregationResultWithCollection( │
│ search_query=None, │
│ groupby_property=None, │
│ aggregations=[IntegerPropertyAggregation(property_name='price', metrics=<NumericMetrics.MEAN: 'MEAN'>)], │
│ filters=[TextPropertyFilter(property_name='subcategory', operator=<ComparisonOperator.LIKE: 'LIKE'>, value='vintage')], │
│ collection='Ecommerce' │
│ ) │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 📚 Sources ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ - object_id='92efe1bb-77ee-4002-8618-4c9d7c3972c2' collection='Ecommerce' │
│ - object_id='f51001bf-c0c6-47d5-b165-4e7ce878900e' collection='Ecommerce' │
│ - object_id='9ba9ed81-c917-4e13-8058-d6ad81896ef3' collection='Ecommerce' │
│ - object_id='41f324c2-a8e8-4cde-87d0-42661435a32c' collection='Ecommerce' │
│ - object_id='d8d8e59e-d935-4a6f-9df1-bc2477452b11' collection='Ecommerce' │
│ - object_id='505238e0-9993-4838-89fc-513c8f53d9f8' collection='Ecommerce' │
│ - object_id='4a1321dc-d3f5-405d-8b21-e656c448ccfd' collection='Ecommerce' │
│ - object_id='b59f7700-2633-43d6-807e-72dda35d545c' collection='Ecommerce' │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
📊 Usage Statistics
┌────────────────┬───────┐
│ LLM Requests: │ 5 │
│ Input Tokens: │ 15998 │
│ Output Tokens: │ 906 │
│ Total Tokens: │ 16904 │
└────────────────┴───────┘
Total Time Taken: 22.77s
4.3 执行聚合
现在,让我们尝试一个需要聚合的问题。让我们看看哪个品牌列出的鞋子最多。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
response = qa.ask("What is the the name of the brand that lists the most shoes?")
response.display()
响应是
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────── 🔍 Original Query ────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ What is the the name of the brand that lists the most shoes? │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────── 📝 Final Answer ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ The brand that lists the most shoes is Loom & Aura, with a total count of 118 shoes across various styles such as Cottagecore, Fairycore, Goblincore, Light Academia, and Dark Academia. │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ 🔭 No Searches Run │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────── 📊 Aggregations Run 1/1 ─────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ AggregationResultWithCollection( │
│ search_query=None, │
│ groupby_property='brand', │
│ aggregations=[TextPropertyAggregation(property_name='collection', metrics=<TextMetrics.TOP_OCCURRENCES: 'TOP_OCCURRENCES'>, top_occurrences_limit=1)], │
│ filters=[], │
│ collection='ECommerce' │
│ ) │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
📊 Usage Statistics
┌────────────────┬──────┐
│ LLM Requests: │ 3 │
│ Input Tokens: │ 5794 │
│ Output Tokens: │ 238 │
│ Total Tokens: │ 6032 │
└────────────────┴──────┘
Total Time Taken: 7.52s
4.4 在多个集合上搜索
在某些情况下,我们需要组合来自多个集合的搜索结果。从上面的结果中,我们可以看到“Loom & Aura”列出的鞋子最多。
让我们想象一下,用户现在想了解更多关于这家公司以及他们销售的商品的信息。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
response = qa.ask(
"Does the brand 'Loom & Aura' have a parent brand or child brands and what countries do they operate from? "
"Also, what's the average price of a shoe from 'Loom & Aura'?"
)
response.display()
响应是
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────── 🔍 Original Query ────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ Does the brand 'Loom & Aura' have a parent brand or child brands and what countries do they operate from? Also, what's the average price of a shoe from 'Loom & Aura'? │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────── 📝 Final Answer ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ The brand "Loom & Aura" operates as a parent brand with several child brands, including "Loom & Aura Active," "Loom & Aura Kids," "Nova Nest," "Vivid Verse," "Loom Luxe," "Saffrom Sage," "Stellar Stitch," "Nova Nectar," "Canvas Core," and │
│ "Loom Lure." "Loom & Aura" itself is based in Italy and operates in multiple countries where its child brands are located, such as the USA, Japan, South Korea, Spain, the UK, and France. │
│ │
│ The average price of a shoe from "Loom & Aura" is approximately $87.11, based on the aggregation of available data on shoe prices. │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────── 🔭 Searches Executed 1/2 ─────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ QueryResultWithCollection(queries=['Loom parent brand and child brands', 'Aura parent brand and child brands'], filters=[[], []], filter_operators='AND', collection='Brands') │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────── 🔭 Searches Executed 2/2 ─────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ QueryResultWithCollection(queries=['countries where Loom & Aura operates'], filters=[[TextPropertyFilter(property_name='name', operator=<ComparisonOperator.EQUALS: '='>, value='Loom & Aura')]], filter_operators='AND', collection='Brands') │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────── 📊 Aggregations Run 1/1 ─────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ AggregationResultWithCollection( │
│ search_query='', │
│ groupby_property=None, │
│ aggregations=[IntegerPropertyAggregation(property_name='price', metrics=<NumericMetrics.MEAN: 'MEAN'>)], │
│ filters=[TextPropertyFilter(property_name='brand', operator=<ComparisonOperator.EQUALS: '='>, value='Loom')], │
│ collection='Ecommerce' │
│ ) │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 📚 Sources ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ - object_id='cb022de3-36ac-4ab9-94c6-cddbbebbb035' collection='Brands' │
│ │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
📊 Usage Statistics
┌────────────────┬───────┐
│ LLM Requests: │ 5 │
│ Input Tokens: │ 11158 │
│ Output Tokens: │ 608 │
│ Total Tokens: │ 11766 │
└────────────────┴───────┘
Total Time Taken: 16.12s
4.5 使用系统提示
我们将使用来自 步骤 3.2 的自定义代理,系统提示如下
"You are a helpful assistant that always generates the final response in the user's language."
"You may have to translate the user query to perform searches. But you must always respond to the user in their own language."
例如,尝试用日语询问品牌“Eko & Stitch”是否在英国设有分支机构或关联公司,而不是使用英语
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
response = multi_lingual_qa.ask(
'"Eko & Stitch"は英国に支店または関連会社がありますか?'
)
print(response.final_answer)
响应是
Eko & Stitchは、英国に拠点があります。1974年に設立され、品質の高いクラフトマンシップとタイムレスなエレガンスで知られています。関連ブランドとして「Eko & Stitch Active」と「Eko & Stitch Kids」があります。また、Eko & Stitchの親会社はNova Nestです。
或者翻译成英文
Eko & Stitch is based in the UK. It was established in 1974 and is known for its high-quality craftsmanship and timeless elegance. It has associated brands, "Eko & Stitch Active" and "Eko & Stitch Kids." Additionally, the parent company of Eko & Stitch is Nova Nest.
总结
本指南向您展示了如何使用 Weaviate 的代理服务构建端到端电商助手——从设置您的 Weaviate Cloud 实例和导入数据集到配置智能处理自然语言查询的查询代理。
查询代理会自动解释查询,以决定是运行标准搜索、过滤器还是聚合。它连接到多个集合,并根据您的查询选择正确的来源。代理还可以使用之前的交互提供更相关的后续答案,并且您可以定义系统提示以确保代理始终以用户的首选语言或风格进行响应。
更多资源
问题和反馈
如果您有任何问题或反馈,请在 用户论坛 中告诉我们。
