用代码代理连接向量检索构建商品搜索智能体
介绍 SmolAgents 的 CodeAgent 让模型以代码形式调用工具,并演示将其与 Qdrant 向量检索结合,通过自定义工具查询商品集合,再由代理给出推荐结果。
## SmolAgents HuggingFace [SmolAgents](<https://github.com/huggingface/smolagents>) 是一个用于构建 AI 代理的 Python 库。 这些代理编写 Python 代码, 用于调用工具并组织其他代理 。 用来 `CodeAgent`. 一个用代码写入其动作的LLM引擎. SmolAgents表示,这个方法被证明比当前业界的做法更有效,让LLM输出它想要称为的工具词典: . [使用 30% 减少步骤](<https://huggingface.co/papers/2402.01030>) (因此,30%减少了LLM的呼叫)和 [以困难的基准提高业绩](<https://huggingface.co/papers/2411.01747>). ## 使用 Qdrant 软件 我们将展示您如何通过建立产品搜索代理, 将 SmolAgents 与 Qdrant 的检索匹配 。 ### 安装 ```shell pip install smolagents qdrant-client fastembed ``` ### 设置 Qdrant 工具 我们将构建一个可以查询 Qdrant 收藏的 SmolAgents 工具。 此工具将使用本地 [FastEmbed](<https://github.com/qdrant/fastembed>). 最初,我们将在 Qdrant 收集 有关 45,000 产品 [亚马逊伯克利天体](<https://qdrant.tech/documentation/datasets/#amazon-berkeley-objects>) 数据集,我们可以搜索到。每个产品都用嵌入其图像的CLIP来代表,因此我们将将文本查询与匹配的CLIP文本模型嵌入其中。 该快照需要 Qdrant 1.20 或稍后。 本地启动它 `docker run --pull=always -p 6333:6333 qdrant/qdrant`. ```py from fastembed import TextEmbedding from qdrant_client import QdrantClient from smolagents import Tool class QdrantQueryTool(Tool): name = "qdrant_query" description = "Uses semantic search to retrieve products from a Qdrant collection." inputs = { "query": { "type": "string", "description": "The query to perform. This should be semantically close to your target documents.", } } output_type = "string" def __init__(self, **kwargs): super().__init__(**kwargs) self.collection_name = "smolagents_products" self.client = QdrantClient(timeout=120) if not self.client.collection_exists(self.collection_name): self.client.recover_snapshot( collection_name=self.collection_name, location="https://snapshots.qdrant.io/amazon-berkeley-objects.snapshot", ) self.embedder = TextEmbedding(model_name="Qdrant/clip-ViT-B-32-text") def forward(self, query: str) -> str: points = self.client.query_points( self.collection_name, query=next(self.embedder.query_embed(query)), limit=5 ).points docs = "Retrieved documents:\n" + "".join( [ f"== Document {str(i)} ==\n" + f"PRODUCT NAME: {point.payload['name']}\n" + f"BRAND: {point.payload.get('brand', '')}\n" for i, point in enumerate(points) ] ) return docs ``` ### 定义代理 我们现在可以安排了 `CodeAgent` 来利用我们的 `QdrantQueryTool`. `InferenceClientModel` 需要一个 Hugging Face 的代号, 并有“ 给推论提供商打电话” 的许可, 以及 推论提供商的信用 。 ```python from smolagents import CodeAgent, InferenceClientModel, LogLevel import os # HuggingFace Access Token # https://huggingface.co/docs/hub/en/security-tokens os.environ["HF_TOKEN"] = "----------" agent = CodeAgent( tools=[QdrantQueryTool()], model=InferenceClientModel(model_id="openai/gpt-oss-120b"), max_steps=4, verbosity_level=LogLevel.DEBUG ) ``` 最后,我们可以通过用户查询来运行代理. ```python agent_output = agent.run("I need a comfortable chair for my home office. What do you recommend?") print(agent_output) ``` 代理商可以打电话 `qdrant_query` 找到产品并在建议中利用结果。 ## 进一步阅读 - [SmolAgents 博客](<https://huggingface.co/blog/smolagents#code-agents>) - [SmolAgents 来源](<https://github.com/huggingface/smolagents>)
查看原始来源