llama-index+ollama实现rag详解

1)doc

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
from llama_index.llms.ollama import Ollama
from llama_index.embeddings.ollama import OllamaEmbedding


# 加载数据
documents = SimpleDirectoryReader("llama-index/docs").load_data()

# Emb
Settings.embed_model = OllamaEmbedding(model_name="llama3.1")  

# Post processing
Settings.llm = Ollama(model="llama3.1", request_timeout=360)  


index = VectorStoreIndex.from_documents(
    documents
)

query_engine = index.as_query_engine()


# 执行查询并获取响应
response = query_engine.query("黑猫叫什么")
print(response)  

2)pdf


# ref:
# https://llamahub.ai/l/readers/llama-index-readers-file?from=

import os
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings, StorageContext, load_index_from_storage
from llama_index.llms.ollama import Ollama
from llama_index.embeddings.ollama import OllamaEmbedding
from llama_index.readers.file import PDFReader


# load pdf
parser = PDFReader()
file_extractor = {".pdf": parser}
documents = SimpleDirectoryReader(
   "llama-index/pdf", file_extractor=file_extractor 
).load_data()
    


# emb 
Settings.embed_model = OllamaEmbedding(model_name="wangshenzhi/llama3-8b-chinese-chat-ollama-q8")  

# llm
Settings.llm = Ollama(model="wangshenzhi/llama3-8b-chinese-chat-ollama-q8", request_timeout=360)  


if not os.path.exists("llama-index/db/pdf-db"):
    index = VectorStoreIndex.from_documents(
        documents
    )
    index.storage_context.persist(persist_dir="llama-index/db/pdf-db")
    
else:
    storage_context = StorageContext.from_defaults(persist_dir="llama-index/db/pdf-db")
    index = load_index_from_storage(storage_context)

query_engine = index.as_query_engine()

# 执行查询并获取响应
response = query_engine.query("如果我想获得澳洲500签证, TOEFL最少要考多少分?")
print(response)  

3)query展示了检索的细节,可以选择几条相似的数据

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, get_response_synthesizer, Settings
from llama_index.core.retrievers import VectorIndexRetriever
from llama_index.core.query_engine import RetrieverQueryEngine

from llama_index.llms.ollama import Ollama
from llama_index.embeddings.ollama import OllamaEmbedding



# Emb
Settings.embed_model = OllamaEmbedding(model_name="wangshenzhi/llama3-8b-chinese-chat-ollama-q8")  

# Post processing
Settings.llm = Ollama(model="wangshenzhi/llama3-8b-chinese-chat-ollama-q8", request_timeout=360)  


# build index
documents = SimpleDirectoryReader("llama-index/docs").load_data()
index = VectorStoreIndex.from_documents(documents)



# configure retriever
retriever = VectorIndexRetriever(
    index=index,
    similarity_top_k=2,
)

# configure response synthesizer
response_synthesizer = get_response_synthesizer()

# assemble query engine
query_engine = RetrieverQueryEngine(
    retriever=retriever,
    response_synthesizer=response_synthesizer,    
)

# query
response = query_engine.query("霸王茶姬香港店什么时候开业?店长薪酬多少?")
print(response)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值