見出し画像

RAG の概念と関連している処理をCohere & LlamaIndexで試す


Cohere DocsのLlamaIndex

今回試したのはCohere DocsLlamaIndexです。
ここに記載しているコードを、CohereのチャットインタフェースCoralの聞いて得た回答は、「CohereLLM サービスを使用して生成と埋め込みの両方を処理し、

RAG の概念と関連している処理

です。完全な RAG モデルを実装しているわけではありません」でした。

RAG(Retrieval Augmented Generation)。
外部データを活用した回答生成の手法です。
Coralによると、実際にはLlamaIndexのコードより、生成プロセスを強化するために情報検索を組み込む複雑なアーキテクチャが、RAGです。

外部データを活用

以前から試したいと考えていたのは、Embeding(エンベディング:埋め込み)とRAGの組み合わせです。
でも知識が追い付きません。
外部データを活用する手段が何でもRAGではない、ということはわかってきました。
RAGでなくても外部データを試す方法を調べていましたが、有料のAPIを使うには、試行錯誤する時間分が課金されるため、控えていました。

チャット、埋め込み、再ランク機能を使用

最近知ったCohereLlamaIndex
カナダのAIベンダーで、LLMやAPIなどの開発環境を用意しています。

トライアルAPIキーを利用

LlamaIndexLLMと外部データを接続するためのインタフェースを提供します。
LlamaIndex & CohereのトライアルAPIキーを組み合わせれば、無料で使えることがわかり、今回試しました。
トライアルAPIキーCohereダッシュボードから取得します

試したのは冒頭に述べたEmbeddingです。
使用したコードはCohere Pipeline with LlamaIndex
Cohere のチャットモデル、埋め込み、再ランク機能を使用して、データに基づいて応答を生成する」コードです。

ただ、このままでは動かなかったので、Cohere CoralChatGPTGeminiに聞きながら進めました。
主な質問の内容がCohereAPIのことだったからか、特にCoralが役立ってくれました。

Colabにテキストデータをアップして参照

下記が今回Google Colab(フリー版)で動かしたコードです。

# パッケージをインストール
!pip install llama-index-llms-cohere
!pip install llama-index-embeddings-cohere
!pip install llama-index-postprocessor-cohere-rerank
!pip install llama-index-readers-file
# Google Colabのシークレに登録したCohereのトライアルAPIキーを呼び出し
from google.colab import userdata
COHERE_API_KEY = userdata.get('COHERE_API_KEY')
# 手元のデータアップロードするフォルダを準備
!mkdir -p 'data/mydata/'

アップロードしたデータは、Gemini → Coralの順番で整理してもらったAIの歴史に関するテキストです。

# Cohere Pipeline with LlamaIndex
# rerank

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.llms.cohere import Cohere
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
from llama_index.embeddings.cohere import CohereEmbedding
from llama_index.postprocessor.cohere_rerank import CohereRerank

# Create the embedding model
# CohereEmbedding クラスのインスタンスを作成。テキストの埋め込み表現を生成するために使用
embed_model = CohereEmbedding(
    cohere_api_key=COHERE_API_KEY,
    model_name="embed-english-v3.0",
    input_type="search_query",
)

# Create the service context with the cohere model for generation and embedding model
# ServiceContext クラスのインスタンスを作成し、LLMと埋め込みモデルを指定
# サービス コンテキストでは、LLM を指定して、テキストの生成、質問応答、テキストの要約などのタスクを実行可能
service_context = ServiceContext.from_defaults(
    llm=Cohere(api_key=COHERE_API_KEY, model="command"),
    embed_model=embed_model
)

# Load the data, for this example data needs to be in a test file 
data = SimpleDirectoryReader(input_files=["/content/data/mydata/histroy_of_AI.txt"]).load_data()

# 前述のデータとサービス コンテキストを渡す。このインデックスは、検索クエリを実行するために使用
index = VectorStoreIndex.from_documents(data, service_context=service_context)

# Create a cohere reranker 
# CohereRerank クラスのインスタンスを作成。検索結果を再ランク付け
cohere_rerank = CohereRerank(api_key=COHERE_API_KEY)

# Create the query engine
# index.as_query_engine() メソッドを呼び出してクエリ エンジンを作成し、 node_postprocessors パラメーターに前述の cohere_rerank オブジェクトを渡す
# クエリ結果の再ランク付けが有効になる
query_engine = index.as_query_engine(node_postprocessors=[cohere_rerank])

# Generate the response
# クエリ "・・・?" の応答を取得
response = query_engine.query("What are the efforts of Meta, Microsoft, and Google that represent the rise of open source LLM ?")

記載した最近の情報を参照するか確認

クエリーに使った質問「What are the names of the LLMs provided by Meta, Microsoft, and Google that represent the rise of open source LLMs ?」(オープンソースLLMの台頭を象徴するMeta、Microsoft、Googleが提供するLLMの名称は?)は、比較的新しい情報なので、通常生成AIに聞いても十分な返事が返ってきません。
参照するテキストにその部分を記載しています。
回答を確認します。

print(response)

参照テキストは日本語ですが、英語で回答しています。
Meta's Llama, Microsoft's Phi, and Google's Gemma」とこちらが用意した内容を参照してくれました。

The significant shift in the field of generative AI occurred when several large language models (LLMs) became available on the open source market. The arrival of open-source LLMs revolutionized the usage of LLMs, which previously required expensive licensing fees to use. Now, anyone can board the ship of LLMs and explore the sea of generative AI.

The appearance of open-source LLMs was a turning point in the field of generative AI due to the impact of large language models. The development of LLMs has accelerated, and the number of users has increased. Open-source LLMs, such as Meta's Llama, Microsoft's Phi, and Google's Gemma, transformed the landscape of generative AI by fostering exploration and providing new perspectives for AI researchers, resulting in a new wave of innovation.

However, open-source LLMs face challenges such as the need for continuous effort and training data quality. The open-source community and developers play a crucial role in overcoming these obstacles to reach the potential of open-source LLMs. As the landscape of AI continues to evolve, open-source LLMs remain a crucial milestone in the development of AI, offering new perspectives and opportunities for future advancements.

(DeepL翻訳)
生成AIの分野で大きな変化が起こったのは、いくつかの大規模言語モデル(LLM)がオープンソース市場で利用可能になったときである。オープンソースのLLMの登場は、それまで高額なライセンス料が必要だったLLMの使い方に革命をもたらした。今や誰でもLLMの船に乗り、生成AIの海を探検することができる。

オープンソースのLLMの登場は、大規模な言語モデルのインパクトにより、ジェネレーティブAIの分野に転機をもたらした。LLMの開発は加速し、利用者も増加した。MetaのLlama、MicrosoftのPhi、GoogleのGemmaといったオープンソースのLLMは、AI研究者の探求心を育み、新たな視点を提供することで、生成AIの状況を一変させ、イノベーションの新たな波をもたらした。

しかし、オープンソースのLLMは、継続的な努力の必要性や学習データの質といった課題に直面している。オープンソースLLMの潜在能力を引き出すために、オープンソースコミュニティと開発者は、これらの障害を克服する上で重要な役割を担っています。AIの状況が進化し続ける中、オープンソースLLMはAIの発展における重要なマイルストーンであり続け、将来の進歩のための新たな展望と機会を提供する。


この記事が気に入ったらサポートをしてみませんか?