多模态 RAG:LlamaIndex、Claude 3 和 SingleStore 的综合应用

2024年05月24日 由 alex 发表 211 0

大型语言模型(LLMs)因其几乎可以替代人类的各种能力而成为城中的热门话题。虽然 OpenAI 和谷歌的模型已经产生了重大影响,但还有另一个竞争者在争夺王位: Anthropic。Anthropic 是一家人工智能公司,它正在制造一些令人惊叹的模型,与主要供应商的流行模型竞争。Anthropic 最近发布了 Claude 3,这是一款多模态 LLM,在人工智能市场备受关注。


什么是多模态模型?

LLM 在创建/提供准确信息方面存在局限性,可以通过不同的技术进行追踪。但是,如果我们的模型不仅能将自然语言作为输入,还能将图像和视频作为输入,为用户提供准确的信息,那会怎样呢?这难道不是一个绝妙的想法吗?能够接受不止一种形式的输入并理解不同模式(文本、图像、音频、视频等)的模型被称为多模态模型。谷歌、OpenAI 和 Anthropic 都有自己的多模态模型,并已在市场上推出功能更强的产品。


12


OpenAI 的 GPT-4V(ision)、谷歌的 Gemini 和 Anthropic 的 Claude-3 系列都是多模态模型的一些显著例子,它们正在给人工智能行业带来革命性的变化。


Claude 3

Anthropic 的 Claude 3 系列由三种人工智能模型组成: Claude 3 Opus、Sonnet 和 Haiku。这些模型的多模态功能可与 GPT-4 和 Gemini-ultra 相媲美。这些 Claude 3 型号在成本、安全性和性能之间取得了适当的平衡。


13


图片显示的是各种 LLM 在不同任务中的表现对比图,包括数学解题、编码和基于知识的问题回答。这些模型包括 Claude 变体、GPT-4、GPT-3.5 和 Gemini,它们的得分分别以百分比表示。正如你所看到的,Claude 3 模型在一系列任务中表现强劲,往往优于比较中展示的其他人工智能模型。


Claude 的 Haiku 是最经济实惠而又高效的模型,可用于客户服务使用案例中的实时响应生成、内容审核等,而且效率更高。


14


你可以从 Anthropic 的官方网站上轻松访问 Claude 模型,了解它是如何解决你的疑问的。你可以抄写手写笔记,了解物体的使用方法和复杂的方程式。


我试着举了个例子,分享了一张我在网上找到的图片,看看克劳德模型能否做出准确的反应。


15


Claude 3 多模态教程

我们将使用 SingleStore 数据库作为向量存储,并使用 SingleStore 笔记本(就像 Jupyter 笔记本一样)来运行所有代码。


此外,我们还将使用 LlamaIndex 这个人工智能框架来构建由 LLM 驱动的应用程序。它为开发人员构建人工智能应用程序提供了一个完整的工具包,为他们提供了不同的应用程序接口,并能整合数据源、各种文件格式、用于存储矢量数据的数据库、应用程序等。


我们将导入所需的库,运行 Anthropic [claude-3-haiku-20240307] 中的多模态模型,将数据存储在 SingleStore 中,然后检索数据,通过文本和图像了解多模态的威力。


让我们来探索该模型在文本和视觉任务方面的能力。

注册 SingleStore 即可开始。我们将使用 SingleStore 的笔记本功能运行命令,并使用 SingleStore 作为数据库存储数据。


首次注册后,你需要创建一个工作区和一个数据库来存储数据。在本教程的稍后部分,你还将看到如何将数据存储为嵌入式数据。


16


创建数据库非常简单。只需进入工作区,然后单击创建数据库,如图所示。


17


返回 SingleStore 主控制面板。单击 "开发 "创建笔记本。


18


然后,单击新建笔记本 > 新建笔记本。


19


创建新笔记本,并随意命名。


20


现在,开始使用下面共享的笔记本代码。


确保选择你创建的工作区和数据库。


21


开始在你刚刚创建的笔记本中逐步添加下面显示的所有代码。


如果需要,安装 LlamaIndex 和其他库


!pip install llama-index --quiet
!pip install llama-index-llms-anthropic --quiet
!pip install llama-index-multi-modal-llms-anthropic --quiet
!pip install llama-index-vector-stores-singlestoredb --quiet
!pip install matplotlib --quiet


from llama_index.llms.anthropic import Anthropic
from llama_index.multi_modal_llms.anthropic import AnthropicMultiModal


设置 API 密钥: 添加 OpenAI 和 Anthropic API 密钥。


import os
os.environ["ANTHROPIC_API_KEY"] = ""
os.environ["OPENAI_API_KEY"] = ""


让我们使用Claude 3的俳句模式来完成聊天


llm = Anthropic(model="claude-3-haiku-20240307")
response = llm.complete("SingleStore is ")
print(response)


使用图像的多模态场景

先下载图像


!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/images/prometheus_paper_card.png' -O 'prometheus_paper_card.png'
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open("prometheus_paper_card.png")
plt.imshow(img)


加载图像


from llama_index.core import SimpleDirectoryReader
# put your local directore here
image_documents = SimpleDirectoryReader(
    input_files=["prometheus_paper_card.png"]
).load_data()
# Initiated Anthropic MultiModal class
anthropic_mm_llm = AnthropicMultiModal(
    model="claude-3-haiku-20240307", max_tokens=300
)


在图像上测试查询


response = anthropic_mm_llm.complete(
    prompt="Describe the images as an alternative text",
    image_documents=image_documents,
)
print(response)


你可以测试多个图像示例


from PIL import Image
import requests
from io import BytesIO
import matplotlib.pyplot as plt
from llama_index.core.multi_modal_llms.generic_utils import load_image_urls
image_urls = [
    "https://images.template.net/99535/free-nature-transparent-background-7g9hh.jpg",
]
img_response = requests.get(image_urls[0])
img = Image.open(BytesIO(img_response.content))
plt.imshow(img)
image_url_documents = load_image_urls(image_urls)


要求模型描述所提到的图像


response = anthropic_mm_llm.complete(
    prompt="Describe the images as an alternative text",
    image_documents=image_url_documents,
)
print(response)


RAG 管道: 矢量存储索引


在本节中,我们将向你展示如何使用 Claude 3 在图像数据上建立 RAG 管道。我们首先使用 Claude 从一组图像中提取文本,然后使用嵌入模型对文本进行索引。最后,我们在数据上建立一个查询管道。


from llama_index.multi_modal_llms.anthropic import AnthropicMultiModal
anthropic_mm_llm = AnthropicMultiModal(max_tokens=300)
!wget "https://www.dropbox.com/scl/fi/c1ec6osn0r2ggnitijqhl/mixed_wiki_images_small.zip?rlkey=swwxc7h4qtwlnhmby5fsnderd&dl=1" -O mixed_wiki_images_small.zip
!unzip mixed_wiki_images_small.zip
from llama_index.core import VectorStoreIndex, StorageContext
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.llms.anthropic import Anthropic
from llama_index.vector_stores.singlestoredb import SingleStoreVectorStore
from llama_index.core import Settings
from llama_index.core import StorageContext
import singlestoredb

# Create a SingleStoreDB vector store
os.environ["SINGLESTOREDB_URL"] = f'{connection_user}:{connection_password}@{connection_host}:{connection_port}/{connection_default_database}'
vector_store = SingleStoreVectorStore()
# Using the embedding model to Gemini
embed_model = OpenAIEmbedding()
anthropic_mm_llm = AnthropicMultiModal(max_tokens=300)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex(
    nodes=nodes,
    storage_context=storage_context,
)


测试查询


from llama_index.llms.anthropic import Anthropic
query_engine = index.as_query_engine(llm=Anthropic())
response = query_engine.query("Tell me more about the porsche")
print(str(response))


多模式 LLM: 行业特定用例

在许多使用案例中,多模态技术都发挥着重要作用,并增强了 LLM 的应用,以下是最受欢迎的案例。


  • 医疗保健。由于多模态模型可以处理各种输入类型,因此在医疗保健行业非常有效。这些模型可以将处方、程序、X 光片和其他图像作为输入,将文本和图像结合起来,提供高质量的回复和补救措施。
  • 客户支持聊天机器人。多模态模型非常有效,因为有时文字可能不足以解决问题,所以公司可以要求提供截图和图像。这样,使用多模态驱动的应用程序就能提高应答能力。
  • 社交媒体情感分析。这可以通过多模态应用程序巧妙地完成,因为它们也能识别图像和文本。
  • 电子商务。使用多模态模型可以简化产品搜索和推荐,因为它们可以从具有多种输入能力的用户那里获得更多上下文信息。


多模态模型正在彻底改变人工智能行业,未来我们只会看到更强大的模型。单模态模型的局限在于只能处理一种类型的输入(主要是文本),而多模态模型则可以处理各种输入类型(文本、图像、音频、视频等),从而获得额外的优势。多模态模型为构建由 LLM 驱动的应用程序铺平了未来的道路,使其具有更多的语境和更高的性能。

文章来源:https://medium.com/gitconnected/multimodal-rag-using-llamaindex-claude-3-and-singlestore-4a1931b8150a
欢迎关注ATYUN官方公众号
商务合作及内容投稿请联系邮箱:bd@atyun.com
评论 登录
热门职位
Maluuba
20000~40000/月
Cisco
25000~30000/月 深圳市
PilotAILabs
30000~60000/年 深圳市
写评论取消
回复取消