AI Agents ด้วย Hugging Face smolagents
Adel Nehme
VP of AI Curriculum, DataCamp


RAG = ผสานการค้นคืนข้อมูลเข้ากับการสร้างข้อความของ LLM

from langchain_community.document_loaders import PyPDFDirectoryLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
# Load documentation from directory
loader = PyPDFDirectoryLoader("cooking_docs", mode="single")
documents = loader.load()
# Split into chunks
splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200
)
chunks = splitter.split_documents(documents)
from langchain_huggingface import HuggingFaceEndpointEmbeddings
from langchain_community.vectorstores import FAISS
# Create embeddings and vector store
embedder = HuggingFaceEndpointEmbeddings(
model="BAAI/bge-base-en-v1.5",
task="feature-extraction",
)
vector_store = FAISS.from_documents(chunks, embedder)
query = "How do I cook salmon with herbs?"
# Similarity search
relevant_docs = vector_store.similarity_search(query, k=3)
# Create a context string
context = "\n\n".join(doc.page_content for doc in relevant_docs)
ชิ้นส่วนข้อมูลที่ค้นคืนได้ 2 อันดับแรก (จับคู่เชิงความหมาย):
[1] พื้นฐานการเตรียมปลาแซลมอน (หน้า 2) ซับปลาแซลมอนให้แห้งเพื่อให้ผิวกรอบสีสวย ปรุงรสด้วยเกลือ พริกไทย และผักชีฝรั่งสดหรือพาร์สลีย์ พักฟิลเลต์ 10 นาทีให้เกลือซึมเข้าเนื้อ และทิ้งไว้ที่อุณหภูมิห้องเพื่อให้สุกสม่ำเสมอ...
[2] การอบปลาแซลมอนในเตาอบ (หน้า 5) อุ่นเตาอบที่ 200°C (392°F) วางฟิลเลต์บนถาดรองกระดาษรองอบ วางมะนาวฝานและ เนยสมุนไพร (ผักชีฝรั่ง/พาร์สลีย์) ด้านบน อบ 10-12 นาทีจนเนื้อขุ่นและร่วนพอดี พักก่อนเสิร์ฟ 2 นาที...
จะวางแผนมื้ออาหารหนึ่งสัปดาห์ในงบไม่เกิน $50 พร้อมครบตามความต้องการทางโภชนาการได้อย่างไร?
คำตอบกระจายอยู่ในหลายเอกสาร (งบประมาณ โภชนาการ เทคนิค และสูตรอาหาร)

AI Agents ด้วย Hugging Face smolagents