استخدام Langfuse لتتبع الاستعلامات في RAG
هذا كتاب طهي بسيط يوضح كيفية استخدام Langfuse لتتبع استعلاماتك في RAG. يتم تنفيذ خط أنابيب RAG باستخدام LlamaIndex وMilvus Lite لتخزين واسترجاع المستندات.
في هذه البداية السريعة، سنوضح لك كيفية إعداد تطبيق LlamaIndex باستخدام Milvus Lite كمخزن متجه. كما سنوضح لك أيضًا كيفية استخدام تكامل Langfuse LlamaIndex لتتبع تطبيقك.
Langfuse عبارة عن منصة هندسة LLM مفتوحة المصدر تساعد الفرق على تصحيح أخطاء تطبيقات LLM وتحليلها وتكرارها بشكل تعاوني. جميع ميزات المنصة مدمجة أصلاً لتسريع سير عمل التطوير.
Milvus Lite هي نسخة خفيفة الوزن من Milvus، وهي قاعدة بيانات متجهة مفتوحة المصدر تدعم تطبيقات الذكاء الاصطناعي مع تضمينات المتجهات والبحث عن التشابه.
الإعداد
تأكد من تثبيت كل من llama-index و langfuse.
$ pip install llama-index langfuse llama-index-vector-stores-milvus --upgrade
قم بتهيئة التكامل. احصل على مفاتيح واجهة برمجة التطبيقات من إعدادات مشروع Langfuse، واستبدل المفتاح العام_المفتاح السري_المفتاح بقيم مفاتيحك. يستخدم هذا المثال OpenAI للتضمينات وإكمال الدردشة، لذلك تحتاج أيضًا إلى تحديد مفتاح OpenAI الخاص بك في متغير البيئة.
import os
# Get keys for your project from the project settings page
# https://cloud.langfuse.com
os.environ["LANGFUSE_PUBLIC_KEY"] = ""
os.environ["LANGFUSE_SECRET_KEY"] = ""
os.environ["LANGFUSE_HOST"] = "https://cloud.langfuse.com" # 🇪🇺 EU region
# os.environ["LANGFUSE_HOST"] = "https://us.cloud.langfuse.com" # 🇺🇸 US region
# Your openai key
os.environ["OPENAI_API_KEY"] = ""
from llama_index.core import Settings
from llama_index.core.callbacks import CallbackManager
from langfuse.llama_index import LlamaIndexCallbackHandler
langfuse_callback_handler = LlamaIndexCallbackHandler()
Settings.callback_manager = CallbackManager([langfuse_callback_handler])
الفهرس باستخدام ميلفوس لايت
from llama_index.core import Document
doc1 = Document(text="""
Maxwell "Max" Silverstein, a lauded movie director, screenwriter, and producer, was born on October 25, 1978, in Boston, Massachusetts. A film enthusiast from a young age, his journey began with home movies shot on a Super 8 camera. His passion led him to the University of Southern California (USC), majoring in Film Production. Eventually, he started his career as an assistant director at Paramount Pictures. Silverstein's directorial debut, “Doors Unseen,” a psychological thriller, earned him recognition at the Sundance Film Festival and marked the beginning of a successful directing career.
""")
doc2 = Document(text="""
Throughout his career, Silverstein has been celebrated for his diverse range of filmography and unique narrative technique. He masterfully blends suspense, human emotion, and subtle humor in his storylines. Among his notable works are "Fleeting Echoes," "Halcyon Dusk," and the Academy Award-winning sci-fi epic, "Event Horizon's Brink." His contribution to cinema revolves around examining human nature, the complexity of relationships, and probing reality and perception. Off-camera, he is a dedicated philanthropist living in Los Angeles with his wife and two children.
""")
# Example index construction + LLM query
from llama_index.core import VectorStoreIndex
from llama_index.core import StorageContext
from llama_index.vector_stores.milvus import MilvusVectorStore
vector_store = MilvusVectorStore(
uri="tmp/milvus_demo.db", dim=1536, overwrite=False
)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(
[doc1,doc2], storage_context=storage_context
)
استعلام
# Query
response = index.as_query_engine().query("What did he do growing up?")
print(response)
# Chat
response = index.as_chat_engine().chat("What did he do growing up?")
print(response)
استكشاف الآثار في لانجفوس
# As we want to immediately see result in Langfuse, we need to flush the callback handler
langfuse_callback_handler.flush()
تم! ✨ سترى آثار فهرسك واستعلامك في مشروع لانجفيوز الخاص بك.
أمثلة على الآثار (روابط عامة):
تتبع في لانجفيوز
آثار لانجفوس
هل أنت مهتم بالمزيد من الميزات المتقدمة؟
راجع مستندات التكامل الكاملة لمعرفة المزيد حول الميزات المتقدمة وكيفية استخدامها:
- قابلية التشغيل البيني مع مجموعة أدوات تطوير البرمجة SDK الخاصة بـ Langfuse Python والتكاملات الأخرى
- إضافة بيانات وصفية وسمات مخصصة إلى التتبعات