LlamaIndex is a data framework for building RAG (Retrieval-Augmented Generation) applications with advanced document processing, vector search, and knowledge management capabilities. The 0G integration brings decentralized compute to LlamaIndex’s powerful data orchestration.
from llama_index.core.agent import ReActAgentfrom llama_index.core.tools import QueryEngineTool, ToolMetadata# Create specialized query engines for different document typesresearch_index = VectorStoreIndex.from_documents(research_docs)financial_index = VectorStoreIndex.from_documents(financial_docs)legal_index = VectorStoreIndex.from_documents(legal_docs)# Create tools for the agentresearch_tool = QueryEngineTool( query_engine=research_index.as_query_engine(), metadata=ToolMetadata( name="research_query", description="Query research documents and academic papers for scientific insights" ))financial_tool = QueryEngineTool( query_engine=financial_index.as_query_engine(), metadata=ToolMetadata( name="financial_query", description="Query financial reports and market data for economic insights" ))legal_tool = QueryEngineTool( query_engine=legal_index.as_query_engine(), metadata=ToolMetadata( name="legal_query", description="Query legal documents and regulations for compliance insights" ))# Create agent with 0G LLMagent = ReActAgent.from_tools( [research_tool, financial_tool, legal_tool], llm=ZG( provider_address="0x3feE5a4dd5FDb8a32dDA97Bed899830605dBD9D3", private_key="your-private-key" ), verbose=True)# Use agent for complex cross-document queriesresponse = agent.chat( "Compare the research findings with the financial performance data and identify any legal compliance issues.")print(response)
from llama_index.core import SimpleDirectoryReaderfrom llama_index.multi_modal_llms.openai import OpenAIMultiModal# Load documents with imagesdocuments = SimpleDirectoryReader( "./mixed_content", required_exts=[".pdf", ".txt", ".png", ".jpg"]).load_data()# Configure multi-modal processing# Note: This example shows the pattern - actual multi-modal support depends on model capabilitiesSettings.llm = ZG( provider_address="0xf07240Efa67755B5311bc75784a061eDB47165Dd", private_key="your-private-key")# Create multi-modal indexindex = VectorStoreIndex.from_documents(documents)# Query with image understandingresponse = query_engine.query( "Analyze the charts and graphs in the documents and explain the trends they show.")
from llama_index.core import Documentimport pandas as pdimport json# Load structured datadf = pd.read_csv("data.csv")json_data = json.load(open("metadata.json"))# Convert structured data to documentsstructured_docs = []# Convert DataFrame to documentsfor _, row in df.iterrows(): doc_text = f"Record: {row.to_dict()}" doc = Document(text=doc_text, metadata={"type": "structured_data", "source": "csv"}) structured_docs.append(doc)# Convert JSON to documentsfor key, value in json_data.items(): doc_text = f"{key}: {json.dumps(value, indent=2)}" doc = Document(text=doc_text, metadata={"type": "json_data", "key": key}) structured_docs.append(doc)# Combine with text documentsall_docs = documents + structured_docs# Create unified indexunified_index = VectorStoreIndex.from_documents(all_docs)# Query across structured and unstructured dataresponse = unified_index.as_query_engine().query( "What insights can you derive by combining the structured data with the document analysis?")
# Different models for different RAG componentsclass TaskSpecificLLMs: def __init__(self): # General purpose model for most queries self.general_llm = ZG( provider_address="0xf07240Efa67755B5311bc75784a061eDB47165Dd", temperature=0.3 ) # Reasoning model for complex analysis self.reasoning_llm = ZG( provider_address="0x3feE5a4dd5FDb8a32dDA97Bed899830605dBD9D3", temperature=0.1 ) def get_llm_for_task(self, task_type: str): if task_type in ["analysis", "reasoning", "comparison"]: return self.reasoning_llm return self.general_llm# Usage in query enginestask_llms = TaskSpecificLLMs()# Create different query engines for different tasksanalysis_engine = index.as_query_engine( llm=task_llms.get_llm_for_task("analysis"), response_mode="tree_summarize")general_engine = index.as_query_engine( llm=task_llms.get_llm_for_task("general"), response_mode="compact")
from llama_index.core.prompts import PromptTemplate# Custom RAG prompt for 0G modelsRAG_PROMPT = PromptTemplate( "Context information is below.\n" "---------------------\n" "{context_str}\n" "---------------------\n" "You are an AI assistant powered by decentralized compute. " "Using the context information and not prior knowledge, " "answer the query with detailed analysis and cite specific sources.\n" "Query: {query_str}\n" "Answer: ")# Apply custom promptquery_engine = index.as_query_engine( text_qa_template=RAG_PROMPT, similarity_top_k=5)
class EnterpriseRAG: def __init__(self): self.llm = ZG( provider_address="0x3feE5a4dd5FDb8a32dDA97Bed899830605dBD9D3", private_key="your-private-key" ) self.indices = {} def add_department_docs(self, department: str, doc_path: str): """Add documents for a specific department""" docs = SimpleDirectoryReader(doc_path).load_data() # Add department metadata for doc in docs: doc.metadata["department"] = department index = VectorStoreIndex.from_documents(docs) self.indices[department] = index def cross_department_query(self, query: str): """Query across all departments""" results = {} for dept, index in self.indices.items(): engine = index.as_query_engine(llm=self.llm) response = engine.query(f"From {dept} perspective: {query}") results[dept] = response return results# Usageenterprise_rag = EnterpriseRAG()enterprise_rag.add_department_docs("engineering", "./engineering_docs")enterprise_rag.add_department_docs("marketing", "./marketing_docs")enterprise_rag.add_department_docs("legal", "./legal_docs")# Cross-department analysisresults = enterprise_rag.cross_department_query( "What are the implications of the new AI regulation?")
class ResearchAssistant: def __init__(self): self.llm = ZG( provider_address="0x3feE5a4dd5FDb8a32dDA97Bed899830605dBD9D3", private_key="your-private-key", temperature=0.2 ) def analyze_research_corpus(self, papers_path: str): """Analyze a corpus of research papers""" papers = SimpleDirectoryReader(papers_path).load_data() # Extract metadata from papers extractors = [ TitleExtractor(), QuestionsAnsweredExtractor(questions=5), SummaryExtractor(summaries=["prev", "self", "next"]) ] pipeline = IngestionPipeline(transformations=extractors) nodes = pipeline.run(documents=papers) index = VectorStoreIndex(nodes) return index.as_query_engine( llm=self.llm, response_mode="tree_summarize" ) def generate_literature_review(self, query_engine, topic: str): """Generate a comprehensive literature review""" queries = [ f"What are the main research questions in {topic}?", f"What methodologies are commonly used in {topic} research?", f"What are the key findings and conclusions in {topic}?", f"What are the current gaps and future directions in {topic}?", f"How has {topic} research evolved over time?" ] sections = {} for query in queries: response = query_engine.query(query) sections[query] = response return sections# Usageassistant = ResearchAssistant()query_engine = assistant.analyze_research_corpus("./ai_papers")review = assistant.generate_literature_review(query_engine, "machine learning")