在这个项目中,我们的目标是创建一个由多智能体架构和本地大语言模型(LLM)驱动的个人笔记本电脑专家系统。该系统将使用一个SQL数据库,包含有关笔记本电脑的全面信息,包括价格、重量和规格。用户可以根据自己的特定需求向系统提问,获取推荐建议。
由LLM驱动的多智能体系统旨在自主或半自主地处理复杂问题,这些问题可能对单次LLM响应来说具有挑战性。在我们的案例中,这些系统特别适用于数据库搜索、跨品牌比较笔记本电脑的功能或选择适当的组件(如GPU)。通过使用多个专门的智能体,我们可以减少“幻觉”的可能性,并提高响应的准确性,特别是在处理需要访问特定数据源的任务时。
在我们的多智能体设置中,每个智能体都由LLM驱动,所有智能体可以使用相同的模型,也可以针对特定任务进行微调。每个智能体都有独特的描述、目标和必要的工具。例如:
-
搜索智能体可能可以访问查询SQL数据库的工具,或在网络上搜索最新信息。
-
比较智能体可能专门分析和比较笔记本电脑的规格。
-
撰写智能体将负责根据其他智能体收集的信息,撰写连贯且用户友好的响应。
这种方法使我们能够将复杂的查询分解为可管理的子任务,每个子任务由专门的智能体处理,从而为用户提供有关笔记本电脑的更准确和全面的响应。现在是时候展示将用于此项目的工具和框架了。
CrewAI
CrewAI是一个用于协调自主或半自主AI智能体合作完成复杂任务和问题的框架。它通过为多个智能体分配角色、目标和任务,使它们能够高效协作。该框架支持多种协作模型:
-
顺序模式:智能体按预定义的顺序完成任务,每个智能体在前一个智能体的工作基础上继续工作。
-
层次模式:一个指定的智能体或语言模型作为管理者,监督整个过程并将任务分配给其他智能体。
这些智能体可以进行沟通、共享信息,并协调它们的努力以实现整体目标。这种方法能够应对单个AI智能体难以处理的多方面挑战。虽然这些是主要的操作模式,但CrewAI团队正在积极开发更复杂的流程,以增强框架的能力和灵活性。
Ollama
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 作为自定义智能体工具
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
CrewAI 安装和设置智能体团队
让我们定义我们的智能体及其角色。我们将为此任务设置两个智能体:
-
“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 agentllm=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 kgThis 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 kgThis 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 coworkerAction 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 answerFinal 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笔记本的信息并进行解释。
对于更复杂的任务,我们可以通过添加具备不同功能的额外智能体来扩展系统,例如互联网搜索功能或能够比较指定规格的智能体。