在当今快节奏的商业环境中,组织被驱动决策,优化运营并保持竞争力的数据所淹没。但是,从这些数据中提取可行的见解仍然是一个重大障碍。与代理AI集成时,检索功能增强的一代(RAG)系统不仅可以通过检索相关信息,还可以实时处理和交付上下文感知的见解来应对这一挑战。这种组合允许企业创建智能代理,以自主查询数据集,适应和提取有关产品功能,集成和操作的见解。
>通过将抹布与代理AI合并,企业可以增强决策并将分散的数据转换为有价值的智能。该博客探讨了使用Agentic AI构建RAG管道的过程,提供技术见解和代码示例,以增强组织中明智的决策能力。
>本文是> > data Science Blogathon的一部分。内容表
>为公司情报构建强大的抹布系统的第一步是收集必要的数据。由于数据可能来自各种网络来源,因此有效地刮擦和组织它是关键。发现和收集相关页面的一种有效技术是广度优先搜索(BFS)。 BFS帮助我们递归地发现从主页开始的链接,从而逐渐将搜索扩展到更深的级别。这样可以确保我们收集所有相关页面,而不会用不必要的数据压倒系统。
在本节中,我们将研究如何使用BFS从网站提取链接,然后将这些页面的内容删除。使用BFS,我们会系统地遍历网站,收集数据并创建一个有意义的数据集,用于在RAG管道中处理。 下面的代码使用BFS从启动URL中进行链接提取。它首先获取主页,提取所有链接(< a>带有HREF属性的标签),然后遵循这些链接到后续页面,递归根据给定深度限制。
>我们保持队列以跟踪访问的URL及其相应的深度,以确保有效的遍历。访问的集合用于防止多次重新访问相同的URL。对于每个URL,我们使用BeautifulSoup来解析HTML并提取所有链接(带有HREF属性的标签)。该过程使用BFS遍历,递归获取每个URL的内容,提取链接并进一步探索直到达到深度极限。这种方法可确保我们在没有冗余的情况下有效地探索网络。 >使用BFS提取相关链接后,下一步就是从这些页面中刮擦内容。我们将寻找关键信息,例如产品功能,集成和任何其他相关数据,这些数据将帮助我们为RAG系统构建结构化数据集。 >
>
>步骤1:加载刮擦数据 >此过程的第一步是将原始的刮擦内容加载到我们的系统中。正如我们之前看到的,刮擦数据以JSON格式存储,每个条目都包含一个URL和相关内容。我们需要确保此数据以适当的格式供AI处理。
>在这里,我们使用Python的内置JSON库将整个数据集加载到存储器中。数据集中的每个条目都包含源的URL和text_content字段,该字段容纳了原始的刮擦文本。此内容是我们将在接下来的一步中处理的内容。 接下来,我们通过数据集迭代以提取每个条目的相关text_content。这样可以确保我们仅处理包含必要内容的有效条目。跳过无效或不完整的条目以维持该过程的完整性。
代码代码片段:
代码代码片段:
>在这里,该代码启动了对GROQ的API调用,将Input_Text和说明作为消息有效负载的一部分发送。系统消息指示执行确切任务的AI模型,而用户消息则提供要处理的内容。我们使用温度,max_tokens和top_p参数来控制生成的输出的随机性和长度。
此代码段将每个块的内容串联到> pm_points 变量,从而产生完整的结构化洞察集。它以利益相关者可以轻松消费或用于进一步分析的格式提取这些见解。以下是原始代码的输出屏幕截图,并掩盖了敏感信息以保持完整性。
AI处理内容并返回结构化的见解后,最后一步是保存此数据以供以后使用。我们将结构化结果写回JSON文件,以确保每个条目都有自己的已处理信息存储以进行进一步分析。
>该代码可有效存储处理后的数据,并允许以后轻松访问。它及其各自的结构化点保存了每个条目,从而简单地检索提取的信息并分析。
>输出
在上一节中,我们专注于从网页中提取数据,并将其转换为JSON等结构化格式。我们还实施了提取和清洁相关数据的技术,从而使我们能够生成一个可以进行更深入分析的数据集。
开始,让我们安装抹布管道的所有必要依赖项: >此外,我们使用句子转换器来生成具有预训练的变压器模型的文本嵌入,从而实现有效的文档处理和检索。 步骤2:加载提取的数据 。
>现在我们有了原始数据,我们使用recursivecharactertextsplitter将文档分解为较小的块。这样可以确保没有单个块超过语言模型的令牌限制。 现在,我们使用sencencetransformer将每个文本块转换为嵌入式。这些嵌入代表了高维矢量空间中文本的含义,这对于以后搜索和检索相关文档很有用。
>现在我们配置了猎犬。该组件根据用户查询搜索最相关的文本块。它将最类似的文档块检索到查询中。 接下来,我们创建一个提示模板,该模板将格式化语言模型的输入。该模板既包含上下文(检索到的块)和用户的查询,从而指导模型以仅基于提供的上下文生成答案。
> stroutputparser用于清洁和格式化模型的输出为字符串格式。
>部署和缩放 这可以通过利用分布式向量存储和优化检索过程来实现,从而确保管道在大量使用下保持响应迅速和快速。借助正确的基础架构和优化技术,代理平台可以增长以支持大规模运营,实现实时见解并保持公司情报的竞争优势。 结论 这种可扩展的解决方案随您的需求而生长,处理复杂的查询和较大的数据集,同时保持准确性。借助正确的基础设施,这个AI驱动的平台成为了更智能运营的基石,使组织能够利用其数据,保持竞争力并通过组织中的智能决策来推动创新。
常见问题
,使企业能够提取实时,可行的见解。为什么数据刮擦对于公司情报很重要?数据刮擦有助于通过从多个来源收集和构造有价值的信息来建立强大的公司智能 >本文所示的媒体不归Analytics Vidhya拥有,并由作者的酌情决定使用。
import requests
from bs4 import BeautifulSoup
from collections import deque
# Function to extract links using BFS
def bfs_link_extraction(start_url, max_depth=3):
visited = set() # To track visited links
queue = deque([(start_url, 0)]) # Queue to store URLs and current depth
all_links = []
while queue:
url, depth = queue.popleft()
if depth > max_depth:
continue
# Fetch the content of the URL
try:
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
# Extract all links in the page
links = soup.find_all('a', href=True)
for link in links:
full_url = link['href']
if full_url.startswith('http') and full_url not in visited:
visited.add(full_url)
queue.append((full_url, depth + 1))
all_links.append(full_url)
except requests.exceptions.RequestException as e:
print(f"Error fetching {url}: {e}")
return all_links
# Start the BFS from the homepage
start_url = 'https://www.example.com' # Replace with the actual homepage URL
all_extracted_links = bfs_link_extraction(start_url)
print(f"Extracted {len(all_extracted_links)} links.")
使用BFS提取数据并刮擦数据步骤1:使用BFS
链接提取
为了开始,我们需要从给定网站收集所有相关链接。使用BFS,我们可以探索主页上的链接,然后从那里遵循其他页面上的链接,直到指定的深度。此方法可确保我们捕获所有可能包含相关公司数据的必要页面,例如产品功能,集成或其他关键细节。
Extracted 1500 links.
>步骤2:从提取的链接中刮擦数据
import requests
from bs4 import BeautifulSoup
from collections import deque
# Function to extract links using BFS
def bfs_link_extraction(start_url, max_depth=3):
visited = set() # To track visited links
queue = deque([(start_url, 0)]) # Queue to store URLs and current depth
all_links = []
while queue:
url, depth = queue.popleft()
if depth > max_depth:
continue
# Fetch the content of the URL
try:
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
# Extract all links in the page
links = soup.find_all('a', href=True)
for link in links:
full_url = link['href']
if full_url.startswith('http') and full_url not in visited:
visited.add(full_url)
queue.append((full_url, depth + 1))
all_links.append(full_url)
except requests.exceptions.RequestException as e:
print(f"Error fetching {url}: {e}")
return all_links
# Start the BFS from the homepage
start_url = 'https://www.example.com' # Replace with the actual homepage URL
all_extracted_links = bfs_link_extraction(start_url)
print(f"Extracted {len(all_extracted_links)} links.")
>对于列表中的每个URL,我们发送HTTP请求以获取页面的内容,并使用BeautifulSoup解析其提取标题和主要内容。我们将提取的数据存储在字典列表中,每个词典都包含URL,标题和内容。最后,我们将刮擦数据保存到JSON文件中,以确保可以在RAG管道中进行以后的处理。此过程可确保有效收集和存储相关数据以进一步使用。
>该代码的输出将是一个保存的JSON文件(Scraped_data.json),其中包含来自链接的刮擦数据。数据结构的一个示例可能是这样的:Extracted 1500 links.
此JSON文件包含我们刮擦的每个页面的URL,标题和内容。现在可以将这些结构化数据用于进一步处理,例如在抹布系统中嵌入生成和提问。以下是原始代码的输出屏幕截图。敏感信息已被掩盖以维持完整性。
用AI代理自动化信息提取
在本节中,我们关注代理AI如何从刮擦数据中提取相关的产品信息,以确保其已准备好利益相关者的消费。我们将分解涉及的关键步骤,从加载数据到处理数据,最后以结构化格式保存结果。
代码代码片段:
import requests
from bs4 import BeautifulSoup
from collections import deque
# Function to extract links using BFS
def bfs_link_extraction(start_url, max_depth=3):
visited = set() # To track visited links
queue = deque([(start_url, 0)]) # Queue to store URLs and current depth
all_links = []
while queue:
url, depth = queue.popleft()
if depth > max_depth:
continue
# Fetch the content of the URL
try:
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
# Extract all links in the page
links = soup.find_all('a', href=True)
for link in links:
full_url = link['href']
if full_url.startswith('http') and full_url not in visited:
visited.add(full_url)
queue.append((full_url, depth + 1))
all_links.append(full_url)
except requests.exceptions.RequestException as e:
print(f"Error fetching {url}: {e}")
return all_links
# Start the BFS from the homepage
start_url = 'https://www.example.com' # Replace with the actual homepage URL
all_extracted_links = bfs_link_extraction(start_url)
print(f"Extracted {len(all_extracted_links)} links.")
>步骤2:提取原始文本内容
Extracted 1500 links.
步骤3:将数据发送给AI代理进行处理
import json
# Function to scrape and extract data from the URLs
def scrape_data_from_links(links):
scraped_data = []
for link in links:
try:
response = requests.get(link)
soup = BeautifulSoup(response.content, 'html.parser')
# Example: Extract 'title' and 'content' (modify according to your needs)
title = soup.find('title').get_text()
content = soup.find('div', class_='content').get_text() # Adjust selector
# Store the extracted data
scraped_data.append({
'url': link,
'title': title,
'content': content
})
except requests.exceptions.RequestException as e:
print(f"Error scraping {link}: {e}")
return scraped_data
# Scrape data from the extracted links
scraped_contents = scrape_data_from_links(all_extracted_links)
# Save scraped data to a JSON file
with open('/content/scraped_data.json', 'w') as outfile:
json.dump(scraped_contents, outfile, indent=4)
print("Data scraping complete.")
API调用配置:指定要使用的模型。在这种情况下,选择语言模型以确保它可以处理文本数据并生成响应。
import requests
from bs4 import BeautifulSoup
from collections import deque
# Function to extract links using BFS
def bfs_link_extraction(start_url, max_depth=3):
visited = set() # To track visited links
queue = deque([(start_url, 0)]) # Queue to store URLs and current depth
all_links = []
while queue:
url, depth = queue.popleft()
if depth > max_depth:
continue
# Fetch the content of the URL
try:
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
# Extract all links in the page
links = soup.find_all('a', href=True)
for link in links:
full_url = link['href']
if full_url.startswith('http') and full_url not in visited:
visited.add(full_url)
queue.append((full_url, depth + 1))
all_links.append(full_url)
except requests.exceptions.RequestException as e:
print(f"Error fetching {url}: {e}")
return all_links
# Start the BFS from the homepage
start_url = 'https://www.example.com' # Replace with the actual homepage URL
all_extracted_links = bfs_link_extraction(start_url)
print(f"Extracted {len(all_extracted_links)} links.")
在处理时,总是有可能遇到错误的可能性,例如不完整的内容或网络问题。通过使用错误处理机制,我们确保该过程对于所有有效条目都可以顺利进行。
代码代码片段:Extracted 1500 links.
步骤6:保存处理的数据代码代码片段:
import json
# Function to scrape and extract data from the URLs
def scrape_data_from_links(links):
scraped_data = []
for link in links:
try:
response = requests.get(link)
soup = BeautifulSoup(response.content, 'html.parser')
# Example: Extract 'title' and 'content' (modify according to your needs)
title = soup.find('title').get_text()
content = soup.find('div', class_='content').get_text() # Adjust selector
# Store the extracted data
scraped_data.append({
'url': link,
'title': title,
'content': content
})
except requests.exceptions.RequestException as e:
print(f"Error scraping {link}: {e}")
return scraped_data
# Scrape data from the extracted links
scraped_contents = scrape_data_from_links(all_extracted_links)
# Save scraped data to a JSON file
with open('/content/scraped_data.json', 'w') as outfile:
json.dump(scraped_contents, outfile, indent=4)
print("Data scraping complete.")
>
[
{
"url": "https://www.example.com/page1",
"title": "Page 1 Title",
"content": "This is the content of the first page. It contains
information about integrations and features."
},
{
"url": "https://www.example.com/page2",
"title": "Page 2 Title",
"content": "Here we describe the functionalities of the product.
It includes various use cases and capabilities."
}
]
>检索增强的生成管道实施>步骤1:设置环境
import requests
from bs4 import BeautifulSoup
from collections import deque
# Function to extract links using BFS
def bfs_link_extraction(start_url, max_depth=3):
visited = set() # To track visited links
queue = deque([(start_url, 0)]) # Queue to store URLs and current depth
all_links = []
while queue:
url, depth = queue.popleft()
if depth > max_depth:
continue
# Fetch the content of the URL
try:
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
# Extract all links in the page
links = soup.find_all('a', href=True)
for link in links:
full_url = link['href']
if full_url.startswith('http') and full_url not in visited:
visited.add(full_url)
queue.append((full_url, depth + 1))
all_links.append(full_url)
except requests.exceptions.RequestException as e:
print(f"Error fetching {url}: {e}")
return all_links
# Start the BFS from the homepage
start_url = 'https://www.example.com' # Replace with the actual homepage URL
all_extracted_links = bfs_link_extraction(start_url)
print(f"Extracted {len(all_extracted_links)} links.")
这些软件包对于在Langchain中集成文档处理,矢量化和OpenAI模型至关重要。 JQ是轻量级的JSON处理器,而Langchain则是构建语言模型管道的核心框架。 Langchain-Openai促进了诸如GPT之类的OpenAI模型的整合,Langchain-Chroma提供了基于色度的矢量商店,用于管理文档嵌入。
现在,我们将加载使用JSONLOADER在上一节中提取和处理的结构化数据。例如,这些数据本可以从网页中刮掉为结构化的JSON,其中键值对与特定主题或问题有关。
在此步骤中,已加载了先前提取的数据(可能包含产品功能,集成和功能)以进行进一步处理。Extracted 1500 links.
步骤4:生成文档块的嵌入
import json
# Function to scrape and extract data from the URLs
def scrape_data_from_links(links):
scraped_data = []
for link in links:
try:
response = requests.get(link)
soup = BeautifulSoup(response.content, 'html.parser')
# Example: Extract 'title' and 'content' (modify according to your needs)
title = soup.find('title').get_text()
content = soup.find('div', class_='content').get_text() # Adjust selector
# Store the extracted data
scraped_data.append({
'url': link,
'title': title,
'content': content
})
except requests.exceptions.RequestException as e:
print(f"Error scraping {link}: {e}")
return scraped_data
# Scrape data from the extracted links
scraped_contents = scrape_data_from_links(all_extracted_links)
# Save scraped data to a JSON file
with open('/content/scraped_data.json', 'w') as outfile:
json.dump(scraped_contents, outfile, indent=4)
print("Data scraping complete.")
> sencencetransformer用于生成文本块的嵌入,创建捕获语义信息的密集矢量表示。函数embed_documents处理多个文档并返回其嵌入,而Embed_query生成了用户查询的嵌入式。 Chroma是一个矢量存储,管理这些嵌入并根据相似性实现有效的检索,从而允许快速准确的文档或查询匹配。>步骤5:设置猎犬
import requests
from bs4 import BeautifulSoup
from collections import deque
# Function to extract links using BFS
def bfs_link_extraction(start_url, max_depth=3):
visited = set() # To track visited links
queue = deque([(start_url, 0)]) # Queue to store URLs and current depth
all_links = []
while queue:
url, depth = queue.popleft()
if depth > max_depth:
continue
# Fetch the content of the URL
try:
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
# Extract all links in the page
links = soup.find_all('a', href=True)
for link in links:
full_url = link['href']
if full_url.startswith('http') and full_url not in visited:
visited.add(full_url)
queue.append((full_url, depth + 1))
all_links.append(full_url)
except requests.exceptions.RequestException as e:
print(f"Error fetching {url}: {e}")
return all_links
# Start the BFS from the homepage
start_url = 'https://www.example.com' # Replace with the actual homepage URL
all_extracted_links = bfs_link_extraction(start_url)
print(f"Extracted {len(all_extracted_links)} links.")
>步骤6:创建提示模板
Extracted 1500 links.
>步骤7:设置语言模型
在此步骤中,我们初始化OpenAI GPT模型。该模型将根据猎犬提供的结构化上下文生成答案。
>
import json
# Function to scrape and extract data from the URLs
def scrape_data_from_links(links):
scraped_data = []
for link in links:
try:
response = requests.get(link)
soup = BeautifulSoup(response.content, 'html.parser')
# Example: Extract 'title' and 'content' (modify according to your needs)
title = soup.find('title').get_text()
content = soup.find('div', class_='content').get_text() # Adjust selector
# Store the extracted data
scraped_data.append({
'url': link,
'title': title,
'content': content
})
except requests.exceptions.RequestException as e:
print(f"Error scraping {link}: {e}")
return scraped_data
# Scrape data from the extracted links
scraped_contents = scrape_data_from_links(all_extracted_links)
# Save scraped data to a JSON file
with open('/content/scraped_data.json', 'w') as outfile:
json.dump(scraped_contents, outfile, indent=4)
print("Data scraping complete.")
我们初始化Chatopenai模型,该模型将处理提示并生成答案。>我们使用较小的模型“ GPT-4O-Mini”进行有效的处理,尽管较大的模型可以用于更复杂的任务。
> RunnablePassThrough确保查询直接传递到提示。
[
{
"url": "https://www.example.com/page1",
"title": "Page 1 Title",
"content": "This is the content of the first page. It contains
information about integrations and features."
},
{
"url": "https://www.example.com/page2",
"title": "Page 2 Title",
"content": "Here we describe the functionalities of the product.
It includes various use cases and capabilities."
}
]
>步骤9:测试抹布管道
对于每个查询,模型都会处理检索到的上下文,并提供了基于上下文的答案。
下面是原始代码的抹布输出的屏幕截图。敏感信息已被掩盖以维持完整性。import json
# Load the scraped JSON file containing the web data
with open('/content/scraped_contents_zluri_all_links.json', 'r') as file:
data = json.load(file)
通过结合Web刮擦,数据提取和高级检索功能(RAG)技术,我们为公司智能创建了一个强大而可扩展的框架。提取链接和刮擦数据的第一步确保我们从网络中收集相关和最新信息。第二部分重点是查明与产品相关的特定细节,从而更容易对数据进行分类和处理数据。
>一旦建立了公司情报系统,下一步就是部署和扩展其供生产使用。您可以在AWS或GCP等云平台上部署该系统以灵活性和可扩展性,或者如果数据隐私为优先级,则选择本地解决方案。为了使系统更加用户友好,请考虑构建一个简单的API或UI,允许用户与平台进行交互并毫不费力地检索见解。随着系统开始处理较大的数据集和更高的查询负载,必须有效扩展。
钥匙要点
奖励: 1。 在此设置中使用检索增强生成(RAG)的目的是什么? RAG通过将信息检索与生成AI相结合,增强了AI模型提供上下文感知响应的能力。它可以对大型数据集进行更明智的查询,从而更容易检索精确的,相关的答案,而不仅仅是执行基本的关键字搜索。
q 2。 需要哪些工具和库来构建博客中描述的系统?使用的主要工具和库包括Python,用于网络刮擦的美丽套件,用于管理文档检索的Langchain,用于自然语言处理的OpenAI模型以及用于存储矢量化文档的Chroma。这些组件共同创建一个全面的公司情报平台。 指针提取过程如何在此系统中起作用?指针提取涉及从刮擦内容中识别特定信息,例如产品功能,集成和故障排除提示。数据是使用及时驱动系统处理的,该系统将信息组织到结构化的,可行的见解中。这是通过AI模型和自定义提示的组合来实现的。
以上是在组织中建立用于智能决策的破布系统的详细内容。更多信息请关注PHP中文网其他相关文章!