التكامل مع أطر نماذج اللغة الكبيرة
تعلّموا ربط قواعد البيانات المتجهية بأطر تنسيق نماذج اللغة الكبيرة الشائعة، مثل LangChain أو LlamaIndex.
التكامل مع أطر نماذج اللغة الكبيرة درس مجاني في Vector Databases: Pinecone, Weaviate & pgvector على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Vector Databases: Pinecone, Weaviate & pgvector، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Vector Databases: Pinecone, Weaviate & pgvector 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
LLM Frameworks for RAG
Welcome! In this lesson, we'll learn how to connect vector databases with powerful LLM orchestration frameworks. These frameworks simplify building complex AI applications like Retrieval-Augmented Generation (RAG).
Think of them as tools that help your language model talk to your vector database efficiently.
Why Use LLM Frameworks?
Building RAG applications involves many steps: loading data, splitting text, generating embeddings, storing them in a vector database, retrieving relevant chunks, and finally, feeding them to an LLM.
LLM frameworks streamline this process by:
- Abstracting complexity: Providing a unified interface for various components.
- Modularity: Allowing you to easily swap out different models or databases.
- Workflow management: Helping chain together different operations.
Introducing LangChain
LangChain is a popular framework for developing applications powered by language models. It's known for its modular design, making it easy to build complex LLM workflows.
Key concepts in LangChain include:
- Chains: Sequences of calls to LLMs or other utilities.
- Agents: LLMs that decide which tools to use and in what order.
- Retrievers: Components that fetch documents from a data source.
- Vector Stores: Integrations with various vector databases.
Introducing LlamaIndex
LlamaIndex (formerly GPT Index) is another leading data framework for LLM applications. It focuses heavily on data ingestion, indexing, and retrieval to augment LLMs.
LlamaIndex is particularly strong in:
- Data connectors: Easily loading data from many sources.
- Data indexes: Structuring data for efficient retrieval (e.g., VectorStoreIndex).
- Query engines: Providing an interface to query your indexed data.
LangChain: Setting up a Vector Store
LangChain allows you to easily integrate with various vector databases. Here, we'll use an in-memory Chroma database to demonstrate the setup.
Notice how `Chroma.from_documents` handles both embedding and storage.
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings import OpenAIEmbeddings
from langchain_core.documents import Document
# Dummy Embedding Model for demonstration
class DummyEmbeddings(OpenAIEmbeddings):
def embed_documents(self, texts):
return [[0.1] * 1536 for _ in texts]
def embed_query(self, text):
return [0.1] * 1536
embeddings = DummyEmbeddings()
docs = [
Document(page_content="The quick brown fox."),
Document(page_content="AI is transforming industries."),
Document(page_content="Vector databases are key."),
]
# Create an in-memory Chroma vector store
db = Chroma.from_documents(docs, embeddings)
print("Chroma vector store initialized.")
print(f"Number of documents: {len(docs)}")LangChain: Document Handling
Before storing data in a vector database, it often needs to be loaded and processed. LangChain provides DocumentLoaders to read data and TextSplitters to break it into manageable chunks.
This ensures optimal retrieval and prompt length for LLMs.
from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import CharacterTextSplitter
import os
# Create a dummy text file
with open("sample.txt", "w") as f:
f.write("This is a long text about RAG. "
"It combines retrieval with generation. "
"Vector databases are essential here.")
# Load documents
loader = TextLoader("sample.txt")
documents = loader.load()
# Split documents into chunks
text_splitter = CharacterTextSplitter(
chunk_size=50, chunk_overlap=0
)
split_docs = text_splitter.split_documents(documents)
print(f"Original doc content: {documents[0].page_content[:40]}...")
print(f"Number of split chunks: {len(split_docs)}")
print(f"First chunk: {split_docs[0].page_content}")
os.remove("sample.txt") # Clean upLangChain: Creating a Retriever
Once your vector store is set up, you can turn it into a Retriever. This component is responsible for fetching relevant documents based on a query, which is crucial for the 'Retrieval' part of RAG.
The retriever abstracts the underlying search logic of the vector database.
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings import OpenAIEmbeddings
from langchain_core.documents import Document
# Dummy Embeddings & Documents
class DummyEmbeddings(OpenAIEmbeddings):
def embed_documents(self, texts): return [[0.1] * 1536 for _ in texts]
def embed_query(self, text): return [0.1] * 1536
embeddings = DummyEmbeddings()
docs = [
Document(page_content="Apples are red."),
Document(page_content="Bananas are yellow."),
Document(page_content="Grapes are purple."),
]
db = Chroma.from_documents(docs, embeddings)
# Convert the vector store into a retriever
retriever = db.as_retriever()
query = "What color are grapes?"
retrieved_docs = retriever.invoke(query)
print(f"Query: '{query}'")
print(f"Retrieved {len(retrieved_docs)} documents.")
for i, doc in enumerate(retrieved_docs):
print(f" Doc {i+1}: {doc.page_content}")LlamaIndex: Indexing Documents
LlamaIndex uses the concept of 'Indexes' to structure your data for efficient retrieval. The VectorStoreIndex is a common type, leveraging a vector database for similarity search.
This example shows how to create an in-memory Chroma-backed index with LlamaIndex.
from llama_index.core import VectorStoreIndex, Document
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.vector_stores.chroma import ChromaVectorStore
import chromadb
from llama_index.core import Settings
# Initialize an in-memory Chroma client
db = chromadb.Client()
chroma_collection = db.get_or_create_collection("my_ll_docs")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
# Dummy documents
documents = [
Document(text="LlamaIndex builds LLM apps."),
Document(text="It helps with data indexing."),
Document(text="Vector dbs are core for retrieval."),
]
# Dummy embedding model for runnable example
class DummyLlamaIndexEmbeddings(OpenAIEmbedding):
def _get_query_embedding(self, query): return [0.2] * 1536
def _get_text_embedding(self, text): return [0.2] * 1536
Settings.embed_model = DummyLlamaIndexEmbeddings()
# Create a VectorStoreIndex
index = VectorStoreIndex.from_documents(
documents, vector_store=vector_store
)
print("LlamaIndex VectorStoreIndex created.")LlamaIndex: Querying the Index
Once an index is created in LlamaIndex, you can use a QueryEngine to perform searches. The query engine handles the retrieval from the underlying vector store and can optionally interact with an LLM to synthesize a response.
Here, we focus on the retrieval aspect.
from llama_index.core import VectorStoreIndex, Document
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.vector_stores.chroma import ChromaVectorStore
import chromadb
from llama_index.core import Settings
# Initialize in-memory Chroma client & store
db = chromadb.Client()
chroma_collection = db.get_or_create_collection("my_ll_docs_query")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
# Dummy documents
documents = [
Document(text="LlamaIndex helps build context-augmented LLM apps."),
Document(text="It provides tools for data ingestion."),
Document(text="Retrieval is key for RAG."),
]
# Dummy embedding model
class DummyLlamaIndexEmbeddings(OpenAIEmbedding):
def _get_query_embedding(self, query): return [0.2] * 1536
def _get_text_embedding(self, text): return [0.2] * 1536
Settings.embed_model = DummyLlamaIndexEmbeddings()
# Create and populate index
index = VectorStoreIndex.from_documents(
documents, vector_store=vector_store
)
# Create a query engine
query_engine = index.as_query_engine()
# Perform a query
query = "What does LlamaIndex do?"
response = query_engine.query(query)
print(f"Query: '{query}'")
print(f"Response (partial): {str(response)[:80]}...")Connecting Frameworks & VDBs
Both LangChain and LlamaIndex offer robust integrations with various vector databases (like Pinecone, Weaviate, pgvector, etc.). They provide a layer of abstraction, allowing you to switch between VDBs with minimal code changes.
This simplifies developing and maintaining RAG applications by decoupling your application logic from the specific database implementation.
Framework Components Check
Which of the following are common components or concepts found in LLM orchestration frameworks (like LangChain or LlamaIndex) when building RAG applications?
Recap: Integrating with Frameworks
Great job! You've learned how LLM orchestration frameworks like LangChain and LlamaIndex simplify building RAG applications by integrating with vector databases.
- They provide abstractions for VDBs.
- They offer tools for document loading, splitting, and indexing.
- They enable efficient retrieval through retrievers and query engines.
These frameworks are essential for managing the complexity of modern AI applications.
الأسئلة الشائعة
هل درس «التكامل مع أطر نماذج اللغة الكبيرة» مجاني؟
نعم — نص درس «التكامل مع أطر نماذج اللغة الكبيرة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Vector Databases: Pinecone, Weaviate & pgvector، انتقل إلى CoddyKit PRO. تتضمن دورة Vector Databases: Pinecone, Weaviate & pgvector 4 دروس في المجموع.
ماذا ستتعلم في «التكامل مع أطر نماذج اللغة الكبيرة»؟
تعلّموا ربط قواعد البيانات المتجهية بأطر تنسيق نماذج اللغة الكبيرة الشائعة، مثل LangChain أو LlamaIndex. تتمرن على Vector Databases: Pinecone, Weaviate & pgvector مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Vector Databases: Pinecone, Weaviate & pgvector؟
لا تُشترط خبرة سابقة. Vector Databases: Pinecone, Weaviate & pgvector على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «التكامل مع أطر نماذج اللغة الكبيرة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Vector Databases: Pinecone, Weaviate & pgvector هذا؟
نعم. كل درس في Vector Databases: Pinecone, Weaviate & pgvector يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- نظرة عامة على بنية نظام RAG
- التكامل مع أطر نماذج اللغة الكبيرة
- استرجاع المعلومات السياقية
- استراتيجيات تقسيم النص لـ RAG