التقييم باستخدام راغاس
يوضّح هذا الدليل كيفية استخدام راغاس لتقييم خط أنابيب التوليد المعزز للاسترجاع (RAG) المبني على نظام ميلفوس.
يجمع نظام RAG بين نظام الاسترجاع والنموذج التوليدي لتوليد نص جديد بناءً على مطالبة معينة. يقوم النظام أولاً باسترجاع المستندات ذات الصلة من مجموعة مستندات باستخدام Milvus، ثم يستخدم نموذجًا توليدًا لتوليد نص جديد بناءً على المستندات المسترجعة.
Ragas هو إطار عمل يساعدك على تقييم خطوط أنابيب RAG الخاصة بك. هناك أدوات وأطر عمل حالية تساعدك على بناء خطوط الأنابيب هذه، لكن تقييمها وقياس أداء خط الأنابيب الخاص بك قد يكون صعبًا. وهنا يأتي دور Ragas (تقييم RAG).
المتطلبات الأساسية
قبل تشغيل هذا الدفتر، تأكد من تثبيت التبعيات التالية:
$ pip install --upgrade pymilvus openai requests tqdm pandas ragas
إذا كنت تستخدم Google Colab، لتمكين التبعيات المثبتة للتو، فقد تحتاج إلى إعادة تشغيل وقت التشغيل (انقر على قائمة "وقت التشغيل" في أعلى الشاشة، وحدد "إعادة تشغيل الجلسة" من القائمة المنسدلة).
سنستخدم OpenAI باعتباره LLM في هذا المثال. يجب عليك إعداد مفتاح api OPENAI_API_KEY
كمتغير بيئة.
import os
os.environ["OPENAI_API_KEY"] = "sk-***********"
تعريف خط أنابيب RAG
سنقوم بتعريف فئة RAG التي تستخدم Milvus كمخزن متجه، و OpenAI كـ LLM. تحتوي الفئة على طريقة load
، التي تقوم بتحميل البيانات النصية إلى Milvus، وطريقة retrieve
، التي تسترجع البيانات النصية الأكثر تشابهًا مع السؤال المعطى، وطريقة answer
، التي تجيب على السؤال المعطى باستخدام المعرفة المسترجعة.
from typing import List
from tqdm import tqdm
from openai import OpenAI
from pymilvus import MilvusClient
class RAG:
"""
RAG (Retrieval-Augmented Generation) class built upon OpenAI and Milvus.
"""
def __init__(self, openai_client: OpenAI, milvus_client: MilvusClient):
self._prepare_openai(openai_client)
self._prepare_milvus(milvus_client)
def _emb_text(self, text: str) -> List[float]:
return (
self.openai_client.embeddings.create(input=text, model=self.embedding_model)
.data[0]
.embedding
)
def _prepare_openai(
self,
openai_client: OpenAI,
embedding_model: str = "text-embedding-3-small",
llm_model: str = "gpt-3.5-turbo",
):
self.openai_client = openai_client
self.embedding_model = embedding_model
self.llm_model = llm_model
self.SYSTEM_PROMPT = """
Human: You are an AI assistant. You are able to find answers to the questions from the contextual passage snippets provided.
"""
self.USER_PROMPT = """
Use the following pieces of information enclosed in <context> tags to provide an answer to the question enclosed in <question> tags.
<context>
{context}
</context>
<question>
{question}
</question>
"""
def _prepare_milvus(
self, milvus_client: MilvusClient, collection_name: str = "rag_collection"
):
self.milvus_client = milvus_client
self.collection_name = collection_name
if self.milvus_client.has_collection(self.collection_name):
self.milvus_client.drop_collection(self.collection_name)
embedding_dim = len(self._emb_text("foo"))
self.milvus_client.create_collection(
collection_name=self.collection_name,
dimension=embedding_dim,
metric_type="IP", # Inner product distance
consistency_level="Strong", # Strong consistency level
)
def load(self, texts: List[str]):
"""
Load the text data into Milvus.
"""
data = []
for i, line in enumerate(tqdm(texts, desc="Creating embeddings")):
data.append({"id": i, "vector": self._emb_text(line), "text": line})
self.milvus_client.insert(collection_name=self.collection_name, data=data)
def retrieve(self, question: str, top_k: int = 3) -> List[str]:
"""
Retrieve the most similar text data to the given question.
"""
search_res = self.milvus_client.search(
collection_name=self.collection_name,
data=[self._emb_text(question)],
limit=top_k,
search_params={"metric_type": "IP", "params": {}}, # Inner product distance
output_fields=["text"], # Return the text field
)
retrieved_texts = [res["entity"]["text"] for res in search_res[0]]
return retrieved_texts[:top_k]
def answer(
self,
question: str,
retrieval_top_k: int = 3,
return_retrieved_text: bool = False,
):
"""
Answer the given question with the retrieved knowledge.
"""
retrieved_texts = self.retrieve(question, top_k=retrieval_top_k)
user_prompt = self.USER_PROMPT.format(
context="\n".join(retrieved_texts), question=question
)
response = self.openai_client.chat.completions.create(
model=self.llm_model,
messages=[
{"role": "system", "content": self.SYSTEM_PROMPT},
{"role": "user", "content": user_prompt},
],
)
if not return_retrieved_text:
return response.choices[0].message.content
else:
return response.choices[0].message.content, retrieved_texts
لنقم بتهيئة فئة RAG مع عملاء OpenAI و Milvus.
openai_client = OpenAI()
milvus_client = MilvusClient(uri="./milvus_demo.db")
my_rag = RAG(openai_client=openai_client, milvus_client=milvus_client)
أما بالنسبة لحجة MilvusClient
:
- إن تعيين
uri
كملف محلي، على سبيل المثال./milvus.db
، هي الطريقة الأكثر ملاءمة، حيث تستخدم تلقائيًا Milvus Lite لتخزين جميع البيانات في هذا الملف. - إذا كان لديك حجم كبير من البيانات، يمكنك إعداد خادم Milvus أكثر أداءً على docker أو kubernetes. في هذا الإعداد، يُرجى استخدام الخادم uri، على سبيل المثال
http://localhost:19530
، كـuri
. - إذا كنت ترغب في استخدام Zilliz Cloud، الخدمة السحابية المدارة بالكامل لـ Milvus، اضبط
uri
وtoken
، والتي تتوافق مع نقطة النهاية العامة ومفتاح Api في Zilliz Cloud.
تشغيل خط أنابيب RAG والحصول على النتائج
نحن نستخدم دليل تطوير Milvus ليكون بمثابة المعرفة الخاصة في RAG الخاص بنا، وهو مصدر بيانات جيد لخط أنابيب RAG بسيط.
قم بتنزيله وتحميله في خط أنابيب RAG.
import os
import urllib.request
url = "https://raw.githubusercontent.com/milvus-io/milvus/master/DEVELOPMENT.md"
file_path = "./Milvus_DEVELOPMENT.md"
if not os.path.exists(file_path):
urllib.request.urlretrieve(url, file_path)
with open(file_path, "r") as file:
file_text = file.read()
# We simply use "# " to separate the content in the file, which can roughly separate the content of each main part of the markdown file.
text_lines = file_text.split("# ")
my_rag.load(text_lines) # Load the text data into RAG pipeline
Creating embeddings: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 47/47 [00:16<00:00, 2.80it/s]
دعونا نحدد سؤال استعلام عن محتوى وثائق دليل التطوير. ثم استخدم الطريقة answer
للحصول على الإجابة ونصوص السياق المسترجعة.
question = "what is the hardware requirements specification if I want to build Milvus and run from source code?"
my_rag.answer(question, return_retrieved_text=True)
('The hardware requirements specification to build and run Milvus from source code is 8GB of RAM and 50GB of free disk space.',
['Hardware Requirements\n\nThe following specification (either physical or virtual machine resources) is recommended for Milvus to build and run from source code.\n\n```\n- 8GB of RAM\n- 50GB of free disk space\n```\n\n##',
'Building Milvus on a local OS/shell environment\n\nThe details below outline the hardware and software requirements for building on Linux and MacOS.\n\n##',
"Software Requirements\n\nAll Linux distributions are available for Milvus development. However a majority of our contributor worked with Ubuntu or CentOS systems, with a small portion of Mac (both x86_64 and Apple Silicon) contributors. If you would like Milvus to build and run on other distributions, you are more than welcome to file an issue and contribute!\n\nHere's a list of verified OS types where Milvus can successfully build and run:\n\n- Debian/Ubuntu\n- Amazon Linux\n- MacOS (x86_64)\n- MacOS (Apple Silicon)\n\n##"])
الآن دعونا نعد بعض الأسئلة مع إجابات الحقيقة الأساسية المقابلة لها. نحصل على الإجابات والسياقات من خط أنابيب RAG الخاص بنا.
from datasets import Dataset
import pandas as pd
question_list = [
"what is the hardware requirements specification if I want to build Milvus and run from source code?",
"What is the programming language used to write Knowhere?",
"What should be ensured before running code coverage?",
]
ground_truth_list = [
"If you want to build Milvus and run from source code, the recommended hardware requirements specification is:\n\n- 8GB of RAM\n- 50GB of free disk space.",
"The programming language used to write Knowhere is C++.",
"Before running code coverage, you should make sure that your code changes are covered by unit tests.",
]
contexts_list = []
answer_list = []
for question in tqdm(question_list, desc="Answering questions"):
answer, contexts = my_rag.answer(question, return_retrieved_text=True)
contexts_list.append(contexts)
answer_list.append(answer)
df = pd.DataFrame(
{
"question": question_list,
"contexts": contexts_list,
"answer": answer_list,
"ground_truth": ground_truth_list,
}
)
rag_results = Dataset.from_pandas(df)
df
Answering questions: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 3/3 [00:03<00:00, 1.29s/it]
السؤال | السياقات | إجابة | الحقيقة_الأرضية | |
---|---|---|---|---|
0 | ما هي مواصفات متطلبات الأجهزة التالية؟ | [مواصفات متطلبات الأجهزة \n\nالمواصفات التالية... | ما هي مواصفات متطلبات الأجهزة الخاصة ببناء أجهزة.... | إذا كنت ترغب في إنشاء برنامج Milvus وتشغيله من المصدر... |
1 | ما هي لغة البرمجة المستخدمة في كتابة... | [CMake & Conan\n\n\nمكتبة خوارزمية لغة البرمجة المستخدمة في... | ما هي لغة البرمجة المستخدمة لكتابة خوارزمية ن... | ما هي لغة البرمجة المستخدمة لكتابة خوارزمية كنو؟ |
2 | ما الذي يجب التأكد منه قبل تشغيل التعليمات البرمجية.... | [تغطية التعليمات البرمجية \nقبل إرسال الكود البرمجي.... | قبل تشغيل تغطية التعليمات البرمجية، يجب عليك التأكد... | قبل تشغيل تغطية التعليمات البرمجية، يجب عليك التأكد ... |
التقييم باستخدام راجاس
نستخدم Ragas لتقييم أداء نتائج خط أنابيب RAG الخاص بنا.
يوفر Ragas مجموعة من المقاييس سهلة الاستخدام. نأخذ Answer relevancy
و Faithfulness
و Context recall
و Context precision
كمقاييس لتقييم خط أنابيب RAG الخاص بنا. لمزيد من المعلومات حول المقاييس، يُرجى الرجوع إلى مقاييس Ragas Metrics.
from ragas import evaluate
from ragas.metrics import (
answer_relevancy,
faithfulness,
context_recall,
context_precision,
)
result = evaluate(
rag_results,
metrics=[
answer_relevancy,
faithfulness,
context_recall,
context_precision,
],
)
result
Evaluating: 0%| | 0/12 [00:00<?, ?it/s]
{'answer_relevancy': 0.9445, 'faithfulness': 1.0000, 'context_recall': 1.0000, 'context_precision': 1.0000}