AI-Master-Book
  • about AI-Master-Book
  • AI Master Book
    • 이상치 탐지 with Python
    • 베이지안 뉴럴네트워크 (BNN) with Python
    • 그래프 뉴럴네트워크 (GNN) with Python
    • 데이터 마케팅 분석 with Python
  • LLM MASTER BOOK
    • OpenAI API 쿡북 with Python
    • 기초부터 심화까지 RAG 쿡북 with Python
    • MCP 에이전트 쿡북 with Python
  • LLMs
    • OpenAI API
      • 1️⃣ChatCompletion
      • 2️⃣DALL-E
      • 3️⃣Text to Speech
      • 4️⃣Video to Transcripts
      • 5️⃣Assistants API
      • 6️⃣Prompt Engineering
      • 7️⃣OpenAI New GPT-4o
    • LangChain
      • LangChain Basic
        • 1️⃣Basic Modules
        • 2️⃣Model I/O
        • 3️⃣Prompts
        • 4️⃣Chains
        • 5️⃣Agents
        • 6️⃣Tools
        • 7️⃣Memory
      • LangChain Intermediate
        • 1️⃣OpenAI LLM
        • 2️⃣Prompt Template
        • 3️⃣Retrieval
        • 4️⃣RAG ChatBot
        • 5️⃣RAG with Gemini
        • 6️⃣New Huggingface-LangChain
        • 7️⃣Huggingface Hub
        • 8️⃣SQL Agent & Chain
        • 9️⃣Expression Language(LCEL)
        • 🔟Llama3-8B with LangChain
      • LangChain Advanced
        • 1️⃣LLM Evaluation
        • 2️⃣RAG Evaluation with RAGAS
        • 3️⃣LangChain with RAGAS
        • 4️⃣RAG Paradigms
        • 5️⃣LangChain: Advance Techniques
        • 6️⃣LangChain with NeMo-Guardrails
        • 7️⃣LangChain vs. LlamaIndex
        • 8️⃣LangChain LCEL vs. LangGraph
    • LlamaIndex
      • LlamaIndex Basic
        • 1️⃣Introduction
        • 2️⃣Customization
        • 3️⃣Data Connectors
        • 4️⃣Documents & Nodes
        • 5️⃣Naive RAG
        • 6️⃣Advanced RAG
        • 7️⃣Llama3-8B with LlamaIndex
        • 8️⃣LlmaPack
      • LlamaIndex Intermediate
        • 1️⃣QueryEngine
        • 2️⃣Agent
        • 3️⃣Evaluation
        • 4️⃣Evaluation-Driven Development
        • 5️⃣Fine-tuning
        • 6️⃣Prompt Compression with LLMLingua
      • LlamaIndex Advanced
        • 1️⃣Agentic RAG: Router Engine
        • 2️⃣Agentic RAG: Tool Calling
        • 3️⃣Building Agent Reasoning Loop
        • 4️⃣Building Multi-document Agent
    • Hugging Face
      • Huggingface Basic
        • 1️⃣Datasets
        • 2️⃣Tokenizer
        • 3️⃣Sentence Embeddings
        • 4️⃣Transformers
        • 5️⃣Sentence Transformers
        • 6️⃣Evaluate
        • 7️⃣Diffusers
      • Huggingface Tasks
        • NLP
          • 1️⃣Sentiment Analysis
          • 2️⃣Zero-shot Classification
          • 3️⃣Aspect-Based Sentiment Analysis
          • 4️⃣Feature Extraction
          • 5️⃣Intent Classification
          • 6️⃣Topic Modeling: BERTopic
          • 7️⃣NER: Token Classification
          • 8️⃣Summarization
          • 9️⃣Translation
          • 🔟Text Generation
        • Audio & Tabular
          • 1️⃣Text-to-Speech: TTS
          • 2️⃣Speech Recognition: Whisper
          • 3️⃣Audio Classification
          • 4️⃣Tabular Qustaion & Answering
        • Vision & Multimodal
          • 1️⃣Image-to-Text
          • 2️⃣Text to Image
          • 3️⃣Image to Image
          • 4️⃣Text or Image-to-Video
          • 5️⃣Depth Estimation
          • 6️⃣Image Classification
          • 7️⃣Object Detection
          • 8️⃣Segmentatio
      • Huggingface Optimization
        • 1️⃣Accelerator
        • 2️⃣Bitsandbytes
        • 3️⃣Flash Attention
        • 4️⃣Quantization
        • 5️⃣Safetensors
        • 6️⃣Optimum-ONNX
        • 7️⃣Optimum-NVIDIA
        • 8️⃣Optimum-Intel
      • Huggingface Fine-tuning
        • 1️⃣Transformer Fine-tuning
        • 2️⃣PEFT Fine-tuning
        • 3️⃣PEFT: Fine-tuning with QLoRA
        • 4️⃣PEFT: Fine-tuning Phi-2 with QLoRA
        • 5️⃣Axoltl Fine-tuning with QLoRA
        • 6️⃣TRL: RLHF Alignment Fine-tuning
        • 7️⃣TRL: DPO Fine-tuning with Phi-3-4k-instruct
        • 8️⃣TRL: ORPO Fine-tuning with Llama3-8B
        • 9️⃣Convert GGUF gemma-2b with llama.cpp
        • 🔟Apple Silicon Fine-tuning Gemma-2B with MLX
        • 🔢LLM Mergekit
    • Agentic LLM
      • Agentic LLM
        • 1️⃣Basic Agentic LLM
        • 2️⃣Multi-agent with CrewAI
        • 3️⃣LangGraph: Multi-agent Basic
        • 4️⃣LangGraph: Agentic RAG with LangChain
        • 5️⃣LangGraph: Agentic RAG with Llama3-8B by Groq
      • Autonomous Agent
        • 1️⃣LLM Autonomous Agent?
        • 2️⃣AutoGPT: Worldcup Winner Search with LangChain
        • 3️⃣BabyAGI: Weather Report with LangChain
        • 4️⃣AutoGen: Writing Blog Post with LangChain
        • 5️⃣LangChain: Autonomous-agent Debates with Tools
        • 6️⃣CAMEL Role-playing Autonomous Cooperative Agents
        • 7️⃣LangChain: Two-player Harry Potter D&D based CAMEL
        • 8️⃣LangChain: Multi-agent Bid for K-Pop Debate
        • 9️⃣LangChain: Multi-agent Authoritarian Speaker Selection
        • 🔟LangChain: Multi-Agent Simulated Environment with PettingZoo
    • Multimodal
      • 1️⃣PaliGemma: Open Vision LLM
      • 2️⃣FLUX.1: Generative Image
    • Building LLM
      • 1️⃣DSPy
      • 2️⃣DSPy RAG
      • 3️⃣DSPy with LangChain
      • 4️⃣Mamba
      • 5️⃣Mamba RAG with LangChain
      • 7️⃣PostgreSQL VectorDB with pgvorco.rs
Powered by GitBook
On this page
  • LlmaPack 사용법
  • What is LlamaPack?
  • LlamaPack 사용 2가지 방법
  • Use Cases
  • Document 준비
  • HybridFusionRetrieverPack
  • QueryRewritingRetrieverPack
  • SentenceWindowRetrieverPack
  • RecursiveRetrieverSmallToBigPack
  • LLamaPack 모듈의 개별적 사용
  1. LLMs
  2. LlamaIndex
  3. LlamaIndex Basic

LlmaPack

PreviousLlama3-8B with LlamaIndexNextLlamaIndex Intermediate

Last updated 1 year ago

LlmaPack 사용법

%pip install pypdf -q -U

What is LlamaPack?

Llama Packs은 사전 패키지된 모듈/템플릿의 커뮤니티 중심 허브

LlamaPack 사용 2가지 방법

1. CLI

llamaindex-cli download-llamapack <pack_name> --download-dir <pack_directory>

2. Python

from llama_index.core.llama_pack import download_llama_pack

pack_cls = download_llama_pack("<pack_name>", "<pack_directory>")

Use Cases

RAG 파이프라인 개발에서 유용한 LlmaPack 4가지를 소개합니다.

Document 준비

  • 데모 목적으로 Optimism과 StarkNet의 분석 및 비교 PDF 문서를 사용하겠습니다.

  • Analysis and Comparison between Optimism and StarkNet

!wget -O Analysis_and_Comparison_between_Optimism_and_StarkNet.pdf https://ceur-ws.org/Vol-3460/papers/DLT_2023_paper_16.pdf
from llama_index.readers.file import PDFReader

reader = PDFReader()
documents = reader.load_data(
    "Analysis_and_Comparison_between_Optimism_and_StarkNet.pdf"
)
len(documents)
14
from llama_index.core.node_parser import SimpleNodeParser

nodes = SimpleNodeParser.from_defaults().get_nodes_from_documents(documents)
len(nodes)
14
import os
from dotenv import load_dotenv  

!echo "OPENAI_API_KEY=<Your OpenAI Key>" >> .env # OpenAI API Key 입력
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
from llama_index.core.llama_pack import download_llama_pack
import nest_asyncio

nest_asyncio.apply()
QUESTION = "유효성 롤업의 목표는 무엇이야?"

HybridFusionRetrieverPack

  • Vector retriever

  • BM25 retriever

llama_pack = download_llama_pack(
    "HybridFusionRetrieverPack", "./hybrid_fusion_pack"
)

hybrid_fusion_pack = llama_pack(
    nodes,
    chunk_size=256,
    vector_similarity_top_k=2,
    bm25_similarity_top_k=2
)
response = hybrid_fusion_pack.run(QUESTION)
Generated queries:
1. 유효성 롤업의 장단점은 무엇인가?
2. 유효성 롤업이 왜 중요한가?
3. 유효성 롤업을 효과적으로 구현하는 방법은?
response
Response(response='The goal of Validity Rollups is to ensure the finalization of withdrawals on Layer 1 as soon as the validity proof of the new state root is accepted on Ethereum.', source_nodes=[NodeWithScore(node=TextNode(id_='960bba74-4039-4cce-b62b-a89764a6bced', embedding=None, metadata={'page_label': '3', 'file_name': 'Analysis_and_Comparison_between_Optimism_and_StarkNet.pdf'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='85a01680-d7be-475d-9355-51aa0504602b', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'page_label': '3', 'file_name': 'Analysis_and_Comparison_between_Optimism_and_StarkNet.pdf'}, hash='162138014c4d86025fcd3966a59c200affab1200ec9a8f16c8e545d32b05cf51')
response.response
'The goal of Validity Rollups is to ensure the finalization of withdrawals on Layer 1 as soon as the validity proof of the new state root is accepted on Ethereum.'

QueryRewritingRetrieverPack

이 팩은 사용자의 쿼리를 기반으로 여러 쿼리를 생성합니다.

llama_pack = download_llama_pack(
    "QueryRewritingRetrieverPack", "./query_rewriting_pack"
)

query_rewriting_pack = llama_pack(
    nodes,
    chunk_size=256,
    vector_similarity_top_k=2,
)
response = query_rewriting_pack.run(QUESTION)
Generated queries:
1. 유효성 롤업의 중요성은 무엇인가?
2. 유효성 롤업의 장단점은 무엇인가?
3. 유효성 롤업을 효과적으로 구현하는 방법은 무엇인가?
response.response
'The goal of Validity Rollups is to enable the finalization of withdrawals on Layer 1 as soon as the validity proof of the new state root is accepted on Ethereum, allowing for the quick withdrawal of funds.'

SentenceWindowRetrieverPack

SentenceWindowRetrieverPack은 문서를 로드하고, 청크로 묶고, 각 청크에 메타데이터로 주변 컨텍스트를 추가하고, 검색 중에 응답 합성을 위해 각 청크에 컨텍스트를 다시 삽입합니다.

#%pip install 'pymemgpt[local]'==0.3.6
#%pip install --upgrade llama-index-embeddings-huggingface
llama_pack = download_llama_pack(
    "SentenceWindowRetrieverPack", "./sentence_window_retriever_pack"
)

# Pass documents to build the pack instance
sentence_window_retriever_pack = llama_pack(documents)
response = sentence_window_retriever_pack.run(QUESTION)
response.response
'The goal of Validity Rollups is to enable the finalization of withdrawals on Layer 1 as soon as the validity proof of the new state root is accepted on Ethereum, allowing for the quick withdrawal of funds.'
response
Response(response='The goal of Validity Rollups is to enable the finalization of withdrawals on Layer 1 as soon as the validity proof of the new state root is accepted on Ethereum, allowing for the quick withdrawal of funds.', source_nodes=[NodeWithScore(node=TextNode(id_='960bba74-4039-4cce-b62b-a89764a6bced', embedding=None, metadata={'page_label': '3', 'file_name': 'Analysis_and_Comparison_between_Optimism_and_StarkNet.pdf'},

RecursiveRetrieverSmallToBigPack

RecursiveRetrieverSmallToBigPack은 문서를 로드하고 계층적 노드 그래프(더 큰 부모 노드와 더 작은 자식 노드)를 작성합니다.

llama_pack = download_llama_pack(
    "RecursiveRetrieverSmallToBigPack", "./recursive_retriever_stb_pack"
)

recursive_retriever_stb_pack = llama_pack(documents)
response = recursive_retriever_stb_pack.run(QUESTION)
response.response
'The goal of Validity Rollups is to enable the finalization of withdrawals on Layer 1 as soon as the validity proof of the new state root is accepted on Ethereum, allowing for the quick withdrawal of funds.'

LLamaPack 모듈의 개별적 사용

Packe에 포함된 모듈을 가져와서 개별적으로 사용할 수 있습니다.

# get the sentence vector index
index = sentence_window_retriever_pack.sentence_index

# get the node parser
node_parser = sentence_window_retriever_pack.node_parser

# get the metadata replacement postprocessor
postprocessor = sentence_window_retriever_pack.postprocessor

# get the query engine
query_engine = sentence_window_retriever_pack.query_engine
index
node_parser
postprocessor
query_engine
8️⃣
Llama Hub
Logo