Sign In
Free Sign Up
  • English
  • Español
  • 简体中文
  • Deutsch
  • 日本語
Sign In
Free Sign Up
  • English
  • Español
  • 简体中文
  • Deutsch
  • 日本語

使用Claude 3和Hugging Face在10分钟内构建RAG应用程序

在人工智能快速发展的浪潮中,基于检索增强生成 (RAG) 的应用正成为各行业数字化转型的利器。RAG 应用结合了检索和生成模型的优势,赋予企业强大的数据分析和预测能力,为业务增长注入新动能。

不同于传统的 AI 应用,RAG 通过检索模型快速访问并筛选海量数据,精准定位用户所需信息,再利用生成模型将信息整合、转化为易于理解的文本或其他形式输出。这一过程极大简化了信息检索 (opens new window)流程,让研究人员能够高效获取跨领域的知识,为决策提供有力支持。

越来越多的企业开始利用 RAG 应用程序来挖掘 AI 潜能。例如,通过分析海量客户数据,RAG 应用可以识别消费趋势、预测用户行为,进而提供个性化推荐,提升用户体验。此外,RAG 架构允许应用直接访问数据库,确保生成式 AI (opens new window) 模型能够根据最新信息生成精准、有效的答案,满足用户查询需求。

为了进一步提升 RAG 应用的性能和效率,开发者可以采用嵌入量化 (opens new window)和检索增强微调 (RAFT) 等技术对大型语言模型进行优化,使其更具鲁棒性,并适应不同的应用场景。

# Claude 3: 助力 RAG 应用性能提升

Anthropic 推出的 Claude 3 (opens new window) 系列大型语言模型为构建高性能 RAG 应用提供了强大的支持。Claude 3 包括 Haiku、Sonnet 和 Opus 三个模型,分别针对不同规模和复杂度的任务需求进行优化。该系列模型以其卓越的准确性、上下文理解能力和安全性著称,为 RAG 应用注入新的活力。

Claude 3 模型的一大亮点在于其超强的上下文处理能力。其中,Opus 模型能够处理高达 200,000 个标记的上下文窗口,远超其他竞争对手,使其能够有效处理长文本输入,例如研究论文、法律文件等,这对于需要分析大量信息的 RAG 应用至关重要。

LLM Benchmark

除了强大的性能,Claude 3 还注重道德和安全。Anthropic 在模型训练过程中采取了多项措施,旨在减少偏见、保护用户隐私,并确保模型能够对复杂问题提供准确、可靠的答案。此外,Claude 3 支持文本和图像等多种输入形式,为内容创作、数据分析等应用场景提供了更多可能性。

# 为什么选择 Hugging Face

Hugging Face (opens new window) 凭借其丰富的模型库、易用的工具和活跃的社区,成为构建 RAG 应用的理想平台。对于希望快速搭建高效 RAG 系统的开发者而言,Hugging Face 提供了以下优势:

  • 海量预训练模型 (opens new window): Hugging Face 平台汇集了大量针对不同自然语言处理任务优化的预训练模型,涵盖了密集检索、序列到序列生成等检索增强生成(RAG) (opens new window) 应用所需的关键组件。开发者可以轻松找到适合自身需求的模型,并快速进行实验和部署。
  • 便捷的开发工具: Hugging Face 提供了 LangChain、Ray 等工具,帮助开发者构建可扩展、高性能的 RAG 系统。这些工具简化了模型集成、微调和部署流程,即使是不熟悉 RAG 的开发者也能快速上手。
  • 丰富的 Embedding 模型选择: Embedding 模型是 RAG 系统中信息检索的关键组件。Hugging Face 提供了多种高质量的 Embedding 模型,能够将文本转换为向量表示,用于计算文本之间的语义相似度,从而实现精准的信息检索。

在本博客中,我们将重点介绍如何利用 Hugging Face 的 Embedding 模型构建 RAG 应用。这些模型能够生成高质量的文本嵌入,确保系统检索到与用户查询最相关的上下文信息,从而提高生成答案的准确性和效率。

Boost Your AI App Efficiency now
Sign up for free to benefit from 150+ QPS with 5,000,000 vectors
Free Trial
Explore our product

# 使用 Claude 3和 Hugging Face 构建 RAG 应用

现在,让我们着手构建一个基于 Claude 3 和 Hugging Face 嵌入模型的 RAG 应用。我们将逐步介绍设置环境、加载文档、创建嵌入和使用 Claude 3 模型从检索到的文档中生成答案的步骤。

# 设置环境

首先,我们需要安装构建 RAG 应用所需的 Python 库。请取消下方代码注释并运行,安装 langchainsentence-transformersanthropic 软件包。如果你的系统中已安装这些库,则可以跳过此步骤。

pip install langchain sentence-transformers anthropic

# 设置环境变量

接下来,我们需要设置连接 MyScale 向量数据库和 Claude 3 API 所需的环境变量。在本示例中,我们将使用 MyScaleDB (opens new window) 作为向量数据库,因为它具备高性能、可扩展性和高效的数据检索能力。

import os
# 设置向量数据库连接
os.environ["MYSCALE_HOST"] = "your-host-name"
os.environ["MYSCALE_PORT"] = "443"
os.environ["MYSCALE_USERNAME"] = "your-user-name"
os.environ["MYSCALE_PASSWORD"] = "your-password-here"

# 设置 Claude 3 的 API 密钥
os.environ["ANTHROPIC_API_KEY"] = "your-claude-api-key-here"

注意:

# 导入必要的库

现在,我们导入所需的库,用于加载文档、拆分、嵌入和与 Claude 3 模型交互。

# 用于将文档拆分成块
from langchain.text_splitter import RecursiveCharacterTextSplitter
# 用于将MyScale用作向量数据库
from langchain_community.vectorstores import MyScale
# 用于使用Hugging Face进行嵌入
from langchain_huggingface import HuggingFaceEmbeddings
# 用于加载维基百科页面
from langchain_community.document_loaders.wikipedia import WikipediaLoader

import anthropic

# 初始化 Claude 3 客户端

接下来, 我们来初始化 anthropic 库中的 Claude 3 客户端,以便与 Claude 3 API 进行通信:

client = anthropic.Anthropic()

# 加载文档

在本示例中,我们将使用WikipediaLoader从维基百科加载与"Fifa"相关的文档。

loader = WikipediaLoader(query="Fifa")

# 加载文档
docs = loader.load()

# 拆分文档

我们使用RecursiveCharacterTextSplitter将加载的文档拆分成可管理的块。

character_splitter = RecursiveCharacterTextSplitter(
    chunk_size=1000,
    chunk_overlap=200
)
docs = character_splitter.split_documents(docs)

这样可以确保将文档拆分为1000个字符的块,并且重叠部分为200个字符。

# 创建嵌入

接下来,我们使用 Hugging Face Embedding 模型为文档块创建嵌入,并将其添加到我们的 MyScale 向量数据库中储存。

embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-base-en-v1.5")
docsearch = MyScale(embedding_function=embeddings.embed_documents)
docsearch.add_documents(docs)

# 测试文档搜索

我们需要测试文档搜索功能,以确保创建的嵌入和向量存储正常工作。

query = "Who won fifa Fifa 2022?"
docs = docsearch.similarity_search(query, 3)
docs

这将对查询“Who won Fifa 2022?”执行相似性搜索,并检索出最相关的 3 个文档。它将返回类似以下的文档:

[Document(page_content="The FIFA World Cup, often called the World Cup, is an international association football competition among the senior men's national teams of the members of the Fédération Internationale de Football Association (FIFA), the sport's global governing body. The tournament has been held every four years since the inaugural tournament in 1930, with the exception of 1942 and 1946 due to the Second World War. The reigning champions are Argentina, who won their third title at the 2022 tournament.\\nThe contest starts with the qualification phase, which takes place over the preceding three years to determine which teams qualify for the tournament phase. In the tournament phase, 32 teams compete for the title at venues within the host nation(s) over the course of about a month. The host nation(s) automatically qualify for the group stage of the tournament. The competition is scheduled to expand to 48 teams, starting with the 2026 tournament.", metadata={'source': '<https://en.wikipedia.org/wiki/FIFA_World_Cup>', 'summary': "The FIFA World Cup, often called the World Cup, is an international association football competition among the senior men's national teams of the members of the Fédération Internationale de Football Association (FIFA), the sport's global governing body. The tournament has been held every four years since the inaugural tournament in 1930, with the exception of 1942 and 1946 due to the Second World War. The reigning champions are Argentina, who won their third title at the 2022 tournament.\\nThe contest starts with the qualification phase, which takes place over the preceding three years to determine which teams qualify for the tournament phase. In the tournament phase, 32 teams compete for the title at venues within the host nation(s) over the course of about a month. The host nation(s) automatically qualify for the group stage of the tournament. The competition is scheduled to expand to 48 teams, starting with the 2026 tournament.\\nAs of the 2022 FIFA World Cup, 22 final tournaments have been held since the event's inception in 1930, and a total of 80 national teams have competed. The trophy has been won by eight national teams. With five wins, Brazil  is the only team to have played in every tournament. The other World Cup winners are Germany and Italy, with four titles each; Argentina, with three titles; France and inaugural winner Uruguay, each with two titles; and England and Spain, with one title each.\\nThe World Cup is the most prestigious association football tournament in the world, as well as the most widely viewed and followed single sporting event in the world. The viewership of the 2018 World Cup was estimated to be 3.57 billion, close to half of the global population, while the engagement with the 2022 World Cup was estimated to be 5 billion, with about 1.5 billion people watching the final match.\\nSeventeen countries have hosted the World Cup, most recently Qatar, who hosted the 2022 event. The 2026 tournament will be jointly hosted by Canada, the United States and Mexico, which will give Mexico the distinction of being the first country to host games in three World Cups.\\n\\n", 'title': 'FIFA World Cup'}),
 Document(page_content="The 2026 FIFA World Cup, marketed as FIFA World Cup 26, will be the 23rd FIFA World Cup, the quadrennial international men's soccer championship contested by the national teams of the member associations of FIFA. The tournament will take place from June 11 to July 19, 2026. It will be jointly hosted by 16 cities in three North American countries: Canada, Mexico, and the United States. The tournament will be the first hosted by three nations and the first North American World Cup since 1994. Argentina is the defending champion.", metadata={'source': '<https://en.wikipedia.org/wiki/2026_FIFA_World_Cup>', 'summary': "The 2026 FIFA World Cup, marketed as FIFA World Cup 26, will be the 23rd FIFA World Cup, the quadrennial international men's soccer championship contested by the national teams of the member associations of FIFA. The tournament will take place from June 11 to July 19, 2026. It will be jointly hosted by 16 cities in three North American countries: Canada, Mexico, and the United States. The tournament will be the first hosted by three nations and the first North American World Cup since 1994. Argentina is the defending champion.\\nThis tournament will be the first to include 48 teams, expanded from 32. The United 2026 bid beat a rival bid by Morocco during a final vote at the 68th FIFA Congress in Moscow. It will be the first World Cup since 2002 to be hosted by more than one nation. With its past hosting of the 1970 and 1986 tournaments, Mexico will become the first country to host or co-host the men's World Cup three times. The United States last hosted the men's World Cup in 1994, whereas it will be Canada's first time hosting or co-hosting the men's tournament. The event will also return to its traditional northern summer schedule after the 2022 edition in Qatar was held in November and December.", 'title': '2026 FIFA World Cup'}),
 Document(page_content="The 2022 FIFA World Cup was the 22nd FIFA World Cup, the world championship for national football teams organized by FIFA. It took place in Qatar from 20 November to 18 December 2022, after the country was awarded the hosting rights in 2010. It was the first World Cup to be held in the Arab world and Muslim world, and the second held entirely in Asia after the 2002 tournament in South Korea and Japan.\\nThis tournament was the last with 32 participating teams, with the number of teams being increased to 48 for the 2026 edition. To avoid the extremes of Qatar's hot climate, the event was held in November and December instead of during the traditional months of May, June, or July. It was held over a reduced time frame of 29 days with 64 matches played in eight venues across five cities. Qatar entered the event—their first World Cup—automatically as the host's national team, alongside 31 teams determined by the qualification process.", metadata={'source': '<https://en.wikipedia.org/wiki/2022_FIFA_World_Cup>', 'summary': "The 2022 FIFA World Cup was the 22nd FIFA World Cup, the world championship for national football teams organized by FIFA. It took place in Qatar from 20 November to 18 December 2022, after the country was awarded the hosting rights in 2010. It was the first World Cup to be held in the Arab world and Muslim world, and the second held entirely in Asia after the 2002 tournament in South Korea and Japan.\\nThis tournament was the last with 32 participating teams, with the number of teams being increased to 48 for the 2026 edition. To avoid the extremes of Qatar's hot climate, the event was held in November and December instead of during the traditional months of May, June, or July. It was held over a reduced time frame of 29 days with 64 matches played in eight venues across five cities. Qatar entered the event—their first World Cup—automatically as the host's national team, alongside 31 teams determined by the qualification process.\\nArgentina were crowned the champions after winning the final against the title holder France 4–2 on penalties following a 3–3 draw after extra time. It was Argentina's third title and their first since 1986, as well as being the first nation from outside of Europe to win the tournament since 2002. French player Kylian Mbappé became the first player to score a hat-trick in a World Cup final since Geoff Hurst in the 1966 final and won the Golden Boot as he scored the most goals (eight) during the tournament. Mbappé also became the first player to score in two consecutive finals since Vavá of Brazil did the same in 1958 and 1962. Argentine captain Lionel Messi was voted the tournament's best player, winning the Golden Ball. The tournament has been considered exceptionally poetic as the capstone of his career, for some commentators fulfilling a previously unmet criterion to be regarded as one of the greatest players of all time. Teammates Emiliano Martínez and Enzo Fernández won the Golden Glove, awarded to the tournament's best goalkeeper; and the Young Player Award, awarded to the tournament's best young player, respectively. With 172 goals, the tournament set a record for the highest number of goals scored in the 32-team format, with every participating team scoring at least one goal. Morocco became the first African nation to top Group stages with 7 points.\\nThe choice to host the World Cup in Qatar attracted significant criticism, with concerns raised over the country's treatment of migrant workers, women, and members of the LGBT community, as well as Qatar's climate, lack of a strong football culture, scheduling changes, and allegations of bribery for hosting rights and wider FIFA corruption.", 'title': '2022 FIFA World Cup'})]

现在,下一步是使用 Claude 3 模型从检索到的上下文中获取所需的结果。

# 准备查询上下文

我们为 Claude 3 模型准备从检索到的文档中的上下文,将检索到的文档的内容连接成一个字符串。

stre = "".join(doc.page_content for doc in docs)

# 设置模型

接下来,指定我们要用于生成答案的模型。

model = 'claude-3-opus-20240229'

# 生成答案

最后,我们使用 Claude 3 模型根据提供的上下文生成答案。

response = client.messages.create(
        system =  "You are a helpful research assistant. You will be shown data from a vast knowledge base. You have to answer the query from the provided context.",
        messages=[
                    {"role": "user", "content":  "Context: " + stre + "\\\\n\\\\n Query: " + query},
                ],
        model= model,
        temperature=0,
        max_tokens=160
    )
response.content[0].text

这个脚本将上下文和查询发送给 Claude 3 模型,并检索生成的回答。您将获得类似以下的输出:

'根据提供的上下文,阿根廷赢得了 2022 年 FIFA 世界杯,成为卫冕冠军。段落中提到:\\n\\n"卫冕冠军是阿根廷,在 2022 年的比赛中赢得了他们的第三个冠军。"'

如此,按照以上步骤,我们的 RAG 应用构建完成,该应用使用 Hugging Face 和 Claude 3 模型从知识库中检索和生成答案。

# MyScaleDB: RAG 应用的理想选择

MyScaleDB 是一款融合了先进向量搜索功能和 SQL 支持的数据库,使其成为构建检索增强生成 (RAG) 应用程序的强大选择。其主要优势包括以下几方面:

  1. 向量搜索和 SQL 的无缝集成:MyScale 建立在 ClickHouse 之上,能够无缝地集成向量和结构化数据。这种独特的功能允许进行复杂且高效的查询,从而提高 RAG 系统的性能和准确性。

  2. 成本效益和可扩展性:MyScaleDB 被设计为市场上最经济高效的向量数据库之一,同时提供与竞争对手相媲美的卓越性能。且 MyScale 自研多尺度树图 (MSTG) 等先进的向量索引技术,在不牺牲精度的情况下最大限度地减少资源消耗。动态批处理和多线程支持高并发请求,确保低延迟和高吞吐量,使其成为可扩展 RAG 应用程序的可靠且高效的数据库解决方案。

  3. 简化的数据管理:将向量数据和结构化数据存储在同一个数据库中,可以简化数据管理任务。开发人员可以使用熟悉的 SQL 语法查询和管理所有数据,而无需维护和同步单独的数据库。

Join Our Newsletter

# 结论

开发简单的RAG应用程序相对比较简单,但优化其性能、准确性和可扩展性却并非易事。选择合适的工具(如LLMs、向量数据库和 Embedding 模型)对于构建能够提供精确且相关响应的高效 RAG 应用程序至关重要。

向量数据库的选择尤其重要,它充当 LLM 的知识库,提供生成相关响应所需的上下文信息。因此,向量数据库必须能够高效地存储和检索大量数据,同时确保高精度和低延迟。

MyScale (opens new window) 在这方面表现出色,在性能和准确性方面超越了领先的向量数据库 (opens new window)。此外,MyScale 为新用户提供了免费的 development pod,可存储 500万个 768 维向量,让开发者们以零成本体验 MyScale 的功能和优势。所以,还在等什么,快来试用吧!

Keep Reading
images
不要将未来建立在专门的向量数据库上

随着人工智能的兴起,向量数据库因其高效存储、管理和检索大规模高维数据的能力而受到了广泛关注。这种能力对于处理文本、图像和视频等非结构化数据的人工智能和生成式人工智能(GenAI)应用至关重要。 向量数据库的主要逻辑是提供相似性搜索功能,而不是传统数据库提供的关键字搜索。这个概念在大型语言模型(LLM)的性能提升中得到了广泛应用,特别是在ChatGPT发布之后。 LLM的最大问题是需要大量的资源 ...

images
使用重新排序函数和MyScale的两阶段检索

正如我们之前的一篇博文所探讨的,向量搜索通过将文本转换为数值向量,突破了传统关键词匹配的局限,为信息检索带来了革命性的进展。这种方法能够捕捉文本的上下文信息,从而提高搜索结果的相关性。然而,向量搜索并非完美无缺。从文本到向量的转换过程不可避免地会导致信息丢失,进而影响搜索的准确性。 为了解 ...

images
MyScale改进了Cowarobot的自动驾驶车队训练工作流程

Cowarobot成立于2015年,是一家专注于为复杂城市环境中的自动驾驶提供一站式解决方案的公司。截至2022年中期,他们在中国的10多个城市设有业务,并拥有1000多辆自动驾驶车辆。他们还与中国的汽车制造 ...

Start building your Al projects with MyScale today

Free Trial
Contact Us