6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ragas の 評価用LLMと埋め込みモデルに OCI Generative AI の Cohere モデルを使う方法

Last updated at Posted at 2024-11-28

はじめに

RAG(Retrieval-Augmented Generation:検索拡張生成)システムの評価フレームワークとして、Ragasが広く活用されています。Ragasはデフォルトでは評価にOpenAIのモデルを使用しますが、OCIのGenerative AIサービスなど他のモデルも利用可能です(Customise models)。
本記事では、LangChainとの組み合わせで Ragas の評価モデルをカスタマイズする方法について解説します。Ragasの基本的な使い方については、他の記事をご参照ください。

Ragasの評価モデル設定手順

LangChainを使用してRagasの評価モデルを統合する場合、基本的には以下の2つのラッパーを使用します:

  • LLMオブジェクト → LangchainLLMWrapper
  • 埋め込みモデルオブジェクト → LangchainEmbeddingsWrapper

ただし、OCI Generative AIのCohereモデルを使用する場合は追加の設定が必要となります。

OCI Generative AI サービスの Cohereモデル使用時の Tips

Ragasはis_finished()メソッドで評価生成の完了を判定します。デフォルトではOpenAIの仕様に従い、finish_reasonがstopの場合に完了と判定します。

一方、OCI Generative AIのCohereモデルでは、生成完了をCOMPLETEで表します。そのため、is_finished_parser()関数を実装してLangchainLLMWrapperに渡す必要があります。

実装例

以下に、評価用データセットを使用したRagasの評価コード例を示します。

評価用LLMと埋め込みモデルのLangChainインスタンスの生成
from langchain_community.chat_models.oci_generative_ai import ChatOCIGenAI
from langchain_community.embeddings import OCIGenAIEmbeddings

# LLMの準備
llm4eval = ChatOCIGenAI(
    model_id="cohere.command-r-plus-08-2024",
    service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",
    compartment_id=os.getenv("OCI_COMPARTMENT_ID"),
    is_stream=False,
    model_kwargs={"temperature": 0.0, "max_tokens": 4000},
)

# 埋め込みモデルの準備
embeddings4eval = OCIGenAIEmbeddings(
    model_id="cohere.embed-multilingual-v3.0",
    service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",
    compartment_id=os.getenv("OCI_COMPARTMENT_ID"),
    truncate="END"
)

データセット
from datasets import Dataset 

# データセットの準備
ds = Dataset.from_dict(
    {
        "question": question,
        "answer": answer,
        "contexts": contexts,
        "ground_truth": ground_truth,
    }
)
Ragusによる評価
from ragas import evaluate
from ragas.metrics import Faithfulness, AnswerRelevancy, ContextPrecision, ContextRecall
from ragas.llms import LangchainLLMWrapper
from ragas.embeddings import LangchainEmbeddingsWrapper
from langchain_core.outputs import LLMResult

# OCI Generative AI Cohere Chat 用の finished_parser を実装
def my_finished_parser(response: LLMResult) -> bool:
    if (response.generations 
        and response.generations[0] 
        and response.generations[0][0].generation_info 
        and response.generations[0][0].generation_info.get('finish_reason') == 'COMPLETE'):
        return True
    return False

# 評価用 LLM のインスタンス生成(ここで、finished_parserを設定)
evaluator_llm = LangchainLLMWrapper(llm4eval, is_finished_parser=my_finished_parser)

# 評価用 埋め込みモデルのインスタンス生成
evaluator_embeddings = LangchainEmbeddingsWrapper(embeddings4eval)

# メトリクスのインスタンスを作成
metrics = [
    Faithfulness(llm=evaluator_llm),
    AnswerRelevancy(llm=evaluator_llm,embeddings=evaluator_embeddings),
    ContextPrecision(llm=evaluator_llm),
    ContextRecall(llm=evaluator_llm)
]

# 評価の実行
result = evaluate(ds, metrics)
print(result)

Jupyter Notebook での実行結果例
image.png

この実装により、OCI Generative AI サービスの LLM と 埋め込みモデルを使って、Ragasの評価メトリクスであるFaithfulness(忠実度)、AnswerRelevancy(回答の関連性)、ContextPrecision(コンテキストの精度)、ContextRecall(コンテキストの再現率)を計算することができます。

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?