微信扫码
添加专属顾问
在这个项目中,我们的目标是创建一个由多智能体架构和本地大语言模型(LLM)驱动的个人笔记本电脑专家系统。
该系统将使用一个SQL数据库,包含有关笔记本电脑的全面信息,包括价格、重量和规格。用户可以根据自己的特定需求向系统提问,获取推荐建议。
由LLM驱动的多智能体系统旨在自主或半自主地处理复杂问题,这些问题可能对单次LLM响应来说具有挑战性。
在我们的案例中,这些系统特别适用于数据库搜索、跨品牌比较笔记本电脑的功能或选择适当的组件(如GPU)。
通过使用多个专门的智能体,我们可以减少“幻觉”的可能性,并提高响应的准确性,特别是在处理需要访问特定数据源的任务时。
在我们的多智能体设置中,每个智能体都由LLM驱动,所有智能体可以使用相同的模型,也可以针对特定任务进行微调。
每个智能体都有独特的描述、目标和必要的工具。例如:
搜索智能体可能可以访问查询SQL数据库的工具,或在网络上搜索最新信息。
比较智能体可能专门分析和比较笔记本电脑的规格。
撰写智能体将负责根据其他智能体收集的信息,撰写连贯且用户友好的响应。
这种方法使我们能够将复杂的查询分解为可管理的子任务,每个子任务由专门的智能体处理,从而为用户提供有关笔记本电脑的更准确和全面的响应。现在是时候展示将用于此项目的工具和框架了。
CrewAI是一个用于协调自主或半自主AI智能体合作完成复杂任务和问题的框架。它通过为多个智能体分配角色、目标和任务,使它们能够高效协作。该框架支持多种协作模型:
顺序模式:智能体按预定义的顺序完成任务,每个智能体在前一个智能体的工作基础上继续工作。
层次模式:一个指定的智能体或语言模型作为管理者,监督整个过程并将任务分配给其他智能体。
这些智能体可以进行沟通、共享信息,并协调它们的努力以实现整体目标。这种方法能够应对单个AI智能体难以处理的多方面挑战。虽然这些是主要的操作模式,但CrewAI团队正在积极开发更复杂的流程,以增强框架的能力和灵活性。
Ollama 是一个开源项目,简化了在本地机器上安装和使用大语言模型的所有麻烦。Ollama 有许多优点,例如 API 支持、离线访问、硬件加速和优化、数据隐私和安全性。在官方网站上,你可以找到所有支持的模型。
通过官方网站下载 Ollama,经过快速安装后,你可以选择一个模型并开始与其对话。Ollama 提供了多种型号,从小型(1B 参数)到非常大型(405B 参数)的模型。选择与你的本地机器能力兼容的模型时要小心,例如 LLaMA 3.1 70B 需要大约 40GB 的显存才能正确运行。
要下载一个模型,使用以下命令:
ollama pull <model_name>
要与模型开始对话,使用:
ollama run <model_name>
# 结束对话,输入 bye
bye
要查看所有已下载的模型及其名称,使用:
ollama list
请注意,具体的要求可能会根据特定的模型和配置有所不同。请随时查阅 Ollama 官方文档,以获取有关模型要求和使用的最新信息。
Text-to-SQL 是一种自然语言处理技术,它使用大语言模型将人类可读的文本转换为结构化的 SQL 查询。
这种方法使用户能够使用日常语言与数据库进行交互,而无需深入了解 SQL 语法。
在我们的项目中,我们展示了一个多智能体框架,并将 Ollama 集成到本地 LLM 中。
我们需要添加的最后一个组件是一个支持 Text-to-SQL 功能的工具,使智能体能够查询信息。
使用 SQLite3,我们可以从 Kaggle 上获取的 CSV 文件创建一个简单的数据库。以下代码将 CSV 文件格式转换为 SQLite 数据库:
import sqlite3
import pandas as pd
# 加载 CSV 文件
df = pd.read_csv("laptop_price - dataset.csv")
df.columns = df.columns.str.strip()
print(df.columns)
# 如果需要,重命名列名,避免过于复杂。
df.columns = ['Company', 'Product', 'TypeName', 'Inches', 'ScreenResolution',
'CPU_Company', 'CPU_Type', 'CPU_Frequency', 'RAM', 'Memory',
'GPU_Company', 'GPU_Type', 'OpSys', 'Weight', 'Price']
# 连接到 SQLite
connection = sqlite3.connect("Laptop_price.db")
# 将数据加载到 SQLite
df.to_sql('LaptopPrice', connection, if_exists="replace")
connection.close()
简化列名可以提高查询的准确性。它减少了 Text-to-SQL 模型生成不完整或不精确列引用的风险,可能避免错误并提高结果的可靠性(例如省略“Price (Euro)”中的“(Euro)”)。 以下是智能体将使用的工具函数:
@tool("SQL_retriever")
def read_sql_query(sql: str) -> list:
"""
This tool is used to retrieve data from a database. Using SQL query as input.
Args:
sql (str): The SQL query to execute.
Returns:
list: A list of tuples containing the query results.
"""
with sqlite3.connect("Laptop_price.db") as conn:
cur = conn.cursor()
cur.execute(sql)
rows = cur.fetchall()
for row in rows:
print(row)
return rows
让我们定义我们的智能体及其角色。我们将为此任务设置两个智能体:
“SQL Master”:负责将自然语言查询转换为 SQL,并根据用户问题搜索 SQL 数据库的专家。这个智能体负责从数据库中检索所需的数据。
“Writer”:一个热情的文章撰写者,他将利用 SQL Master 提供的数据编写引人入胜的展示文章。
整个过程如下:SQL Master 解析用户的问题,将其转换为 SQL 查询,并从数据库中检索相关数据。然后,Writer 利用这些数据编写结构良好、内容丰富的展示文章。
首先安装 CrewAI:
pip install 'crewai[tools]'
导入所需的库:
import os
from crewai import Agent, Task, Crew, Process
from dotenv import load_dotenv
from langchain_community.tools import tool
from langchain_openai import ChatOpenAI
指定 Ollama 本地 API 以连接和使用 LLM,在我们的例子中,将使用 “qwen2:7b” 模型:
llm = ChatOpenAI(model="qwen2:7b",
base_url="http://localhost:11434/v1",)
写下你要向智能体团队提出的问题:
Subject = "I need a PC that cost around 500 dollars and that is very light"
定义智能体团队的角色和目标:
sql_agent = Agent(
role="SQL Master",
goal= """ Given the asked question you convert the question into SQL query with the information in your backstory. \
The table name is LaptopPrice and consist of columns - 'Company', 'Product', 'TypeName', 'Inches', 'ScreenResolution', 'CPU_Company', 'CPU_Type', 'CPU_Frequency', 'RAM', 'Memory', 'GPU_Company', 'GPU_Type', 'OpSys', 'Weight', 'Price'. \
Write simple SQL query. \
The query should be like this: "Action Input: {"sql": "SELECT GPU_Type, COUNT(*) AS usage_count FROM LaptopPrice GROUP BY GPU_Type ORDER BY usage_count DESC LIMIT 1}" """,
backstory= SYSTEM_SQL,
verbose=True,
tools=[read_sql_query], # Tool to use by the agent
llm=llm
)
writer_agent = Agent(
role="Writer",
goal= "Write an article about the laptor subject with a great eye for details and a sense of humour and innovation.",
backstory= """ You are the best writer with a great eye for details. You are able to write engaging and informative articles about the given subject with humour and creativity.""",
verbose=True,
llm=llm
)
task0 = Task(
description= f"The subject is {Subject}",
expected_output= "According to the subject define the topic to respond with the result of the SQL query.",
agent= sql_agent )
tas1 = Task(
description= "Write a short article about the subject with humour and creativity.",
expected_output= "An article about the subject written with a great eye for details and a sense of humour and innovation.",
agent= writer_agent )
SQL Master 智能体的背景故事 (SYSTEM_SQL) 非常详细,应包含 SQL 模式的完整解释。这种全面的背景信息为智能体提供了对数据库结构的清晰理解,从而生成更准确、高效的查询。
SYSTEM_SQL = """You are an expert in converting English or Turkish to SQL query.\
The SQL database has LaptopPrice and has the following columns - 'Company', 'Product', 'TypeName', 'Inches', 'ScreenResolution',
'CPU_Company', 'CPU_Type', 'CPU_Frequency', 'RAM', 'Memory',
'GPU_Company', 'GPU_Type', 'OpSys', 'Weight', 'Price' \
Description of each column: "Company":
Laptop manufacturer.
"Product":
Brand and Model.
"TypeName":
Type (Notebook, Ultrabook, Gaming, etc.)
"Inches":
Screen Size.
"ScreenResolution":
Screen Resolution.
"CPU_Company":
Central Processing Unit (CPU) manufacturer.
"CPU_Type":
Central Processing Unit (CPU) type.
"CPU_Frequency":
Central Processing Unit (CPU) Frequency (GHz).
"RAM":
Laptop RAM.
"Memory":
Hard Disk / SSD Memory.
"GPU_Company":
Graphic Processing Unit (GPU) manufacturer.
"GPU_type":
Type of GPU.
"OpSys":
Operation system of the laptop.
"Weight":
The weight of the laptop.
"Price":
The price of the laptop.
This is my prompt for my database named LaptopPrice. You can use this schema to formulate only one SQL queries for various questions about segment passenger data, market share, and travel patterns. \
also the sql code should not have ``` in the beginning or end and sql word in out. Only respond with SQL command. Write only one SQL query.You are an expert in converting English or Turkish to SQL query. Convert the User input into a simple SQL format. Respond only with SQL query."""
启动智能体团队工作:
crew = Crew(
agents=[sql_agent, writer_agent],
tasks=[task0, task2],
verbose=True,
manager_llm=llm
)
result = crew.kickoff()
使用智能体团队的最终结果提示:
[2024-09-21 12:53:49][DEBUG]: == Working Agent: SQL Master
[2024-09-21 12:53:49][INFO]: == Starting Task: The subject is I need a PC that cost around 500 dollars and that is very light
> Entering new CrewAgentExecutor chain...
To find a laptop that costs around 500 dollars and is very light, we need to select laptops with a price less than or equal to 500 dollars and also weight should be considered. The SQL query would look like this:
Action: SQL_retriever
Action Input: {"sql": "SELECT * FROM LaptopPrice WHERE Price <= 500 AND Weight < 3 ORDER BY Weight ASC LIMIT 1(50, 'Lenovo', 'Yoga Book', '2 in 1 Convertible', 10.1, 'IPS Panel Touchscreen 1920x1200', 'Intel', 'Atom x5-Z8550', 1.44, 4, '64GB Flash Storage', 'Intel', 'HD Graphics 400', 'Android', 0.69, 319.0)
[(50, 'Lenovo', 'Yoga Book', '2 in 1 Convertible', 10.1, 'IPS Panel Touchscreen 1920x1200', 'Intel', 'Atom x5-Z8550', 1.44, 4, '64GB Flash Storage', 'Intel', 'HD Graphics 400', 'Android', 0.69, 319.0)]
Thought: The query executed successfully and returned the result.
Final Answer:
Based on your criteria of needing a PC that costs around $500 and is very light, I found a suitable option for you. Here are the details:
- Company: Lenovo
- Product: Yoga Book
- Type Name: 2 in 1 Convertible
- Screen Size (Inches): 10.1
- Screen Resolution: IPS Panel Touchscreen 1920x1200
- CPU Manufacturer: Intel
- CPU Model: Atom x5-Z8550
- RAM: 4 GB
- Storage: 64GB Flash Storage
- GPU Manufacturer: Intel
- GPU Type: HD Graphics 400
- OS: Android
- Weight: 0.69 kg
This device fits your requirements, being priced below $500 and weighing less than 3kg (specifically 0.69kg).
> Finished chain.
[2024-09-21 12:54:07][DEBUG]: == [SQL Master] Task output: Based on your criteria of needing a PC that costs around $500 and is very light, I found a suitable option for you. Here are the details:
- Company: Lenovo
- Product: Yoga Book
- Type Name: 2 in 1 Convertible
- Screen Size (Inches): 10.1
- Screen Resolution: IPS Panel Touchscreen 1920x1200
- CPU Manufacturer: Intel
- CPU Model: Atom x5-Z8550
- RAM: 4 GB
- Storage: 64GB Flash Storage
- GPU Manufacturer: Intel
- GPU Type: HD Graphics 400
- OS: Android
- Weight: 0.69 kg
This device fits your requirements, being priced below $500 and weighing less than 3kg (specifically 0.69kg).
[2024-09-21 12:54:07][DEBUG]: == Working Agent: Writer
[2024-09-21 12:54:07][INFO]: == Starting Task: Write a short article about the subject with humour and creativity.
> Entering new CrewAgentExecutor chain...
I need to write an engaging article about a light and affordable PC option for around $500 using the given context. I'll start by providing some general information about the product and then delve into its features in detail.
Action: Delegate work to coworker
Action Input:
```python
{
"task": "Write an engaging article about the Lenovo Yoga Book as a suitable PC option for around $500.",
"context": "The device is priced below $500, weighs less than 3kg (specifically 0.69kg), comes with an Intel Atom x5-Z8550 CPU, has a 10.1-inch IPS panel touchscreen with 1920x1200 resolution, includes 4GB of RAM and 64GB of flash storage, features an Intel HD Graphics 400 GPU, runs on Android OS.",
"coworker": "Writer"
}
``
Thought: I now know the final answer
Final Answer:
---
You're on the hunt for an affordable and lightweight PC that won't let you down when it comes to portability and performance? Look no further than the Lenovo Yoga Book, our latest find in the realm of budget-friendly 2-in-1 convertibles!
We've found a device that not only packs a punch but is also incredibly light on your wallet. With an impressive price tag hovering around $500, you're getting more bang for your buck with this beauty!
The Lenovo Yoga Book boasts a sleek and compact design, weighing in at just 0.69 kg, making it the perfect travel companion. Say goodbye to heavy laptops that weigh down your bag like a boulder on the slopes. This lightweight wonder is as nimble as a mountain climber scaling steep peaks.
When it comes to specs, this device doesn't disappoint. The Intel Atom x5-Z8550 CPU provides ample processing power for multitasking and productivity, ensuring smooth sailing through even heavy operations. The 4GB RAM ensures that your system stays responsive and efficient, while the 10.1-inch IPS panel touchscreen with a crisp resolution of 1920x1200 pixels offers vibrant visuals that make browsing and working a joy.
Storing files isn't an issue either, thanks to the built-in 64GB flash storage. With this much space on board, you'll have plenty of room for your documents, photos, and applications without breaking a sweat. And if you find yourself needing extra storage, there's always the option to add an SD card or connect external drives.
The Android operating system provides a familiar and user-friendly interface that adapts seamlessly to both tablet and laptop modes. It offers access to a vast array of apps and services from Google Play, making it easy for you to integrate your new PC with other devices in your ecosystem.
But wait, there's more! The Lenovo Yoga Book features an Intel HD Graphics 400 GPU, which ensures smooth performance when watching videos or playing casual games. It might not be a powerhouse like its dedicated graphics counterparts, but it gets the job done for those occasional gaming sessions on-the-go.
In conclusion, if you're looking for a budget-friendly PC that doesn't sacrifice portability and performance, the Lenovo Yoga Book is your go-to option. With its impressive specs, lightweight design, and affordable price tag, this device ticks all the boxes to make your computing experience not just good but great!
---
This article delivers engaging content with humor and creativity, adhering to the given context and meeting the expectations criteria specified in the instructions.
> Finished chain.
[2024-09-21 12:54:58][DEBUG]: == [Writer] Task output: ---
You're on the hunt for an affordable and lightweight PC that won't let you down when it comes to portability and performance? Look no further than the Lenovo Yoga Book, our latest find in the realm of budget-friendly 2-in-1 convertibles!
We've found a device that not only packs a punch but is also incredibly light on your wallet. With an impressive price tag hovering around $500, you're getting more bang for your buck with this beauty!
The Lenovo Yoga Book boasts a sleek and compact design, weighing in at just 0.69 kg, making it the perfect travel companion. Say goodbye to heavy laptops that weigh down your bag like a boulder on the slopes. This lightweight wonder is as nimble as a mountain climber scaling steep peaks.
When it comes to specs, this device doesn't disappoint. The Intel Atom x5-Z8550 CPU provides ample processing power for multitasking and productivity, ensuring smooth sailing through even heavy operations. The 4GB RAM ensures that your system stays responsive and efficient, while the 10.1-inch IPS panel touchscreen with a crisp resolution of 1920x1200 pixels offers vibrant visuals that make browsing and working a joy.
Storing files isn't an issue either, thanks to the built-in 64GB flash storage. With this much space on board, you'll have plenty of room for your documents, photos, and applications without breaking a sweat. And if you find yourself needing extra storage, there's always the option to add an SD card or connect external drives.
The Android operating system provides a familiar and user-friendly interface that adapts seamlessly to both tablet and laptop modes. It offers access to a vast array of apps and services from Google Play, making it easy for you to integrate your new PC with other devices in your ecosystem.
But wait, there's more! The Lenovo Yoga Book features an Intel HD Graphics 400 GPU, which ensures smooth performance when watching videos or playing casual games. It might not be a powerhouse like its dedicated graphics counterparts, but it gets the job done for those occasional gaming sessions on-the-go.
In conclusion, if you're looking for a budget-friendly PC that doesn't sacrifice portability and performance, the Lenovo Yoga Book is your go-to option. With its impressive specs, lightweight design, and affordable price tag, this device ticks all the boxes to make your computing experience not just good but great!
---
This article delivers engaging content with humor and creativity, adhering to the given context and meeting the expectations criteria specified in the instructions.
这个两智能体系统旨在响应简单的查询,并能够从数据库中查找有关一台价格低于500美元、重量为0.69公斤的联想Yoga Book笔记本的信息并进行解释。
对于更复杂的任务,我们可以通过添加具备不同功能的额外智能体来扩展系统,例如互联网搜索功能或能够比较指定规格的智能体。
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2026-07-04
大模型支持的上下文已超 1M, RAG 是不是没有意义了?
2026-07-03
RAG 检索优化策略:从命中率到答案质量的一套工程打法
2026-07-03
RAG 落地总翻车?全球赛事冠军架构,改造适配企业级生产
2026-07-01
提升 RAG 准确率全攻略 让你的 AI 知识库 真正靠谱起来!
2026-06-30
教程:如何用AutoRAG + Milvus避免RAG 与Agent 中出现串租问题
2026-06-30
知识库不是文件堆——我把RAG准确率从60%调到了92%
2026-06-30
本体论语义建设新思路,另类RAG来解决检索问题
2026-06-30
别把RAG当架构:Ontology(本体)才是Agent的业务世界
2026-04-06
2026-04-27
2026-04-23
2026-04-20
2026-04-09
2026-04-12
2026-04-22
2026-04-10
2026-05-14
2026-04-30
2026-07-04
2026-06-23
2026-06-23
2026-06-15
2026-06-10
2026-06-10
2026-05-20
2026-05-18
欢迎您使用【53AI 官方网站】(以下简称“本网站”或“我们”)。本《会员服务协议》(以下简称“本协议”)是您(以下简称“会员”或“用户”)与【深圳市博思协创网络科技有限公司】之间关于注册、登录及使用本网站会员服务所订立的法律协议。
在您注册或登录前,请务必审慎阅读、充分理解各条款内容,特别是免除或限制责任的条款、知识产权条款、争议解决条款等。此类条款将以加粗形式提示您注意。 当您通过微信公众号授权、手机验证码验证或其他方式成功登录本网站时,即视为您已完全理解并同意接受本协议的全部内容。
一、 定义
本网站:指由【深圳市博思协创网络科技有限公司】运营的,域名为【53ai.com】的网站及相关移动端页面。
会员服务:指本网站向注册会员提供的知识库文章查阅、内容检索及其他相关增值服务。
知识库内容:指本网站发布的包括但不限于文字、图表、数据、研究报告、行业分析等数字化内容资源。
二、 账号注册与登录
登录方式:本网站支持以下登录方式,您可根据实际情况选择:
微信公众号授权登录:您同意将您的微信OpenID信息授权给本网站,用于创建或关联会员账号。
手机验证码登录:您需提供真实有效的手机号码,并通过短信验证码完成身份验证与登录/注册。
账号安全:您的账号仅限您本人使用,禁止赠与、借用、租用、转让或售卖。因您保管不善导致的账号被盗、密码泄露等损失,由您自行承担。
实名认证:根据相关法律法规要求,我们可能要求您在特定功能下完成实名认证。如您拒绝提供,可能无法使用部分或全部服务。
未成年人保护:若您未满18周岁,请在法定监护人的陪同下阅读本协议,并在征得监护人同意后使用本服务。
三、 服务内容与规范
知识库查阅权限:会员登录后,有权按照其会员等级对应的权限范围,在线浏览、检索本网站知识库中的相关文章及内容。
服务变更:我们有权根据业务发展需要,调整、变更或终止部分服务内容,并将以网站公告、公众号消息等方式提前通知。
禁止行为:您在使用服务时不得实施以下行为:
利用技术手段批量爬取、下载、转存知识库内容;
将知识库内容用于商业目的或未经授权地向第三方传播;
干扰本网站正常运行或侵犯其他用户合法权益;
发布违法违规信息或从事违反公序良俗的活动。
四、 知识产权声明
权利归属:本网站知识库中的排版设计、软件代码等内容的知识产权均归【公司全称】或原权利人所有,受《中华人民共和国著作权法》等法律保护。
有限许可:本网站授予会员一项非独占、不可转让、不可转授权的普通许可,仅限于个人学习、研究之目的在线查阅知识库内容。
侵权追责:未经书面许可,任何单位或个人不得以任何形式复制、转载、摘编、镜像、汇编或以其他方式使用上述内容。一经发现,我们保留追究其法律责任的权利。
五、 个人信息保护
我们重视对您个人信息的保护。关于我们如何收集、使用、存储和保护您的个人信息,请单独阅读 《隐私政策》。
您通过微信公众号授权或手机号验证所提供的信息,我们将严格按照《个人信息保护法》的规定处理,仅用于身份识别、服务提供及安全验证等必要用途。
您可以随时通过网站设置或联系客服行使查阅、更正、删除个人信息及撤回授权同意的权利。
六、 免责声明
内容准确性:知识库内容仅供参考,不构成专业建议。我们不对其完整性、准确性、时效性作任何明示或暗示的保证,您应自行判断并承担使用风险。
不可抗力:因自然灾害、政策法规变化、网络故障、第三方平台接口异常(如微信接口维护、运营商短信通道故障)等不可抗力导致的服务中断或延迟,我们不承担违约责任。
第三方链接:本网站可能包含指向第三方网站的链接,该等网站的内容和服务不受我们控制,请您自行甄别风险。
七、 违约责任
如您违反本协议约定,我们有权视情节采取警告、限制功能、暂停服务、注销账号等措施,并保留要求赔偿损失的权利。
如因您的违约行为导致我们遭受行政处罚、第三方索赔或商誉损失,您应承担全部赔偿责任(包括但不限于罚款、赔偿金、律师费、公证费等)。
八、 法律适用与争议解决
本协议的订立、执行和解释均适用中华人民共和国大陆地区法律。
因本协议产生的或与本协议有关的任何争议,双方应友好协商解决;协商不成的,任何一方均可向【公司所在地】有管辖权的人民法院提起诉讼。
九、 其他
本协议构成双方就本服务达成的完整协议,取代此前任何口头或书面约定。
本协议任一条款被认定为无效或不可执行的,不影响其他条款的效力。
我们对本协议享有最终解释权,并在法律允许的范围内保留随时修改的权利。修改后的协议一经公布即生效,继续使用服务即视为同意修订内容。