LlamaIndex API Example

news/2025/10/16 2:36:07/文章来源:https://www.cnblogs.com/xtkyxnx/p/19144666

LlamaIndex API Example

Posted on 2025-10-16 02:32  吾以观复  阅读(0)  评论(0)    收藏  举报

关联知识库:LlamaIndex API Example

Reader and Query Engine

documents = SimpleDirectoryReader('files').load_data()

response = query_engine.query("summarize each document in a few sentences")

doc and textnode and index

doc = Document(
text=text,
metadata={'author': 'John Doe','category': 'others'},
id_='1'
)

nodes = [
TextNode(
text="Lionel Messi is a football player from Argentina."
),
TextNode(
text="He has won the Ballon d'Or trophy 7 times."
),
TextNode(text="Lionel Messi's hometown is Rosario."),
TextNode(text="He was born on June 24, 1987.")
]
index = SummaryIndex(nodes)

replace llm

from llama_index.core.settings import Settings
Settings.llm = OpenAI(temperature=0.8, model="gpt-4")

Import Pipeline 4 cache

from llama_index.core import SimpleDirectoryReader
from llama_index.core.extractors import SummaryExtractor,QuestionsAnsweredExtractor
from llama_index.core.node_parser import TokenTextSplitter
from llama_index.core.ingestion import IngestionPipeline, IngestionCache
from llama_index.core.schema import TransformComponent

class CustomTransformation(TransformComponent):
def call(self, nodes, **kwargs):
# run any node transformation logic here
return nodes

reader = SimpleDirectoryReader('files')
documents = reader.load_data()
try:
cached_hashes = IngestionCache.from_persist_path(
"./ingestion_cache.json"
)
print("Cache file found. Running using cache...")
except:
cached_hashes = ""
print("No cache file found. Running without cache...")

pipeline = IngestionPipeline(
transformations = [
CustomTransformation(),
TokenTextSplitter(
separator=" ",
chunk_size=512,
chunk_overlap=128),
SummaryExtractor(),
QuestionsAnsweredExtractor(
questions=3
)
],
cache=cached_hashes
)

nodes = pipeline.run(documents=documents, show_progress=True)
pipeline.cache.persist("./ingestion_cache.json")

print("All documents loaded")


ingest uploaded documents

from global_settings import STORAGE_PATH, INDEX_STORAGE, CACHE_FILE
from logging_functions import log_action
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
from llama_index.core.ingestion import IngestionPipeline, IngestionCache
from llama_index.core.node_parser import TokenTextSplitter
from llama_index.core.extractors import SummaryExtractor
from llama_index.embeddings.openai import OpenAIEmbedding

def ingest_documents():
documents = SimpleDirectoryReader(
STORAGE_PATH,
filename_as_id = True
).load_data()
for doc in documents:
print(doc.id_)
log_action(
f"File '{doc.id_}' uploaded user",
action_type="UPLOAD"
)

try: cached_hashes = IngestionCache.from_persist_path(CACHE_FILE)print("Cache file found. Running using cache...")
except:cached_hashes = ""print("No cache file found. Running without cache...")
pipeline = IngestionPipeline(transformations=[TokenTextSplitter(chunk_size=1024, chunk_overlap=20),SummaryExtractor(summaries=['self']),OpenAIEmbedding()],cache=cached_hashes
)nodes = pipeline.run(documents=documents)
pipeline.cache.persist(CACHE_FILE)return nodes

if name == "main":
embedded_nodes = ingest_documents()

VectorStoreIndex

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
documents = SimpleDirectoryReader("files").load_data()
index = VectorStoreIndex.from_documents(documents)
print("Index created successfully!")

use local embedding

或者集成LangChain,使用其提供的嵌入模型

from llama_index.embeddings.huggingface import HuggingFaceEmbedding
embedding_model = HuggingFaceEmbedding(
model_name="WhereIsAI/UAE-Large-V1"
)
embeddings = embedding_model.get_text_embedding(
"The quick brown fox jumps over the lazy cat!"
)
print(embeddings[:15])

Index持久化 from 内存 to 磁盘

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
documents = SimpleDirectoryReader("data").load_data()
index = VectorStoreIndex.from_documents(documents)

index.storage_context.persist(persist_dir="index_cache")
print("Index persisted to disk.")

reload index

from llama_index.core import StorageContext, load_index_from_storage
storage_context = StorageContext.from_defaults(persist_dir="index_cache")
index = load_index_from_storage(storage_context)

print("Index loaded successfully!")

use chromadb

import chromadb
from llama_index.vector_stores.chroma import ChromaVectorStore
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, StorageContext

db = chromadb.PersistentClient(path="chroma_database")
chroma_collection = db.get_or_create_collection(
"my_chroma_store"
)

vector_store = ChromaVectorStore(
chroma_collection=chroma_collection
)
storage_context = StorageContext.from_defaults(
vector_store=vector_store
)

documents = SimpleDirectoryReader("files").load_data()
index = VectorStoreIndex.from_documents(
documents=documents,
storage_context=storage_context
)

the following part displays the entire contents of the ChromaDB collection

results = chroma_collection.get()
print(results)

''' We can use the next part to rebuild the Index from the ChromaDB in future sessions
index = VectorStoreIndex.from_vector_store(
vector_store=vector_store,
storage_context=storage_context
)
'''

Index class

from llama_index.core import SummaryIndex, SimpleDirectoryReader
documents = SimpleDirectoryReader("files").load_data()
index = SummaryIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("How many documents have you loaded?")
print(response)

from llama_index.core import TreeIndex, SimpleDirectoryReader
documents = SimpleDirectoryReader("files").load_data()
index = TreeIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("Tell me about dogs")
print(response)

from llama_index.core import ComposableGraph, SimpleDirectoryReader, TreeIndex, SummaryIndex

documents = SimpleDirectoryReader("files").load_data()
index1 = TreeIndex.from_documents([documents[0]])
index2 = TreeIndex.from_documents([documents[1]])
summary1 = "A short introduction to ancient Rome"
summary2 = "Some facts about dogs"

graph = ComposableGraph.from_indices(
SummaryIndex, [index1, index2],
index_summaries=[summary1, summary2]
)
query_engine = graph.as_query_engine()

response = query_engine.query("What can you tell me?")
print(response)

from llama_index.core import DocumentSummaryIndex, SimpleDirectoryReader

documents = SimpleDirectoryReader("files").load_data()
index = DocumentSummaryIndex.from_documents(
documents,
show_progress=True
)

summary1 = index.get_document_summary(documents[0].doc_id)
summary2 = index.get_document_summary(documents[1].doc_id)
print("\nSummary of the first document:" + summary1)
print("\nSummary of the second document:" + summary2)

from llama_index.core import KeywordTableIndex, SimpleDirectoryReader
documents = SimpleDirectoryReader("files").load_data()
index = KeywordTableIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("What famous buildings were in ancient Rome?")
print(response)

from llama_index.core import KnowledgeGraphIndex, SimpleDirectoryReader
documents = SimpleDirectoryReader("files").load_data()
index = KnowledgeGraphIndex.from_documents(documents, max_triplets_per_chunk=2, use_async=True)
query_engine = index.as_query_engine()
response = query_engine.query("Tell me about dogs.")
print(response)

token mock test

import tiktoken
from llama_index.core import TreeIndex, SimpleDirectoryReader, Settings
from llama_index.core.llms.mock import MockLLM
from llama_index.core.callbacks import CallbackManager, TokenCountingHandler

llm = MockLLM(max_tokens=256)
token_counter = TokenCountingHandler(
tokenizer=tiktoken.encoding_for_model("gpt-3.5-turbo").encode
)
callback_manager = CallbackManager([token_counter])

Settings.callback_manager=callback_manager
Settings.llm=llm

documents = SimpleDirectoryReader("cost_prediction_samples").load_data()

index = TreeIndex.from_documents(
documents=documents,
num_children=2,
show_progress=True)

print("Total LLM Token Count:", token_counter.total_llm_token_count)

import tiktoken
from llama_index.core import MockEmbedding, VectorStoreIndex, SimpleDirectoryReader, Settings
from llama_index.core.callbacks import CallbackManager, TokenCountingHandler
from llama_index.core.llms.mock import MockLLM

embed_model = MockEmbedding(embed_dim=1536)
llm = MockLLM(max_tokens=256)
token_counter = TokenCountingHandler(
tokenizer=tiktoken.encoding_for_model("gpt-3.5-turbo").encode
)
callback_manager = CallbackManager([token_counter])

Settings.embed_model=embed_model
Settings.llm=llm
Settings.callback_manager=callback_manager

documents = SimpleDirectoryReader("cost_prediction_samples").load_data()
index = VectorStoreIndex.from_documents(
documents=documents,
show_progress=True)
print("Embedding Token Count:", token_counter.total_embedding_token_count)

query_engine = index.as_query_engine()
response = query_engine.query("What's the cat's name?")
print("Query LLM Token Count:", token_counter.total_llm_token_count)
print("Query Embedding Token Count:",token_counter.total_embedding_token_count)

PITS - index_builder.py

from llama_index.core import VectorStoreIndex, TreeIndex, load_index_from_storage
from llama_index.core import StorageContext
from global_settings import INDEX_STORAGE
from document_uploader import ingest_documents

def build_indexes(nodes):
try:
storage_context = StorageContext.from_defaults(
persist_dir=INDEX_STORAGE
)
vector_index = load_index_from_storage(
storage_context, index_id="vector"
)
tree_index = load_index_from_storage(
storage_context, index_id="tree"
)
print("All indices loaded from storage.")
except Exception as e:
print(f"Error occurred while loading indices: {e}")
storage_context = StorageContext.from_defaults()
vector_index = VectorStoreIndex(
nodes, storage_context=storage_context
)
vector_index.set_index_id("vector")
tree_index = TreeIndex(
nodes, storage_context=storage_context
)
tree_index.set_index_id("tree")
storage_context.persist(
persist_dir=INDEX_STORAGE
)
print("New indexes created and persisted.")
return vector_index, tree_index

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/937949.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

AI中间件机遇与挑战:从Agent到组织级智能的技术演进

AI中间件机遇与挑战:从Agent到组织级智能的技术演进Posted on 2025-10-16 02:32 吾以观复 阅读(0) 评论(0) 收藏 举报关联知识库:AI中间件机遇与挑战:从Agent到组织级智能的技术演进️ AI中间件机遇与挑战:从…

# Redis日常使用与性能排查指南

# Redis日常使用与性能排查指南Posted on 2025-10-16 02:32 吾以观复 阅读(0) 评论(0) 收藏 举报关联知识库:# Redis日常使用与性能排查指南Redis日常使用与性能排查指南 草稿内容 常用命令:info指令 9大块 s…

金耀初讲座——高效演化神经结构搜索

金耀初讲座——高效演化神经结构搜索![assets/金耀初讲座——高效演化神经结构搜索/Untitled.png]] ![assets/金耀初讲座——高效演化神经结构搜索/Untitled 1.png]] ![assets/金耀初讲座——高效演化神经结构搜索/Unt…

二手车检查

二手车检查车源:二手车之家app和懂车帝app,因为上面车商具有营业资格,可初步筛选车商 询问时:漆面状态(哪些面补过漆) 换件情况 四门(大事故),四梁(前后横纵防撞梁),六柱(车身骨架),所有玻璃(批号显示…

图文并茂展示CSS li 排版大合集,总有一款是你刚好需要的

@目录🐱 A. 基础列表样式🌟 1. 默认样式📝 无序列表🔢 有序列表✨ 2. 自定义项目符号🚀 B. 高级布局与定位🖼️ 3. 使用图片作为项目符号🧹 4. 移除默认样式🧭 5. 水平导航栏💫 C. 创意与装饰效果�…

The lamentable decline of reading

https://www.ft.com/content/583de986-a295-4697-a2fe-3c6b13c99145 The lamentable decline of readingChildhood encouragement, libraries and government support can reverse the trendTHE EDITORIAL BOARDAdd to…

[FT.COM]The world should prepare for the looming quantum era

https://www.ft.com/content/96e14cb0-f49f-4632-b94f-2d1cdc625f8b The world should prepare for the looming quantum eraNew breakthroughs underscore the technology’s potential and perilsTHE EDITORIAL BOAR…

10.15 闲话

镜中的昆虫曹髦,字彦士,常称其为“高贵乡公”。甘露五年五月己丑日,在诛杀司马昭的过程中被成济刺死。 我认为三国杀对曹髦的刻画是非常成功的。【潜龙】属于前期劣势,后期爆发的技能。【清正】和【酒诗】都不算能…

函数的类型注释器

在看别人的代码的时候你是否会看到经常会有这种情况 def haha(aa:str) -> np.ndarray:pass这里面的:str还有->代表什么呢? 其实他们就是为了让我们的代码的函数更加容易理解,规范输入输出的类型,所以使用了函…

如何手动构建一个线性回归模型

import numpy as np from utils.features import prepare_for_training # 预处理 import torch as t# 现在开始构建线性回归 class LinearRegression():"""总结一下这个函数具体做了什么事情:1. 预处理…

Web Components 微前端实现与应用

Web Components 微前端实现与应用 1. 核心架构设计 1.1 微前端架构模式 // 微前端核心接口定义 interface MicroFrontendConfig {name: string;entry: string; // 应用入口container: string; // 挂载容…

DshanPI-A1 RK3576 gmrender-resurrect B站投屏

演示效果 一、环境信息类别 具体配置板卡 DshanPI-A1主控芯片 RK3576操作系统 Armbian桌面系统 GNOME窗口系统 WaylandGPU 驱动 Panfrost二、实现原理核心组件:gmrender-resurrect 是一款接收 DLNA 服务内容,并通过 …

组件级异步加载与预加载策略

组件级异步加载与预加载策略 1. 核心架构设计 1.1 异步组件加载器 // 组件加载状态枚举 enum ComponentLoadStatus {IDLE = idle,LOADING = loading,LOADED = loaded,ERROR = error }// 组件配置接口 interface Compon…

好记性不如烂笔头之C语言优先级查询

优先级 运算符 名称与含义 使用形式 结合方向  说明1 [] 数组下标 数组名[ int] 左到右() 圆括号. 成员选择(对象)-> 成员选择(指针)2 - 负号运算符 -表达式 右到左~ 按位取反 ~表达式++ 自增 ++变量名/变量…

SAM系列论文浅析

SAM(Segment Anything Model)系列代表了计算机视觉基础模型从"专用工具"向"通用感知系统"的演进。本文从视觉语言模型的角度深入分析SAM系列三代模型的技术演进,重点剖析SAM3如何通过引入可提示…

2023 ICPC Xian

2023 ICPC Xian ICPC Xian 也是非常坐牢的一场 E 从能力值小的人开始考虑,遍历他能胜利多少次,若他能胜利 \(x\) 次,则必须在交换操作后有一个长度为 \(2^x\) 的区间里面都是比他弱的,从小到大遍历胜利次数,同时维…

2025-10-15 ?

?Kasino game you have 1 coin,determine to join the following game or not. if you have n coins( n is a real number) before this round,you will have 9n w.p. 1/2,and have 0.1n w.p. 1/2 you will play infi…

为什么一部电影,一本书一看就喜欢

为什么一部电影,一本书一看就喜欢,我知道这跟这部作品的,要表达的思想有关,可为什么。。。因为这部电影你还没看完,内容你还不知道,你是怎么喜欢上的呢。真的很奇怪。。。也许,这是很肤浅的认识吧。就现在以前的…

20251015打卡

冒一下泡,我还活着哈哈哈